680-NET-skip-GRO-for-foreign-MAC-addresses.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. Subject: NET: skip GRO for foreign MAC addresses
  2. For network drivers using napi_gro_receive, packets are run through GRO,
  3. even when the destination MAC address does not match, and they're supposed
  4. to be delivered to another host behind a different bridge port.
  5. This can be very expensive, because for drivers without TSO or scatter-
  6. gather, this can only be undone by copying the skb and checksumming it
  7. again.
  8. To be able to track foreign MAC addresses in an inexpensive way, create
  9. a mask of changed bits in MAC addresses of upper devices. This allows
  10. handling VLANs and bridge devices with different addresses (as long as
  11. they are not too different).
  12. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  13. --- a/net/core/dev.c
  14. +++ b/net/core/dev.c
  15. @@ -4002,6 +4002,9 @@ static enum gro_result dev_gro_receive(s
  16. enum gro_result ret;
  17. int grow;
  18. + if (skb->gro_skip)
  19. + goto normal;
  20. +
  21. if (!(skb->dev->features & NETIF_F_GRO))
  22. goto normal;
  23. @@ -5067,6 +5070,48 @@ static void __netdev_adjacent_dev_unlink
  24. &upper_dev->adj_list.lower);
  25. }
  26. +static void __netdev_addr_mask(unsigned char *mask, const unsigned char *addr,
  27. + struct net_device *dev)
  28. +{
  29. + int i;
  30. +
  31. + for (i = 0; i < dev->addr_len; i++)
  32. + mask[i] |= addr[i] ^ dev->dev_addr[i];
  33. +}
  34. +
  35. +static void __netdev_upper_mask(unsigned char *mask, struct net_device *dev,
  36. + struct net_device *lower)
  37. +{
  38. + struct net_device *cur;
  39. + struct list_head *iter;
  40. +
  41. + netdev_for_each_upper_dev_rcu(dev, cur, iter) {
  42. + __netdev_addr_mask(mask, cur->dev_addr, lower);
  43. + __netdev_upper_mask(mask, cur, lower);
  44. + }
  45. +}
  46. +
  47. +static void __netdev_update_addr_mask(struct net_device *dev)
  48. +{
  49. + unsigned char mask[MAX_ADDR_LEN];
  50. + struct net_device *cur;
  51. + struct list_head *iter;
  52. +
  53. + memset(mask, 0, sizeof(mask));
  54. + __netdev_upper_mask(mask, dev, dev);
  55. + memcpy(dev->local_addr_mask, mask, dev->addr_len);
  56. +
  57. + netdev_for_each_lower_dev(dev, cur, iter)
  58. + __netdev_update_addr_mask(cur);
  59. +}
  60. +
  61. +static void netdev_update_addr_mask(struct net_device *dev)
  62. +{
  63. + rcu_read_lock();
  64. + __netdev_update_addr_mask(dev);
  65. + rcu_read_unlock();
  66. +}
  67. +
  68. static int __netdev_upper_dev_link(struct net_device *dev,
  69. struct net_device *upper_dev, bool master,
  70. void *private)
  71. @@ -5127,6 +5172,7 @@ static int __netdev_upper_dev_link(struc
  72. goto rollback_lower_mesh;
  73. }
  74. + netdev_update_addr_mask(dev);
  75. call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev);
  76. return 0;
  77. @@ -5244,6 +5290,7 @@ void netdev_upper_dev_unlink(struct net_
  78. list_for_each_entry(i, &upper_dev->all_adj_list.upper, list)
  79. __netdev_adjacent_dev_unlink(dev, i->dev);
  80. + netdev_update_addr_mask(dev);
  81. call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev);
  82. }
  83. EXPORT_SYMBOL(netdev_upper_dev_unlink);
  84. @@ -5763,6 +5810,7 @@ int dev_set_mac_address(struct net_devic
  85. if (err)
  86. return err;
  87. dev->addr_assign_type = NET_ADDR_SET;
  88. + netdev_update_addr_mask(dev);
  89. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  90. add_device_randomness(dev->dev_addr, dev->addr_len);
  91. return 0;
  92. --- a/include/linux/netdevice.h
  93. +++ b/include/linux/netdevice.h
  94. @@ -1556,6 +1556,8 @@ struct net_device {
  95. struct netdev_hw_addr_list mc;
  96. struct netdev_hw_addr_list dev_addrs;
  97. + unsigned char local_addr_mask[MAX_ADDR_LEN];
  98. +
  99. #ifdef CONFIG_SYSFS
  100. struct kset *queues_kset;
  101. #endif
  102. --- a/include/linux/skbuff.h
  103. +++ b/include/linux/skbuff.h
  104. @@ -597,7 +597,8 @@ struct sk_buff {
  105. #endif
  106. __u8 ipvs_property:1;
  107. __u8 inner_protocol_type:1;
  108. - /* 4 or 6 bit hole */
  109. + __u8 gro_skip:1;
  110. + /* 3 or 5 bit hole */
  111. #ifdef CONFIG_NET_SCHED
  112. __u16 tc_index; /* traffic control index */
  113. --- a/net/ethernet/eth.c
  114. +++ b/net/ethernet/eth.c
  115. @@ -172,6 +172,18 @@ u32 eth_get_headlen(void *data, unsigned
  116. }
  117. EXPORT_SYMBOL(eth_get_headlen);
  118. +static inline bool
  119. +eth_check_local_mask(const void *addr1, const void *addr2, const void *mask)
  120. +{
  121. + const u16 *a1 = addr1;
  122. + const u16 *a2 = addr2;
  123. + const u16 *m = mask;
  124. +
  125. + return (((a1[0] ^ a2[0]) & ~m[0]) |
  126. + ((a1[1] ^ a2[1]) & ~m[1]) |
  127. + ((a1[2] ^ a2[2]) & ~m[2]));
  128. +}
  129. +
  130. /**
  131. * eth_type_trans - determine the packet's protocol ID.
  132. * @skb: received socket data
  133. @@ -199,8 +211,12 @@ __be16 eth_type_trans(struct sk_buff *sk
  134. skb->pkt_type = PACKET_MULTICAST;
  135. }
  136. else if (unlikely(!ether_addr_equal_64bits(eth->h_dest,
  137. - dev->dev_addr)))
  138. + dev->dev_addr))) {
  139. skb->pkt_type = PACKET_OTHERHOST;
  140. + if (eth_check_local_mask(eth->h_dest, dev->dev_addr,
  141. + dev->local_addr_mask))
  142. + skb->gro_skip = 1;
  143. + }
  144. /*
  145. * Some variants of DSA tagging don't have an ethertype field