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