Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / target / linux / generic / pending-4.9 / 680-NET-skip-GRO-for-foreign-MAC-addresses.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Subject: net: replace GRO optimization patch with a new one that supports VLANs/bridges with different MAC addresses
3
4 Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 ---
6  include/linux/netdevice.h |  2 ++
7  include/linux/skbuff.h    |  3 ++-
8  net/core/dev.c            | 48 +++++++++++++++++++++++++++++++++++++++++++++++
9  net/ethernet/eth.c        | 18 +++++++++++++++++-
10  4 files changed, 69 insertions(+), 2 deletions(-)
11
12 --- a/include/linux/netdevice.h
13 +++ b/include/linux/netdevice.h
14 @@ -1749,6 +1749,8 @@ struct net_device {
15         struct netdev_hw_addr_list      mc;
16         struct netdev_hw_addr_list      dev_addrs;
17  
18 +       unsigned char           local_addr_mask[MAX_ADDR_LEN];
19 +
20  #ifdef CONFIG_SYSFS
21         struct kset             *queues_kset;
22  #endif
23 --- a/include/linux/skbuff.h
24 +++ b/include/linux/skbuff.h
25 @@ -747,7 +747,8 @@ struct sk_buff {
26  #ifdef CONFIG_NET_SWITCHDEV
27         __u8                    offload_fwd_mark:1;
28  #endif
29 -       /* 2, 4 or 5 bit hole */
30 +       __u8                    gro_skip:1;
31 +       /* 1, 3 or 4 bit hole */
32  
33  #ifdef CONFIG_NET_SCHED
34         __u16                   tc_index;       /* traffic control index */
35 --- a/net/core/dev.c
36 +++ b/net/core/dev.c
37 @@ -4572,6 +4572,9 @@ static enum gro_result dev_gro_receive(s
38         enum gro_result ret;
39         int grow;
40  
41 +       if (skb->gro_skip)
42 +               goto normal;
43 +
44         if (!(skb->dev->features & NETIF_F_GRO))
45                 goto normal;
46  
47 @@ -5864,6 +5867,48 @@ static void __netdev_adjacent_dev_unlink
48                                            &upper_dev->adj_list.lower);
49  }
50  
51 +static void __netdev_addr_mask(unsigned char *mask, const unsigned char *addr,
52 +                              struct net_device *dev)
53 +{
54 +       int i;
55 +
56 +       for (i = 0; i < dev->addr_len; i++)
57 +               mask[i] |= addr[i] ^ dev->dev_addr[i];
58 +}
59 +
60 +static void __netdev_upper_mask(unsigned char *mask, struct net_device *dev,
61 +                               struct net_device *lower)
62 +{
63 +       struct net_device *cur;
64 +       struct list_head *iter;
65 +
66 +       netdev_for_each_upper_dev_rcu(dev, cur, iter) {
67 +               __netdev_addr_mask(mask, cur->dev_addr, lower);
68 +               __netdev_upper_mask(mask, cur, lower);
69 +       }
70 +}
71 +
72 +static void __netdev_update_addr_mask(struct net_device *dev)
73 +{
74 +       unsigned char mask[MAX_ADDR_LEN];
75 +       struct net_device *cur;
76 +       struct list_head *iter;
77 +
78 +       memset(mask, 0, sizeof(mask));
79 +       __netdev_upper_mask(mask, dev, dev);
80 +       memcpy(dev->local_addr_mask, mask, dev->addr_len);
81 +
82 +       netdev_for_each_lower_dev(dev, cur, iter)
83 +               __netdev_update_addr_mask(cur);
84 +}
85 +
86 +static void netdev_update_addr_mask(struct net_device *dev)
87 +{
88 +       rcu_read_lock();
89 +       __netdev_update_addr_mask(dev);
90 +       rcu_read_unlock();
91 +}
92 +
93  static int __netdev_upper_dev_link(struct net_device *dev,
94                                    struct net_device *upper_dev, bool master,
95                                    void *upper_priv, void *upper_info)
96 @@ -5936,6 +5981,7 @@ static int __netdev_upper_dev_link(struc
97                         goto rollback_lower_mesh;
98         }
99  
100 +       netdev_update_addr_mask(dev);
101         ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, dev,
102                                             &changeupper_info.info);
103         ret = notifier_to_errno(ret);
104 @@ -6062,6 +6108,7 @@ void netdev_upper_dev_unlink(struct net_
105         list_for_each_entry(i, &upper_dev->all_adj_list.upper, list)
106                 __netdev_adjacent_dev_unlink(dev, i->dev, i->ref_nr);
107  
108 +       netdev_update_addr_mask(dev);
109         call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, dev,
110                                       &changeupper_info.info);
111  }
112 @@ -6664,6 +6711,7 @@ int dev_set_mac_address(struct net_devic
113         if (err)
114                 return err;
115         dev->addr_assign_type = NET_ADDR_SET;
116 +       netdev_update_addr_mask(dev);
117         call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
118         add_device_randomness(dev->dev_addr, dev->addr_len);
119         return 0;
120 --- a/net/ethernet/eth.c
121 +++ b/net/ethernet/eth.c
122 @@ -143,6 +143,18 @@ u32 eth_get_headlen(void *data, unsigned
123  }
124  EXPORT_SYMBOL(eth_get_headlen);
125  
126 +static inline bool
127 +eth_check_local_mask(const void *addr1, const void *addr2, const void *mask)
128 +{
129 +       const u16 *a1 = addr1;
130 +       const u16 *a2 = addr2;
131 +       const u16 *m = mask;
132 +
133 +       return (((a1[0] ^ a2[0]) & ~m[0]) |
134 +               ((a1[1] ^ a2[1]) & ~m[1]) |
135 +               ((a1[2] ^ a2[2]) & ~m[2]));
136 +}
137 +
138  /**
139   * eth_type_trans - determine the packet's protocol ID.
140   * @skb: received socket data
141 @@ -171,8 +183,12 @@ __be16 eth_type_trans(struct sk_buff *sk
142                         skb->pkt_type = PACKET_MULTICAST;
143         }
144         else if (unlikely(!ether_addr_equal_64bits(eth->h_dest,
145 -                                                  dev->dev_addr)))
146 +                                                  dev->dev_addr))) {
147                 skb->pkt_type = PACKET_OTHERHOST;
148 +               if (eth_check_local_mask(eth->h_dest, dev->dev_addr,
149 +                                        dev->local_addr_mask))
150 +                       skb->gro_skip = 1;
151 +       }
152  
153         /*
154          * Some variants of DSA tagging don't have an ethertype field