Skip to content

Commit cc4e383

Browse files
kuba-moodavem330
authored andcommitted
udp_tunnel: add central NIC RX port offload infrastructure
Cater to devices which: (a) may want to sleep in the callbacks; (b) only have IPv4 support; (c) need all the programming to happen while the netdev is up. Drivers attach UDP tunnel offload info struct to their netdevs, where they declare how many UDP ports of various tunnel types they support. Core takes care of tracking which ports to offload. Use a fixed-size array since this matches what almost all drivers do, and avoids a complexity and uncertainty around memory allocations in an atomic context. Make sure that tunnel drivers don't try to replay the ports when new NIC netdev is registered. Automatic replays would mess up reference counting, and will be removed completely once all drivers are converted. v4: - use a #define NULL to avoid build issues with CONFIG_INET=n. Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 84a4160 commit cc4e383

File tree

8 files changed

+983
-5
lines changed

8 files changed

+983
-5
lines changed

drivers/net/geneve.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,9 +1796,11 @@ static int geneve_netdevice_event(struct notifier_block *unused,
17961796
event == NETDEV_UDP_TUNNEL_DROP_INFO) {
17971797
geneve_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
17981798
} else if (event == NETDEV_UNREGISTER) {
1799-
geneve_offload_rx_ports(dev, false);
1799+
if (!dev->udp_tunnel_nic_info)
1800+
geneve_offload_rx_ports(dev, false);
18001801
} else if (event == NETDEV_REGISTER) {
1801-
geneve_offload_rx_ports(dev, true);
1802+
if (!dev->udp_tunnel_nic_info)
1803+
geneve_offload_rx_ports(dev, true);
18021804
}
18031805

18041806
return NOTIFY_DONE;

drivers/net/vxlan.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4477,10 +4477,12 @@ static int vxlan_netdevice_event(struct notifier_block *unused,
44774477
struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
44784478

44794479
if (event == NETDEV_UNREGISTER) {
4480-
vxlan_offload_rx_ports(dev, false);
4480+
if (!dev->udp_tunnel_nic_info)
4481+
vxlan_offload_rx_ports(dev, false);
44814482
vxlan_handle_lowerdev_unregister(vn, dev);
44824483
} else if (event == NETDEV_REGISTER) {
4483-
vxlan_offload_rx_ports(dev, true);
4484+
if (!dev->udp_tunnel_nic_info)
4485+
vxlan_offload_rx_ports(dev, true);
44844486
} else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
44854487
event == NETDEV_UDP_TUNNEL_DROP_INFO) {
44864488
vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);

include/linux/netdevice.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ struct wpan_dev;
6565
struct mpls_dev;
6666
/* UDP Tunnel offloads */
6767
struct udp_tunnel_info;
68+
struct udp_tunnel_nic_info;
69+
struct udp_tunnel_nic;
6870
struct bpf_prog;
6971
struct xdp_buff;
7072

@@ -1836,6 +1838,10 @@ enum netdev_priv_flags {
18361838
*
18371839
* @macsec_ops: MACsec offloading ops
18381840
*
1841+
* @udp_tunnel_nic_info: static structure describing the UDP tunnel
1842+
* offload capabilities of the device
1843+
* @udp_tunnel_nic: UDP tunnel offload state
1844+
*
18391845
* FIXME: cleanup struct net_device such that network protocol info
18401846
* moves out.
18411847
*/
@@ -2134,6 +2140,8 @@ struct net_device {
21342140
/* MACsec management functions */
21352141
const struct macsec_ops *macsec_ops;
21362142
#endif
2143+
const struct udp_tunnel_nic_info *udp_tunnel_nic_info;
2144+
struct udp_tunnel_nic *udp_tunnel_nic;
21372145
};
21382146
#define to_net_dev(d) container_of(d, struct net_device, dev)
21392147

include/net/udp_tunnel.h

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct udp_tunnel_info {
115115
unsigned short type;
116116
sa_family_t sa_family;
117117
__be16 port;
118+
u8 hw_priv;
118119
};
119120

120121
/* Notify network devices of offloadable types */
@@ -181,4 +182,140 @@ static inline void udp_tunnel_encap_enable(struct socket *sock)
181182
udp_encap_enable();
182183
}
183184

