1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2007-2014 Nicira, Inc. 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #include <linux/init.h> 9 #include <linux/module.h> 10 #include <linux/if_arp.h> 11 #include <linux/if_vlan.h> 12 #include <linux/in.h> 13 #include <linux/ip.h> 14 #include <linux/jhash.h> 15 #include <linux/delay.h> 16 #include <linux/time.h> 17 #include <linux/etherdevice.h> 18 #include <linux/genetlink.h> 19 #include <linux/kernel.h> 20 #include <linux/kthread.h> 21 #include <linux/mutex.h> 22 #include <linux/percpu.h> 23 #include <linux/rcupdate.h> 24 #include <linux/tcp.h> 25 #include <linux/udp.h> 26 #include <linux/ethtool.h> 27 #include <linux/wait.h> 28 #include <asm/div64.h> 29 #include <linux/highmem.h> 30 #include <linux/netfilter_bridge.h> 31 #include <linux/netfilter_ipv4.h> 32 #include <linux/inetdevice.h> 33 #include <linux/list.h> 34 #include <linux/openvswitch.h> 35 #include <linux/rculist.h> 36 #include <linux/dmi.h> 37 #include <net/genetlink.h> 38 #include <net/net_namespace.h> 39 #include <net/netns/generic.h> 40 #include <net/pkt_cls.h> 41 42 #include "datapath.h" 43 #include "flow.h" 44 #include "flow_table.h" 45 #include "flow_netlink.h" 46 #include "meter.h" 47 #include "openvswitch_trace.h" 48 #include "vport-internal_dev.h" 49 #include "vport-netdev.h" 50 51 unsigned int ovs_net_id __read_mostly; 52 53 static struct genl_family dp_packet_genl_family; 54 static struct genl_family dp_flow_genl_family; 55 static struct genl_family dp_datapath_genl_family; 56 57 static const struct nla_policy flow_policy[]; 58 59 static const struct genl_multicast_group ovs_dp_flow_multicast_group = { 60 .name = OVS_FLOW_MCGROUP, 61 }; 62 63 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = { 64 .name = OVS_DATAPATH_MCGROUP, 65 }; 66 67 static const struct genl_multicast_group ovs_dp_vport_multicast_group = { 68 .name = OVS_VPORT_MCGROUP, 69 }; 70 71 /* Check if need to build a reply message. 72 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */ 73 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info, 74 unsigned int group) 75 { 76 return info->nlhdr->nlmsg_flags & NLM_F_ECHO || 77 genl_has_listeners(family, genl_info_net(info), group); 78 } 79 80 static void ovs_notify(struct genl_family *family, 81 struct sk_buff *skb, struct genl_info *info) 82 { 83 genl_notify(family, skb, info, 0, GFP_KERNEL); 84 } 85 86 /** 87 * DOC: Locking: 88 * 89 * All writes e.g. Writes to device state (add/remove datapath, port, set 90 * operations on vports, etc.), Writes to other state (flow table 91 * modifications, set miscellaneous datapath parameters, etc.) are protected 92 * by ovs_lock. 93 * 94 * Reads are protected by RCU. 95 * 96 * There are a few special cases (mostly stats) that have their own 97 * synchronization but they nest under all of above and don't interact with 98 * each other. 99 * 100 * The RTNL lock nests inside ovs_mutex. 101 */ 102 103 static DEFINE_MUTEX(ovs_mutex); 104 105 void ovs_lock(void) 106 { 107 mutex_lock(&ovs_mutex); 108 } 109 110 void ovs_unlock(void) 111 { 112 mutex_unlock(&ovs_mutex); 113 } 114 115 #ifdef CONFIG_LOCKDEP 116 int lockdep_ovsl_is_held(void) 117 { 118 if (debug_locks) 119 return lockdep_is_held(&ovs_mutex); 120 else 121 return 1; 122 } 123 #endif 124 125 static struct vport *new_vport(const struct vport_parms *); 126 static int queue_gso_packets(struct datapath *dp, struct sk_buff *, 127 const struct sw_flow_key *, 128 const struct dp_upcall_info *, 129 uint32_t cutlen); 130 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *, 131 const struct sw_flow_key *, 132 const struct dp_upcall_info *, 133 uint32_t cutlen); 134 135 static void ovs_dp_masks_rebalance(struct work_struct *work); 136 137 static int ovs_dp_set_upcall_portids(struct datapath *, const struct nlattr *); 138 139 /* Must be called with rcu_read_lock or ovs_mutex. */ 140 const char *ovs_dp_name(const struct datapath *dp) 141 { 142 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); 143 return ovs_vport_name(vport); 144 } 145 146 static int get_dpifindex(const struct datapath *dp) 147 { 148 struct vport *local; 149 int ifindex; 150 151 rcu_read_lock(); 152 153 local = ovs_vport_rcu(dp, OVSP_LOCAL); 154 if (local) 155 ifindex = local->dev->ifindex; 156 else 157 ifindex = 0; 158 159 rcu_read_unlock(); 160 161 return ifindex; 162 } 163 164 static void destroy_dp_rcu(struct rcu_head *rcu) 165 { 166 struct datapath *dp = container_of(rcu, struct datapath, rcu); 167 168 ovs_flow_tbl_destroy(&dp->table); 169 free_percpu(dp->stats_percpu); 170 kfree(dp->ports); 171 ovs_meters_exit(dp); 172 kfree(rcu_dereference_raw(dp->upcall_portids)); 173 kfree(dp); 174 } 175 176 static struct hlist_head *vport_hash_bucket(const struct datapath *dp, 177 u16 port_no) 178 { 179 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; 180 } 181 182 /* Called with ovs_mutex or RCU read lock. */ 183 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) 184 { 185 struct vport *vport; 186 struct hlist_head *head; 187 188 head = vport_hash_bucket(dp, port_no); 189 hlist_for_each_entry_rcu(vport, head, dp_hash_node, 190 lockdep_ovsl_is_held()) { 191 if (vport->port_no == port_no) 192 return vport; 193 } 194 return NULL; 195 } 196 197 /* Called with ovs_mutex. */ 198 static struct vport *new_vport(const struct vport_parms *parms) 199 { 200 struct vport *vport; 201 202 vport = ovs_vport_add(parms); 203 if (!IS_ERR(vport)) { 204 struct datapath *dp = parms->dp; 205 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); 206 207 hlist_add_head_rcu(&vport->dp_hash_node, head); 208 } 209 return vport; 210 } 211 212 void ovs_dp_detach_port(struct vport *p) 213 { 214 ASSERT_OVSL(); 215 216 /* First drop references to device. */ 217 hlist_del_rcu(&p->dp_hash_node); 218 219 /* Then destroy it. */ 220 ovs_vport_del(p); 221 } 222 223 /* Must be called with rcu_read_lock. */ 224 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key) 225 { 226 const struct vport *p = OVS_CB(skb)->input_vport; 227 struct datapath *dp = p->dp; 228 struct sw_flow *flow; 229 struct sw_flow_actions *sf_acts; 230 struct dp_stats_percpu *stats; 231 u64 *stats_counter; 232 u32 n_mask_hit; 233 u32 n_cache_hit; 234 int error; 235 236 stats = this_cpu_ptr(dp->stats_percpu); 237 238 /* Look up flow. */ 239 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb), 240 &n_mask_hit, &n_cache_hit); 241 if (unlikely(!flow)) { 242 struct dp_upcall_info upcall; 243 244 memset(&upcall, 0, sizeof(upcall)); 245 upcall.cmd = OVS_PACKET_CMD_MISS; 246 247 if (dp->user_features & OVS_DP_F_DISPATCH_UPCALL_PER_CPU) 248 upcall.portid = 249 ovs_dp_get_upcall_portid(dp, smp_processor_id()); 250 else 251 upcall.portid = ovs_vport_find_upcall_portid(p, skb); 252 253 upcall.mru = OVS_CB(skb)->mru; 254 error = ovs_dp_upcall(dp, skb, key, &upcall, 0); 255 switch (error) { 256 case 0: 257 case -EAGAIN: 258 case -ERESTARTSYS: 259 case -EINTR: 260 consume_skb(skb); 261 break; 262 default: 263 kfree_skb(skb); 264 break; 265 } 266 stats_counter = &stats->n_missed; 267 goto out; 268 } 269 270 ovs_flow_stats_update(flow, key->tp.flags, skb); 271 sf_acts = rcu_dereference(flow->sf_acts); 272 error = ovs_execute_actions(dp, skb, sf_acts, key); 273 if (unlikely(error)) 274 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n", 275 ovs_dp_name(dp), error); 276 277 stats_counter = &stats->n_hit; 278 279 out: 280 /* Update datapath statistics. */ 281 u64_stats_update_begin(&stats->syncp); 282 (*stats_counter)++; 283 stats->n_mask_hit += n_mask_hit; 284 stats->n_cache_hit += n_cache_hit; 285 u64_stats_update_end(&stats->syncp); 286 } 287 288 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, 289 const struct sw_flow_key *key, 290 const struct dp_upcall_info *upcall_info, 291 uint32_t cutlen) 292 { 293 struct dp_stats_percpu *stats; 294 int err; 295 296 if (trace_ovs_dp_upcall_enabled()) 297 trace_ovs_dp_upcall(dp, skb, key, upcall_info); 298 299 if (upcall_info->portid == 0) { 300 err = -ENOTCONN; 301 goto err; 302 } 303 304 if (!skb_is_gso(skb)) 305 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen); 306 else 307 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen); 308 if (err) 309 goto err; 310 311 return 0; 312 313 err: 314 stats = this_cpu_ptr(dp->stats_percpu); 315 316 u64_stats_update_begin(&stats->syncp); 317 stats->n_lost++; 318 u64_stats_update_end(&stats->syncp); 319 320 return err; 321 } 322 323 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb, 324 const struct sw_flow_key *key, 325 const struct dp_upcall_info *upcall_info, 326 uint32_t cutlen) 327 { 328 unsigned int gso_type = skb_shinfo(skb)->gso_type; 329 struct sw_flow_key later_key; 330 struct sk_buff *segs, *nskb; 331 int err; 332 333 BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_GSO_CB_OFFSET); 334 segs = __skb_gso_segment(skb, NETIF_F_SG, false); 335 if (IS_ERR(segs)) 336 return PTR_ERR(segs); 337 if (segs == NULL) 338 return -EINVAL; 339 340 if (gso_type & SKB_GSO_UDP) { 341 /* The initial flow key extracted by ovs_flow_key_extract() 342 * in this case is for a first fragment, so we need to 343 * properly mark later fragments. 344 */ 345 later_key = *key; 346 later_key.ip.frag = OVS_FRAG_TYPE_LATER; 347 } 348 349 /* Queue all of the segments. */ 350 skb_list_walk_safe(segs, skb, nskb) { 351 if (gso_type & SKB_GSO_UDP && skb != segs) 352 key = &later_key; 353 354 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen); 355 if (err) 356 break; 357 358 } 359 360 /* Free all of the segments. */ 361 skb_list_walk_safe(segs, skb, nskb) { 362 if (err) 363 kfree_skb(skb); 364 else 365 consume_skb(skb); 366 } 367 return err; 368 } 369 370 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info, 371 unsigned int hdrlen, int actions_attrlen) 372 { 373 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) 374 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */ 375 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */ 376 + nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */ 377 + nla_total_size(sizeof(u64)); /* OVS_PACKET_ATTR_HASH */ 378 379 /* OVS_PACKET_ATTR_USERDATA */ 380 if (upcall_info->userdata) 381 size += NLA_ALIGN(upcall_info->userdata->nla_len); 382 383 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */ 384 if (upcall_info->egress_tun_info) 385 size += nla_total_size(ovs_tun_key_attr_size()); 386 387 /* OVS_PACKET_ATTR_ACTIONS */ 388 if (upcall_info->actions_len) 389 size += nla_total_size(actions_attrlen); 390 391 /* OVS_PACKET_ATTR_MRU */ 392 if (upcall_info->mru) 393 size += nla_total_size(sizeof(upcall_info->mru)); 394 395 return size; 396 } 397 398 static void pad_packet(struct datapath *dp, struct sk_buff *skb) 399 { 400 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) { 401 size_t plen = NLA_ALIGN(skb->len) - skb->len; 402 403 if (plen > 0) 404 skb_put_zero(skb, plen); 405 } 406 } 407 408 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, 409 const struct sw_flow_key *key, 410 const struct dp_upcall_info *upcall_info, 411 uint32_t cutlen) 412 { 413 struct ovs_header *upcall; 414 struct sk_buff *nskb = NULL; 415 struct sk_buff *user_skb = NULL; /* to be queued to userspace */ 416 struct nlattr *nla; 417 size_t len; 418 unsigned int hlen; 419 int err, dp_ifindex; 420 u64 hash; 421 422 dp_ifindex = get_dpifindex(dp); 423 if (!dp_ifindex) 424 return -ENODEV; 425 426 if (skb_vlan_tag_present(skb)) { 427 nskb = skb_clone(skb, GFP_ATOMIC); 428 if (!nskb) 429 return -ENOMEM; 430 431 nskb = __vlan_hwaccel_push_inside(nskb); 432 if (!nskb) 433 return -ENOMEM; 434 435 skb = nskb; 436 } 437 438 if (nla_attr_size(skb->len) > USHRT_MAX) { 439 err = -EFBIG; 440 goto out; 441 } 442 443 /* Complete checksum if needed */ 444 if (skb->ip_summed == CHECKSUM_PARTIAL && 445 (err = skb_csum_hwoffload_help(skb, 0))) 446 goto out; 447 448 /* Older versions of OVS user space enforce alignment of the last 449 * Netlink attribute to NLA_ALIGNTO which would require extensive 450 * padding logic. Only perform zerocopy if padding is not required. 451 */ 452 if (dp->user_features & OVS_DP_F_UNALIGNED) 453 hlen = skb_zerocopy_headlen(skb); 454 else 455 hlen = skb->len; 456 457 len = upcall_msg_size(upcall_info, hlen - cutlen, 458 OVS_CB(skb)->acts_origlen); 459 user_skb = genlmsg_new(len, GFP_ATOMIC); 460 if (!user_skb) { 461 err = -ENOMEM; 462 goto out; 463 } 464 465 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 466 0, upcall_info->cmd); 467 if (!upcall) { 468 err = -EINVAL; 469 goto out; 470 } 471 upcall->dp_ifindex = dp_ifindex; 472 473 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb); 474 if (err) 475 goto out; 476 477 if (upcall_info->userdata) 478 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, 479 nla_len(upcall_info->userdata), 480 nla_data(upcall_info->userdata)); 481 482 if (upcall_info->egress_tun_info) { 483 nla = nla_nest_start_noflag(user_skb, 484 OVS_PACKET_ATTR_EGRESS_TUN_KEY); 485 if (!nla) { 486 err = -EMSGSIZE; 487 goto out; 488 } 489 err = ovs_nla_put_tunnel_info(user_skb, 490 upcall_info->egress_tun_info); 491 if (err) 492 goto out; 493 494 nla_nest_end(user_skb, nla); 495 } 496 497 if (upcall_info->actions_len) { 498 nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS); 499 if (!nla) { 500 err = -EMSGSIZE; 501 goto out; 502 } 503 err = ovs_nla_put_actions(upcall_info->actions, 504 upcall_info->actions_len, 505 user_skb); 506 if (!err) 507 nla_nest_end(user_skb, nla); 508 else 509 nla_nest_cancel(user_skb, nla); 510 } 511 512 /* Add OVS_PACKET_ATTR_MRU */ 513 if (upcall_info->mru && 514 nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU, upcall_info->mru)) { 515 err = -ENOBUFS; 516 goto out; 517 } 518 519 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */ 520 if (cutlen > 0 && 521 nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN, skb->len)) { 522 err = -ENOBUFS; 523 goto out; 524 } 525 526 /* Add OVS_PACKET_ATTR_HASH */ 527 hash = skb_get_hash_raw(skb); 528 if (skb->sw_hash) 529 hash |= OVS_PACKET_HASH_SW_BIT; 530 531 if (skb->l4_hash) 532 hash |= OVS_PACKET_HASH_L4_BIT; 533 534 if (nla_put(user_skb, OVS_PACKET_ATTR_HASH, sizeof (u64), &hash)) { 535 err = -ENOBUFS; 536 goto out; 537 } 538 539 /* Only reserve room for attribute header, packet data is added 540 * in skb_zerocopy() */ 541 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) { 542 err = -ENOBUFS; 543 goto out; 544 } 545 nla->nla_len = nla_attr_size(skb->len - cutlen); 546 547 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen); 548 if (err) 549 goto out; 550 551 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */ 552 pad_packet(dp, user_skb); 553 554 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len; 555 556 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid); 557 user_skb = NULL; 558 out: 559 if (err) 560 skb_tx_error(skb); 561 consume_skb(user_skb); 562 consume_skb(nskb); 563 564 return err; 565 } 566 567 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) 568 { 569 struct ovs_header *ovs_header = info->userhdr; 570 struct net *net = sock_net(skb->sk); 571 struct nlattr **a = info->attrs; 572 struct sw_flow_actions *acts; 573 struct sk_buff *packet; 574 struct sw_flow *flow; 575 struct sw_flow_actions *sf_acts; 576 struct datapath *dp; 577 struct vport *input_vport; 578 u16 mru = 0; 579 u64 hash; 580 int len; 581 int err; 582 bool log = !a[OVS_PACKET_ATTR_PROBE]; 583 584 err = -EINVAL; 585 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || 586 !a[OVS_PACKET_ATTR_ACTIONS]) 587 goto err; 588 589 len = nla_len(a[OVS_PACKET_ATTR_PACKET]); 590 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); 591 err = -ENOMEM; 592 if (!packet) 593 goto err; 594 skb_reserve(packet, NET_IP_ALIGN); 595 596 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); 597 598 /* Set packet's mru */ 599 if (a[OVS_PACKET_ATTR_MRU]) { 600 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]); 601 packet->ignore_df = 1; 602 } 603 OVS_CB(packet)->mru = mru; 604 605 if (a[OVS_PACKET_ATTR_HASH]) { 606 hash = nla_get_u64(a[OVS_PACKET_ATTR_HASH]); 607 608 __skb_set_hash(packet, hash & 0xFFFFFFFFULL, 609 !!(hash & OVS_PACKET_HASH_SW_BIT), 610 !!(hash & OVS_PACKET_HASH_L4_BIT)); 611 } 612 613 /* Build an sw_flow for sending this packet. */ 614 flow = ovs_flow_alloc(); 615 err = PTR_ERR(flow); 616 if (IS_ERR(flow)) 617 goto err_kfree_skb; 618 619 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY], 620 packet, &flow->key, log); 621 if (err) 622 goto err_flow_free; 623 624 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS], 625 &flow->key, &acts, log); 626 if (err) 627 goto err_flow_free; 628 629 rcu_assign_pointer(flow->sf_acts, acts); 630 packet->priority = flow->key.phy.priority; 631 packet->mark = flow->key.phy.skb_mark; 632 633 rcu_read_lock(); 634 dp = get_dp_rcu(net, ovs_header->dp_ifindex); 635 err = -ENODEV; 636 if (!dp) 637 goto err_unlock; 638 639 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port); 640 if (!input_vport) 641 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL); 642 643 if (!input_vport) 644 goto err_unlock; 645 646 packet->dev = input_vport->dev; 647 OVS_CB(packet)->input_vport = input_vport; 648 sf_acts = rcu_dereference(flow->sf_acts); 649 650 local_bh_disable(); 651 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key); 652 local_bh_enable(); 653 rcu_read_unlock(); 654 655 ovs_flow_free(flow, false); 656 return err; 657 658 err_unlock: 659 rcu_read_unlock(); 660 err_flow_free: 661 ovs_flow_free(flow, false); 662 err_kfree_skb: 663 kfree_skb(packet); 664 err: 665 return err; 666 } 667 668 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { 669 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, 670 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, 671 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, 672 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG }, 673 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 }, 674 [OVS_PACKET_ATTR_HASH] = { .type = NLA_U64 }, 675 }; 676 677 static const struct genl_small_ops dp_packet_genl_ops[] = { 678 { .cmd = OVS_PACKET_CMD_EXECUTE, 679 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 680 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 681 .doit = ovs_packet_cmd_execute 682 } 683 }; 684 685 static struct genl_family dp_packet_genl_family __ro_after_init = { 686 .hdrsize = sizeof(struct ovs_header), 687 .name = OVS_PACKET_FAMILY, 688 .version = OVS_PACKET_VERSION, 689 .maxattr = OVS_PACKET_ATTR_MAX, 690 .policy = packet_policy, 691 .netnsok = true, 692 .parallel_ops = true, 693 .small_ops = dp_packet_genl_ops, 694 .n_small_ops = ARRAY_SIZE(dp_packet_genl_ops), 695 .module = THIS_MODULE, 696 }; 697 698 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats, 699 struct ovs_dp_megaflow_stats *mega_stats) 700 { 701 int i; 702 703 memset(mega_stats, 0, sizeof(*mega_stats)); 704 705 stats->n_flows = ovs_flow_tbl_count(&dp->table); 706 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table); 707 708 stats->n_hit = stats->n_missed = stats->n_lost = 0; 709 710 for_each_possible_cpu(i) { 711 const struct dp_stats_percpu *percpu_stats; 712 struct dp_stats_percpu local_stats; 713 unsigned int start; 714 715 percpu_stats = per_cpu_ptr(dp->stats_percpu, i); 716 717 do { 718 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp); 719 local_stats = *percpu_stats; 720 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start)); 721 722 stats->n_hit += local_stats.n_hit; 723 stats->n_missed += local_stats.n_missed; 724 stats->n_lost += local_stats.n_lost; 725 mega_stats->n_mask_hit += local_stats.n_mask_hit; 726 mega_stats->n_cache_hit += local_stats.n_cache_hit; 727 } 728 } 729 730 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags) 731 { 732 return ovs_identifier_is_ufid(sfid) && 733 !(ufid_flags & OVS_UFID_F_OMIT_KEY); 734 } 735 736 static bool should_fill_mask(uint32_t ufid_flags) 737 { 738 return !(ufid_flags & OVS_UFID_F_OMIT_MASK); 739 } 740 741 static bool should_fill_actions(uint32_t ufid_flags) 742 { 743 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS); 744 } 745 746 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts, 747 const struct sw_flow_id *sfid, 748 uint32_t ufid_flags) 749 { 750 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header)); 751 752 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback 753 * see ovs_nla_put_identifier() 754 */ 755 if (sfid && ovs_identifier_is_ufid(sfid)) 756 len += nla_total_size(sfid->ufid_len); 757 else 758 len += nla_total_size(ovs_key_attr_size()); 759 760 /* OVS_FLOW_ATTR_KEY */ 761 if (!sfid || should_fill_key(sfid, ufid_flags)) 762 len += nla_total_size(ovs_key_attr_size()); 763 764 /* OVS_FLOW_ATTR_MASK */ 765 if (should_fill_mask(ufid_flags)) 766 len += nla_total_size(ovs_key_attr_size()); 767 768 /* OVS_FLOW_ATTR_ACTIONS */ 769 if (should_fill_actions(ufid_flags)) 770 len += nla_total_size(acts->orig_len); 771 772 return len 773 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ 774 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ 775 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */ 776 } 777 778 /* Called with ovs_mutex or RCU read lock. */ 779 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow, 780 struct sk_buff *skb) 781 { 782 struct ovs_flow_stats stats; 783 __be16 tcp_flags; 784 unsigned long used; 785 786 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags); 787 788 if (used && 789 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used), 790 OVS_FLOW_ATTR_PAD)) 791 return -EMSGSIZE; 792 793 if (stats.n_packets && 794 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS, 795 sizeof(struct ovs_flow_stats), &stats, 796 OVS_FLOW_ATTR_PAD)) 797 return -EMSGSIZE; 798 799 if ((u8)ntohs(tcp_flags) && 800 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags))) 801 return -EMSGSIZE; 802 803 return 0; 804 } 805 806 /* Called with ovs_mutex or RCU read lock. */ 807 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow, 808 struct sk_buff *skb, int skb_orig_len) 809 { 810 struct nlattr *start; 811 int err; 812 813 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if 814 * this is the first flow to be dumped into 'skb'. This is unusual for 815 * Netlink but individual action lists can be longer than 816 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. 817 * The userspace caller can always fetch the actions separately if it 818 * really wants them. (Most userspace callers in fact don't care.) 819 * 820 * This can only fail for dump operations because the skb is always 821 * properly sized for single flows. 822 */ 823 start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS); 824 if (start) { 825 const struct sw_flow_actions *sf_acts; 826 827 sf_acts = rcu_dereference_ovsl(flow->sf_acts); 828 err = ovs_nla_put_actions(sf_acts->actions, 829 sf_acts->actions_len, skb); 830 831 if (!err) 832 nla_nest_end(skb, start); 833 else { 834 if (skb_orig_len) 835 return err; 836 837 nla_nest_cancel(skb, start); 838 } 839 } else if (skb_orig_len) { 840 return -EMSGSIZE; 841 } 842 843 return 0; 844 } 845 846 /* Called with ovs_mutex or RCU read lock. */ 847 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex, 848 struct sk_buff *skb, u32 portid, 849 u32 seq, u32 flags, u8 cmd, u32 ufid_flags) 850 { 851 const int skb_orig_len = skb->len; 852 struct ovs_header *ovs_header; 853 int err; 854 855 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, 856 flags, cmd); 857 if (!ovs_header) 858 return -EMSGSIZE; 859 860 ovs_header->dp_ifindex = dp_ifindex; 861 862 err = ovs_nla_put_identifier(flow, skb); 863 if (err) 864 goto error; 865 866 if (should_fill_key(&flow->id, ufid_flags)) { 867 err = ovs_nla_put_masked_key(flow, skb); 868 if (err) 869 goto error; 870 } 871 872 if (should_fill_mask(ufid_flags)) { 873 err = ovs_nla_put_mask(flow, skb); 874 if (err) 875 goto error; 876 } 877 878 err = ovs_flow_cmd_fill_stats(flow, skb); 879 if (err) 880 goto error; 881 882 if (should_fill_actions(ufid_flags)) { 883 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len); 884 if (err) 885 goto error; 886 } 887 888 genlmsg_end(skb, ovs_header); 889 return 0; 890 891 error: 892 genlmsg_cancel(skb, ovs_header); 893 return err; 894 } 895 896 /* May not be called with RCU read lock. */ 897 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts, 898 const struct sw_flow_id *sfid, 899 struct genl_info *info, 900 bool always, 901 uint32_t ufid_flags) 902 { 903 struct sk_buff *skb; 904 size_t len; 905 906 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0)) 907 return NULL; 908 909 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags); 910 skb = genlmsg_new(len, GFP_KERNEL); 911 if (!skb) 912 return ERR_PTR(-ENOMEM); 913 914 return skb; 915 } 916 917 /* Called with ovs_mutex. */ 918 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow, 919 int dp_ifindex, 920 struct genl_info *info, u8 cmd, 921 bool always, u32 ufid_flags) 922 { 923 struct sk_buff *skb; 924 int retval; 925 926 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), 927 &flow->id, info, always, ufid_flags); 928 if (IS_ERR_OR_NULL(skb)) 929 return skb; 930 931 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb, 932 info->snd_portid, info->snd_seq, 0, 933 cmd, ufid_flags); 934 if (WARN_ON_ONCE(retval < 0)) { 935 kfree_skb(skb); 936 skb = ERR_PTR(retval); 937 } 938 return skb; 939 } 940 941 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info) 942 { 943 struct net *net = sock_net(skb->sk); 944 struct nlattr **a = info->attrs; 945 struct ovs_header *ovs_header = info->userhdr; 946 struct sw_flow *flow = NULL, *new_flow; 947 struct sw_flow_mask mask; 948 struct sk_buff *reply; 949 struct datapath *dp; 950 struct sw_flow_actions *acts; 951 struct sw_flow_match match; 952 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 953 int error; 954 bool log = !a[OVS_FLOW_ATTR_PROBE]; 955 956 /* Must have key and actions. */ 957 error = -EINVAL; 958 if (!a[OVS_FLOW_ATTR_KEY]) { 959 OVS_NLERR(log, "Flow key attr not present in new flow."); 960 goto error; 961 } 962 if (!a[OVS_FLOW_ATTR_ACTIONS]) { 963 OVS_NLERR(log, "Flow actions attr not present in new flow."); 964 goto error; 965 } 966 967 /* Most of the time we need to allocate a new flow, do it before 968 * locking. 969 */ 970 new_flow = ovs_flow_alloc(); 971 if (IS_ERR(new_flow)) { 972 error = PTR_ERR(new_flow); 973 goto error; 974 } 975 976 /* Extract key. */ 977 ovs_match_init(&match, &new_flow->key, false, &mask); 978 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], 979 a[OVS_FLOW_ATTR_MASK], log); 980 if (error) 981 goto err_kfree_flow; 982 983 /* Extract flow identifier. */ 984 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID], 985 &new_flow->key, log); 986 if (error) 987 goto err_kfree_flow; 988 989 /* unmasked key is needed to match when ufid is not used. */ 990 if (ovs_identifier_is_key(&new_flow->id)) 991 match.key = new_flow->id.unmasked_key; 992 993 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask); 994 995 /* Validate actions. */ 996 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS], 997 &new_flow->key, &acts, log); 998 if (error) { 999 OVS_NLERR(log, "Flow actions may not be safe on all matching packets."); 1000 goto err_kfree_flow; 1001 } 1002 1003 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false, 1004 ufid_flags); 1005 if (IS_ERR(reply)) { 1006 error = PTR_ERR(reply); 1007 goto err_kfree_acts; 1008 } 1009 1010 ovs_lock(); 1011 dp = get_dp(net, ovs_header->dp_ifindex); 1012 if (unlikely(!dp)) { 1013 error = -ENODEV; 1014 goto err_unlock_ovs; 1015 } 1016 1017 /* Check if this is a duplicate flow */ 1018 if (ovs_identifier_is_ufid(&new_flow->id)) 1019 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id); 1020 if (!flow) 1021 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key); 1022 if (likely(!flow)) { 1023 rcu_assign_pointer(new_flow->sf_acts, acts); 1024 1025 /* Put flow in bucket. */ 1026 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask); 1027 if (unlikely(error)) { 1028 acts = NULL; 1029 goto err_unlock_ovs; 1030 } 1031 1032 if (unlikely(reply)) { 1033 error = ovs_flow_cmd_fill_info(new_flow, 1034 ovs_header->dp_ifindex, 1035 reply, info->snd_portid, 1036 info->snd_seq, 0, 1037 OVS_FLOW_CMD_NEW, 1038 ufid_flags); 1039 BUG_ON(error < 0); 1040 } 1041 ovs_unlock(); 1042 } else { 1043 struct sw_flow_actions *old_acts; 1044 1045 /* Bail out if we're not allowed to modify an existing flow. 1046 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL 1047 * because Generic Netlink treats the latter as a dump 1048 * request. We also accept NLM_F_EXCL in case that bug ever 1049 * gets fixed. 1050 */ 1051 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE 1052 | NLM_F_EXCL))) { 1053 error = -EEXIST; 1054 goto err_unlock_ovs; 1055 } 1056 /* The flow identifier has to be the same for flow updates. 1057 * Look for any overlapping flow. 1058 */ 1059 if (unlikely(!ovs_flow_cmp(flow, &match))) { 1060 if (ovs_identifier_is_key(&flow->id)) 1061 flow = ovs_flow_tbl_lookup_exact(&dp->table, 1062 &match); 1063 else /* UFID matches but key is different */ 1064 flow = NULL; 1065 if (!flow) { 1066 error = -ENOENT; 1067 goto err_unlock_ovs; 1068 } 1069 } 1070 /* Update actions. */ 1071 old_acts = ovsl_dereference(flow->sf_acts); 1072 rcu_assign_pointer(flow->sf_acts, acts); 1073 1074 if (unlikely(reply)) { 1075 error = ovs_flow_cmd_fill_info(flow, 1076 ovs_header->dp_ifindex, 1077 reply, info->snd_portid, 1078 info->snd_seq, 0, 1079 OVS_FLOW_CMD_NEW, 1080 ufid_flags); 1081 BUG_ON(error < 0); 1082 } 1083 ovs_unlock(); 1084 1085 ovs_nla_free_flow_actions_rcu(old_acts); 1086 ovs_flow_free(new_flow, false); 1087 } 1088 1089 if (reply) 1090 ovs_notify(&dp_flow_genl_family, reply, info); 1091 return 0; 1092 1093 err_unlock_ovs: 1094 ovs_unlock(); 1095 kfree_skb(reply); 1096 err_kfree_acts: 1097 ovs_nla_free_flow_actions(acts); 1098 err_kfree_flow: 1099 ovs_flow_free(new_flow, false); 1100 error: 1101 return error; 1102 } 1103 1104 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */ 1105 static noinline_for_stack 1106 struct sw_flow_actions *get_flow_actions(struct net *net, 1107 const struct nlattr *a, 1108 const struct sw_flow_key *key, 1109 const struct sw_flow_mask *mask, 1110 bool log) 1111 { 1112 struct sw_flow_actions *acts; 1113 struct sw_flow_key masked_key; 1114 int error; 1115 1116 ovs_flow_mask_key(&masked_key, key, true, mask); 1117 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log); 1118 if (error) { 1119 OVS_NLERR(log, 1120 "Actions may not be safe on all matching packets"); 1121 return ERR_PTR(error); 1122 } 1123 1124 return acts; 1125 } 1126 1127 /* Factor out match-init and action-copy to avoid 1128 * "Wframe-larger-than=1024" warning. Because mask is only 1129 * used to get actions, we new a function to save some 1130 * stack space. 1131 * 1132 * If there are not key and action attrs, we return 0 1133 * directly. In the case, the caller will also not use the 1134 * match as before. If there is action attr, we try to get 1135 * actions and save them to *acts. Before returning from 1136 * the function, we reset the match->mask pointer. Because 1137 * we should not to return match object with dangling reference 1138 * to mask. 1139 * */ 1140 static noinline_for_stack int 1141 ovs_nla_init_match_and_action(struct net *net, 1142 struct sw_flow_match *match, 1143 struct sw_flow_key *key, 1144 struct nlattr **a, 1145 struct sw_flow_actions **acts, 1146 bool log) 1147 { 1148 struct sw_flow_mask mask; 1149 int error = 0; 1150 1151 if (a[OVS_FLOW_ATTR_KEY]) { 1152 ovs_match_init(match, key, true, &mask); 1153 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY], 1154 a[OVS_FLOW_ATTR_MASK], log); 1155 if (error) 1156 goto error; 1157 } 1158 1159 if (a[OVS_FLOW_ATTR_ACTIONS]) { 1160 if (!a[OVS_FLOW_ATTR_KEY]) { 1161 OVS_NLERR(log, 1162 "Flow key attribute not present in set flow."); 1163 error = -EINVAL; 1164 goto error; 1165 } 1166 1167 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key, 1168 &mask, log); 1169 if (IS_ERR(*acts)) { 1170 error = PTR_ERR(*acts); 1171 goto error; 1172 } 1173 } 1174 1175 /* On success, error is 0. */ 1176 error: 1177 match->mask = NULL; 1178 return error; 1179 } 1180 1181 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) 1182 { 1183 struct net *net = sock_net(skb->sk); 1184 struct nlattr **a = info->attrs; 1185 struct ovs_header *ovs_header = info->userhdr; 1186 struct sw_flow_key key; 1187 struct sw_flow *flow; 1188 struct sk_buff *reply = NULL; 1189 struct datapath *dp; 1190 struct sw_flow_actions *old_acts = NULL, *acts = NULL; 1191 struct sw_flow_match match; 1192 struct sw_flow_id sfid; 1193 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 1194 int error = 0; 1195 bool log = !a[OVS_FLOW_ATTR_PROBE]; 1196 bool ufid_present; 1197 1198 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log); 1199 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) { 1200 OVS_NLERR(log, 1201 "Flow set message rejected, Key attribute missing."); 1202 return -EINVAL; 1203 } 1204 1205 error = ovs_nla_init_match_and_action(net, &match, &key, a, 1206 &acts, log); 1207 if (error) 1208 goto error; 1209 1210 if (acts) { 1211 /* Can allocate before locking if have acts. */ 1212 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false, 1213 ufid_flags); 1214 if (IS_ERR(reply)) { 1215 error = PTR_ERR(reply); 1216 goto err_kfree_acts; 1217 } 1218 } 1219 1220 ovs_lock(); 1221 dp = get_dp(net, ovs_header->dp_ifindex); 1222 if (unlikely(!dp)) { 1223 error = -ENODEV; 1224 goto err_unlock_ovs; 1225 } 1226 /* Check that the flow exists. */ 1227 if (ufid_present) 1228 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid); 1229 else 1230 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); 1231 if (unlikely(!flow)) { 1232 error = -ENOENT; 1233 goto err_unlock_ovs; 1234 } 1235 1236 /* Update actions, if present. */ 1237 if (likely(acts)) { 1238 old_acts = ovsl_dereference(flow->sf_acts); 1239 rcu_assign_pointer(flow->sf_acts, acts); 1240 1241 if (unlikely(reply)) { 1242 error = ovs_flow_cmd_fill_info(flow, 1243 ovs_header->dp_ifindex, 1244 reply, info->snd_portid, 1245 info->snd_seq, 0, 1246 OVS_FLOW_CMD_SET, 1247 ufid_flags); 1248 BUG_ON(error < 0); 1249 } 1250 } else { 1251 /* Could not alloc without acts before locking. */ 1252 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, 1253 info, OVS_FLOW_CMD_SET, false, 1254 ufid_flags); 1255 1256 if (IS_ERR(reply)) { 1257 error = PTR_ERR(reply); 1258 goto err_unlock_ovs; 1259 } 1260 } 1261 1262 /* Clear stats. */ 1263 if (a[OVS_FLOW_ATTR_CLEAR]) 1264 ovs_flow_stats_clear(flow); 1265 ovs_unlock(); 1266 1267 if (reply) 1268 ovs_notify(&dp_flow_genl_family, reply, info); 1269 if (old_acts) 1270 ovs_nla_free_flow_actions_rcu(old_acts); 1271 1272 return 0; 1273 1274 err_unlock_ovs: 1275 ovs_unlock(); 1276 kfree_skb(reply); 1277 err_kfree_acts: 1278 ovs_nla_free_flow_actions(acts); 1279 error: 1280 return error; 1281 } 1282 1283 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) 1284 { 1285 struct nlattr **a = info->attrs; 1286 struct ovs_header *ovs_header = info->userhdr; 1287 struct net *net = sock_net(skb->sk); 1288 struct sw_flow_key key; 1289 struct sk_buff *reply; 1290 struct sw_flow *flow; 1291 struct datapath *dp; 1292 struct sw_flow_match match; 1293 struct sw_flow_id ufid; 1294 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 1295 int err = 0; 1296 bool log = !a[OVS_FLOW_ATTR_PROBE]; 1297 bool ufid_present; 1298 1299 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); 1300 if (a[OVS_FLOW_ATTR_KEY]) { 1301 ovs_match_init(&match, &key, true, NULL); 1302 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL, 1303 log); 1304 } else if (!ufid_present) { 1305 OVS_NLERR(log, 1306 "Flow get message rejected, Key attribute missing."); 1307 err = -EINVAL; 1308 } 1309 if (err) 1310 return err; 1311 1312 ovs_lock(); 1313 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1314 if (!dp) { 1315 err = -ENODEV; 1316 goto unlock; 1317 } 1318 1319 if (ufid_present) 1320 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); 1321 else 1322 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); 1323 if (!flow) { 1324 err = -ENOENT; 1325 goto unlock; 1326 } 1327 1328 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info, 1329 OVS_FLOW_CMD_GET, true, ufid_flags); 1330 if (IS_ERR(reply)) { 1331 err = PTR_ERR(reply); 1332 goto unlock; 1333 } 1334 1335 ovs_unlock(); 1336 return genlmsg_reply(reply, info); 1337 unlock: 1338 ovs_unlock(); 1339 return err; 1340 } 1341 1342 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) 1343 { 1344 struct nlattr **a = info->attrs; 1345 struct ovs_header *ovs_header = info->userhdr; 1346 struct net *net = sock_net(skb->sk); 1347 struct sw_flow_key key; 1348 struct sk_buff *reply; 1349 struct sw_flow *flow = NULL; 1350 struct datapath *dp; 1351 struct sw_flow_match match; 1352 struct sw_flow_id ufid; 1353 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 1354 int err; 1355 bool log = !a[OVS_FLOW_ATTR_PROBE]; 1356 bool ufid_present; 1357 1358 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); 1359 if (a[OVS_FLOW_ATTR_KEY]) { 1360 ovs_match_init(&match, &key, true, NULL); 1361 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], 1362 NULL, log); 1363 if (unlikely(err)) 1364 return err; 1365 } 1366 1367 ovs_lock(); 1368 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1369 if (unlikely(!dp)) { 1370 err = -ENODEV; 1371 goto unlock; 1372 } 1373 1374 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) { 1375 err = ovs_flow_tbl_flush(&dp->table); 1376 goto unlock; 1377 } 1378 1379 if (ufid_present) 1380 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); 1381 else 1382 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); 1383 if (unlikely(!flow)) { 1384 err = -ENOENT; 1385 goto unlock; 1386 } 1387 1388 ovs_flow_tbl_remove(&dp->table, flow); 1389 ovs_unlock(); 1390 1391 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts, 1392 &flow->id, info, false, ufid_flags); 1393 if (likely(reply)) { 1394 if (!IS_ERR(reply)) { 1395 rcu_read_lock(); /*To keep RCU checker happy. */ 1396 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, 1397 reply, info->snd_portid, 1398 info->snd_seq, 0, 1399 OVS_FLOW_CMD_DEL, 1400 ufid_flags); 1401 rcu_read_unlock(); 1402 if (WARN_ON_ONCE(err < 0)) { 1403 kfree_skb(reply); 1404 goto out_free; 1405 } 1406 1407 ovs_notify(&dp_flow_genl_family, reply, info); 1408 } else { 1409 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, 1410 PTR_ERR(reply)); 1411 } 1412 } 1413 1414 out_free: 1415 ovs_flow_free(flow, true); 1416 return 0; 1417 unlock: 1418 ovs_unlock(); 1419 return err; 1420 } 1421 1422 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1423 { 1424 struct nlattr *a[__OVS_FLOW_ATTR_MAX]; 1425 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 1426 struct table_instance *ti; 1427 struct datapath *dp; 1428 u32 ufid_flags; 1429 int err; 1430 1431 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a, 1432 OVS_FLOW_ATTR_MAX, flow_policy, NULL); 1433 if (err) 1434 return err; 1435 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 1436 1437 rcu_read_lock(); 1438 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); 1439 if (!dp) { 1440 rcu_read_unlock(); 1441 return -ENODEV; 1442 } 1443 1444 ti = rcu_dereference(dp->table.ti); 1445 for (;;) { 1446 struct sw_flow *flow; 1447 u32 bucket, obj; 1448 1449 bucket = cb->args[0]; 1450 obj = cb->args[1]; 1451 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj); 1452 if (!flow) 1453 break; 1454 1455 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb, 1456 NETLINK_CB(cb->skb).portid, 1457 cb->nlh->nlmsg_seq, NLM_F_MULTI, 1458 OVS_FLOW_CMD_GET, ufid_flags) < 0) 1459 break; 1460 1461 cb->args[0] = bucket; 1462 cb->args[1] = obj; 1463 } 1464 rcu_read_unlock(); 1465 return skb->len; 1466 } 1467 1468 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { 1469 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, 1470 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED }, 1471 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, 1472 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, 1473 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG }, 1474 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 }, 1475 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 }, 1476 }; 1477 1478 static const struct genl_small_ops dp_flow_genl_ops[] = { 1479 { .cmd = OVS_FLOW_CMD_NEW, 1480 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1481 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1482 .doit = ovs_flow_cmd_new 1483 }, 1484 { .cmd = OVS_FLOW_CMD_DEL, 1485 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1486 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1487 .doit = ovs_flow_cmd_del 1488 }, 1489 { .cmd = OVS_FLOW_CMD_GET, 1490 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1491 .flags = 0, /* OK for unprivileged users. */ 1492 .doit = ovs_flow_cmd_get, 1493 .dumpit = ovs_flow_cmd_dump 1494 }, 1495 { .cmd = OVS_FLOW_CMD_SET, 1496 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1497 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1498 .doit = ovs_flow_cmd_set, 1499 }, 1500 }; 1501 1502 static struct genl_family dp_flow_genl_family __ro_after_init = { 1503 .hdrsize = sizeof(struct ovs_header), 1504 .name = OVS_FLOW_FAMILY, 1505 .version = OVS_FLOW_VERSION, 1506 .maxattr = OVS_FLOW_ATTR_MAX, 1507 .policy = flow_policy, 1508 .netnsok = true, 1509 .parallel_ops = true, 1510 .small_ops = dp_flow_genl_ops, 1511 .n_small_ops = ARRAY_SIZE(dp_flow_genl_ops), 1512 .mcgrps = &ovs_dp_flow_multicast_group, 1513 .n_mcgrps = 1, 1514 .module = THIS_MODULE, 1515 }; 1516 1517 static size_t ovs_dp_cmd_msg_size(void) 1518 { 1519 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); 1520 1521 msgsize += nla_total_size(IFNAMSIZ); 1522 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats)); 1523 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats)); 1524 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */ 1525 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_MASKS_CACHE_SIZE */ 1526 1527 return msgsize; 1528 } 1529 1530 /* Called with ovs_mutex. */ 1531 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, 1532 u32 portid, u32 seq, u32 flags, u8 cmd) 1533 { 1534 struct ovs_header *ovs_header; 1535 struct ovs_dp_stats dp_stats; 1536 struct ovs_dp_megaflow_stats dp_megaflow_stats; 1537 int err; 1538 1539 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, 1540 flags, cmd); 1541 if (!ovs_header) 1542 goto error; 1543 1544 ovs_header->dp_ifindex = get_dpifindex(dp); 1545 1546 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); 1547 if (err) 1548 goto nla_put_failure; 1549 1550 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats); 1551 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), 1552 &dp_stats, OVS_DP_ATTR_PAD)) 1553 goto nla_put_failure; 1554 1555 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS, 1556 sizeof(struct ovs_dp_megaflow_stats), 1557 &dp_megaflow_stats, OVS_DP_ATTR_PAD)) 1558 goto nla_put_failure; 1559 1560 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features)) 1561 goto nla_put_failure; 1562 1563 if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE, 1564 ovs_flow_tbl_masks_cache_size(&dp->table))) 1565 goto nla_put_failure; 1566 1567 genlmsg_end(skb, ovs_header); 1568 return 0; 1569 1570 nla_put_failure: 1571 genlmsg_cancel(skb, ovs_header); 1572 error: 1573 return -EMSGSIZE; 1574 } 1575 1576 static struct sk_buff *ovs_dp_cmd_alloc_info(void) 1577 { 1578 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL); 1579 } 1580 1581 /* Called with rcu_read_lock or ovs_mutex. */ 1582 static struct datapath *lookup_datapath(struct net *net, 1583 const struct ovs_header *ovs_header, 1584 struct nlattr *a[OVS_DP_ATTR_MAX + 1]) 1585 { 1586 struct datapath *dp; 1587 1588 if (!a[OVS_DP_ATTR_NAME]) 1589 dp = get_dp(net, ovs_header->dp_ifindex); 1590 else { 1591 struct vport *vport; 1592 1593 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); 1594 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; 1595 } 1596 return dp ? dp : ERR_PTR(-ENODEV); 1597 } 1598 1599 static void ovs_dp_reset_user_features(struct sk_buff *skb, 1600 struct genl_info *info) 1601 { 1602 struct datapath *dp; 1603 1604 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, 1605 info->attrs); 1606 if (IS_ERR(dp)) 1607 return; 1608 1609 WARN(dp->user_features, "Dropping previously announced user features\n"); 1610 dp->user_features = 0; 1611 } 1612 1613 static int ovs_dp_set_upcall_portids(struct datapath *dp, 1614 const struct nlattr *ids) 1615 { 1616 struct dp_nlsk_pids *old, *dp_nlsk_pids; 1617 1618 if (!nla_len(ids) || nla_len(ids) % sizeof(u32)) 1619 return -EINVAL; 1620 1621 old = ovsl_dereference(dp->upcall_portids); 1622 1623 dp_nlsk_pids = kmalloc(sizeof(*dp_nlsk_pids) + nla_len(ids), 1624 GFP_KERNEL); 1625 if (!dp_nlsk_pids) 1626 return -ENOMEM; 1627 1628 dp_nlsk_pids->n_pids = nla_len(ids) / sizeof(u32); 1629 nla_memcpy(dp_nlsk_pids->pids, ids, nla_len(ids)); 1630 1631 rcu_assign_pointer(dp->upcall_portids, dp_nlsk_pids); 1632 1633 kfree_rcu(old, rcu); 1634 1635 return 0; 1636 } 1637 1638 u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id) 1639 { 1640 struct dp_nlsk_pids *dp_nlsk_pids; 1641 1642 dp_nlsk_pids = rcu_dereference(dp->upcall_portids); 1643 1644 if (dp_nlsk_pids) { 1645 if (cpu_id < dp_nlsk_pids->n_pids) { 1646 return dp_nlsk_pids->pids[cpu_id]; 1647 } else if (dp_nlsk_pids->n_pids > 0 && 1648 cpu_id >= dp_nlsk_pids->n_pids) { 1649 /* If the number of netlink PIDs is mismatched with 1650 * the number of CPUs as seen by the kernel, log this 1651 * and send the upcall to an arbitrary socket (0) in 1652 * order to not drop packets 1653 */ 1654 pr_info_ratelimited("cpu_id mismatch with handler threads"); 1655 return dp_nlsk_pids->pids[cpu_id % 1656 dp_nlsk_pids->n_pids]; 1657 } else { 1658 return 0; 1659 } 1660 } else { 1661 return 0; 1662 } 1663 } 1664 1665 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[]) 1666 { 1667 u32 user_features = 0, old_features = dp->user_features; 1668 int err; 1669 1670 if (a[OVS_DP_ATTR_USER_FEATURES]) { 1671 user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]); 1672 1673 if (user_features & ~(OVS_DP_F_VPORT_PIDS | 1674 OVS_DP_F_UNALIGNED | 1675 OVS_DP_F_TC_RECIRC_SHARING | 1676 OVS_DP_F_DISPATCH_UPCALL_PER_CPU)) 1677 return -EOPNOTSUPP; 1678 1679 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 1680 if (user_features & OVS_DP_F_TC_RECIRC_SHARING) 1681 return -EOPNOTSUPP; 1682 #endif 1683 } 1684 1685 if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) { 1686 int err; 1687 u32 cache_size; 1688 1689 cache_size = nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]); 1690 err = ovs_flow_tbl_masks_cache_resize(&dp->table, cache_size); 1691 if (err) 1692 return err; 1693 } 1694 1695 dp->user_features = user_features; 1696 1697 if (dp->user_features & OVS_DP_F_DISPATCH_UPCALL_PER_CPU && 1698 a[OVS_DP_ATTR_PER_CPU_PIDS]) { 1699 /* Upcall Netlink Port IDs have been updated */ 1700 err = ovs_dp_set_upcall_portids(dp, 1701 a[OVS_DP_ATTR_PER_CPU_PIDS]); 1702 if (err) 1703 return err; 1704 } 1705 1706 if ((dp->user_features & OVS_DP_F_TC_RECIRC_SHARING) && 1707 !(old_features & OVS_DP_F_TC_RECIRC_SHARING)) 1708 tc_skb_ext_tc_enable(); 1709 else if (!(dp->user_features & OVS_DP_F_TC_RECIRC_SHARING) && 1710 (old_features & OVS_DP_F_TC_RECIRC_SHARING)) 1711 tc_skb_ext_tc_disable(); 1712 1713 return 0; 1714 } 1715 1716 static int ovs_dp_stats_init(struct datapath *dp) 1717 { 1718 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu); 1719 if (!dp->stats_percpu) 1720 return -ENOMEM; 1721 1722 return 0; 1723 } 1724 1725 static int ovs_dp_vport_init(struct datapath *dp) 1726 { 1727 int i; 1728 1729 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS, 1730 sizeof(struct hlist_head), 1731 GFP_KERNEL); 1732 if (!dp->ports) 1733 return -ENOMEM; 1734 1735 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) 1736 INIT_HLIST_HEAD(&dp->ports[i]); 1737 1738 return 0; 1739 } 1740 1741 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) 1742 { 1743 struct nlattr **a = info->attrs; 1744 struct vport_parms parms; 1745 struct sk_buff *reply; 1746 struct datapath *dp; 1747 struct vport *vport; 1748 struct ovs_net *ovs_net; 1749 int err; 1750 1751 err = -EINVAL; 1752 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) 1753 goto err; 1754 1755 reply = ovs_dp_cmd_alloc_info(); 1756 if (!reply) 1757 return -ENOMEM; 1758 1759 err = -ENOMEM; 1760 dp = kzalloc(sizeof(*dp), GFP_KERNEL); 1761 if (dp == NULL) 1762 goto err_destroy_reply; 1763 1764 ovs_dp_set_net(dp, sock_net(skb->sk)); 1765 1766 /* Allocate table. */ 1767 err = ovs_flow_tbl_init(&dp->table); 1768 if (err) 1769 goto err_destroy_dp; 1770 1771 err = ovs_dp_stats_init(dp); 1772 if (err) 1773 goto err_destroy_table; 1774 1775 err = ovs_dp_vport_init(dp); 1776 if (err) 1777 goto err_destroy_stats; 1778 1779 err = ovs_meters_init(dp); 1780 if (err) 1781 goto err_destroy_ports; 1782 1783 /* Set up our datapath device. */ 1784 parms.name = nla_data(a[OVS_DP_ATTR_NAME]); 1785 parms.type = OVS_VPORT_TYPE_INTERNAL; 1786 parms.options = NULL; 1787 parms.dp = dp; 1788 parms.port_no = OVSP_LOCAL; 1789 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID]; 1790 1791 /* So far only local changes have been made, now need the lock. */ 1792 ovs_lock(); 1793 1794 err = ovs_dp_change(dp, a); 1795 if (err) 1796 goto err_unlock_and_destroy_meters; 1797 1798 vport = new_vport(&parms); 1799 if (IS_ERR(vport)) { 1800 err = PTR_ERR(vport); 1801 if (err == -EBUSY) 1802 err = -EEXIST; 1803 1804 if (err == -EEXIST) { 1805 /* An outdated user space instance that does not understand 1806 * the concept of user_features has attempted to create a new 1807 * datapath and is likely to reuse it. Drop all user features. 1808 */ 1809 if (info->genlhdr->version < OVS_DP_VER_FEATURES) 1810 ovs_dp_reset_user_features(skb, info); 1811 } 1812 1813 goto err_unlock_and_destroy_meters; 1814 } 1815 1816 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, 1817 info->snd_seq, 0, OVS_DP_CMD_NEW); 1818 BUG_ON(err < 0); 1819 1820 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); 1821 list_add_tail_rcu(&dp->list_node, &ovs_net->dps); 1822 1823 ovs_unlock(); 1824 1825 ovs_notify(&dp_datapath_genl_family, reply, info); 1826 return 0; 1827 1828 err_unlock_and_destroy_meters: 1829 ovs_unlock(); 1830 ovs_meters_exit(dp); 1831 err_destroy_ports: 1832 kfree(dp->ports); 1833 err_destroy_stats: 1834 free_percpu(dp->stats_percpu); 1835 err_destroy_table: 1836 ovs_flow_tbl_destroy(&dp->table); 1837 err_destroy_dp: 1838 kfree(dp); 1839 err_destroy_reply: 1840 kfree_skb(reply); 1841 err: 1842 return err; 1843 } 1844 1845 /* Called with ovs_mutex. */ 1846 static void __dp_destroy(struct datapath *dp) 1847 { 1848 struct flow_table *table = &dp->table; 1849 int i; 1850 1851 if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING) 1852 tc_skb_ext_tc_disable(); 1853 1854 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 1855 struct vport *vport; 1856 struct hlist_node *n; 1857 1858 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) 1859 if (vport->port_no != OVSP_LOCAL) 1860 ovs_dp_detach_port(vport); 1861 } 1862 1863 list_del_rcu(&dp->list_node); 1864 1865 /* OVSP_LOCAL is datapath internal port. We need to make sure that 1866 * all ports in datapath are destroyed first before freeing datapath. 1867 */ 1868 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); 1869 1870 /* Flush sw_flow in the tables. RCU cb only releases resource 1871 * such as dp, ports and tables. That may avoid some issues 1872 * such as RCU usage warning. 1873 */ 1874 table_instance_flow_flush(table, ovsl_dereference(table->ti), 1875 ovsl_dereference(table->ufid_ti)); 1876 1877 /* RCU destroy the ports, meters and flow tables. */ 1878 call_rcu(&dp->rcu, destroy_dp_rcu); 1879 } 1880 1881 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) 1882 { 1883 struct sk_buff *reply; 1884 struct datapath *dp; 1885 int err; 1886 1887 reply = ovs_dp_cmd_alloc_info(); 1888 if (!reply) 1889 return -ENOMEM; 1890 1891 ovs_lock(); 1892 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1893 err = PTR_ERR(dp); 1894 if (IS_ERR(dp)) 1895 goto err_unlock_free; 1896 1897 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, 1898 info->snd_seq, 0, OVS_DP_CMD_DEL); 1899 BUG_ON(err < 0); 1900 1901 __dp_destroy(dp); 1902 ovs_unlock(); 1903 1904 ovs_notify(&dp_datapath_genl_family, reply, info); 1905 1906 return 0; 1907 1908 err_unlock_free: 1909 ovs_unlock(); 1910 kfree_skb(reply); 1911 return err; 1912 } 1913 1914 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) 1915 { 1916 struct sk_buff *reply; 1917 struct datapath *dp; 1918 int err; 1919 1920 reply = ovs_dp_cmd_alloc_info(); 1921 if (!reply) 1922 return -ENOMEM; 1923 1924 ovs_lock(); 1925 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1926 err = PTR_ERR(dp); 1927 if (IS_ERR(dp)) 1928 goto err_unlock_free; 1929 1930 err = ovs_dp_change(dp, info->attrs); 1931 if (err) 1932 goto err_unlock_free; 1933 1934 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, 1935 info->snd_seq, 0, OVS_DP_CMD_SET); 1936 BUG_ON(err < 0); 1937 1938 ovs_unlock(); 1939 ovs_notify(&dp_datapath_genl_family, reply, info); 1940 1941 return 0; 1942 1943 err_unlock_free: 1944 ovs_unlock(); 1945 kfree_skb(reply); 1946 return err; 1947 } 1948 1949 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) 1950 { 1951 struct sk_buff *reply; 1952 struct datapath *dp; 1953 int err; 1954 1955 reply = ovs_dp_cmd_alloc_info(); 1956 if (!reply) 1957 return -ENOMEM; 1958 1959 ovs_lock(); 1960 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1961 if (IS_ERR(dp)) { 1962 err = PTR_ERR(dp); 1963 goto err_unlock_free; 1964 } 1965 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, 1966 info->snd_seq, 0, OVS_DP_CMD_GET); 1967 BUG_ON(err < 0); 1968 ovs_unlock(); 1969 1970 return genlmsg_reply(reply, info); 1971 1972 err_unlock_free: 1973 ovs_unlock(); 1974 kfree_skb(reply); 1975 return err; 1976 } 1977 1978 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1979 { 1980 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); 1981 struct datapath *dp; 1982 int skip = cb->args[0]; 1983 int i = 0; 1984 1985 ovs_lock(); 1986 list_for_each_entry(dp, &ovs_net->dps, list_node) { 1987 if (i >= skip && 1988 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, 1989 cb->nlh->nlmsg_seq, NLM_F_MULTI, 1990 OVS_DP_CMD_GET) < 0) 1991 break; 1992 i++; 1993 } 1994 ovs_unlock(); 1995 1996 cb->args[0] = i; 1997 1998 return skb->len; 1999 } 2000 2001 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { 2002 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, 2003 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, 2004 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 }, 2005 [OVS_DP_ATTR_MASKS_CACHE_SIZE] = NLA_POLICY_RANGE(NLA_U32, 0, 2006 PCPU_MIN_UNIT_SIZE / sizeof(struct mask_cache_entry)), 2007 }; 2008 2009 static const struct genl_small_ops dp_datapath_genl_ops[] = { 2010 { .cmd = OVS_DP_CMD_NEW, 2011 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2012 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2013 .doit = ovs_dp_cmd_new 2014 }, 2015 { .cmd = OVS_DP_CMD_DEL, 2016 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2017 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2018 .doit = ovs_dp_cmd_del 2019 }, 2020 { .cmd = OVS_DP_CMD_GET, 2021 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2022 .flags = 0, /* OK for unprivileged users. */ 2023 .doit = ovs_dp_cmd_get, 2024 .dumpit = ovs_dp_cmd_dump 2025 }, 2026 { .cmd = OVS_DP_CMD_SET, 2027 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2028 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2029 .doit = ovs_dp_cmd_set, 2030 }, 2031 }; 2032 2033 static struct genl_family dp_datapath_genl_family __ro_after_init = { 2034 .hdrsize = sizeof(struct ovs_header), 2035 .name = OVS_DATAPATH_FAMILY, 2036 .version = OVS_DATAPATH_VERSION, 2037 .maxattr = OVS_DP_ATTR_MAX, 2038 .policy = datapath_policy, 2039 .netnsok = true, 2040 .parallel_ops = true, 2041 .small_ops = dp_datapath_genl_ops, 2042 .n_small_ops = ARRAY_SIZE(dp_datapath_genl_ops), 2043 .mcgrps = &ovs_dp_datapath_multicast_group, 2044 .n_mcgrps = 1, 2045 .module = THIS_MODULE, 2046 }; 2047 2048 /* Called with ovs_mutex or RCU read lock. */ 2049 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, 2050 struct net *net, u32 portid, u32 seq, 2051 u32 flags, u8 cmd, gfp_t gfp) 2052 { 2053 struct ovs_header *ovs_header; 2054 struct ovs_vport_stats vport_stats; 2055 int err; 2056 2057 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, 2058 flags, cmd); 2059 if (!ovs_header) 2060 return -EMSGSIZE; 2061 2062 ovs_header->dp_ifindex = get_dpifindex(vport->dp); 2063 2064 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || 2065 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || 2066 nla_put_string(skb, OVS_VPORT_ATTR_NAME, 2067 ovs_vport_name(vport)) || 2068 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex)) 2069 goto nla_put_failure; 2070 2071 if (!net_eq(net, dev_net(vport->dev))) { 2072 int id = peernet2id_alloc(net, dev_net(vport->dev), gfp); 2073 2074 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id)) 2075 goto nla_put_failure; 2076 } 2077 2078 ovs_vport_get_stats(vport, &vport_stats); 2079 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS, 2080 sizeof(struct ovs_vport_stats), &vport_stats, 2081 OVS_VPORT_ATTR_PAD)) 2082 goto nla_put_failure; 2083 2084 if (ovs_vport_get_upcall_portids(vport, skb)) 2085 goto nla_put_failure; 2086 2087 err = ovs_vport_get_options(vport, skb); 2088 if (err == -EMSGSIZE) 2089 goto error; 2090 2091 genlmsg_end(skb, ovs_header); 2092 return 0; 2093 2094 nla_put_failure: 2095 err = -EMSGSIZE; 2096 error: 2097 genlmsg_cancel(skb, ovs_header); 2098 return err; 2099 } 2100 2101 static struct sk_buff *ovs_vport_cmd_alloc_info(void) 2102 { 2103 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2104 } 2105 2106 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */ 2107 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net, 2108 u32 portid, u32 seq, u8 cmd) 2109 { 2110 struct sk_buff *skb; 2111 int retval; 2112 2113 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 2114 if (!skb) 2115 return ERR_PTR(-ENOMEM); 2116 2117 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd, 2118 GFP_KERNEL); 2119 BUG_ON(retval < 0); 2120 2121 return skb; 2122 } 2123 2124 /* Called with ovs_mutex or RCU read lock. */ 2125 static struct vport *lookup_vport(struct net *net, 2126 const struct ovs_header *ovs_header, 2127 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) 2128 { 2129 struct datapath *dp; 2130 struct vport *vport; 2131 2132 if (a[OVS_VPORT_ATTR_IFINDEX]) 2133 return ERR_PTR(-EOPNOTSUPP); 2134 if (a[OVS_VPORT_ATTR_NAME]) { 2135 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); 2136 if (!vport) 2137 return ERR_PTR(-ENODEV); 2138 if (ovs_header->dp_ifindex && 2139 ovs_header->dp_ifindex != get_dpifindex(vport->dp)) 2140 return ERR_PTR(-ENODEV); 2141 return vport; 2142 } else if (a[OVS_VPORT_ATTR_PORT_NO]) { 2143 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); 2144 2145 if (port_no >= DP_MAX_PORTS) 2146 return ERR_PTR(-EFBIG); 2147 2148 dp = get_dp(net, ovs_header->dp_ifindex); 2149 if (!dp) 2150 return ERR_PTR(-ENODEV); 2151 2152 vport = ovs_vport_ovsl_rcu(dp, port_no); 2153 if (!vport) 2154 return ERR_PTR(-ENODEV); 2155 return vport; 2156 } else 2157 return ERR_PTR(-EINVAL); 2158 2159 } 2160 2161 static unsigned int ovs_get_max_headroom(struct datapath *dp) 2162 { 2163 unsigned int dev_headroom, max_headroom = 0; 2164 struct net_device *dev; 2165 struct vport *vport; 2166 int i; 2167 2168 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 2169 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node, 2170 lockdep_ovsl_is_held()) { 2171 dev = vport->dev; 2172 dev_headroom = netdev_get_fwd_headroom(dev); 2173 if (dev_headroom > max_headroom) 2174 max_headroom = dev_headroom; 2175 } 2176 } 2177 2178 return max_headroom; 2179 } 2180 2181 /* Called with ovs_mutex */ 2182 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom) 2183 { 2184 struct vport *vport; 2185 int i; 2186 2187 dp->max_headroom = new_headroom; 2188 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 2189 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node, 2190 lockdep_ovsl_is_held()) 2191 netdev_set_rx_headroom(vport->dev, new_headroom); 2192 } 2193 } 2194 2195 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) 2196 { 2197 struct nlattr **a = info->attrs; 2198 struct ovs_header *ovs_header = info->userhdr; 2199 struct vport_parms parms; 2200 struct sk_buff *reply; 2201 struct vport *vport; 2202 struct datapath *dp; 2203 unsigned int new_headroom; 2204 u32 port_no; 2205 int err; 2206 2207 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || 2208 !a[OVS_VPORT_ATTR_UPCALL_PID]) 2209 return -EINVAL; 2210 if (a[OVS_VPORT_ATTR_IFINDEX]) 2211 return -EOPNOTSUPP; 2212 2213 port_no = a[OVS_VPORT_ATTR_PORT_NO] 2214 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0; 2215 if (port_no >= DP_MAX_PORTS) 2216 return -EFBIG; 2217 2218 reply = ovs_vport_cmd_alloc_info(); 2219 if (!reply) 2220 return -ENOMEM; 2221 2222 ovs_lock(); 2223 restart: 2224 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 2225 err = -ENODEV; 2226 if (!dp) 2227 goto exit_unlock_free; 2228 2229 if (port_no) { 2230 vport = ovs_vport_ovsl(dp, port_no); 2231 err = -EBUSY; 2232 if (vport) 2233 goto exit_unlock_free; 2234 } else { 2235 for (port_no = 1; ; port_no++) { 2236 if (port_no >= DP_MAX_PORTS) { 2237 err = -EFBIG; 2238 goto exit_unlock_free; 2239 } 2240 vport = ovs_vport_ovsl(dp, port_no); 2241 if (!vport) 2242 break; 2243 } 2244 } 2245 2246 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); 2247 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); 2248 parms.options = a[OVS_VPORT_ATTR_OPTIONS]; 2249 parms.dp = dp; 2250 parms.port_no = port_no; 2251 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID]; 2252 2253 vport = new_vport(&parms); 2254 err = PTR_ERR(vport); 2255 if (IS_ERR(vport)) { 2256 if (err == -EAGAIN) 2257 goto restart; 2258 goto exit_unlock_free; 2259 } 2260 2261 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), 2262 info->snd_portid, info->snd_seq, 0, 2263 OVS_VPORT_CMD_NEW, GFP_KERNEL); 2264 2265 new_headroom = netdev_get_fwd_headroom(vport->dev); 2266 2267 if (new_headroom > dp->max_headroom) 2268 ovs_update_headroom(dp, new_headroom); 2269 else 2270 netdev_set_rx_headroom(vport->dev, dp->max_headroom); 2271 2272 BUG_ON(err < 0); 2273 ovs_unlock(); 2274 2275 ovs_notify(&dp_vport_genl_family, reply, info); 2276 return 0; 2277 2278 exit_unlock_free: 2279 ovs_unlock(); 2280 kfree_skb(reply); 2281 return err; 2282 } 2283 2284 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) 2285 { 2286 struct nlattr **a = info->attrs; 2287 struct sk_buff *reply; 2288 struct vport *vport; 2289 int err; 2290 2291 reply = ovs_vport_cmd_alloc_info(); 2292 if (!reply) 2293 return -ENOMEM; 2294 2295 ovs_lock(); 2296 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); 2297 err = PTR_ERR(vport); 2298 if (IS_ERR(vport)) 2299 goto exit_unlock_free; 2300 2301 if (a[OVS_VPORT_ATTR_TYPE] && 2302 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { 2303 err = -EINVAL; 2304 goto exit_unlock_free; 2305 } 2306 2307 if (a[OVS_VPORT_ATTR_OPTIONS]) { 2308 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); 2309 if (err) 2310 goto exit_unlock_free; 2311 } 2312 2313 2314 if (a[OVS_VPORT_ATTR_UPCALL_PID]) { 2315 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID]; 2316 2317 err = ovs_vport_set_upcall_portids(vport, ids); 2318 if (err) 2319 goto exit_unlock_free; 2320 } 2321 2322 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), 2323 info->snd_portid, info->snd_seq, 0, 2324 OVS_VPORT_CMD_SET, GFP_KERNEL); 2325 BUG_ON(err < 0); 2326 2327 ovs_unlock(); 2328 ovs_notify(&dp_vport_genl_family, reply, info); 2329 return 0; 2330 2331 exit_unlock_free: 2332 ovs_unlock(); 2333 kfree_skb(reply); 2334 return err; 2335 } 2336 2337 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) 2338 { 2339 bool update_headroom = false; 2340 struct nlattr **a = info->attrs; 2341 struct sk_buff *reply; 2342 struct datapath *dp; 2343 struct vport *vport; 2344 unsigned int new_headroom; 2345 int err; 2346 2347 reply = ovs_vport_cmd_alloc_info(); 2348 if (!reply) 2349 return -ENOMEM; 2350 2351 ovs_lock(); 2352 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); 2353 err = PTR_ERR(vport); 2354 if (IS_ERR(vport)) 2355 goto exit_unlock_free; 2356 2357 if (vport->port_no == OVSP_LOCAL) { 2358 err = -EINVAL; 2359 goto exit_unlock_free; 2360 } 2361 2362 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), 2363 info->snd_portid, info->snd_seq, 0, 2364 OVS_VPORT_CMD_DEL, GFP_KERNEL); 2365 BUG_ON(err < 0); 2366 2367 /* the vport deletion may trigger dp headroom update */ 2368 dp = vport->dp; 2369 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom) 2370 update_headroom = true; 2371 2372 netdev_reset_rx_headroom(vport->dev); 2373 ovs_dp_detach_port(vport); 2374 2375 if (update_headroom) { 2376 new_headroom = ovs_get_max_headroom(dp); 2377 2378 if (new_headroom < dp->max_headroom) 2379 ovs_update_headroom(dp, new_headroom); 2380 } 2381 ovs_unlock(); 2382 2383 ovs_notify(&dp_vport_genl_family, reply, info); 2384 return 0; 2385 2386 exit_unlock_free: 2387 ovs_unlock(); 2388 kfree_skb(reply); 2389 return err; 2390 } 2391 2392 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) 2393 { 2394 struct nlattr **a = info->attrs; 2395 struct ovs_header *ovs_header = info->userhdr; 2396 struct sk_buff *reply; 2397 struct vport *vport; 2398 int err; 2399 2400 reply = ovs_vport_cmd_alloc_info(); 2401 if (!reply) 2402 return -ENOMEM; 2403 2404 rcu_read_lock(); 2405 vport = lookup_vport(sock_net(skb->sk), ovs_header, a); 2406 err = PTR_ERR(vport); 2407 if (IS_ERR(vport)) 2408 goto exit_unlock_free; 2409 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), 2410 info->snd_portid, info->snd_seq, 0, 2411 OVS_VPORT_CMD_GET, GFP_ATOMIC); 2412 BUG_ON(err < 0); 2413 rcu_read_unlock(); 2414 2415 return genlmsg_reply(reply, info); 2416 2417 exit_unlock_free: 2418 rcu_read_unlock(); 2419 kfree_skb(reply); 2420 return err; 2421 } 2422 2423 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 2424 { 2425 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 2426 struct datapath *dp; 2427 int bucket = cb->args[0], skip = cb->args[1]; 2428 int i, j = 0; 2429 2430 rcu_read_lock(); 2431 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); 2432 if (!dp) { 2433 rcu_read_unlock(); 2434 return -ENODEV; 2435 } 2436 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { 2437 struct vport *vport; 2438 2439 j = 0; 2440 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { 2441 if (j >= skip && 2442 ovs_vport_cmd_fill_info(vport, skb, 2443 sock_net(skb->sk), 2444 NETLINK_CB(cb->skb).portid, 2445 cb->nlh->nlmsg_seq, 2446 NLM_F_MULTI, 2447 OVS_VPORT_CMD_GET, 2448 GFP_ATOMIC) < 0) 2449 goto out; 2450 2451 j++; 2452 } 2453 skip = 0; 2454 } 2455 out: 2456 rcu_read_unlock(); 2457 2458 cb->args[0] = i; 2459 cb->args[1] = j; 2460 2461 return skb->len; 2462 } 2463 2464 static void ovs_dp_masks_rebalance(struct work_struct *work) 2465 { 2466 struct ovs_net *ovs_net = container_of(work, struct ovs_net, 2467 masks_rebalance.work); 2468 struct datapath *dp; 2469 2470 ovs_lock(); 2471 2472 list_for_each_entry(dp, &ovs_net->dps, list_node) 2473 ovs_flow_masks_rebalance(&dp->table); 2474 2475 ovs_unlock(); 2476 2477 schedule_delayed_work(&ovs_net->masks_rebalance, 2478 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL)); 2479 } 2480 2481 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { 2482 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, 2483 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, 2484 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, 2485 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, 2486 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC }, 2487 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, 2488 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 }, 2489 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 }, 2490 }; 2491 2492 static const struct genl_small_ops dp_vport_genl_ops[] = { 2493 { .cmd = OVS_VPORT_CMD_NEW, 2494 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2495 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2496 .doit = ovs_vport_cmd_new 2497 }, 2498 { .cmd = OVS_VPORT_CMD_DEL, 2499 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2500 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2501 .doit = ovs_vport_cmd_del 2502 }, 2503 { .cmd = OVS_VPORT_CMD_GET, 2504 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2505 .flags = 0, /* OK for unprivileged users. */ 2506 .doit = ovs_vport_cmd_get, 2507 .dumpit = ovs_vport_cmd_dump 2508 }, 2509 { .cmd = OVS_VPORT_CMD_SET, 2510 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2511 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2512 .doit = ovs_vport_cmd_set, 2513 }, 2514 }; 2515 2516 struct genl_family dp_vport_genl_family __ro_after_init = { 2517 .hdrsize = sizeof(struct ovs_header), 2518 .name = OVS_VPORT_FAMILY, 2519 .version = OVS_VPORT_VERSION, 2520 .maxattr = OVS_VPORT_ATTR_MAX, 2521 .policy = vport_policy, 2522 .netnsok = true, 2523 .parallel_ops = true, 2524 .small_ops = dp_vport_genl_ops, 2525 .n_small_ops = ARRAY_SIZE(dp_vport_genl_ops), 2526 .mcgrps = &ovs_dp_vport_multicast_group, 2527 .n_mcgrps = 1, 2528 .module = THIS_MODULE, 2529 }; 2530 2531 static struct genl_family * const dp_genl_families[] = { 2532 &dp_datapath_genl_family, 2533 &dp_vport_genl_family, 2534 &dp_flow_genl_family, 2535 &dp_packet_genl_family, 2536 &dp_meter_genl_family, 2537 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 2538 &dp_ct_limit_genl_family, 2539 #endif 2540 }; 2541 2542 static void dp_unregister_genl(int n_families) 2543 { 2544 int i; 2545 2546 for (i = 0; i < n_families; i++) 2547 genl_unregister_family(dp_genl_families[i]); 2548 } 2549 2550 static int __init dp_register_genl(void) 2551 { 2552 int err; 2553 int i; 2554 2555 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { 2556 2557 err = genl_register_family(dp_genl_families[i]); 2558 if (err) 2559 goto error; 2560 } 2561 2562 return 0; 2563 2564 error: 2565 dp_unregister_genl(i); 2566 return err; 2567 } 2568 2569 static int __net_init ovs_init_net(struct net *net) 2570 { 2571 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 2572 int err; 2573 2574 INIT_LIST_HEAD(&ovs_net->dps); 2575 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); 2576 INIT_DELAYED_WORK(&ovs_net->masks_rebalance, ovs_dp_masks_rebalance); 2577 2578 err = ovs_ct_init(net); 2579 if (err) 2580 return err; 2581 2582 schedule_delayed_work(&ovs_net->masks_rebalance, 2583 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL)); 2584 return 0; 2585 } 2586 2587 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet, 2588 struct list_head *head) 2589 { 2590 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 2591 struct datapath *dp; 2592 2593 list_for_each_entry(dp, &ovs_net->dps, list_node) { 2594 int i; 2595 2596 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 2597 struct vport *vport; 2598 2599 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) { 2600 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL) 2601 continue; 2602 2603 if (dev_net(vport->dev) == dnet) 2604 list_add(&vport->detach_list, head); 2605 } 2606 } 2607 } 2608 } 2609 2610 static void __net_exit ovs_exit_net(struct net *dnet) 2611 { 2612 struct datapath *dp, *dp_next; 2613 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id); 2614 struct vport *vport, *vport_next; 2615 struct net *net; 2616 LIST_HEAD(head); 2617 2618 ovs_lock(); 2619 2620 ovs_ct_exit(dnet); 2621 2622 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) 2623 __dp_destroy(dp); 2624 2625 down_read(&net_rwsem); 2626 for_each_net(net) 2627 list_vports_from_net(net, dnet, &head); 2628 up_read(&net_rwsem); 2629 2630 /* Detach all vports from given namespace. */ 2631 list_for_each_entry_safe(vport, vport_next, &head, detach_list) { 2632 list_del(&vport->detach_list); 2633 ovs_dp_detach_port(vport); 2634 } 2635 2636 ovs_unlock(); 2637 2638 cancel_delayed_work_sync(&ovs_net->masks_rebalance); 2639 cancel_work_sync(&ovs_net->dp_notify_work); 2640 } 2641 2642 static struct pernet_operations ovs_net_ops = { 2643 .init = ovs_init_net, 2644 .exit = ovs_exit_net, 2645 .id = &ovs_net_id, 2646 .size = sizeof(struct ovs_net), 2647 }; 2648 2649 static int __init dp_init(void) 2650 { 2651 int err; 2652 2653 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > 2654 sizeof_field(struct sk_buff, cb)); 2655 2656 pr_info("Open vSwitch switching datapath\n"); 2657 2658 err = action_fifos_init(); 2659 if (err) 2660 goto error; 2661 2662 err = ovs_internal_dev_rtnl_link_register(); 2663 if (err) 2664 goto error_action_fifos_exit; 2665 2666 err = ovs_flow_init(); 2667 if (err) 2668 goto error_unreg_rtnl_link; 2669 2670 err = ovs_vport_init(); 2671 if (err) 2672 goto error_flow_exit; 2673 2674 err = register_pernet_device(&ovs_net_ops); 2675 if (err) 2676 goto error_vport_exit; 2677 2678 err = register_netdevice_notifier(&ovs_dp_device_notifier); 2679 if (err) 2680 goto error_netns_exit; 2681 2682 err = ovs_netdev_init(); 2683 if (err) 2684 goto error_unreg_notifier; 2685 2686 err = dp_register_genl(); 2687 if (err < 0) 2688 goto error_unreg_netdev; 2689 2690 return 0; 2691 2692 error_unreg_netdev: 2693 ovs_netdev_exit(); 2694 error_unreg_notifier: 2695 unregister_netdevice_notifier(&ovs_dp_device_notifier); 2696 error_netns_exit: 2697 unregister_pernet_device(&ovs_net_ops); 2698 error_vport_exit: 2699 ovs_vport_exit(); 2700 error_flow_exit: 2701 ovs_flow_exit(); 2702 error_unreg_rtnl_link: 2703 ovs_internal_dev_rtnl_link_unregister(); 2704 error_action_fifos_exit: 2705 action_fifos_exit(); 2706 error: 2707 return err; 2708 } 2709 2710 static void dp_cleanup(void) 2711 { 2712 dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); 2713 ovs_netdev_exit(); 2714 unregister_netdevice_notifier(&ovs_dp_device_notifier); 2715 unregister_pernet_device(&ovs_net_ops); 2716 rcu_barrier(); 2717 ovs_vport_exit(); 2718 ovs_flow_exit(); 2719 ovs_internal_dev_rtnl_link_unregister(); 2720 action_fifos_exit(); 2721 } 2722 2723 module_init(dp_init); 2724 module_exit(dp_cleanup); 2725 2726 MODULE_DESCRIPTION("Open vSwitch switching datapath"); 2727 MODULE_LICENSE("GPL"); 2728 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY); 2729 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY); 2730 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY); 2731 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY); 2732 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY); 2733 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY); 2734