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 {
24 #define UDP_TUNNEL_NIC_USE_CNT_MAX U16_MAX
29 * struct udp_tunnel_nic - UDP tunnel port offload state
30 * @work: async work for talking to hardware from process context
31 * @dev: netdev pointer
32 * @need_sync: at least one port start changed
33 * @need_replay: space was freed, we need a replay of all ports
34 * @work_pending: @work is currently scheduled
35 * @n_tables: number of tables under @entries
36 * @missed: bitmap of tables which overflown
37 * @entries: table of tables of ports currently offloaded
39 struct udp_tunnel_nic {
40 struct work_struct work;
42 struct net_device *dev;
48 unsigned int n_tables;
50 struct udp_tunnel_nic_table_entry **entries;
53 /* We ensure all work structs are done using driver state, but not the code.
54 * We need a workqueue we can flush before module gets removed.
56 static struct workqueue_struct *udp_tunnel_nic_workqueue;
58 static const char *udp_tunnel_nic_tunnel_type_name(unsigned int type)
61 case UDP_TUNNEL_TYPE_VXLAN:
63 case UDP_TUNNEL_TYPE_GENEVE:
65 case UDP_TUNNEL_TYPE_VXLAN_GPE:
73 udp_tunnel_nic_entry_is_free(struct udp_tunnel_nic_table_entry *entry)
75 return entry->use_cnt == 0 && !entry->flags;
79 udp_tunnel_nic_entry_is_present(struct udp_tunnel_nic_table_entry *entry)
81 return entry->use_cnt && !(entry->flags & ~UDP_TUNNEL_NIC_ENTRY_FROZEN);
85 udp_tunnel_nic_entry_is_frozen(struct udp_tunnel_nic_table_entry *entry)
87 return entry->flags & UDP_TUNNEL_NIC_ENTRY_FROZEN;
91 udp_tunnel_nic_entry_freeze_used(struct udp_tunnel_nic_table_entry *entry)
93 if (!udp_tunnel_nic_entry_is_free(entry))
94 entry->flags |= UDP_TUNNEL_NIC_ENTRY_FROZEN;
98 udp_tunnel_nic_entry_unfreeze(struct udp_tunnel_nic_table_entry *entry)
100 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_FROZEN;
104 udp_tunnel_nic_entry_is_queued(struct udp_tunnel_nic_table_entry *entry)
106 return entry->flags & (UDP_TUNNEL_NIC_ENTRY_ADD |
107 UDP_TUNNEL_NIC_ENTRY_DEL);
111 udp_tunnel_nic_entry_queue(struct udp_tunnel_nic *utn,
112 struct udp_tunnel_nic_table_entry *entry,
115 entry->flags |= flag;
120 udp_tunnel_nic_ti_from_entry(struct udp_tunnel_nic_table_entry *entry,
121 struct udp_tunnel_info *ti)
123 memset(ti, 0, sizeof(*ti));
124 ti->port = entry->port;
125 ti->type = entry->type;
126 ti->hw_priv = entry->hw_priv;
130 udp_tunnel_nic_is_empty(struct net_device *dev, struct udp_tunnel_nic *utn)
132 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
135 for (i = 0; i < utn->n_tables; i++)
136 for (j = 0; j < info->tables[i].n_entries; j++)
137 if (!udp_tunnel_nic_entry_is_free(&utn->entries[i][j]))
143 udp_tunnel_nic_should_replay(struct net_device *dev, struct udp_tunnel_nic *utn)
145 const struct udp_tunnel_nic_table_info *table;
151 for (i = 0; i < utn->n_tables; i++) {
152 table = &dev->udp_tunnel_nic_info->tables[i];
153 if (!test_bit(i, &utn->missed))
156 for (j = 0; j < table->n_entries; j++)
157 if (udp_tunnel_nic_entry_is_free(&utn->entries[i][j]))
165 __udp_tunnel_nic_get_port(struct net_device *dev, unsigned int table,
166 unsigned int idx, struct udp_tunnel_info *ti)
168 struct udp_tunnel_nic_table_entry *entry;
169 struct udp_tunnel_nic *utn;
171 utn = dev->udp_tunnel_nic;
172 entry = &utn->entries[table][idx];
175 udp_tunnel_nic_ti_from_entry(entry, ti);
179 __udp_tunnel_nic_set_port_priv(struct net_device *dev, unsigned int table,
180 unsigned int idx, u8 priv)
182 dev->udp_tunnel_nic->entries[table][idx].hw_priv = priv;
186 udp_tunnel_nic_entry_update_done(struct udp_tunnel_nic_table_entry *entry,
189 bool dodgy = entry->flags & UDP_TUNNEL_NIC_ENTRY_OP_FAIL;
191 WARN_ON_ONCE(entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD &&
192 entry->flags & UDP_TUNNEL_NIC_ENTRY_DEL);
194 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD &&
195 (!err || (err == -EEXIST && dodgy)))
196 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_ADD;
198 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_DEL &&
199 (!err || (err == -ENOENT && dodgy)))
200 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_DEL;
203 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_OP_FAIL;
205 entry->flags |= UDP_TUNNEL_NIC_ENTRY_OP_FAIL;
209 udp_tunnel_nic_device_sync_one(struct net_device *dev,
210 struct udp_tunnel_nic *utn,
211 unsigned int table, unsigned int idx)
213 struct udp_tunnel_nic_table_entry *entry;
214 struct udp_tunnel_info ti;
217 entry = &utn->entries[table][idx];
218 if (!udp_tunnel_nic_entry_is_queued(entry))
221 udp_tunnel_nic_ti_from_entry(entry, &ti);
222 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD)
223 err = dev->udp_tunnel_nic_info->set_port(dev, table, idx, &ti);
225 err = dev->udp_tunnel_nic_info->unset_port(dev, table, idx,
227 udp_tunnel_nic_entry_update_done(entry, err);
231 "UDP tunnel port sync failed port %d type %s: %d\n",
232 be16_to_cpu(entry->port),
233 udp_tunnel_nic_tunnel_type_name(entry->type),
238 udp_tunnel_nic_device_sync_by_port(struct net_device *dev,
239 struct udp_tunnel_nic *utn)
241 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
244 for (i = 0; i < utn->n_tables; i++)
245 for (j = 0; j < info->tables[i].n_entries; j++)
246 udp_tunnel_nic_device_sync_one(dev, utn, i, j);
250 udp_tunnel_nic_device_sync_by_table(struct net_device *dev,
251 struct udp_tunnel_nic *utn)
253 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
257 for (i = 0; i < utn->n_tables; i++) {
258 /* Find something that needs sync in this table */
259 for (j = 0; j < info->tables[i].n_entries; j++)
260 if (udp_tunnel_nic_entry_is_queued(&utn->entries[i][j]))
262 if (j == info->tables[i].n_entries)
265 err = info->sync_table(dev, i);
267 netdev_warn(dev, "UDP tunnel port sync failed for table %d: %d\n",
270 for (j = 0; j < info->tables[i].n_entries; j++) {
271 struct udp_tunnel_nic_table_entry *entry;
273 entry = &utn->entries[i][j];
274 if (udp_tunnel_nic_entry_is_queued(entry))
275 udp_tunnel_nic_entry_update_done(entry, err);
281 __udp_tunnel_nic_device_sync(struct net_device *dev, struct udp_tunnel_nic *utn)
286 if (dev->udp_tunnel_nic_info->sync_table)
287 udp_tunnel_nic_device_sync_by_table(dev, utn);
289 udp_tunnel_nic_device_sync_by_port(dev, utn);
292 /* Can't replay directly here, in case we come from the tunnel driver's
293 * notification - trying to replay may deadlock inside tunnel driver.
295 utn->need_replay = udp_tunnel_nic_should_replay(dev, utn);
299 udp_tunnel_nic_device_sync(struct net_device *dev, struct udp_tunnel_nic *utn)
301 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
307 /* Drivers which sleep in the callback need to update from
308 * the workqueue, if we come from the tunnel driver's notification.
310 may_sleep = info->flags & UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
312 __udp_tunnel_nic_device_sync(dev, utn);
313 if (may_sleep || utn->need_replay) {
314 queue_work(udp_tunnel_nic_workqueue, &utn->work);
315 utn->work_pending = 1;
320 udp_tunnel_nic_table_is_capable(const struct udp_tunnel_nic_table_info *table,
321 struct udp_tunnel_info *ti)
323 return table->tunnel_types & ti->type;
327 udp_tunnel_nic_is_capable(struct net_device *dev, struct udp_tunnel_nic *utn,
328 struct udp_tunnel_info *ti)
330 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
333 /* Special case IPv4-only NICs */
334 if (info->flags & UDP_TUNNEL_NIC_INFO_IPV4_ONLY &&
335 ti->sa_family != AF_INET)
338 for (i = 0; i < utn->n_tables; i++)
339 if (udp_tunnel_nic_table_is_capable(&info->tables[i], ti))
345 udp_tunnel_nic_has_collision(struct net_device *dev, struct udp_tunnel_nic *utn,
346 struct udp_tunnel_info *ti)
348 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
349 struct udp_tunnel_nic_table_entry *entry;
352 for (i = 0; i < utn->n_tables; i++)
353 for (j = 0; j < info->tables[i].n_entries; j++) {
354 entry = &utn->entries[i][j];
356 if (!udp_tunnel_nic_entry_is_free(entry) &&
357 entry->port == ti->port &&
358 entry->type != ti->type) {
359 __set_bit(i, &utn->missed);
367 udp_tunnel_nic_entry_adj(struct udp_tunnel_nic *utn,
368 unsigned int table, unsigned int idx, int use_cnt_adj)
370 struct udp_tunnel_nic_table_entry *entry = &utn->entries[table][idx];
371 bool dodgy = entry->flags & UDP_TUNNEL_NIC_ENTRY_OP_FAIL;
372 unsigned int from, to;
374 WARN_ON(entry->use_cnt + (u32)use_cnt_adj > U16_MAX);
376 /* If not going from used to unused or vice versa - all done.
377 * For dodgy entries make sure we try to sync again (queue the entry).
379 entry->use_cnt += use_cnt_adj;
380 if (!dodgy && !entry->use_cnt == !(entry->use_cnt - use_cnt_adj))
383 /* Cancel the op before it was sent to the device, if possible,
384 * otherwise we'd need to take special care to issue commands
385 * in the same order the ports arrived.
387 if (use_cnt_adj < 0) {
388 from = UDP_TUNNEL_NIC_ENTRY_ADD;
389 to = UDP_TUNNEL_NIC_ENTRY_DEL;
391 from = UDP_TUNNEL_NIC_ENTRY_DEL;
392 to = UDP_TUNNEL_NIC_ENTRY_ADD;
395 if (entry->flags & from) {
396 entry->flags &= ~from;
401 udp_tunnel_nic_entry_queue(utn, entry, to);
405 udp_tunnel_nic_entry_try_adj(struct udp_tunnel_nic *utn,
406 unsigned int table, unsigned int idx,
407 struct udp_tunnel_info *ti, int use_cnt_adj)
409 struct udp_tunnel_nic_table_entry *entry = &utn->entries[table][idx];
411 if (udp_tunnel_nic_entry_is_free(entry) ||
412 entry->port != ti->port ||
413 entry->type != ti->type)
416 if (udp_tunnel_nic_entry_is_frozen(entry))
419 udp_tunnel_nic_entry_adj(utn, table, idx, use_cnt_adj);
423 /* Try to find existing matching entry and adjust its use count, instead of
424 * adding a new one. Returns true if entry was found. In case of delete the
425 * entry may have gotten removed in the process, in which case it will be
426 * queued for removal.
429 udp_tunnel_nic_try_existing(struct net_device *dev, struct udp_tunnel_nic *utn,
430 struct udp_tunnel_info *ti, int use_cnt_adj)
432 const struct udp_tunnel_nic_table_info *table;
435 for (i = 0; i < utn->n_tables; i++) {
436 table = &dev->udp_tunnel_nic_info->tables[i];
437 if (!udp_tunnel_nic_table_is_capable(table, ti))
440 for (j = 0; j < table->n_entries; j++)
441 if (udp_tunnel_nic_entry_try_adj(utn, i, j, ti,
450 udp_tunnel_nic_add_existing(struct net_device *dev, struct udp_tunnel_nic *utn,
451 struct udp_tunnel_info *ti)
453 return udp_tunnel_nic_try_existing(dev, utn, ti, +1);
457 udp_tunnel_nic_del_existing(struct net_device *dev, struct udp_tunnel_nic *utn,
458 struct udp_tunnel_info *ti)
460 return udp_tunnel_nic_try_existing(dev, utn, ti, -1);
464 udp_tunnel_nic_add_new(struct net_device *dev, struct udp_tunnel_nic *utn,
465 struct udp_tunnel_info *ti)
467 const struct udp_tunnel_nic_table_info *table;
470 for (i = 0; i < utn->n_tables; i++) {
471 table = &dev->udp_tunnel_nic_info->tables[i];
472 if (!udp_tunnel_nic_table_is_capable(table, ti))
475 for (j = 0; j < table->n_entries; j++) {
476 struct udp_tunnel_nic_table_entry *entry;
478 entry = &utn->entries[i][j];
479 if (!udp_tunnel_nic_entry_is_free(entry))
482 entry->port = ti->port;
483 entry->type = ti->type;
485 udp_tunnel_nic_entry_queue(utn, entry,
486 UDP_TUNNEL_NIC_ENTRY_ADD);
490 /* The different table may still fit this port in, but there
491 * are no devices currently which have multiple tables accepting
492 * the same tunnel type, and false positives are okay.
494 __set_bit(i, &utn->missed);
501 __udp_tunnel_nic_add_port(struct net_device *dev, struct udp_tunnel_info *ti)
503 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
504 struct udp_tunnel_nic *utn;
506 utn = dev->udp_tunnel_nic;
509 if (!netif_running(dev) && info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY)
511 if (info->flags & UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN &&
512 ti->port == htons(IANA_VXLAN_UDP_PORT)) {
513 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
514 netdev_warn(dev, "device assumes port 4789 will be used by vxlan tunnels\n");
518 if (!udp_tunnel_nic_is_capable(dev, utn, ti))
521 /* It may happen that a tunnel of one type is removed and different
522 * tunnel type tries to reuse its port before the device was informed.
523 * Rely on utn->missed to re-add this port later.
525 if (udp_tunnel_nic_has_collision(dev, utn, ti))
528 if (!udp_tunnel_nic_add_existing(dev, utn, ti))
529 udp_tunnel_nic_add_new(dev, utn, ti);
531 udp_tunnel_nic_device_sync(dev, utn);
535 __udp_tunnel_nic_del_port(struct net_device *dev, struct udp_tunnel_info *ti)
537 struct udp_tunnel_nic *utn;
539 utn = dev->udp_tunnel_nic;
543 if (!udp_tunnel_nic_is_capable(dev, utn, ti))
546 udp_tunnel_nic_del_existing(dev, utn, ti);
548 udp_tunnel_nic_device_sync(dev, utn);
551 static void __udp_tunnel_nic_reset_ntf(struct net_device *dev)
553 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
554 struct udp_tunnel_nic *utn;
559 utn = dev->udp_tunnel_nic;
563 utn->need_sync = false;
564 for (i = 0; i < utn->n_tables; i++)
565 for (j = 0; j < info->tables[i].n_entries; j++) {
566 struct udp_tunnel_nic_table_entry *entry;
568 entry = &utn->entries[i][j];
570 entry->flags &= ~(UDP_TUNNEL_NIC_ENTRY_DEL |
571 UDP_TUNNEL_NIC_ENTRY_OP_FAIL);
572 /* We don't release rtnl across ops */
573 WARN_ON(entry->flags & UDP_TUNNEL_NIC_ENTRY_FROZEN);
577 udp_tunnel_nic_entry_queue(utn, entry,
578 UDP_TUNNEL_NIC_ENTRY_ADD);
581 __udp_tunnel_nic_device_sync(dev, utn);
585 __udp_tunnel_nic_dump_size(struct net_device *dev, unsigned int table)
587 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
588 struct udp_tunnel_nic *utn;
592 utn = dev->udp_tunnel_nic;
597 for (j = 0; j < info->tables[table].n_entries; j++) {
598 if (!udp_tunnel_nic_entry_is_present(&utn->entries[table][j]))
601 size += nla_total_size(0) + /* _TABLE_ENTRY */
602 nla_total_size(sizeof(__be16)) + /* _ENTRY_PORT */
603 nla_total_size(sizeof(u32)); /* _ENTRY_TYPE */
610 __udp_tunnel_nic_dump_write(struct net_device *dev, unsigned int table,
613 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
614 struct udp_tunnel_nic *utn;
618 utn = dev->udp_tunnel_nic;
622 for (j = 0; j < info->tables[table].n_entries; j++) {
623 if (!udp_tunnel_nic_entry_is_present(&utn->entries[table][j]))
626 nest = nla_nest_start(skb, ETHTOOL_A_TUNNEL_UDP_TABLE_ENTRY);
630 if (nla_put_be16(skb, ETHTOOL_A_TUNNEL_UDP_ENTRY_PORT,
631 utn->entries[table][j].port) ||
632 nla_put_u32(skb, ETHTOOL_A_TUNNEL_UDP_ENTRY_TYPE,
633 ilog2(utn->entries[table][j].type)))
636 nla_nest_end(skb, nest);
642 nla_nest_cancel(skb, nest);
646 static const struct udp_tunnel_nic_ops __udp_tunnel_nic_ops = {
647 .get_port = __udp_tunnel_nic_get_port,
648 .set_port_priv = __udp_tunnel_nic_set_port_priv,
649 .add_port = __udp_tunnel_nic_add_port,
650 .del_port = __udp_tunnel_nic_del_port,
651 .reset_ntf = __udp_tunnel_nic_reset_ntf,
652 .dump_size = __udp_tunnel_nic_dump_size,
653 .dump_write = __udp_tunnel_nic_dump_write,
657 udp_tunnel_nic_flush(struct net_device *dev, struct udp_tunnel_nic *utn)
659 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
662 for (i = 0; i < utn->n_tables; i++)
663 for (j = 0; j < info->tables[i].n_entries; j++) {
664 int adj_cnt = -utn->entries[i][j].use_cnt;
667 udp_tunnel_nic_entry_adj(utn, i, j, adj_cnt);
670 __udp_tunnel_nic_device_sync(dev, utn);
672 for (i = 0; i < utn->n_tables; i++)
673 memset(utn->entries[i], 0, array_size(info->tables[i].n_entries,
674 sizeof(**utn->entries)));
675 WARN_ON(utn->need_sync);
676 utn->need_replay = 0;
680 udp_tunnel_nic_replay(struct net_device *dev, struct udp_tunnel_nic *utn)
682 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
683 struct udp_tunnel_nic_shared_node *node;
686 /* Freeze all the ports we are already tracking so that the replay
687 * does not double up the refcount.
689 for (i = 0; i < utn->n_tables; i++)
690 for (j = 0; j < info->tables[i].n_entries; j++)
691 udp_tunnel_nic_entry_freeze_used(&utn->entries[i][j]);
693 utn->need_replay = 0;
696 udp_tunnel_get_rx_info(dev);
698 list_for_each_entry(node, &info->shared->devices, list)
699 udp_tunnel_get_rx_info(node->dev);
702 for (i = 0; i < utn->n_tables; i++)
703 for (j = 0; j < info->tables[i].n_entries; j++)
704 udp_tunnel_nic_entry_unfreeze(&utn->entries[i][j]);
707 static void udp_tunnel_nic_device_sync_work(struct work_struct *work)
709 struct udp_tunnel_nic *utn =
710 container_of(work, struct udp_tunnel_nic, work);
713 utn->work_pending = 0;
714 __udp_tunnel_nic_device_sync(utn->dev, utn);
716 if (utn->need_replay)
717 udp_tunnel_nic_replay(utn->dev, utn);
721 static struct udp_tunnel_nic *
722 udp_tunnel_nic_alloc(const struct udp_tunnel_nic_info *info,
723 unsigned int n_tables)
725 struct udp_tunnel_nic *utn;
728 utn = kzalloc(sizeof(*utn), GFP_KERNEL);
731 utn->n_tables = n_tables;
732 INIT_WORK(&utn->work, udp_tunnel_nic_device_sync_work);
734 utn->entries = kmalloc_array(n_tables, sizeof(void *), GFP_KERNEL);
738 for (i = 0; i < n_tables; i++) {
739 utn->entries[i] = kcalloc(info->tables[i].n_entries,
740 sizeof(*utn->entries[i]), GFP_KERNEL);
741 if (!utn->entries[i])
742 goto err_free_prev_entries;
747 err_free_prev_entries:
749 kfree(utn->entries[i]);
756 static void udp_tunnel_nic_free(struct udp_tunnel_nic *utn)
760 for (i = 0; i < utn->n_tables; i++)
761 kfree(utn->entries[i]);
766 static int udp_tunnel_nic_register(struct net_device *dev)
768 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
769 struct udp_tunnel_nic_shared_node *node = NULL;
770 struct udp_tunnel_nic *utn;
771 unsigned int n_tables, i;
773 BUILD_BUG_ON(sizeof(utn->missed) * BITS_PER_BYTE <
774 UDP_TUNNEL_NIC_MAX_TABLES);
775 /* Expect use count of at most 2 (IPv4, IPv6) per device */
776 BUILD_BUG_ON(UDP_TUNNEL_NIC_USE_CNT_MAX <
777 UDP_TUNNEL_NIC_MAX_SHARING_DEVICES * 2);
779 /* Check that the driver info is sane */
780 if (WARN_ON(!info->set_port != !info->unset_port) ||
781 WARN_ON(!info->set_port == !info->sync_table) ||
782 WARN_ON(!info->tables[0].n_entries))
785 if (WARN_ON(info->shared &&
786 info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY))
790 for (i = 1; i < UDP_TUNNEL_NIC_MAX_TABLES; i++) {
791 if (!info->tables[i].n_entries)
795 if (WARN_ON(!info->tables[i - 1].n_entries))
799 /* Create UDP tunnel state structures */
801 node = kzalloc(sizeof(*node), GFP_KERNEL);
808 if (info->shared && info->shared->udp_tunnel_nic_info) {
809 utn = info->shared->udp_tunnel_nic_info;
811 utn = udp_tunnel_nic_alloc(info, n_tables);
819 if (!info->shared->udp_tunnel_nic_info) {
820 INIT_LIST_HEAD(&info->shared->devices);
821 info->shared->udp_tunnel_nic_info = utn;
824 list_add_tail(&node->list, &info->shared->devices);
829 dev->udp_tunnel_nic = utn;
831 if (!(info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY))
832 udp_tunnel_get_rx_info(dev);
838 udp_tunnel_nic_unregister(struct net_device *dev, struct udp_tunnel_nic *utn)
840 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info;
842 /* For a shared table remove this dev from the list of sharing devices
843 * and if there are other devices just detach.
846 struct udp_tunnel_nic_shared_node *node, *first;
848 list_for_each_entry(node, &info->shared->devices, list)
849 if (node->dev == dev)
851 if (list_entry_is_head(node, &info->shared->devices, list))
854 list_del(&node->list);
857 first = list_first_entry_or_null(&info->shared->devices,
858 typeof(*first), list);
860 udp_tunnel_drop_rx_info(dev);
861 utn->dev = first->dev;
865 info->shared->udp_tunnel_nic_info = NULL;
868 /* Flush before we check work, so we don't waste time adding entries
869 * from the work which we will boot immediately.
871 udp_tunnel_nic_flush(dev, utn);
873 /* Wait for the work to be done using the state, netdev core will
874 * retry unregister until we give up our reference on this device.
876 if (utn->work_pending)
879 udp_tunnel_nic_free(utn);
881 dev->udp_tunnel_nic = NULL;
886 udp_tunnel_nic_netdevice_event(struct notifier_block *unused,
887 unsigned long event, void *ptr)
889 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
890 const struct udp_tunnel_nic_info *info;
891 struct udp_tunnel_nic *utn;
893 info = dev->udp_tunnel_nic_info;
897 if (event == NETDEV_REGISTER) {
900 err = udp_tunnel_nic_register(dev);
902 netdev_WARN(dev, "failed to register for UDP tunnel offloads: %d", err);
903 return notifier_from_errno(err);
905 /* All other events will need the udp_tunnel_nic state */
906 utn = dev->udp_tunnel_nic;
910 if (event == NETDEV_UNREGISTER) {
911 udp_tunnel_nic_unregister(dev, utn);
915 /* All other events only matter if NIC has to be programmed open */
916 if (!(info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY))
919 if (event == NETDEV_UP) {
920 WARN_ON(!udp_tunnel_nic_is_empty(dev, utn));
921 udp_tunnel_get_rx_info(dev);
924 if (event == NETDEV_GOING_DOWN) {
925 udp_tunnel_nic_flush(dev, utn);
932 static struct notifier_block udp_tunnel_nic_notifier_block __read_mostly = {
933 .notifier_call = udp_tunnel_nic_netdevice_event,
936 static int __init udp_tunnel_nic_init_module(void)
940 udp_tunnel_nic_workqueue = alloc_ordered_workqueue("udp_tunnel_nic", 0);
941 if (!udp_tunnel_nic_workqueue)
945 udp_tunnel_nic_ops = &__udp_tunnel_nic_ops;
948 err = register_netdevice_notifier(&udp_tunnel_nic_notifier_block);
956 udp_tunnel_nic_ops = NULL;
958 destroy_workqueue(udp_tunnel_nic_workqueue);
961 late_initcall(udp_tunnel_nic_init_module);
963 static void __exit udp_tunnel_nic_cleanup_module(void)
965 unregister_netdevice_notifier(&udp_tunnel_nic_notifier_block);
968 udp_tunnel_nic_ops = NULL;
971 destroy_workqueue(udp_tunnel_nic_workqueue);
973 module_exit(udp_tunnel_nic_cleanup_module);
975 MODULE_LICENSE("GPL");