1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Facebook Inc.
4 #include <linux/ethtool_netlink.h>
5 #include <linux/netdevice.h>
6 #include <linux/slab.h>
7 #include <linux/types.h>
8 #include <linux/workqueue.h>
9 #include <net/udp_tunnel.h>
10 #include <net/vxlan.h>
12 enum udp_tunnel_nic_table_entry_flags {
13 UDP_TUNNEL_NIC_ENTRY_ADD = BIT(0),
14 UDP_TUNNEL_NIC_ENTRY_DEL = BIT(1),
15 UDP_TUNNEL_NIC_ENTRY_OP_FAIL = BIT(2),
16 UDP_TUNNEL_NIC_ENTRY_FROZEN = BIT(3),
19 struct udp_tunnel_nic_table_entry {
28 * struct udp_tunnel_nic - UDP tunnel port offload state
29 * @work: async work for talking to hardware from process context
30 * @dev: netdev pointer
31 * @need_sync: at least one port start changed
32 * @need_replay: space was freed, we need a replay of all ports
33 * @work_pending: @work is currently scheduled
34 * @n_tables: number of tables under @entries
35 * @missed: bitmap of tables which overflown
36 * @entries: table of tables of ports currently offloaded
38 struct udp_tunnel_nic {
39 struct work_struct work;
41 struct net_device *dev;
47 unsigned int n_tables;
49 struct udp_tunnel_nic_table_entry **entries;
52 /* We ensure all work structs are done using driver state, but not the code.
53 * We need a workqueue we can flush before module gets removed.
55 static struct workqueue_struct *udp_tunnel_nic_workqueue;
57 static const char *udp_tunnel_nic_tunnel_type_name(unsigned int type)
60 case UDP_TUNNEL_TYPE_VXLAN:
62 case UDP_TUNNEL_TYPE_GENEVE:
64 case UDP_TUNNEL_TYPE_VXLAN_GPE:
72 udp_tunnel_nic_entry_is_free(struct udp_tunnel_nic_table_entry *entry)
74 return entry->use_cnt == 0 && !entry->flags;
78 udp_tunnel_nic_entry_is_present(struct udp_tunnel_nic_table_entry *entry)
80 return entry->use_cnt && !(entry->flags & ~UDP_TUNNEL_NIC_ENTRY_FROZEN);
84 udp_tunnel_nic_entry_is_frozen(struct udp_tunnel_nic_table_entry *entry)
86 return entry->flags & UDP_TUNNEL_NIC_ENTRY_FROZEN;
90 udp_tunnel_nic_entry_freeze_used(struct udp_tunnel_nic_table_entry *entry)
92 if (!udp_tunnel_nic_entry_is_free(entry))
93 entry->flags |= UDP_TUNNEL_NIC_ENTRY_FROZEN;
97 udp_tunnel_nic_entry_unfreeze(struct udp_tunnel_nic_table_entry *entry)
99 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_FROZEN;
103 udp_tunnel_nic_entry_is_queued(struct udp_tunnel_nic_table_entry *entry)
105 return entry->flags & (UDP_TUNNEL_NIC_ENTRY_ADD |
106 UDP_TUNNEL_NIC_ENTRY_DEL);
110 udp_tunnel_nic_entry_queue(struct udp_tunnel_nic *utn,
111 struct udp_tunnel_nic_table_entry *entry,
114 entry->flags |= flag;
119 udp_tunnel_nic_ti_from_entry(struct udp_tunnel_nic_table_entry *entry,
120 struct udp_tunnel_info *ti)
122 memset(ti, 0, sizeof(*ti));
123 ti->port = entry->port;
124 ti->type = entry->type;
125 ti->hw_priv = entry->hw_priv;
129 udp_tunnel_nic_is_empty(struct net_device *dev, struct udp_tunnel_nic *utn)
131 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
134 for (i = 0; i < utn->n_tables; i++)
135 for (j = 0; j < info->tables[i].n_entries; j++)
136 if (!udp_tunnel_nic_entry_is_free(&utn->entries[i][j]))
142 udp_tunnel_nic_should_replay(struct net_device *dev, struct udp_tunnel_nic *utn)
144 const struct udp_tunnel_nic_table_info *table;
150 for (i = 0; i < utn->n_tables; i++) {
151 table = &dev->udp_tunnel_nic_info->tables[i];
152 if (!test_bit(i, &utn->missed))
155 for (j = 0; j < table->n_entries; j++)
156 if (udp_tunnel_nic_entry_is_free(&utn->entries[i][j]))
164 __udp_tunnel_nic_get_port(struct net_device *dev, unsigned int table,
165 unsigned int idx, struct udp_tunnel_info *ti)
167 struct udp_tunnel_nic_table_entry *entry;
168 struct udp_tunnel_nic *utn;
170 utn = dev->udp_tunnel_nic;
171 entry = &utn->entries[table][idx];
174 udp_tunnel_nic_ti_from_entry(entry, ti);
178 __udp_tunnel_nic_set_port_priv(struct net_device *dev, unsigned int table,
179 unsigned int idx, u8 priv)
181 dev->udp_tunnel_nic->entries[table][idx].hw_priv = priv;
185 udp_tunnel_nic_entry_update_done(struct udp_tunnel_nic_table_entry *entry,
188 bool dodgy = entry->flags & UDP_TUNNEL_NIC_ENTRY_OP_FAIL;
190 WARN_ON_ONCE(entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD &&
191 entry->flags & UDP_TUNNEL_NIC_ENTRY_DEL);
193 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD &&
194 (!err || (err == -EEXIST && dodgy)))
195 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_ADD;
197 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_DEL &&
198 (!err || (err == -ENOENT && dodgy)))
199 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_DEL;
202 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_OP_FAIL;
204 entry->flags |= UDP_TUNNEL_NIC_ENTRY_OP_FAIL;
208 udp_tunnel_nic_device_sync_one(struct net_device *dev,
209 struct udp_tunnel_nic *utn,
210 unsigned int table, unsigned int idx)
212 struct udp_tunnel_nic_table_entry *entry;
213 struct udp_tunnel_info ti;
216 entry = &utn->entries[table][idx];
217 if (!udp_tunnel_nic_entry_is_queued(entry))
220 udp_tunnel_nic_ti_from_entry(entry, &ti);
221 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD)
222 err = dev->udp_tunnel_nic_info->set_port(dev, table, idx, &ti);
224 err = dev->udp_tunnel_nic_info->unset_port(dev, table, idx,
226 udp_tunnel_nic_entry_update_done(entry, err);
230 "UDP tunnel port sync failed port %d type %s: %d\n",
231 be16_to_cpu(entry->port),
232 udp_tunnel_nic_tunnel_type_name(entry->type),
237 udp_tunnel_nic_device_sync_by_port(struct net_device *dev,
238 struct udp_tunnel_nic *utn)
240 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
243 for (i = 0; i < utn->n_tables; i++)
244 for (j = 0; j < info->tables[i].n_entries; j++)
245 udp_tunnel_nic_device_sync_one(dev, utn, i, j);
249 udp_tunnel_nic_device_sync_by_table(struct net_device *dev,
250 struct udp_tunnel_nic *utn)
252 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
256 for (i = 0; i < utn->n_tables; i++) {
257 /* Find something that needs sync in this table */
258 for (j = 0; j < info->tables[i].n_entries; j++)
259 if (udp_tunnel_nic_entry_is_queued(&utn->entries[i][j]))
261 if (j == info->tables[i].n_entries)
264 err = info->sync_table(dev, i);
266 netdev_warn(dev, "UDP tunnel port sync failed for table %d: %d\n",
269 for (j = 0; j < info->tables[i].n_entries; j++) {
270 struct udp_tunnel_nic_table_entry *entry;
272 entry = &utn->entries[i][j];
273 if (udp_tunnel_nic_entry_is_queued(entry))
274 udp_tunnel_nic_entry_update_done(entry, err);
280 __udp_tunnel_nic_device_sync(struct net_device *dev, struct udp_tunnel_nic *utn)
285 if (dev->udp_tunnel_nic_info->sync_table)
286 udp_tunnel_nic_device_sync_by_table(dev, utn);
288 udp_tunnel_nic_device_sync_by_port(dev, utn);
291 /* Can't replay directly here, in case we come from the tunnel driver's
292 * notification - trying to replay may deadlock inside tunnel driver.
294 utn->need_replay = udp_tunnel_nic_should_replay(dev, utn);
298 udp_tunnel_nic_device_sync(struct net_device *dev, struct udp_tunnel_nic *utn)
300 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
306 /* Drivers which sleep in the callback need to update from
307 * the workqueue, if we come from the tunnel driver's notification.
309 may_sleep = info->flags & UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
311 __udp_tunnel_nic_device_sync(dev, utn);
312 if (may_sleep || utn->need_replay) {
313 queue_work(udp_tunnel_nic_workqueue, &utn->work);
314 utn->work_pending = 1;
319 udp_tunnel_nic_table_is_capable(const struct udp_tunnel_nic_table_info *table,
320 struct udp_tunnel_info *ti)
322 return table->tunnel_types & ti->type;
326 udp_tunnel_nic_is_capable(struct net_device *dev, struct udp_tunnel_nic *utn,
327 struct udp_tunnel_info *ti)
329 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
332 /* Special case IPv4-only NICs */
333 if (info->flags & UDP_TUNNEL_NIC_INFO_IPV4_ONLY &&
334 ti->sa_family != AF_INET)
337 for (i = 0; i < utn->n_tables; i++)
338 if (udp_tunnel_nic_table_is_capable(&info->tables[i], ti))
344 udp_tunnel_nic_has_collision(struct net_device *dev, struct udp_tunnel_nic *utn,
345 struct udp_tunnel_info *ti)
347 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
348 struct udp_tunnel_nic_table_entry *entry;
351 for (i = 0; i < utn->n_tables; i++)
352 for (j = 0; j < info->tables[i].n_entries; j++) {
353 entry = &utn->entries[i][j];
355 if (!udp_tunnel_nic_entry_is_free(entry) &&
356 entry->port == ti->port &&
357 entry->type != ti->type) {
358 __set_bit(i, &utn->missed);
366 udp_tunnel_nic_entry_adj(struct udp_tunnel_nic *utn,
367 unsigned int table, unsigned int idx, int use_cnt_adj)
369 struct udp_tunnel_nic_table_entry *entry = &utn->entries[table][idx];
370 bool dodgy = entry->flags & UDP_TUNNEL_NIC_ENTRY_OP_FAIL;
371 unsigned int from, to;
373 /* If not going from used to unused or vice versa - all done.
374 * For dodgy entries make sure we try to sync again (queue the entry).
376 entry->use_cnt += use_cnt_adj;
377 if (!dodgy && !entry->use_cnt == !(entry->use_cnt - use_cnt_adj))
380 /* Cancel the op before it was sent to the device, if possible,
381 * otherwise we'd need to take special care to issue commands
382 * in the same order the ports arrived.
384 if (use_cnt_adj < 0) {
385 from = UDP_TUNNEL_NIC_ENTRY_ADD;
386 to = UDP_TUNNEL_NIC_ENTRY_DEL;
388 from = UDP_TUNNEL_NIC_ENTRY_DEL;
389 to = UDP_TUNNEL_NIC_ENTRY_ADD;
392 if (entry->flags & from) {
393 entry->flags &= ~from;
398 udp_tunnel_nic_entry_queue(utn, entry, to);
402 udp_tunnel_nic_entry_try_adj(struct udp_tunnel_nic *utn,
403 unsigned int table, unsigned int idx,
404 struct udp_tunnel_info *ti, int use_cnt_adj)
406 struct udp_tunnel_nic_table_entry *entry = &utn->entries[table][idx];
408 if (udp_tunnel_nic_entry_is_free(entry) ||
409 entry->port != ti->port ||
410 entry->type != ti->type)
413 if (udp_tunnel_nic_entry_is_frozen(entry))
416 udp_tunnel_nic_entry_adj(utn, table, idx, use_cnt_adj);
420 /* Try to find existing matching entry and adjust its use count, instead of
421 * adding a new one. Returns true if entry was found. In case of delete the
422 * entry may have gotten removed in the process, in which case it will be
423 * queued for removal.
426 udp_tunnel_nic_try_existing(struct net_device *dev, struct udp_tunnel_nic *utn,
427 struct udp_tunnel_info *ti, int use_cnt_adj)
429 const struct udp_tunnel_nic_table_info *table;
432 for (i = 0; i < utn->n_tables; i++) {
433 table = &dev->udp_tunnel_nic_info->tables[i];
434 if (!udp_tunnel_nic_table_is_capable(table, ti))
437 for (j = 0; j < table->n_entries; j++)
438 if (udp_tunnel_nic_entry_try_adj(utn, i, j, ti,
447 udp_tunnel_nic_add_existing(struct net_device *dev, struct udp_tunnel_nic *utn,
448 struct udp_tunnel_info *ti)
450 return udp_tunnel_nic_try_existing(dev, utn, ti, +1);
454 udp_tunnel_nic_del_existing(struct net_device *dev, struct udp_tunnel_nic *utn,
455 struct udp_tunnel_info *ti)
457 return udp_tunnel_nic_try_existing(dev, utn, ti, -1);
461 udp_tunnel_nic_add_new(struct net_device *dev, struct udp_tunnel_nic *utn,
462 struct udp_tunnel_info *ti)
464 const struct udp_tunnel_nic_table_info *table;
467 for (i = 0; i < utn->n_tables; i++) {
468 table = &dev->udp_tunnel_nic_info->tables[i];
469 if (!udp_tunnel_nic_table_is_capable(table, ti))
472 for (j = 0; j < table->n_entries; j++) {
473 struct udp_tunnel_nic_table_entry *entry;
475 entry = &utn->entries[i][j];
476 if (!udp_tunnel_nic_entry_is_free(entry))
479 entry->port = ti->port;
480 entry->type = ti->type;
482 udp_tunnel_nic_entry_queue(utn, entry,
483 UDP_TUNNEL_NIC_ENTRY_ADD);
487 /* The different table may still fit this port in, but there
488 * are no devices currently which have multiple tables accepting
489 * the same tunnel type, and false positives are okay.
491 __set_bit(i, &utn->missed);
498 __udp_tunnel_nic_add_port(struct net_device *dev, struct udp_tunnel_info *ti)
500 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
501 struct udp_tunnel_nic *utn;
503 utn = dev->udp_tunnel_nic;
506 if (!netif_running(dev) && info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY)
508 if (info->flags & UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN &&
509 ti->port == htons(IANA_VXLAN_UDP_PORT)) {
510 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
511 netdev_warn(dev, "device assumes port 4789 will be used by vxlan tunnels\n");
515 if (!udp_tunnel_nic_is_capable(dev, utn, ti))
518 /* It may happen that a tunnel of one type is removed and different
519 * tunnel type tries to reuse its port before the device was informed.
520 * Rely on utn->missed to re-add this port later.
522 if (udp_tunnel_nic_has_collision(dev, utn, ti))
525 if (!udp_tunnel_nic_add_existing(dev, utn, ti))
526 udp_tunnel_nic_add_new(dev, utn, ti);
528 udp_tunnel_nic_device_sync(dev, utn);
532 __udp_tunnel_nic_del_port(struct net_device *dev, struct udp_tunnel_info *ti)
534 struct udp_tunnel_nic *utn;
536 utn = dev->udp_tunnel_nic;
540 if (!udp_tunnel_nic_is_capable(dev, utn, ti))
543 udp_tunnel_nic_del_existing(dev, utn, ti);
545 udp_tunnel_nic_device_sync(dev, utn);
548 static void __udp_tunnel_nic_reset_ntf(struct net_device *dev)
550 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
551 struct udp_tunnel_nic *utn;
556 utn = dev->udp_tunnel_nic;
560 utn->need_sync = false;
561 for (i = 0; i < utn->n_tables; i++)
562 for (j = 0; j < info->tables[i].n_entries; j++) {
563 struct udp_tunnel_nic_table_entry *entry;
565 entry = &utn->entries[i][j];
567 entry->flags &= ~(UDP_TUNNEL_NIC_ENTRY_DEL |
568 UDP_TUNNEL_NIC_ENTRY_OP_FAIL);
569 /* We don't release rtnl across ops */
570 WARN_ON(entry->flags & UDP_TUNNEL_NIC_ENTRY_FROZEN);
574 udp_tunnel_nic_entry_queue(utn, entry,
575 UDP_TUNNEL_NIC_ENTRY_ADD);
578 __udp_tunnel_nic_device_sync(dev, utn);
582 __udp_tunnel_nic_dump_size(struct net_device *dev, unsigned int table)
584 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
585 struct udp_tunnel_nic *utn;
589 utn = dev->udp_tunnel_nic;
594 for (j = 0; j < info->tables[table].n_entries; j++) {
595 if (!udp_tunnel_nic_entry_is_present(&utn->entries[table][j]))
598 size += nla_total_size(0) + /* _TABLE_ENTRY */
599 nla_total_size(sizeof(__be16)) + /* _ENTRY_PORT */
600 nla_total_size(sizeof(u32)); /* _ENTRY_TYPE */
607 __udp_tunnel_nic_dump_write(struct net_device *dev, unsigned int table,
610 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
611 struct udp_tunnel_nic *utn;
615 utn = dev->udp_tunnel_nic;
619 for (j = 0; j < info->tables[table].n_entries; j++) {
620 if (!udp_tunnel_nic_entry_is_present(&utn->entries[table][j]))
623 nest = nla_nest_start(skb, ETHTOOL_A_TUNNEL_UDP_TABLE_ENTRY);
625 if (nla_put_be16(skb, ETHTOOL_A_TUNNEL_UDP_ENTRY_PORT,
626 utn->entries[table][j].port) ||
627 nla_put_u32(skb, ETHTOOL_A_TUNNEL_UDP_ENTRY_TYPE,
628 ilog2(utn->entries[table][j].type)))
631 nla_nest_end(skb, nest);
637 nla_nest_cancel(skb, nest);
641 static const struct udp_tunnel_nic_ops __udp_tunnel_nic_ops = {
642 .get_port = __udp_tunnel_nic_get_port,
643 .set_port_priv = __udp_tunnel_nic_set_port_priv,
644 .add_port = __udp_tunnel_nic_add_port,
645 .del_port = __udp_tunnel_nic_del_port,
646 .reset_ntf = __udp_tunnel_nic_reset_ntf,
647 .dump_size = __udp_tunnel_nic_dump_size,
648 .dump_write = __udp_tunnel_nic_dump_write,
652 udp_tunnel_nic_flush(struct net_device *dev, struct udp_tunnel_nic *utn)
654 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
657 for (i = 0; i < utn->n_tables; i++)
658 for (j = 0; j < info->tables[i].n_entries; j++) {
659 int adj_cnt = -utn->entries[i][j].use_cnt;
662 udp_tunnel_nic_entry_adj(utn, i, j, adj_cnt);
665 __udp_tunnel_nic_device_sync(dev, utn);
667 for (i = 0; i < utn->n_tables; i++)
668 memset(utn->entries[i], 0, array_size(info->tables[i].n_entries,
669 sizeof(**utn->entries)));
670 WARN_ON(utn->need_sync);
671 utn->need_replay = 0;
675 udp_tunnel_nic_replay(struct net_device *dev, struct udp_tunnel_nic *utn)
677 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
680 /* Freeze all the ports we are already tracking so that the replay
681 * does not double up the refcount.
683 for (i = 0; i < utn->n_tables; i++)
684 for (j = 0; j < info->tables[i].n_entries; j++)
685 udp_tunnel_nic_entry_freeze_used(&utn->entries[i][j]);
687 utn->need_replay = 0;
689 udp_tunnel_get_rx_info(dev);
691 for (i = 0; i < utn->n_tables; i++)
692 for (j = 0; j < info->tables[i].n_entries; j++)
693 udp_tunnel_nic_entry_unfreeze(&utn->entries[i][j]);
696 static void udp_tunnel_nic_device_sync_work(struct work_struct *work)
698 struct udp_tunnel_nic *utn =
699 container_of(work, struct udp_tunnel_nic, work);
702 utn->work_pending = 0;
703 __udp_tunnel_nic_device_sync(utn->dev, utn);
705 if (utn->need_replay)
706 udp_tunnel_nic_replay(utn->dev, utn);
710 static struct udp_tunnel_nic *
711 udp_tunnel_nic_alloc(const struct udp_tunnel_nic_info *info,
712 unsigned int n_tables)
714 struct udp_tunnel_nic *utn;
717 utn = kzalloc(sizeof(*utn), GFP_KERNEL);
720 utn->n_tables = n_tables;
721 INIT_WORK(&utn->work, udp_tunnel_nic_device_sync_work);
723 utn->entries = kmalloc_array(n_tables, sizeof(void *), GFP_KERNEL);
727 for (i = 0; i < n_tables; i++) {
728 utn->entries[i] = kcalloc(info->tables[i].n_entries,
729 sizeof(*utn->entries[i]), GFP_KERNEL);
730 if (!utn->entries[i])
731 goto err_free_prev_entries;
736 err_free_prev_entries:
738 kfree(utn->entries[i]);
745 static int udp_tunnel_nic_register(struct net_device *dev)
747 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
748 struct udp_tunnel_nic *utn;
749 unsigned int n_tables, i;
751 BUILD_BUG_ON(sizeof(utn->missed) * BITS_PER_BYTE <
752 UDP_TUNNEL_NIC_MAX_TABLES);
754 if (WARN_ON(!info->set_port != !info->unset_port) ||
755 WARN_ON(!info->set_port == !info->sync_table) ||
756 WARN_ON(!info->tables[0].n_entries))
760 for (i = 1; i < UDP_TUNNEL_NIC_MAX_TABLES; i++) {
761 if (!info->tables[i].n_entries)
765 if (WARN_ON(!info->tables[i - 1].n_entries))
769 utn = udp_tunnel_nic_alloc(info, n_tables);
775 dev->udp_tunnel_nic = utn;
777 if (!(info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY))
778 udp_tunnel_get_rx_info(dev);
784 udp_tunnel_nic_unregister(struct net_device *dev, struct udp_tunnel_nic *utn)
788 /* Flush before we check work, so we don't waste time adding entries
789 * from the work which we will boot immediately.
791 udp_tunnel_nic_flush(dev, utn);
793 /* Wait for the work to be done using the state, netdev core will
794 * retry unregister until we give up our reference on this device.
796 if (utn->work_pending)
799 for (i = 0; i < utn->n_tables; i++)
800 kfree(utn->entries[i]);
803 dev->udp_tunnel_nic = NULL;
808 udp_tunnel_nic_netdevice_event(struct notifier_block *unused,
809 unsigned long event, void *ptr)
811 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
812 const struct udp_tunnel_nic_info *info;
813 struct udp_tunnel_nic *utn;
815 info = dev->udp_tunnel_nic_info;
819 if (event == NETDEV_REGISTER) {
822 err = udp_tunnel_nic_register(dev);
824 netdev_WARN(dev, "failed to register for UDP tunnel offloads: %d", err);
825 return notifier_from_errno(err);
827 /* All other events will need the udp_tunnel_nic state */
828 utn = dev->udp_tunnel_nic;
832 if (event == NETDEV_UNREGISTER) {
833 udp_tunnel_nic_unregister(dev, utn);
837 /* All other events only matter if NIC has to be programmed open */
838 if (!(info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY))
841 if (event == NETDEV_UP) {
842 WARN_ON(!udp_tunnel_nic_is_empty(dev, utn));
843 udp_tunnel_get_rx_info(dev);
846 if (event == NETDEV_GOING_DOWN) {
847 udp_tunnel_nic_flush(dev, utn);
854 static struct notifier_block udp_tunnel_nic_notifier_block __read_mostly = {
855 .notifier_call = udp_tunnel_nic_netdevice_event,
858 static int __init udp_tunnel_nic_init_module(void)
862 udp_tunnel_nic_workqueue = alloc_workqueue("udp_tunnel_nic", 0, 0);
863 if (!udp_tunnel_nic_workqueue)
867 udp_tunnel_nic_ops = &__udp_tunnel_nic_ops;
870 err = register_netdevice_notifier(&udp_tunnel_nic_notifier_block);
878 udp_tunnel_nic_ops = NULL;
880 destroy_workqueue(udp_tunnel_nic_workqueue);
883 late_initcall(udp_tunnel_nic_init_module);
885 static void __exit udp_tunnel_nic_cleanup_module(void)
887 unregister_netdevice_notifier(&udp_tunnel_nic_notifier_block);
890 udp_tunnel_nic_ops = NULL;
893 destroy_workqueue(udp_tunnel_nic_workqueue);
895 module_exit(udp_tunnel_nic_cleanup_module);
897 MODULE_LICENSE("GPL");