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