1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (c) 2020 Facebook Inc. 3 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> 11 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), 17 }; 18 19 struct udp_tunnel_nic_table_entry { 20 __be16 port; 21 u8 type; 22 u8 flags; 23 u16 use_cnt; 24 #define UDP_TUNNEL_NIC_USE_CNT_MAX U16_MAX 25 u8 hw_priv; 26 }; 27 28 /** 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 * @lock: protects all fields 33 * @need_sync: at least one port start changed 34 * @need_replay: space was freed, we need a replay of all ports 35 * @n_tables: number of tables under @entries 36 * @missed: bitmap of tables which overflown 37 * @entries: table of tables of ports currently offloaded 38 */ 39 struct udp_tunnel_nic { 40 struct delayed_work work; 41 42 struct net_device *dev; 43 44 struct mutex lock; 45 46 u8 need_sync:1; 47 u8 need_replay:1; 48 49 unsigned int n_tables; 50 unsigned long missed; 51 struct udp_tunnel_nic_table_entry *entries[] __counted_by(n_tables); 52 }; 53 54 /* We ensure all work structs are done using driver state, but not the code. 55 * We need a workqueue we can flush before module gets removed. 56 */ 57 static struct workqueue_struct *udp_tunnel_nic_workqueue; 58 59 static const char *udp_tunnel_nic_tunnel_type_name(unsigned int type) 60 { 61 switch (type) { 62 case UDP_TUNNEL_TYPE_VXLAN: 63 return "vxlan"; 64 case UDP_TUNNEL_TYPE_GENEVE: 65 return "geneve"; 66 case UDP_TUNNEL_TYPE_VXLAN_GPE: 67 return "vxlan-gpe"; 68 default: 69 return "unknown"; 70 } 71 } 72 73 static bool 74 udp_tunnel_nic_entry_is_free(struct udp_tunnel_nic_table_entry *entry) 75 { 76 return entry->use_cnt == 0 && !entry->flags; 77 } 78 79 static bool 80 udp_tunnel_nic_entry_is_present(struct udp_tunnel_nic_table_entry *entry) 81 { 82 return entry->use_cnt && !(entry->flags & ~UDP_TUNNEL_NIC_ENTRY_FROZEN); 83 } 84 85 static bool 86 udp_tunnel_nic_entry_is_frozen(struct udp_tunnel_nic_table_entry *entry) 87 { 88 return entry->flags & UDP_TUNNEL_NIC_ENTRY_FROZEN; 89 } 90 91 static void 92 udp_tunnel_nic_entry_freeze_used(struct udp_tunnel_nic_table_entry *entry) 93 { 94 if (!udp_tunnel_nic_entry_is_free(entry)) 95 entry->flags |= UDP_TUNNEL_NIC_ENTRY_FROZEN; 96 } 97 98 static void 99 udp_tunnel_nic_entry_unfreeze(struct udp_tunnel_nic_table_entry *entry) 100 { 101 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_FROZEN; 102 } 103 104 static bool 105 udp_tunnel_nic_entry_is_queued(struct udp_tunnel_nic_table_entry *entry) 106 { 107 return entry->flags & (UDP_TUNNEL_NIC_ENTRY_ADD | 108 UDP_TUNNEL_NIC_ENTRY_DEL); 109 } 110 111 static void 112 udp_tunnel_nic_entry_queue(struct udp_tunnel_nic *utn, 113 struct udp_tunnel_nic_table_entry *entry, 114 unsigned int flag) 115 { 116 entry->flags |= flag; 117 utn->need_sync = 1; 118 } 119 120 static void 121 udp_tunnel_nic_ti_from_entry(struct udp_tunnel_nic_table_entry *entry, 122 struct udp_tunnel_info *ti) 123 { 124 memset(ti, 0, sizeof(*ti)); 125 ti->port = entry->port; 126 ti->type = entry->type; 127 ti->hw_priv = entry->hw_priv; 128 } 129 130 static bool 131 udp_tunnel_nic_is_empty(struct net_device *dev, struct udp_tunnel_nic *utn) 132 { 133 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 134 unsigned int i, j; 135 136 for (i = 0; i < utn->n_tables; i++) 137 for (j = 0; j < info->tables[i].n_entries; j++) 138 if (!udp_tunnel_nic_entry_is_free(&utn->entries[i][j])) 139 return false; 140 return true; 141 } 142 143 static bool 144 udp_tunnel_nic_should_replay(struct net_device *dev, struct udp_tunnel_nic *utn) 145 { 146 const struct udp_tunnel_nic_table_info *table; 147 unsigned int i, j; 148 149 if (!utn->missed) 150 return false; 151 152 for (i = 0; i < utn->n_tables; i++) { 153 table = &dev->udp_tunnel_nic_info->tables[i]; 154 if (!test_bit(i, &utn->missed)) 155 continue; 156 157 for (j = 0; j < table->n_entries; j++) 158 if (udp_tunnel_nic_entry_is_free(&utn->entries[i][j])) 159 return true; 160 } 161 162 return false; 163 } 164 165 static void 166 __udp_tunnel_nic_get_port(struct net_device *dev, unsigned int table, 167 unsigned int idx, struct udp_tunnel_info *ti) 168 { 169 struct udp_tunnel_nic_table_entry *entry; 170 struct udp_tunnel_nic *utn; 171 172 utn = dev->udp_tunnel_nic; 173 entry = &utn->entries[table][idx]; 174 175 if (entry->use_cnt) 176 udp_tunnel_nic_ti_from_entry(entry, ti); 177 } 178 179 static void 180 __udp_tunnel_nic_set_port_priv(struct net_device *dev, unsigned int table, 181 unsigned int idx, u8 priv) 182 { 183 dev->udp_tunnel_nic->entries[table][idx].hw_priv = priv; 184 } 185 186 static void 187 udp_tunnel_nic_entry_update_done(struct udp_tunnel_nic_table_entry *entry, 188 int err) 189 { 190 bool dodgy = entry->flags & UDP_TUNNEL_NIC_ENTRY_OP_FAIL; 191 192 WARN_ON_ONCE(entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD && 193 entry->flags & UDP_TUNNEL_NIC_ENTRY_DEL); 194 195 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD && 196 (!err || (err == -EEXIST && dodgy))) 197 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_ADD; 198 199 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_DEL && 200 (!err || (err == -ENOENT && dodgy))) 201 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_DEL; 202 203 if (!err) 204 entry->flags &= ~UDP_TUNNEL_NIC_ENTRY_OP_FAIL; 205 else 206 entry->flags |= UDP_TUNNEL_NIC_ENTRY_OP_FAIL; 207 } 208 209 static void 210 udp_tunnel_nic_device_sync_one(struct net_device *dev, 211 struct udp_tunnel_nic *utn, 212 unsigned int table, unsigned int idx) 213 { 214 struct udp_tunnel_nic_table_entry *entry; 215 struct udp_tunnel_info ti; 216 int err; 217 218 entry = &utn->entries[table][idx]; 219 if (!udp_tunnel_nic_entry_is_queued(entry)) 220 return; 221 222 udp_tunnel_nic_ti_from_entry(entry, &ti); 223 if (entry->flags & UDP_TUNNEL_NIC_ENTRY_ADD) 224 err = dev->udp_tunnel_nic_info->set_port(dev, table, idx, &ti); 225 else 226 err = dev->udp_tunnel_nic_info->unset_port(dev, table, idx, 227 &ti); 228 udp_tunnel_nic_entry_update_done(entry, err); 229 230 if (err) 231 netdev_warn(dev, 232 "UDP tunnel port sync failed port %d type %s: %d\n", 233 be16_to_cpu(entry->port), 234 udp_tunnel_nic_tunnel_type_name(entry->type), 235 err); 236 } 237 238 static void 239 udp_tunnel_nic_device_sync_by_port(struct net_device *dev, 240 struct udp_tunnel_nic *utn) 241 { 242 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 243 unsigned int i, j; 244 245 for (i = 0; i < utn->n_tables; i++) 246 for (j = 0; j < info->tables[i].n_entries; j++) 247 udp_tunnel_nic_device_sync_one(dev, utn, i, j); 248 } 249 250 static void 251 udp_tunnel_nic_device_sync_by_table(struct net_device *dev, 252 struct udp_tunnel_nic *utn) 253 { 254 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 255 unsigned int i, j; 256 int err; 257 258 for (i = 0; i < utn->n_tables; i++) { 259 /* Find something that needs sync in this table */ 260 for (j = 0; j < info->tables[i].n_entries; j++) 261 if (udp_tunnel_nic_entry_is_queued(&utn->entries[i][j])) 262 break; 263 if (j == info->tables[i].n_entries) 264 continue; 265 266 err = info->sync_table(dev, i); 267 if (err) 268 netdev_warn(dev, "UDP tunnel port sync failed for table %d: %d\n", 269 i, err); 270 271 for (j = 0; j < info->tables[i].n_entries; j++) { 272 struct udp_tunnel_nic_table_entry *entry; 273 274 entry = &utn->entries[i][j]; 275 if (udp_tunnel_nic_entry_is_queued(entry)) 276 udp_tunnel_nic_entry_update_done(entry, err); 277 } 278 } 279 } 280 281 static void 282 __udp_tunnel_nic_device_sync(struct net_device *dev, struct udp_tunnel_nic *utn) 283 { 284 if (!utn->need_sync) 285 return; 286 287 if (dev->udp_tunnel_nic_info->sync_table) 288 udp_tunnel_nic_device_sync_by_table(dev, utn); 289 else 290 udp_tunnel_nic_device_sync_by_port(dev, utn); 291 292 utn->need_sync = 0; 293 /* Can't replay directly here, in case we come from the tunnel driver's 294 * notification - trying to replay may deadlock inside tunnel driver. 295 */ 296 utn->need_replay = udp_tunnel_nic_should_replay(dev, utn); 297 } 298 299 static void 300 udp_tunnel_nic_device_sync(struct net_device *dev, struct udp_tunnel_nic *utn) 301 { 302 if (!utn->need_sync) 303 return; 304 305 queue_delayed_work(udp_tunnel_nic_workqueue, &utn->work, 0); 306 } 307 308 static bool 309 udp_tunnel_nic_table_is_capable(const struct udp_tunnel_nic_table_info *table, 310 struct udp_tunnel_info *ti) 311 { 312 return table->tunnel_types & ti->type; 313 } 314 315 static bool 316 udp_tunnel_nic_is_capable(struct net_device *dev, struct udp_tunnel_nic *utn, 317 struct udp_tunnel_info *ti) 318 { 319 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 320 unsigned int i; 321 322 /* Special case IPv4-only NICs */ 323 if (info->flags & UDP_TUNNEL_NIC_INFO_IPV4_ONLY && 324 ti->sa_family != AF_INET) 325 return false; 326 327 for (i = 0; i < utn->n_tables; i++) 328 if (udp_tunnel_nic_table_is_capable(&info->tables[i], ti)) 329 return true; 330 return false; 331 } 332 333 static int 334 udp_tunnel_nic_has_collision(struct net_device *dev, struct udp_tunnel_nic *utn, 335 struct udp_tunnel_info *ti) 336 { 337 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 338 struct udp_tunnel_nic_table_entry *entry; 339 unsigned int i, j; 340 341 for (i = 0; i < utn->n_tables; i++) 342 for (j = 0; j < info->tables[i].n_entries; j++) { 343 entry = &utn->entries[i][j]; 344 345 if (!udp_tunnel_nic_entry_is_free(entry) && 346 entry->port == ti->port && 347 entry->type != ti->type) { 348 __set_bit(i, &utn->missed); 349 return true; 350 } 351 } 352 return false; 353 } 354 355 static void 356 udp_tunnel_nic_entry_adj(struct udp_tunnel_nic *utn, 357 unsigned int table, unsigned int idx, int use_cnt_adj) 358 { 359 struct udp_tunnel_nic_table_entry *entry = &utn->entries[table][idx]; 360 bool dodgy = entry->flags & UDP_TUNNEL_NIC_ENTRY_OP_FAIL; 361 unsigned int from, to; 362 363 WARN_ON(entry->use_cnt + (u32)use_cnt_adj > U16_MAX); 364 365 /* If not going from used to unused or vice versa - all done. 366 * For dodgy entries make sure we try to sync again (queue the entry). 367 */ 368 entry->use_cnt += use_cnt_adj; 369 if (!dodgy && !entry->use_cnt == !(entry->use_cnt - use_cnt_adj)) 370 return; 371 372 /* Cancel the op before it was sent to the device, if possible, 373 * otherwise we'd need to take special care to issue commands 374 * in the same order the ports arrived. 375 */ 376 if (use_cnt_adj < 0) { 377 from = UDP_TUNNEL_NIC_ENTRY_ADD; 378 to = UDP_TUNNEL_NIC_ENTRY_DEL; 379 } else { 380 from = UDP_TUNNEL_NIC_ENTRY_DEL; 381 to = UDP_TUNNEL_NIC_ENTRY_ADD; 382 } 383 384 if (entry->flags & from) { 385 entry->flags &= ~from; 386 if (!dodgy) 387 return; 388 } 389 390 udp_tunnel_nic_entry_queue(utn, entry, to); 391 } 392 393 static bool 394 udp_tunnel_nic_entry_try_adj(struct udp_tunnel_nic *utn, 395 unsigned int table, unsigned int idx, 396 struct udp_tunnel_info *ti, int use_cnt_adj) 397 { 398 struct udp_tunnel_nic_table_entry *entry = &utn->entries[table][idx]; 399 400 if (udp_tunnel_nic_entry_is_free(entry) || 401 entry->port != ti->port || 402 entry->type != ti->type) 403 return false; 404 405 if (udp_tunnel_nic_entry_is_frozen(entry)) 406 return true; 407 408 udp_tunnel_nic_entry_adj(utn, table, idx, use_cnt_adj); 409 return true; 410 } 411 412 /* Try to find existing matching entry and adjust its use count, instead of 413 * adding a new one. Returns true if entry was found. In case of delete the 414 * entry may have gotten removed in the process, in which case it will be 415 * queued for removal. 416 */ 417 static bool 418 udp_tunnel_nic_try_existing(struct net_device *dev, struct udp_tunnel_nic *utn, 419 struct udp_tunnel_info *ti, int use_cnt_adj) 420 { 421 const struct udp_tunnel_nic_table_info *table; 422 unsigned int i, j; 423 424 for (i = 0; i < utn->n_tables; i++) { 425 table = &dev->udp_tunnel_nic_info->tables[i]; 426 if (!udp_tunnel_nic_table_is_capable(table, ti)) 427 continue; 428 429 for (j = 0; j < table->n_entries; j++) 430 if (udp_tunnel_nic_entry_try_adj(utn, i, j, ti, 431 use_cnt_adj)) 432 return true; 433 } 434 435 return false; 436 } 437 438 static bool 439 udp_tunnel_nic_add_existing(struct net_device *dev, struct udp_tunnel_nic *utn, 440 struct udp_tunnel_info *ti) 441 { 442 return udp_tunnel_nic_try_existing(dev, utn, ti, +1); 443 } 444 445 static bool 446 udp_tunnel_nic_del_existing(struct net_device *dev, struct udp_tunnel_nic *utn, 447 struct udp_tunnel_info *ti) 448 { 449 return udp_tunnel_nic_try_existing(dev, utn, ti, -1); 450 } 451 452 static bool 453 udp_tunnel_nic_add_new(struct net_device *dev, struct udp_tunnel_nic *utn, 454 struct udp_tunnel_info *ti) 455 { 456 const struct udp_tunnel_nic_table_info *table; 457 unsigned int i, j; 458 459 for (i = 0; i < utn->n_tables; i++) { 460 table = &dev->udp_tunnel_nic_info->tables[i]; 461 if (!udp_tunnel_nic_table_is_capable(table, ti)) 462 continue; 463 464 for (j = 0; j < table->n_entries; j++) { 465 struct udp_tunnel_nic_table_entry *entry; 466 467 entry = &utn->entries[i][j]; 468 if (!udp_tunnel_nic_entry_is_free(entry)) 469 continue; 470 471 entry->port = ti->port; 472 entry->type = ti->type; 473 entry->use_cnt = 1; 474 udp_tunnel_nic_entry_queue(utn, entry, 475 UDP_TUNNEL_NIC_ENTRY_ADD); 476 return true; 477 } 478 479 /* The different table may still fit this port in, but there 480 * are no devices currently which have multiple tables accepting 481 * the same tunnel type, and false positives are okay. 482 */ 483 __set_bit(i, &utn->missed); 484 } 485 486 return false; 487 } 488 489 static void 490 __udp_tunnel_nic_add_port(struct net_device *dev, struct udp_tunnel_info *ti) 491 { 492 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 493 struct udp_tunnel_nic *utn; 494 495 utn = dev->udp_tunnel_nic; 496 if (!utn) 497 return; 498 if (!netif_running(dev) && info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY) 499 return; 500 if (info->flags & UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN && 501 ti->port == htons(IANA_VXLAN_UDP_PORT)) { 502 if (ti->type != UDP_TUNNEL_TYPE_VXLAN) 503 netdev_warn(dev, "device assumes port 4789 will be used by vxlan tunnels\n"); 504 return; 505 } 506 507 if (!udp_tunnel_nic_is_capable(dev, utn, ti)) 508 return; 509 510 /* It may happen that a tunnel of one type is removed and different 511 * tunnel type tries to reuse its port before the device was informed. 512 * Rely on utn->missed to re-add this port later. 513 */ 514 if (udp_tunnel_nic_has_collision(dev, utn, ti)) 515 return; 516 517 if (!udp_tunnel_nic_add_existing(dev, utn, ti)) 518 udp_tunnel_nic_add_new(dev, utn, ti); 519 520 udp_tunnel_nic_device_sync(dev, utn); 521 } 522 523 static void 524 __udp_tunnel_nic_del_port(struct net_device *dev, struct udp_tunnel_info *ti) 525 { 526 struct udp_tunnel_nic *utn; 527 528 utn = dev->udp_tunnel_nic; 529 if (!utn) 530 return; 531 532 if (!udp_tunnel_nic_is_capable(dev, utn, ti)) 533 return; 534 535 udp_tunnel_nic_del_existing(dev, utn, ti); 536 537 udp_tunnel_nic_device_sync(dev, utn); 538 } 539 540 static void __udp_tunnel_nic_reset_ntf(struct net_device *dev) 541 { 542 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 543 struct udp_tunnel_nic *utn; 544 unsigned int i, j; 545 546 utn = dev->udp_tunnel_nic; 547 if (!utn) 548 return; 549 550 mutex_lock(&utn->lock); 551 552 utn->need_sync = false; 553 for (i = 0; i < utn->n_tables; i++) 554 for (j = 0; j < info->tables[i].n_entries; j++) { 555 struct udp_tunnel_nic_table_entry *entry; 556 557 entry = &utn->entries[i][j]; 558 559 entry->flags &= ~(UDP_TUNNEL_NIC_ENTRY_DEL | 560 UDP_TUNNEL_NIC_ENTRY_OP_FAIL); 561 /* We don't release utn lock across ops */ 562 WARN_ON(entry->flags & UDP_TUNNEL_NIC_ENTRY_FROZEN); 563 if (!entry->use_cnt) 564 continue; 565 566 udp_tunnel_nic_entry_queue(utn, entry, 567 UDP_TUNNEL_NIC_ENTRY_ADD); 568 } 569 570 __udp_tunnel_nic_device_sync(dev, utn); 571 572 mutex_unlock(&utn->lock); 573 } 574 575 static size_t 576 __udp_tunnel_nic_dump_size(struct net_device *dev, unsigned int table) 577 { 578 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 579 struct udp_tunnel_nic *utn; 580 unsigned int j; 581 size_t size; 582 583 utn = dev->udp_tunnel_nic; 584 if (!utn) 585 return 0; 586 587 size = 0; 588 for (j = 0; j < info->tables[table].n_entries; j++) { 589 if (!udp_tunnel_nic_entry_is_present(&utn->entries[table][j])) 590 continue; 591 592 size += nla_total_size(0) + /* _TABLE_ENTRY */ 593 nla_total_size(sizeof(__be16)) + /* _ENTRY_PORT */ 594 nla_total_size(sizeof(u32)); /* _ENTRY_TYPE */ 595 } 596 597 return size; 598 } 599 600 static int 601 __udp_tunnel_nic_dump_write(struct net_device *dev, unsigned int table, 602 struct sk_buff *skb) 603 { 604 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 605 struct udp_tunnel_nic *utn; 606 struct nlattr *nest; 607 unsigned int j; 608 609 utn = dev->udp_tunnel_nic; 610 if (!utn) 611 return 0; 612 613 for (j = 0; j < info->tables[table].n_entries; j++) { 614 if (!udp_tunnel_nic_entry_is_present(&utn->entries[table][j])) 615 continue; 616 617 nest = nla_nest_start(skb, ETHTOOL_A_TUNNEL_UDP_TABLE_ENTRY); 618 if (!nest) 619 return -EMSGSIZE; 620 621 if (nla_put_be16(skb, ETHTOOL_A_TUNNEL_UDP_ENTRY_PORT, 622 utn->entries[table][j].port) || 623 nla_put_u32(skb, ETHTOOL_A_TUNNEL_UDP_ENTRY_TYPE, 624 ilog2(utn->entries[table][j].type))) 625 goto err_cancel; 626 627 nla_nest_end(skb, nest); 628 } 629 630 return 0; 631 632 err_cancel: 633 nla_nest_cancel(skb, nest); 634 return -EMSGSIZE; 635 } 636 637 static void __udp_tunnel_nic_assert_locked(struct net_device *dev) 638 { 639 struct udp_tunnel_nic *utn; 640 641 utn = dev->udp_tunnel_nic; 642 if (utn) 643 lockdep_assert_held(&utn->lock); 644 } 645 646 static void __udp_tunnel_nic_lock(struct net_device *dev) 647 { 648 struct udp_tunnel_nic *utn; 649 650 utn = dev->udp_tunnel_nic; 651 if (utn) 652 mutex_lock(&utn->lock); 653 } 654 655 static void __udp_tunnel_nic_unlock(struct net_device *dev) 656 { 657 struct udp_tunnel_nic *utn; 658 659 utn = dev->udp_tunnel_nic; 660 if (utn) 661 mutex_unlock(&utn->lock); 662 } 663 664 static const struct udp_tunnel_nic_ops __udp_tunnel_nic_ops = { 665 .get_port = __udp_tunnel_nic_get_port, 666 .set_port_priv = __udp_tunnel_nic_set_port_priv, 667 .add_port = __udp_tunnel_nic_add_port, 668 .del_port = __udp_tunnel_nic_del_port, 669 .reset_ntf = __udp_tunnel_nic_reset_ntf, 670 .dump_size = __udp_tunnel_nic_dump_size, 671 .dump_write = __udp_tunnel_nic_dump_write, 672 .assert_locked = __udp_tunnel_nic_assert_locked, 673 .lock = __udp_tunnel_nic_lock, 674 .unlock = __udp_tunnel_nic_unlock, 675 }; 676 677 static void 678 udp_tunnel_nic_flush(struct net_device *dev, struct udp_tunnel_nic *utn) 679 { 680 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 681 unsigned int i, j; 682 683 for (i = 0; i < utn->n_tables; i++) 684 for (j = 0; j < info->tables[i].n_entries; j++) { 685 int adj_cnt = -utn->entries[i][j].use_cnt; 686 687 if (adj_cnt) 688 udp_tunnel_nic_entry_adj(utn, i, j, adj_cnt); 689 } 690 691 __udp_tunnel_nic_device_sync(dev, utn); 692 693 for (i = 0; i < utn->n_tables; i++) 694 memset(utn->entries[i], 0, array_size(info->tables[i].n_entries, 695 sizeof(**utn->entries))); 696 WARN_ON(utn->need_sync); 697 utn->need_replay = 0; 698 } 699 700 static void 701 udp_tunnel_nic_replay(struct net_device *dev, struct udp_tunnel_nic *utn) 702 { 703 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 704 struct udp_tunnel_nic_shared_node *node; 705 unsigned int i, j; 706 707 /* Freeze all the ports we are already tracking so that the replay 708 * does not double up the refcount. 709 */ 710 for (i = 0; i < utn->n_tables; i++) 711 for (j = 0; j < info->tables[i].n_entries; j++) 712 udp_tunnel_nic_entry_freeze_used(&utn->entries[i][j]); 713 utn->missed = 0; 714 utn->need_replay = 0; 715 716 if (!info->shared) { 717 udp_tunnel_get_rx_info(dev); 718 } else { 719 list_for_each_entry(node, &info->shared->devices, list) 720 udp_tunnel_get_rx_info(node->dev); 721 } 722 723 for (i = 0; i < utn->n_tables; i++) 724 for (j = 0; j < info->tables[i].n_entries; j++) 725 udp_tunnel_nic_entry_unfreeze(&utn->entries[i][j]); 726 } 727 728 static void udp_tunnel_nic_device_sync_work(struct work_struct *work) 729 { 730 struct udp_tunnel_nic *utn = 731 container_of(work, struct udp_tunnel_nic, work.work); 732 733 /* We cannot block on RTNL here, otherwise we would deadlock with 734 * udp_tunnel_nic_unregister() calling cancel_delayed_work_sync() 735 * while holding RTNL. Requeue with 1 jiffy delay if RTNL is contended. 736 */ 737 if (!rtnl_trylock()) { 738 queue_delayed_work(udp_tunnel_nic_workqueue, &utn->work, 1); 739 return; 740 } 741 mutex_lock(&utn->lock); 742 __udp_tunnel_nic_device_sync(utn->dev, utn); 743 744 if (utn->need_replay) 745 udp_tunnel_nic_replay(utn->dev, utn); 746 747 mutex_unlock(&utn->lock); 748 rtnl_unlock(); 749 } 750 751 static struct udp_tunnel_nic * 752 udp_tunnel_nic_alloc(const struct udp_tunnel_nic_info *info, 753 unsigned int n_tables) 754 { 755 struct udp_tunnel_nic *utn; 756 unsigned int i; 757 758 utn = kzalloc_flex(*utn, entries, n_tables); 759 if (!utn) 760 return NULL; 761 utn->n_tables = n_tables; 762 INIT_DELAYED_WORK(&utn->work, udp_tunnel_nic_device_sync_work); 763 mutex_init(&utn->lock); 764 765 for (i = 0; i < n_tables; i++) { 766 utn->entries[i] = kzalloc_objs(*utn->entries[i], 767 info->tables[i].n_entries); 768 if (!utn->entries[i]) 769 goto err_free_prev_entries; 770 } 771 772 return utn; 773 774 err_free_prev_entries: 775 while (i--) 776 kfree(utn->entries[i]); 777 kfree(utn); 778 return NULL; 779 } 780 781 static void udp_tunnel_nic_free(struct udp_tunnel_nic *utn) 782 { 783 unsigned int i; 784 785 for (i = 0; i < utn->n_tables; i++) 786 kfree(utn->entries[i]); 787 kfree(utn); 788 } 789 790 static int udp_tunnel_nic_register(struct net_device *dev) 791 { 792 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 793 struct udp_tunnel_nic_shared_node *node = NULL; 794 struct udp_tunnel_nic *utn; 795 unsigned int n_tables, i; 796 797 BUILD_BUG_ON(sizeof(utn->missed) * BITS_PER_BYTE < 798 UDP_TUNNEL_NIC_MAX_TABLES); 799 /* Expect use count of at most 2 (IPv4, IPv6) per device */ 800 BUILD_BUG_ON(UDP_TUNNEL_NIC_USE_CNT_MAX < 801 UDP_TUNNEL_NIC_MAX_SHARING_DEVICES * 2); 802 803 /* Check that the driver info is sane */ 804 if (WARN_ON(!info->set_port != !info->unset_port) || 805 WARN_ON(!info->set_port == !info->sync_table) || 806 WARN_ON(!info->tables[0].n_entries)) 807 return -EINVAL; 808 809 if (WARN_ON(info->shared && 810 info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY)) 811 return -EINVAL; 812 813 n_tables = 1; 814 for (i = 1; i < UDP_TUNNEL_NIC_MAX_TABLES; i++) { 815 if (!info->tables[i].n_entries) 816 continue; 817 818 n_tables++; 819 if (WARN_ON(!info->tables[i - 1].n_entries)) 820 return -EINVAL; 821 } 822 823 /* Create UDP tunnel state structures */ 824 if (info->shared) { 825 node = kzalloc_obj(*node); 826 if (!node) 827 return -ENOMEM; 828 829 node->dev = dev; 830 } 831 832 if (info->shared && info->shared->udp_tunnel_nic_info) { 833 utn = info->shared->udp_tunnel_nic_info; 834 } else { 835 utn = udp_tunnel_nic_alloc(info, n_tables); 836 if (!utn) { 837 kfree(node); 838 return -ENOMEM; 839 } 840 } 841 842 if (info->shared) { 843 if (!info->shared->udp_tunnel_nic_info) { 844 INIT_LIST_HEAD(&info->shared->devices); 845 info->shared->udp_tunnel_nic_info = utn; 846 } 847 848 list_add_tail(&node->list, &info->shared->devices); 849 } 850 851 utn->dev = dev; 852 dev_hold(dev); 853 dev->udp_tunnel_nic = utn; 854 855 if (!(info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY)) { 856 udp_tunnel_nic_lock(dev); 857 udp_tunnel_get_rx_info(dev); 858 udp_tunnel_nic_unlock(dev); 859 } 860 861 return 0; 862 } 863 864 static void 865 udp_tunnel_nic_unregister(struct net_device *dev, struct udp_tunnel_nic *utn) 866 { 867 const struct udp_tunnel_nic_info *info = dev->udp_tunnel_nic_info; 868 869 udp_tunnel_nic_lock(dev); 870 871 /* For a shared table remove this dev from the list of sharing devices 872 * and if there are other devices just detach. 873 */ 874 if (info->shared) { 875 struct udp_tunnel_nic_shared_node *node, *first; 876 877 list_for_each_entry(node, &info->shared->devices, list) 878 if (node->dev == dev) 879 break; 880 if (list_entry_is_head(node, &info->shared->devices, list)) { 881 udp_tunnel_nic_unlock(dev); 882 return; 883 } 884 885 list_del(&node->list); 886 kfree(node); 887 888 first = list_first_entry_or_null(&info->shared->devices, 889 typeof(*first), list); 890 if (first) { 891 udp_tunnel_drop_rx_info(dev); 892 utn->dev = first->dev; 893 udp_tunnel_nic_unlock(dev); 894 goto release_dev; 895 } 896 897 info->shared->udp_tunnel_nic_info = NULL; 898 } 899 900 /* Flush before we check work, so we don't waste time adding entries 901 * from the work which we will boot immediately. 902 */ 903 udp_tunnel_nic_flush(dev, utn); 904 udp_tunnel_nic_unlock(dev); 905 906 /* Make sure no work is running or queued before freeing @utn. 907 * The work handler uses rtnl_trylock(), so it will not deadlock 908 * against the RTNL we are holding here. 909 */ 910 cancel_delayed_work_sync(&utn->work); 911 912 udp_tunnel_nic_free(utn); 913 release_dev: 914 dev->udp_tunnel_nic = NULL; 915 dev_put(dev); 916 } 917 918 static int 919 udp_tunnel_nic_netdevice_event(struct notifier_block *unused, 920 unsigned long event, void *ptr) 921 { 922 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 923 const struct udp_tunnel_nic_info *info; 924 struct udp_tunnel_nic *utn; 925 926 info = dev->udp_tunnel_nic_info; 927 if (!info) 928 return NOTIFY_DONE; 929 930 if (event == NETDEV_REGISTER) { 931 int err; 932 933 err = udp_tunnel_nic_register(dev); 934 if (err) 935 netdev_warn(dev, "failed to register for UDP tunnel offloads: %d", err); 936 return notifier_from_errno(err); 937 } 938 /* All other events will need the udp_tunnel_nic state */ 939 utn = dev->udp_tunnel_nic; 940 if (!utn) 941 return NOTIFY_DONE; 942 943 if (event == NETDEV_UNREGISTER) { 944 udp_tunnel_nic_unregister(dev, utn); 945 return NOTIFY_OK; 946 } 947 948 /* All other events only matter if NIC has to be programmed open */ 949 if (!(info->flags & UDP_TUNNEL_NIC_INFO_OPEN_ONLY)) 950 return NOTIFY_DONE; 951 952 if (event == NETDEV_UP) { 953 udp_tunnel_nic_lock(dev); 954 WARN_ON(!udp_tunnel_nic_is_empty(dev, utn)); 955 udp_tunnel_get_rx_info(dev); 956 udp_tunnel_nic_unlock(dev); 957 return NOTIFY_OK; 958 } 959 if (event == NETDEV_GOING_DOWN) { 960 udp_tunnel_nic_lock(dev); 961 udp_tunnel_nic_flush(dev, utn); 962 udp_tunnel_nic_unlock(dev); 963 return NOTIFY_OK; 964 } 965 966 return NOTIFY_DONE; 967 } 968 969 static struct notifier_block udp_tunnel_nic_notifier_block __read_mostly = { 970 .notifier_call = udp_tunnel_nic_netdevice_event, 971 }; 972 973 static int __init udp_tunnel_nic_init_module(void) 974 { 975 int err; 976 977 udp_tunnel_nic_workqueue = alloc_ordered_workqueue("udp_tunnel_nic", 0); 978 if (!udp_tunnel_nic_workqueue) 979 return -ENOMEM; 980 981 rtnl_lock(); 982 udp_tunnel_nic_ops = &__udp_tunnel_nic_ops; 983 rtnl_unlock(); 984 985 err = register_netdevice_notifier(&udp_tunnel_nic_notifier_block); 986 if (err) 987 goto err_unset_ops; 988 989 return 0; 990 991 err_unset_ops: 992 rtnl_lock(); 993 udp_tunnel_nic_ops = NULL; 994 rtnl_unlock(); 995 destroy_workqueue(udp_tunnel_nic_workqueue); 996 return err; 997 } 998 late_initcall(udp_tunnel_nic_init_module); 999 1000 static void __exit udp_tunnel_nic_cleanup_module(void) 1001 { 1002 unregister_netdevice_notifier(&udp_tunnel_nic_notifier_block); 1003 1004 rtnl_lock(); 1005 udp_tunnel_nic_ops = NULL; 1006 rtnl_unlock(); 1007 1008 destroy_workqueue(udp_tunnel_nic_workqueue); 1009 } 1010 module_exit(udp_tunnel_nic_cleanup_module); 1011 1012 MODULE_LICENSE("GPL"); 1013