185+
#define UDP_TUNNEL_NIC_MAX_TABLES 4
186+
187+
enum udp_tunnel_nic_info_flags {
188+
/* Device callbacks may sleep */
189+
UDP_TUNNEL_NIC_INFO_MAY_SLEEP = BIT(0),
190+
/* Device only supports offloads when it's open, all ports
191+
* will be removed before close and re-added after open.
192+
*/
193+
UDP_TUNNEL_NIC_INFO_OPEN_ONLY = BIT(1),
194+
/* Device supports only IPv4 tunnels */
195+
UDP_TUNNEL_NIC_INFO_IPV4_ONLY = BIT(2),
196+
};
197+
198+
/**
199+
* struct udp_tunnel_nic_info - driver UDP tunnel offload information
200+
* @set_port: callback for adding a new port
201+
* @unset_port: callback for removing a port
202+
* @sync_table: callback for syncing the entire port table at once
203+
* @flags: device flags from enum udp_tunnel_nic_info_flags
204+
* @tables: UDP port tables this device has
205+
* @tables.n_entries: number of entries in this table
206+
* @tables.tunnel_types: types of tunnels this table accepts
207+
*
208+
* Drivers are expected to provide either @set_port and @unset_port callbacks
209+
* or the @sync_table callback. Callbacks are invoked with rtnl lock held.
210+
*
211+
* Known limitations:
212+
* - UDP tunnel port notifications are fundamentally best-effort -
213+
* it is likely the driver will both see skbs which use a UDP tunnel port,
214+
* while not being a tunneled skb, and tunnel skbs from other ports -
215+
* drivers should only use these ports for non-critical RX-side offloads,
216+
* e.g. the checksum offload;
217+
* - none of the devices care about the socket family at present, so we don't
218+
* track it. Please extend this code if you care.
219+
*/
220+
struct udp_tunnel_nic_info {
221+
/* one-by-one */
222+
int (*set_port)(struct net_device *dev,
223+
unsigned int table, unsigned int entry,
224+
struct udp_tunnel_info *ti);
225+
int (*unset_port)(struct net_device *dev,
226+
unsigned int table, unsigned int entry,
227+
struct udp_tunnel_info *ti);
228+
229+
/* all at once */
230+
int (*sync_table)(struct net_device *dev, unsigned int table);
231+
232+
unsigned int flags;
233+
234+
struct udp_tunnel_nic_table_info {
235+
unsigned int n_entries;
236+
unsigned int tunnel_types;
237+
} tables[UDP_TUNNEL_NIC_MAX_TABLES];
238+
};
239+
240+
/* UDP tunnel module dependencies
241+
*
242+
* Tunnel drivers are expected to have a hard dependency on the udp_tunnel
243+
* module. NIC drivers are not, they just attach their
244+
* struct udp_tunnel_nic_info to the netdev and wait for callbacks to come.
245+
* Loading a tunnel driver will cause the udp_tunnel module to be loaded
246+
* and only then will all the required state structures be allocated.
247+
* Since we want a weak dependency from the drivers and the core to udp_tunnel
248+
* we call things through the following stubs.
249+
*/
250+
struct udp_tunnel_nic_ops {
251+
void (*get_port)(struct net_device *dev, unsigned int table,
252+
unsigned int idx, struct udp_tunnel_info *ti);
253+
void (*set_port_priv)(struct net_device *dev, unsigned int table,
254+
unsigned int idx, u8 priv);
255+
void (*add_port)(struct net_device *dev, struct udp_tunnel_info *ti);
256+
void (*del_port)(struct net_device *dev, struct udp_tunnel_info *ti);
257+
void (*reset_ntf)(struct net_device *dev);
258+
};
259+
260+
#ifdef CONFIG_INET
261+
extern const struct udp_tunnel_nic_ops *udp_tunnel_nic_ops;
262+
#else
263+
#define udp_tunnel_nic_ops ((struct udp_tunnel_nic_ops *)NULL)
264+
#endif
265+
266+
static inline void
267+
udp_tunnel_nic_get_port(struct net_device *dev, unsigned int table,
268+
unsigned int idx, struct udp_tunnel_info *ti)
269+
{
270+
/* This helper is used from .sync_table, we indicate empty entries
271+
* by zero'ed @ti. Drivers which need to know the details of a port
272+
* when it gets deleted should use the .set_port / .unset_port
273+
* callbacks.
274+
* Zero out here, otherwise !CONFIG_INET causes uninitilized warnings.
275+
*/
276+
memset(ti, 0, sizeof(*ti));
277+
278+
if (udp_tunnel_nic_ops)
279+
udp_tunnel_nic_ops->get_port(dev, table, idx, ti);
280+
}
281+
282+
static inline void
283+
udp_tunnel_nic_set_port_priv(struct net_device *dev, unsigned int table,
284+
unsigned int idx, u8 priv)
285+
{
286+
if (udp_tunnel_nic_ops)
287+
udp_tunnel_nic_ops->set_port_priv(dev, table, idx, priv);
288+
}
289+
290+
static inline void
291+
udp_tunnel_nic_add_port(struct net_device *dev, struct udp_tunnel_info *ti)
292+
{
293+
if (udp_tunnel_nic_ops)
294+
udp_tunnel_nic_ops->add_port(dev, ti);
295+
}
296+
297+
static inline void
298+
udp_tunnel_nic_del_port(struct net_device *dev, struct udp_tunnel_info *ti)
299+
{
300+
if (udp_tunnel_nic_ops)
301+
udp_tunnel_nic_ops->del_port(dev, ti);
302+
}
303+
304+
/**
305+
* udp_tunnel_nic_reset_ntf() - device-originating reset notification
306+
* @dev: network interface device structure
307+
*
308+
* Called by the driver to inform the core that the entire UDP tunnel port
309+
* state has been lost, usually due to device reset. Core will assume device
310+
* forgot all the ports and issue .set_port and .sync_table callbacks as
311+
* necessary.
312+
*
313+
* This function must be called with rtnl lock held, and will issue all
314+
* the callbacks before returning.
315+
*/
316+
static inline void udp_tunnel_nic_reset_ntf(struct net_device *dev)
317+
{
318+
if (udp_tunnel_nic_ops)
319+
udp_tunnel_nic_ops->reset_ntf(dev);
320+
}
184321
#endif

