610-v5.13-11-net-resolve-forwarding-path-from-virtual-netdevice-a.patch 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. From: Pablo Neira Ayuso <[email protected]>
  2. Date: Wed, 24 Mar 2021 02:30:32 +0100
  3. Subject: [PATCH] net: resolve forwarding path from virtual netdevice and
  4. HW destination address
  5. This patch adds dev_fill_forward_path() which resolves the path to reach
  6. the real netdevice from the IP forwarding side. This function takes as
  7. input the netdevice and the destination hardware address and it walks
  8. down the devices calling .ndo_fill_forward_path() for each device until
  9. the real device is found.
  10. For instance, assuming the following topology:
  11. IP forwarding
  12. / \
  13. br0 eth0
  14. / \
  15. eth1 eth2
  16. .
  17. .
  18. .
  19. ethX
  20. ab:cd:ef:ab:cd:ef
  21. where eth1 and eth2 are bridge ports and eth0 provides WAN connectivity.
  22. ethX is the interface in another box which is connected to the eth1
  23. bridge port.
  24. For packets going through IP forwarding to br0 whose destination MAC
  25. address is ab:cd:ef:ab:cd:ef, dev_fill_forward_path() provides the
  26. following path:
  27. br0 -> eth1
  28. .ndo_fill_forward_path for br0 looks up at the FDB for the bridge port
  29. from the destination MAC address to get the bridge port eth1.
  30. This information allows to create a fast path that bypasses the classic
  31. bridge and IP forwarding paths, so packets go directly from the bridge
  32. port eth1 to eth0 (wan interface) and vice versa.
  33. fast path
  34. .------------------------.
  35. / \
  36. | IP forwarding |
  37. | / \ \/
  38. | br0 eth0
  39. . / \
  40. -> eth1 eth2
  41. .
  42. .
  43. .
  44. ethX
  45. ab:cd:ef:ab:cd:ef
  46. Signed-off-by: Pablo Neira Ayuso <[email protected]>
  47. ---
  48. --- a/include/linux/netdevice.h
  49. +++ b/include/linux/netdevice.h
  50. @@ -848,6 +848,27 @@ typedef u16 (*select_queue_fallback_t)(s
  51. struct sk_buff *skb,
  52. struct net_device *sb_dev);
  53. +enum net_device_path_type {
  54. + DEV_PATH_ETHERNET = 0,
  55. +};
  56. +
  57. +struct net_device_path {
  58. + enum net_device_path_type type;
  59. + const struct net_device *dev;
  60. +};
  61. +
  62. +#define NET_DEVICE_PATH_STACK_MAX 5
  63. +
  64. +struct net_device_path_stack {
  65. + int num_paths;
  66. + struct net_device_path path[NET_DEVICE_PATH_STACK_MAX];
  67. +};
  68. +
  69. +struct net_device_path_ctx {
  70. + const struct net_device *dev;
  71. + const u8 *daddr;
  72. +};
  73. +
  74. enum tc_setup_type {
  75. TC_SETUP_QDISC_MQPRIO,
  76. TC_SETUP_CLSU32,
  77. @@ -1294,6 +1315,8 @@ struct netdev_net_notifier {
  78. * struct net_device *(*ndo_get_peer_dev)(struct net_device *dev);
  79. * If a device is paired with a peer device, return the peer instance.
  80. * The caller must be under RCU read context.
  81. + * int (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx, struct net_device_path *path);
  82. + * Get the forwarding path to reach the real device from the HW destination address
  83. */
  84. struct net_device_ops {
  85. int (*ndo_init)(struct net_device *dev);
  86. @@ -1502,6 +1525,8 @@ struct net_device_ops {
  87. int (*ndo_tunnel_ctl)(struct net_device *dev,
  88. struct ip_tunnel_parm *p, int cmd);
  89. struct net_device * (*ndo_get_peer_dev)(struct net_device *dev);
  90. + int (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx,
  91. + struct net_device_path *path);
  92. };
  93. /**
  94. @@ -2849,6 +2874,8 @@ void dev_remove_offload(struct packet_of
  95. int dev_get_iflink(const struct net_device *dev);
  96. int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb);
  97. +int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
  98. + struct net_device_path_stack *stack);
  99. struct net_device *__dev_get_by_flags(struct net *net, unsigned short flags,
  100. unsigned short mask);
  101. struct net_device *dev_get_by_name(struct net *net, const char *name);
  102. --- a/net/core/dev.c
  103. +++ b/net/core/dev.c
  104. @@ -847,6 +847,52 @@ int dev_fill_metadata_dst(struct net_dev
  105. }
  106. EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
  107. +static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack)
  108. +{
  109. + int k = stack->num_paths++;
  110. +
  111. + if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX))
  112. + return NULL;
  113. +
  114. + return &stack->path[k];
  115. +}
  116. +
  117. +int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
  118. + struct net_device_path_stack *stack)
  119. +{
  120. + const struct net_device *last_dev;
  121. + struct net_device_path_ctx ctx = {
  122. + .dev = dev,
  123. + .daddr = daddr,
  124. + };
  125. + struct net_device_path *path;
  126. + int ret = 0;
  127. +
  128. + stack->num_paths = 0;
  129. + while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) {
  130. + last_dev = ctx.dev;
  131. + path = dev_fwd_path(stack);
  132. + if (!path)
  133. + return -1;
  134. +
  135. + memset(path, 0, sizeof(struct net_device_path));
  136. + ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path);
  137. + if (ret < 0)
  138. + return -1;
  139. +
  140. + if (WARN_ON_ONCE(last_dev == ctx.dev))
  141. + return -1;
  142. + }
  143. + path = dev_fwd_path(stack);
  144. + if (!path)
  145. + return -1;
  146. + path->type = DEV_PATH_ETHERNET;
  147. + path->dev = ctx.dev;
  148. +
  149. + return ret;
  150. +}
  151. +EXPORT_SYMBOL_GPL(dev_fill_forward_path);
  152. +
  153. /**
  154. * __dev_get_by_name - find a device by its name
  155. * @net: the applicable net namespace