net/ipv4/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ obj-y := route.o inetpeer.o protocol.o \
1414
udp_offload.o arp.o icmp.o devinet.o af_inet.o igmp.o \
1515
fib_frontend.o fib_semantics.o fib_trie.o fib_notifier.o \
1616
inet_fragment.o ping.o ip_tunnel_core.o gre_offload.o \
17-
metrics.o netlink.o nexthop.o
17+
metrics.o netlink.o nexthop.o udp_tunnel_stub.o
1818

1919
obj-$(CONFIG_BPFILTER) += bpfilter/
2020

@@ -29,6 +29,7 @@ gre-y := gre_demux.o
2929
obj-$(CONFIG_NET_FOU) += fou.o
3030
obj-$(CONFIG_NET_IPGRE_DEMUX) += gre.o
3131
obj-$(CONFIG_NET_IPGRE) += ip_gre.o
32+
udp_tunnel-y := udp_tunnel_core.o udp_tunnel_nic.o
3233
obj-$(CONFIG_NET_UDP_TUNNEL) += udp_tunnel.o
3334
obj-$(CONFIG_NET_IPVTI) += ip_vti.o
3435
obj-$(CONFIG_SYN_COOKIES) += syncookies.o
File renamed without changes.

0 commit comments

Comments
 (0)