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 /* The flow identifier has to be the same for flow updates. 1117 * Look for any overlapping flow. 1118 */ 1119 if (unlikely(!ovs_flow_cmp(flow, &match))) { 1120 if (ovs_identifier_is_key(&flow->id)) 1121 flow = ovs_flow_tbl_lookup_exact(&dp->table, 1122 &match); 1123 else /* UFID matches but key is different */ 1124 flow = NULL; 1125 if (!flow) { 1126 error = -ENOENT; 1127 goto err_unlock_ovs; 1128 } 1129 } 1130 /* Update actions. */ 1131 old_acts = ovsl_dereference(flow->sf_acts); 1132 rcu_assign_pointer(flow->sf_acts, acts); 1133 1134 if (unlikely(reply)) { 1135 error = ovs_flow_cmd_fill_info(flow, 1136 ovs_header->dp_ifindex, 1137 reply, info->snd_portid, 1138 info->snd_seq, 0, 1139 OVS_FLOW_CMD_NEW, 1140 ufid_flags); 1141 BUG_ON(error < 0); 1142 } 1143 ovs_unlock(); 1144 1145 ovs_nla_free_flow_actions_rcu(old_acts); 1146 ovs_flow_free(new_flow, false); 1147 } 1148 1149 if (reply) 1150 ovs_notify(&dp_flow_genl_family, reply, info); 1151 1152 kfree(key); 1153 return 0; 1154 1155 err_unlock_ovs: 1156 ovs_unlock(); 1157 kfree_skb(reply); 1158 err_kfree_acts: 1159 ovs_nla_free_flow_actions(acts); 1160 err_kfree_key: 1161 kfree(key); 1162 err_kfree_flow: 1163 ovs_flow_free(new_flow, false); 1164 error: 1165 return error; 1166 } 1167 1168 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */ 1169 static noinline_for_stack 1170 struct sw_flow_actions *get_flow_actions(struct net *net, 1171 const struct nlattr *a, 1172 const struct sw_flow_key *key, 1173 const struct sw_flow_mask *mask, 1174 bool log) 1175 { 1176 struct sw_flow_actions *acts; 1177 struct sw_flow_key masked_key; 1178 int error; 1179 1180 ovs_flow_mask_key(&masked_key, key, true, mask); 1181 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log); 1182 if (error) { 1183 OVS_NLERR(log, 1184 "Actions may not be safe on all matching packets"); 1185 return ERR_PTR(error); 1186 } 1187 1188 return acts; 1189 } 1190 1191 /* Factor out match-init and action-copy to avoid 1192 * "Wframe-larger-than=1024" warning. Because mask is only 1193 * used to get actions, we new a function to save some 1194 * stack space. 1195 * 1196 * If there are not key and action attrs, we return 0 1197 * directly. In the case, the caller will also not use the 1198 * match as before. If there is action attr, we try to get 1199 * actions and save them to *acts. Before returning from 1200 * the function, we reset the match->mask pointer. Because 1201 * we should not to return match object with dangling reference 1202 * to mask. 1203 * */ 1204 static noinline_for_stack int 1205 ovs_nla_init_match_and_action(struct net *net, 1206 struct sw_flow_match *match, 1207 struct sw_flow_key *key, 1208 struct nlattr **a, 1209 struct sw_flow_actions **acts, 1210 bool log) 1211 { 1212 struct sw_flow_mask mask; 1213 int error = 0; 1214 1215 if (a[OVS_FLOW_ATTR_KEY]) { 1216 ovs_match_init(match, key, true, &mask); 1217 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY], 1218 a[OVS_FLOW_ATTR_MASK], log); 1219 if (error) 1220 goto error; 1221 } 1222 1223 if (a[OVS_FLOW_ATTR_ACTIONS]) { 1224 if (!a[OVS_FLOW_ATTR_KEY]) { 1225 OVS_NLERR(log, 1226 "Flow key attribute not present in set flow."); 1227 error = -EINVAL; 1228 goto error; 1229 } 1230 1231 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key, 1232 &mask, log); 1233 if (IS_ERR(*acts)) { 1234 error = PTR_ERR(*acts); 1235 goto error; 1236 } 1237 } 1238 1239 /* On success, error is 0. */ 1240 error: 1241 match->mask = NULL; 1242 return error; 1243 } 1244 1245 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) 1246 { 1247 struct net *net = sock_net(skb->sk); 1248 struct nlattr **a = info->attrs; 1249 struct ovs_header *ovs_header = genl_info_userhdr(info); 1250 struct sw_flow_key key; 1251 struct sw_flow *flow; 1252 struct sk_buff *reply = NULL; 1253 struct datapath *dp; 1254 struct sw_flow_actions *old_acts = NULL, *acts = NULL; 1255 struct sw_flow_match match; 1256 struct sw_flow_id sfid; 1257 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 1258 int error = 0; 1259 bool log = !a[OVS_FLOW_ATTR_PROBE]; 1260 bool ufid_present; 1261 1262 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log); 1263 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) { 1264 OVS_NLERR(log, 1265 "Flow set message rejected, Key attribute missing."); 1266 return -EINVAL; 1267 } 1268 1269 error = ovs_nla_init_match_and_action(net, &match, &key, a, 1270 &acts, log); 1271 if (error) 1272 goto error; 1273 1274 if (acts) { 1275 /* Can allocate before locking if have acts. */ 1276 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false, 1277 ufid_flags); 1278 if (IS_ERR(reply)) { 1279 error = PTR_ERR(reply); 1280 goto err_kfree_acts; 1281 } 1282 } 1283 1284 ovs_lock(); 1285 dp = get_dp(net, ovs_header->dp_ifindex); 1286 if (unlikely(!dp)) { 1287 error = -ENODEV; 1288 goto err_unlock_ovs; 1289 } 1290 /* Check that the flow exists. */ 1291 if (ufid_present) 1292 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid); 1293 else 1294 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); 1295 if (unlikely(!flow)) { 1296 error = -ENOENT; 1297 goto err_unlock_ovs; 1298 } 1299 1300 /* Update actions, if present. */ 1301 if (likely(acts)) { 1302 old_acts = ovsl_dereference(flow->sf_acts); 1303 rcu_assign_pointer(flow->sf_acts, acts); 1304 1305 if (unlikely(reply)) { 1306 error = ovs_flow_cmd_fill_info(flow, 1307 ovs_header->dp_ifindex, 1308 reply, info->snd_portid, 1309 info->snd_seq, 0, 1310 OVS_FLOW_CMD_SET, 1311 ufid_flags); 1312 BUG_ON(error < 0); 1313 } 1314 } else { 1315 /* Could not alloc without acts before locking. */ 1316 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, 1317 info, OVS_FLOW_CMD_SET, false, 1318 ufid_flags); 1319 1320 if (IS_ERR(reply)) { 1321 error = PTR_ERR(reply); 1322 reply = NULL; 1323 goto err_unlock_ovs; 1324 } 1325 } 1326 1327 /* Clear stats. */ 1328 if (a[OVS_FLOW_ATTR_CLEAR]) 1329 ovs_flow_stats_clear(flow); 1330 ovs_unlock(); 1331 1332 if (reply) 1333 ovs_notify(&dp_flow_genl_family, reply, info); 1334 if (old_acts) 1335 ovs_nla_free_flow_actions_rcu(old_acts); 1336 1337 return 0; 1338 1339 err_unlock_ovs: 1340 ovs_unlock(); 1341 kfree_skb(reply); 1342 err_kfree_acts: 1343 ovs_nla_free_flow_actions(acts); 1344 error: 1345 return error; 1346 } 1347 1348 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) 1349 { 1350 struct nlattr **a = info->attrs; 1351 struct ovs_header *ovs_header = genl_info_userhdr(info); 1352 struct net *net = sock_net(skb->sk); 1353 struct sw_flow_key key; 1354 struct sk_buff *reply; 1355 struct sw_flow *flow; 1356 struct datapath *dp; 1357 struct sw_flow_match match; 1358 struct sw_flow_id ufid; 1359 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 1360 int err = 0; 1361 bool log = !a[OVS_FLOW_ATTR_PROBE]; 1362 bool ufid_present; 1363 1364 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); 1365 if (a[OVS_FLOW_ATTR_KEY]) { 1366 ovs_match_init(&match, &key, true, NULL); 1367 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL, 1368 log); 1369 } else if (!ufid_present) { 1370 OVS_NLERR(log, 1371 "Flow get message rejected, Key attribute missing."); 1372 err = -EINVAL; 1373 } 1374 if (err) 1375 return err; 1376 1377 ovs_lock(); 1378 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1379 if (!dp) { 1380 err = -ENODEV; 1381 goto unlock; 1382 } 1383 1384 if (ufid_present) 1385 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); 1386 else 1387 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); 1388 if (!flow) { 1389 err = -ENOENT; 1390 goto unlock; 1391 } 1392 1393 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info, 1394 OVS_FLOW_CMD_GET, true, ufid_flags); 1395 if (IS_ERR(reply)) { 1396 err = PTR_ERR(reply); 1397 goto unlock; 1398 } 1399 1400 ovs_unlock(); 1401 return genlmsg_reply(reply, info); 1402 unlock: 1403 ovs_unlock(); 1404 return err; 1405 } 1406 1407 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) 1408 { 1409 struct nlattr **a = info->attrs; 1410 struct ovs_header *ovs_header = genl_info_userhdr(info); 1411 struct net *net = sock_net(skb->sk); 1412 struct sw_flow_key key; 1413 struct sk_buff *reply; 1414 struct sw_flow *flow = NULL; 1415 struct datapath *dp; 1416 struct sw_flow_match match; 1417 struct sw_flow_id ufid; 1418 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 1419 int err; 1420 bool log = !a[OVS_FLOW_ATTR_PROBE]; 1421 bool ufid_present; 1422 1423 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); 1424 if (a[OVS_FLOW_ATTR_KEY]) { 1425 ovs_match_init(&match, &key, true, NULL); 1426 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], 1427 NULL, log); 1428 if (unlikely(err)) 1429 return err; 1430 } 1431 1432 ovs_lock(); 1433 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1434 if (unlikely(!dp)) { 1435 err = -ENODEV; 1436 goto unlock; 1437 } 1438 1439 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) { 1440 err = ovs_flow_tbl_flush(&dp->table); 1441 goto unlock; 1442 } 1443 1444 if (ufid_present) 1445 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); 1446 else 1447 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); 1448 if (unlikely(!flow)) { 1449 err = -ENOENT; 1450 goto unlock; 1451 } 1452 1453 ovs_flow_tbl_remove(&dp->table, flow); 1454 ovs_unlock(); 1455 1456 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts, 1457 &flow->id, info, false, ufid_flags); 1458 if (likely(reply)) { 1459 if (!IS_ERR(reply)) { 1460 rcu_read_lock(); /*To keep RCU checker happy. */ 1461 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, 1462 reply, info->snd_portid, 1463 info->snd_seq, 0, 1464 OVS_FLOW_CMD_DEL, 1465 ufid_flags); 1466 rcu_read_unlock(); 1467 if (WARN_ON_ONCE(err < 0)) { 1468 kfree_skb(reply); 1469 goto out_free; 1470 } 1471 1472 ovs_notify(&dp_flow_genl_family, reply, info); 1473 } else { 1474 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, 1475 PTR_ERR(reply)); 1476 } 1477 } 1478 1479 out_free: 1480 ovs_flow_free(flow, true); 1481 return 0; 1482 unlock: 1483 ovs_unlock(); 1484 return err; 1485 } 1486 1487 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1488 { 1489 struct nlattr *a[__OVS_FLOW_ATTR_MAX]; 1490 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 1491 struct table_instance *ti; 1492 struct datapath *dp; 1493 u32 ufid_flags; 1494 int err; 1495 1496 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a, 1497 OVS_FLOW_ATTR_MAX, flow_policy, NULL); 1498 if (err) 1499 return err; 1500 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); 1501 1502 rcu_read_lock(); 1503 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); 1504 if (!dp) { 1505 rcu_read_unlock(); 1506 return -ENODEV; 1507 } 1508 1509 ti = rcu_dereference(dp->table.ti); 1510 for (;;) { 1511 struct sw_flow *flow; 1512 u32 bucket, obj; 1513 1514 bucket = cb->args[0]; 1515 obj = cb->args[1]; 1516 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj); 1517 if (!flow) 1518 break; 1519 1520 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb, 1521 NETLINK_CB(cb->skb).portid, 1522 cb->nlh->nlmsg_seq, NLM_F_MULTI, 1523 OVS_FLOW_CMD_GET, ufid_flags) < 0) 1524 break; 1525 1526 cb->args[0] = bucket; 1527 cb->args[1] = obj; 1528 } 1529 rcu_read_unlock(); 1530 return skb->len; 1531 } 1532 1533 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { 1534 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, 1535 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED }, 1536 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, 1537 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, 1538 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG }, 1539 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 }, 1540 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 }, 1541 }; 1542 1543 static const struct genl_small_ops dp_flow_genl_ops[] = { 1544 { .cmd = OVS_FLOW_CMD_NEW, 1545 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1546 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1547 .doit = ovs_flow_cmd_new 1548 }, 1549 { .cmd = OVS_FLOW_CMD_DEL, 1550 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1551 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1552 .doit = ovs_flow_cmd_del 1553 }, 1554 { .cmd = OVS_FLOW_CMD_GET, 1555 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1556 .flags = 0, /* OK for unprivileged users. */ 1557 .doit = ovs_flow_cmd_get, 1558 .dumpit = ovs_flow_cmd_dump 1559 }, 1560 { .cmd = OVS_FLOW_CMD_SET, 1561 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 1562 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1563 .doit = ovs_flow_cmd_set, 1564 }, 1565 }; 1566 1567 static struct genl_family dp_flow_genl_family __ro_after_init = { 1568 .hdrsize = sizeof(struct ovs_header), 1569 .name = OVS_FLOW_FAMILY, 1570 .version = OVS_FLOW_VERSION, 1571 .maxattr = OVS_FLOW_ATTR_MAX, 1572 .policy = flow_policy, 1573 .netnsok = true, 1574 .parallel_ops = true, 1575 .small_ops = dp_flow_genl_ops, 1576 .n_small_ops = ARRAY_SIZE(dp_flow_genl_ops), 1577 .resv_start_op = OVS_FLOW_CMD_SET + 1, 1578 .mcgrps = &ovs_dp_flow_multicast_group, 1579 .n_mcgrps = 1, 1580 .module = THIS_MODULE, 1581 }; 1582 1583 static size_t ovs_dp_cmd_msg_size(void) 1584 { 1585 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); 1586 1587 msgsize += nla_total_size(IFNAMSIZ); 1588 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats)); 1589 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats)); 1590 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */ 1591 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_MASKS_CACHE_SIZE */ 1592 msgsize += nla_total_size(sizeof(u32) * nr_cpu_ids); /* OVS_DP_ATTR_PER_CPU_PIDS */ 1593 1594 return msgsize; 1595 } 1596 1597 /* Called with ovs_mutex. */ 1598 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, 1599 u32 portid, u32 seq, u32 flags, u8 cmd) 1600 { 1601 struct ovs_header *ovs_header; 1602 struct ovs_dp_stats dp_stats; 1603 struct ovs_dp_megaflow_stats dp_megaflow_stats; 1604 struct dp_nlsk_pids *pids = ovsl_dereference(dp->upcall_portids); 1605 int err, pids_len; 1606 1607 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, 1608 flags, cmd); 1609 if (!ovs_header) 1610 goto error; 1611 1612 ovs_header->dp_ifindex = get_dpifindex(dp); 1613 1614 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); 1615 if (err) 1616 goto nla_put_failure; 1617 1618 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats); 1619 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), 1620 &dp_stats, OVS_DP_ATTR_PAD)) 1621 goto nla_put_failure; 1622 1623 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS, 1624 sizeof(struct ovs_dp_megaflow_stats), 1625 &dp_megaflow_stats, OVS_DP_ATTR_PAD)) 1626 goto nla_put_failure; 1627 1628 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features)) 1629 goto nla_put_failure; 1630 1631 if (nla_put_u32(skb, OVS_DP_ATTR_MASKS_CACHE_SIZE, 1632 ovs_flow_tbl_masks_cache_size(&dp->table))) 1633 goto nla_put_failure; 1634 1635 if (dp->user_features & OVS_DP_F_DISPATCH_UPCALL_PER_CPU && pids) { 1636 pids_len = min(pids->n_pids, nr_cpu_ids) * sizeof(u32); 1637 if (nla_put(skb, OVS_DP_ATTR_PER_CPU_PIDS, pids_len, &pids->pids)) 1638 goto nla_put_failure; 1639 } 1640 1641 genlmsg_end(skb, ovs_header); 1642 return 0; 1643 1644 nla_put_failure: 1645 genlmsg_cancel(skb, ovs_header); 1646 error: 1647 return -EMSGSIZE; 1648 } 1649 1650 static struct sk_buff *ovs_dp_cmd_alloc_info(void) 1651 { 1652 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL); 1653 } 1654 1655 /* Called with rcu_read_lock or ovs_mutex. */ 1656 static struct datapath *lookup_datapath(struct net *net, 1657 const struct ovs_header *ovs_header, 1658 struct nlattr *a[OVS_DP_ATTR_MAX + 1]) 1659 { 1660 struct datapath *dp; 1661 1662 if (!a[OVS_DP_ATTR_NAME]) 1663 dp = get_dp(net, ovs_header->dp_ifindex); 1664 else { 1665 struct vport *vport; 1666 1667 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); 1668 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; 1669 } 1670 return dp ? dp : ERR_PTR(-ENODEV); 1671 } 1672 1673 static void ovs_dp_reset_user_features(struct sk_buff *skb, 1674 struct genl_info *info) 1675 { 1676 struct datapath *dp; 1677 1678 dp = lookup_datapath(sock_net(skb->sk), genl_info_userhdr(info), 1679 info->attrs); 1680 if (IS_ERR(dp)) 1681 return; 1682 1683 pr_warn("%s: Dropping previously announced user features\n", 1684 ovs_dp_name(dp)); 1685 dp->user_features = 0; 1686 } 1687 1688 static int ovs_dp_set_upcall_portids(struct datapath *dp, 1689 const struct nlattr *ids) 1690 { 1691 struct dp_nlsk_pids *old, *dp_nlsk_pids; 1692 1693 if (!nla_len(ids) || nla_len(ids) % sizeof(u32)) 1694 return -EINVAL; 1695 1696 old = ovsl_dereference(dp->upcall_portids); 1697 1698 dp_nlsk_pids = kmalloc(sizeof(*dp_nlsk_pids) + nla_len(ids), 1699 GFP_KERNEL); 1700 if (!dp_nlsk_pids) 1701 return -ENOMEM; 1702 1703 dp_nlsk_pids->n_pids = nla_len(ids) / sizeof(u32); 1704 nla_memcpy(dp_nlsk_pids->pids, ids, nla_len(ids)); 1705 1706 rcu_assign_pointer(dp->upcall_portids, dp_nlsk_pids); 1707 1708 kfree_rcu(old, rcu); 1709 1710 return 0; 1711 } 1712 1713 u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id) 1714 { 1715 struct dp_nlsk_pids *dp_nlsk_pids; 1716 1717 dp_nlsk_pids = rcu_dereference(dp->upcall_portids); 1718 1719 if (dp_nlsk_pids) { 1720 if (cpu_id < dp_nlsk_pids->n_pids) { 1721 return dp_nlsk_pids->pids[cpu_id]; 1722 } else if (dp_nlsk_pids->n_pids > 0 && 1723 cpu_id >= dp_nlsk_pids->n_pids) { 1724 /* If the number of netlink PIDs is mismatched with 1725 * the number of CPUs as seen by the kernel, log this 1726 * and send the upcall to an arbitrary socket (0) in 1727 * order to not drop packets 1728 */ 1729 pr_info_ratelimited("cpu_id mismatch with handler threads"); 1730 return dp_nlsk_pids->pids[cpu_id % 1731 dp_nlsk_pids->n_pids]; 1732 } else { 1733 return 0; 1734 } 1735 } else { 1736 return 0; 1737 } 1738 } 1739 1740 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[]) 1741 { 1742 u32 user_features = 0, old_features = dp->user_features; 1743 int err; 1744 1745 if (a[OVS_DP_ATTR_USER_FEATURES]) { 1746 user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]); 1747 1748 if (user_features & ~(OVS_DP_F_VPORT_PIDS | 1749 OVS_DP_F_UNALIGNED | 1750 OVS_DP_F_TC_RECIRC_SHARING | 1751 OVS_DP_F_DISPATCH_UPCALL_PER_CPU)) 1752 return -EOPNOTSUPP; 1753 1754 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 1755 if (user_features & OVS_DP_F_TC_RECIRC_SHARING) 1756 return -EOPNOTSUPP; 1757 #endif 1758 } 1759 1760 if (a[OVS_DP_ATTR_MASKS_CACHE_SIZE]) { 1761 int err; 1762 u32 cache_size; 1763 1764 cache_size = nla_get_u32(a[OVS_DP_ATTR_MASKS_CACHE_SIZE]); 1765 err = ovs_flow_tbl_masks_cache_resize(&dp->table, cache_size); 1766 if (err) 1767 return err; 1768 } 1769 1770 dp->user_features = user_features; 1771 1772 if (dp->user_features & OVS_DP_F_DISPATCH_UPCALL_PER_CPU && 1773 a[OVS_DP_ATTR_PER_CPU_PIDS]) { 1774 /* Upcall Netlink Port IDs have been updated */ 1775 err = ovs_dp_set_upcall_portids(dp, 1776 a[OVS_DP_ATTR_PER_CPU_PIDS]); 1777 if (err) 1778 return err; 1779 } 1780 1781 if ((dp->user_features & OVS_DP_F_TC_RECIRC_SHARING) && 1782 !(old_features & OVS_DP_F_TC_RECIRC_SHARING)) 1783 tc_skb_ext_tc_enable(); 1784 else if (!(dp->user_features & OVS_DP_F_TC_RECIRC_SHARING) && 1785 (old_features & OVS_DP_F_TC_RECIRC_SHARING)) 1786 tc_skb_ext_tc_disable(); 1787 1788 return 0; 1789 } 1790 1791 static int ovs_dp_stats_init(struct datapath *dp) 1792 { 1793 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu); 1794 if (!dp->stats_percpu) 1795 return -ENOMEM; 1796 1797 return 0; 1798 } 1799 1800 static int ovs_dp_vport_init(struct datapath *dp) 1801 { 1802 int i; 1803 1804 dp->ports = kmalloc_objs(struct hlist_head, DP_VPORT_HASH_BUCKETS); 1805 if (!dp->ports) 1806 return -ENOMEM; 1807 1808 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) 1809 INIT_HLIST_HEAD(&dp->ports[i]); 1810 1811 return 0; 1812 } 1813 1814 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) 1815 { 1816 struct nlattr **a = info->attrs; 1817 struct vport_parms parms; 1818 struct sk_buff *reply; 1819 struct datapath *dp; 1820 struct vport *vport; 1821 struct ovs_net *ovs_net; 1822 int err; 1823 1824 err = -EINVAL; 1825 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) 1826 goto err; 1827 1828 reply = ovs_dp_cmd_alloc_info(); 1829 if (!reply) 1830 return -ENOMEM; 1831 1832 err = -ENOMEM; 1833 dp = kzalloc_obj(*dp); 1834 if (dp == NULL) 1835 goto err_destroy_reply; 1836 1837 ovs_dp_set_net(dp, sock_net(skb->sk)); 1838 1839 /* Allocate table. */ 1840 err = ovs_flow_tbl_init(&dp->table); 1841 if (err) 1842 goto err_destroy_dp; 1843 1844 err = ovs_dp_stats_init(dp); 1845 if (err) 1846 goto err_destroy_table; 1847 1848 err = ovs_dp_vport_init(dp); 1849 if (err) 1850 goto err_destroy_stats; 1851 1852 err = ovs_meters_init(dp); 1853 if (err) 1854 goto err_destroy_ports; 1855 1856 /* Set up our datapath device. */ 1857 parms.name = nla_data(a[OVS_DP_ATTR_NAME]); 1858 parms.type = OVS_VPORT_TYPE_INTERNAL; 1859 parms.options = NULL; 1860 parms.dp = dp; 1861 parms.port_no = OVSP_LOCAL; 1862 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID]; 1863 parms.desired_ifindex = nla_get_s32_default(a[OVS_DP_ATTR_IFINDEX], 0); 1864 1865 /* So far only local changes have been made, now need the lock. */ 1866 ovs_lock(); 1867 1868 err = ovs_dp_change(dp, a); 1869 if (err) 1870 goto err_unlock_and_destroy_meters; 1871 1872 vport = new_vport(&parms); 1873 if (IS_ERR(vport)) { 1874 err = PTR_ERR(vport); 1875 if (err == -EBUSY) 1876 err = -EEXIST; 1877 1878 if (err == -EEXIST) { 1879 /* An outdated user space instance that does not understand 1880 * the concept of user_features has attempted to create a new 1881 * datapath and is likely to reuse it. Drop all user features. 1882 */ 1883 if (info->genlhdr->version < OVS_DP_VER_FEATURES) 1884 ovs_dp_reset_user_features(skb, info); 1885 } 1886 1887 goto err_destroy_portids; 1888 } 1889 1890 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, 1891 info->snd_seq, 0, OVS_DP_CMD_NEW); 1892 BUG_ON(err < 0); 1893 1894 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); 1895 list_add_tail_rcu(&dp->list_node, &ovs_net->dps); 1896 1897 ovs_unlock(); 1898 1899 ovs_notify(&dp_datapath_genl_family, reply, info); 1900 return 0; 1901 1902 err_destroy_portids: 1903 kfree(rcu_dereference_raw(dp->upcall_portids)); 1904 err_unlock_and_destroy_meters: 1905 ovs_unlock(); 1906 ovs_meters_exit(dp); 1907 err_destroy_ports: 1908 kfree(dp->ports); 1909 err_destroy_stats: 1910 free_percpu(dp->stats_percpu); 1911 err_destroy_table: 1912 ovs_flow_tbl_destroy(&dp->table); 1913 err_destroy_dp: 1914 kfree(dp); 1915 err_destroy_reply: 1916 kfree_skb(reply); 1917 err: 1918 return err; 1919 } 1920 1921 /* Called with ovs_mutex. */ 1922 static void __dp_destroy(struct datapath *dp) 1923 { 1924 struct flow_table *table = &dp->table; 1925 int i; 1926 1927 if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING) 1928 tc_skb_ext_tc_disable(); 1929 1930 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 1931 struct vport *vport; 1932 struct hlist_node *n; 1933 1934 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) 1935 if (vport->port_no != OVSP_LOCAL) 1936 ovs_dp_detach_port(vport); 1937 } 1938 1939 list_del_rcu(&dp->list_node); 1940 1941 /* OVSP_LOCAL is datapath internal port. We need to make sure that 1942 * all ports in datapath are destroyed first before freeing datapath. 1943 */ 1944 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); 1945 1946 /* Flush sw_flow in the tables. RCU cb only releases resource 1947 * such as dp, ports and tables. That may avoid some issues 1948 * such as RCU usage warning. 1949 */ 1950 table_instance_flow_flush(table, ovsl_dereference(table->ti), 1951 ovsl_dereference(table->ufid_ti)); 1952 1953 /* RCU destroy the ports, meters and flow tables. */ 1954 call_rcu(&dp->rcu, destroy_dp_rcu); 1955 } 1956 1957 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) 1958 { 1959 struct sk_buff *reply; 1960 struct datapath *dp; 1961 int err; 1962 1963 reply = ovs_dp_cmd_alloc_info(); 1964 if (!reply) 1965 return -ENOMEM; 1966 1967 ovs_lock(); 1968 dp = lookup_datapath(sock_net(skb->sk), genl_info_userhdr(info), 1969 info->attrs); 1970 err = PTR_ERR(dp); 1971 if (IS_ERR(dp)) 1972 goto err_unlock_free; 1973 1974 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, 1975 info->snd_seq, 0, OVS_DP_CMD_DEL); 1976 BUG_ON(err < 0); 1977 1978 __dp_destroy(dp); 1979 ovs_unlock(); 1980 1981 ovs_notify(&dp_datapath_genl_family, reply, info); 1982 1983 return 0; 1984 1985 err_unlock_free: 1986 ovs_unlock(); 1987 kfree_skb(reply); 1988 return err; 1989 } 1990 1991 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) 1992 { 1993 struct sk_buff *reply; 1994 struct datapath *dp; 1995 int err; 1996 1997 reply = ovs_dp_cmd_alloc_info(); 1998 if (!reply) 1999 return -ENOMEM; 2000 2001 ovs_lock(); 2002 dp = lookup_datapath(sock_net(skb->sk), genl_info_userhdr(info), 2003 info->attrs); 2004 err = PTR_ERR(dp); 2005 if (IS_ERR(dp)) 2006 goto err_unlock_free; 2007 2008 err = ovs_dp_change(dp, info->attrs); 2009 if (err) 2010 goto err_unlock_free; 2011 2012 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, 2013 info->snd_seq, 0, OVS_DP_CMD_SET); 2014 BUG_ON(err < 0); 2015 2016 ovs_unlock(); 2017 ovs_notify(&dp_datapath_genl_family, reply, info); 2018 2019 return 0; 2020 2021 err_unlock_free: 2022 ovs_unlock(); 2023 kfree_skb(reply); 2024 return err; 2025 } 2026 2027 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) 2028 { 2029 struct sk_buff *reply; 2030 struct datapath *dp; 2031 int err; 2032 2033 reply = ovs_dp_cmd_alloc_info(); 2034 if (!reply) 2035 return -ENOMEM; 2036 2037 ovs_lock(); 2038 dp = lookup_datapath(sock_net(skb->sk), genl_info_userhdr(info), 2039 info->attrs); 2040 if (IS_ERR(dp)) { 2041 err = PTR_ERR(dp); 2042 goto err_unlock_free; 2043 } 2044 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, 2045 info->snd_seq, 0, OVS_DP_CMD_GET); 2046 BUG_ON(err < 0); 2047 ovs_unlock(); 2048 2049 return genlmsg_reply(reply, info); 2050 2051 err_unlock_free: 2052 ovs_unlock(); 2053 kfree_skb(reply); 2054 return err; 2055 } 2056 2057 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 2058 { 2059 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); 2060 struct datapath *dp; 2061 int skip = cb->args[0]; 2062 int i = 0; 2063 2064 ovs_lock(); 2065 list_for_each_entry(dp, &ovs_net->dps, list_node) { 2066 if (i >= skip && 2067 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, 2068 cb->nlh->nlmsg_seq, NLM_F_MULTI, 2069 OVS_DP_CMD_GET) < 0) 2070 break; 2071 i++; 2072 } 2073 ovs_unlock(); 2074 2075 cb->args[0] = i; 2076 2077 return skb->len; 2078 } 2079 2080 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { 2081 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, 2082 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, 2083 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 }, 2084 [OVS_DP_ATTR_MASKS_CACHE_SIZE] = NLA_POLICY_RANGE(NLA_U32, 0, 2085 PCPU_MIN_UNIT_SIZE / sizeof(struct mask_cache_entry)), 2086 [OVS_DP_ATTR_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 0), 2087 }; 2088 2089 static const struct genl_small_ops dp_datapath_genl_ops[] = { 2090 { .cmd = OVS_DP_CMD_NEW, 2091 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2092 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2093 .doit = ovs_dp_cmd_new 2094 }, 2095 { .cmd = OVS_DP_CMD_DEL, 2096 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2097 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2098 .doit = ovs_dp_cmd_del 2099 }, 2100 { .cmd = OVS_DP_CMD_GET, 2101 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2102 .flags = 0, /* OK for unprivileged users. */ 2103 .doit = ovs_dp_cmd_get, 2104 .dumpit = ovs_dp_cmd_dump 2105 }, 2106 { .cmd = OVS_DP_CMD_SET, 2107 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2108 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2109 .doit = ovs_dp_cmd_set, 2110 }, 2111 }; 2112 2113 static struct genl_family dp_datapath_genl_family __ro_after_init = { 2114 .hdrsize = sizeof(struct ovs_header), 2115 .name = OVS_DATAPATH_FAMILY, 2116 .version = OVS_DATAPATH_VERSION, 2117 .maxattr = OVS_DP_ATTR_MAX, 2118 .policy = datapath_policy, 2119 .netnsok = true, 2120 .parallel_ops = true, 2121 .small_ops = dp_datapath_genl_ops, 2122 .n_small_ops = ARRAY_SIZE(dp_datapath_genl_ops), 2123 .resv_start_op = OVS_DP_CMD_SET + 1, 2124 .mcgrps = &ovs_dp_datapath_multicast_group, 2125 .n_mcgrps = 1, 2126 .module = THIS_MODULE, 2127 }; 2128 2129 /* Called with ovs_mutex or RCU read lock. */ 2130 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, 2131 struct net *net, u32 portid, u32 seq, 2132 u32 flags, u8 cmd, gfp_t gfp) 2133 { 2134 struct ovs_header *ovs_header; 2135 struct ovs_vport_stats vport_stats; 2136 struct net *net_vport; 2137 int err; 2138 2139 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, 2140 flags, cmd); 2141 if (!ovs_header) 2142 return -EMSGSIZE; 2143 2144 ovs_header->dp_ifindex = get_dpifindex(vport->dp); 2145 2146 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || 2147 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || 2148 nla_put_string(skb, OVS_VPORT_ATTR_NAME, 2149 ovs_vport_name(vport)) || 2150 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex)) 2151 goto nla_put_failure; 2152 2153 rcu_read_lock(); 2154 net_vport = dev_net_rcu(vport->dev); 2155 if (!net_eq(net, net_vport)) { 2156 int id = peernet2id_alloc(net, net_vport, GFP_ATOMIC); 2157 2158 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id)) 2159 goto nla_put_failure_unlock; 2160 } 2161 rcu_read_unlock(); 2162 2163 ovs_vport_get_stats(vport, &vport_stats); 2164 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS, 2165 sizeof(struct ovs_vport_stats), &vport_stats, 2166 OVS_VPORT_ATTR_PAD)) 2167 goto nla_put_failure; 2168 2169 if (ovs_vport_get_upcall_stats(vport, skb)) 2170 goto nla_put_failure; 2171 2172 if (ovs_vport_get_upcall_portids(vport, skb)) 2173 goto nla_put_failure; 2174 2175 err = ovs_vport_get_options(vport, skb); 2176 if (err == -EMSGSIZE) 2177 goto error; 2178 2179 genlmsg_end(skb, ovs_header); 2180 return 0; 2181 2182 nla_put_failure_unlock: 2183 rcu_read_unlock(); 2184 nla_put_failure: 2185 err = -EMSGSIZE; 2186 error: 2187 genlmsg_cancel(skb, ovs_header); 2188 return err; 2189 } 2190 2191 static size_t ovs_vport_cmd_msg_size(void) 2192 { 2193 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); 2194 2195 msgsize += nla_total_size(sizeof(u32)); /* OVS_VPORT_ATTR_PORT_NO */ 2196 msgsize += nla_total_size(sizeof(u32)); /* OVS_VPORT_ATTR_TYPE */ 2197 msgsize += nla_total_size(IFNAMSIZ); /* OVS_VPORT_ATTR_NAME */ 2198 msgsize += nla_total_size(sizeof(u32)); /* OVS_VPORT_ATTR_IFINDEX */ 2199 msgsize += nla_total_size(sizeof(s32)); /* OVS_VPORT_ATTR_NETNSID */ 2200 2201 /* OVS_VPORT_ATTR_STATS */ 2202 msgsize += nla_total_size_64bit(sizeof(struct ovs_vport_stats)); 2203 2204 /* OVS_VPORT_ATTR_UPCALL_STATS(OVS_VPORT_UPCALL_ATTR_SUCCESS + 2205 * OVS_VPORT_UPCALL_ATTR_FAIL) 2206 */ 2207 msgsize += nla_total_size(nla_total_size_64bit(sizeof(u64)) + 2208 nla_total_size_64bit(sizeof(u64))); 2209 2210 /* OVS_VPORT_ATTR_UPCALL_PID */ 2211 msgsize += nla_total_size(nr_cpu_ids * sizeof(u32)); 2212 2213 /* OVS_VPORT_ATTR_OPTIONS(OVS_TUNNEL_ATTR_DST_PORT + 2214 * OVS_TUNNEL_ATTR_EXTENSION(OVS_VXLAN_EXT_GBP)) 2215 */ 2216 msgsize += nla_total_size(nla_total_size(sizeof(u16)) + 2217 nla_total_size(nla_total_size(0))); 2218 2219 return msgsize; 2220 } 2221 2222 static struct sk_buff *ovs_vport_cmd_alloc_info(void) 2223 { 2224 return genlmsg_new(ovs_vport_cmd_msg_size(), GFP_KERNEL); 2225 } 2226 2227 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */ 2228 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net, 2229 u32 portid, u32 seq, u8 cmd) 2230 { 2231 struct sk_buff *skb; 2232 int retval; 2233 2234 skb = ovs_vport_cmd_alloc_info(); 2235 if (!skb) 2236 return ERR_PTR(-ENOMEM); 2237 2238 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd, 2239 GFP_KERNEL); 2240 BUG_ON(retval < 0); 2241 2242 return skb; 2243 } 2244 2245 /* Called with ovs_mutex or RCU read lock. */ 2246 static struct vport *lookup_vport(struct net *net, 2247 const struct ovs_header *ovs_header, 2248 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) 2249 { 2250 struct datapath *dp; 2251 struct vport *vport; 2252 2253 if (a[OVS_VPORT_ATTR_IFINDEX]) 2254 return ERR_PTR(-EOPNOTSUPP); 2255 if (a[OVS_VPORT_ATTR_NAME]) { 2256 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); 2257 if (!vport) 2258 return ERR_PTR(-ENODEV); 2259 if (ovs_header->dp_ifindex && 2260 ovs_header->dp_ifindex != get_dpifindex(vport->dp)) 2261 return ERR_PTR(-ENODEV); 2262 return vport; 2263 } else if (a[OVS_VPORT_ATTR_PORT_NO]) { 2264 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); 2265 2266 if (port_no >= DP_MAX_PORTS) 2267 return ERR_PTR(-EFBIG); 2268 2269 dp = get_dp(net, ovs_header->dp_ifindex); 2270 if (!dp) 2271 return ERR_PTR(-ENODEV); 2272 2273 vport = ovs_vport_ovsl_rcu(dp, port_no); 2274 if (!vport) 2275 return ERR_PTR(-ENODEV); 2276 return vport; 2277 } else 2278 return ERR_PTR(-EINVAL); 2279 2280 } 2281 2282 static unsigned int ovs_get_max_headroom(struct datapath *dp) 2283 { 2284 unsigned int dev_headroom, max_headroom = 0; 2285 struct net_device *dev; 2286 struct vport *vport; 2287 int i; 2288 2289 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 2290 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node, 2291 lockdep_ovsl_is_held()) { 2292 dev = vport->dev; 2293 dev_headroom = netdev_get_fwd_headroom(dev); 2294 if (dev_headroom > max_headroom) 2295 max_headroom = dev_headroom; 2296 } 2297 } 2298 2299 return max_headroom; 2300 } 2301 2302 /* Called with ovs_mutex */ 2303 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom) 2304 { 2305 struct vport *vport; 2306 int i; 2307 2308 dp->max_headroom = new_headroom; 2309 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 2310 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node, 2311 lockdep_ovsl_is_held()) 2312 netdev_set_rx_headroom(vport->dev, new_headroom); 2313 } 2314 } 2315 2316 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) 2317 { 2318 struct nlattr **a = info->attrs; 2319 struct ovs_header *ovs_header = genl_info_userhdr(info); 2320 struct vport_parms parms; 2321 struct sk_buff *reply; 2322 struct vport *vport; 2323 struct datapath *dp; 2324 unsigned int new_headroom; 2325 u32 port_no; 2326 int err; 2327 2328 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || 2329 !a[OVS_VPORT_ATTR_UPCALL_PID]) 2330 return -EINVAL; 2331 2332 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); 2333 2334 if (a[OVS_VPORT_ATTR_IFINDEX] && parms.type != OVS_VPORT_TYPE_INTERNAL) 2335 return -EOPNOTSUPP; 2336 2337 port_no = nla_get_u32_default(a[OVS_VPORT_ATTR_PORT_NO], 0); 2338 if (port_no >= DP_MAX_PORTS) 2339 return -EFBIG; 2340 2341 reply = ovs_vport_cmd_alloc_info(); 2342 if (!reply) 2343 return -ENOMEM; 2344 2345 ovs_lock(); 2346 restart: 2347 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 2348 err = -ENODEV; 2349 if (!dp) 2350 goto exit_unlock_free; 2351 2352 if (port_no) { 2353 vport = ovs_vport_ovsl(dp, port_no); 2354 err = -EBUSY; 2355 if (vport) 2356 goto exit_unlock_free; 2357 } else { 2358 for (port_no = 1; ; port_no++) { 2359 if (port_no >= DP_MAX_PORTS) { 2360 err = -EFBIG; 2361 goto exit_unlock_free; 2362 } 2363 vport = ovs_vport_ovsl(dp, port_no); 2364 if (!vport) 2365 break; 2366 } 2367 } 2368 2369 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); 2370 parms.options = a[OVS_VPORT_ATTR_OPTIONS]; 2371 parms.dp = dp; 2372 parms.port_no = port_no; 2373 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID]; 2374 parms.desired_ifindex = nla_get_s32_default(a[OVS_VPORT_ATTR_IFINDEX], 2375 0); 2376 2377 vport = new_vport(&parms); 2378 err = PTR_ERR(vport); 2379 if (IS_ERR(vport)) { 2380 if (err == -EAGAIN) 2381 goto restart; 2382 goto exit_unlock_free; 2383 } 2384 2385 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), 2386 info->snd_portid, info->snd_seq, 0, 2387 OVS_VPORT_CMD_NEW, GFP_KERNEL); 2388 2389 new_headroom = netdev_get_fwd_headroom(vport->dev); 2390 2391 if (new_headroom > dp->max_headroom) 2392 ovs_update_headroom(dp, new_headroom); 2393 else 2394 netdev_set_rx_headroom(vport->dev, dp->max_headroom); 2395 2396 BUG_ON(err < 0); 2397 ovs_unlock(); 2398 2399 ovs_notify(&dp_vport_genl_family, reply, info); 2400 return 0; 2401 2402 exit_unlock_free: 2403 ovs_unlock(); 2404 kfree_skb(reply); 2405 return err; 2406 } 2407 2408 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) 2409 { 2410 struct nlattr **a = info->attrs; 2411 struct sk_buff *reply; 2412 struct vport *vport; 2413 int err; 2414 2415 reply = ovs_vport_cmd_alloc_info(); 2416 if (!reply) 2417 return -ENOMEM; 2418 2419 ovs_lock(); 2420 vport = lookup_vport(sock_net(skb->sk), genl_info_userhdr(info), a); 2421 err = PTR_ERR(vport); 2422 if (IS_ERR(vport)) 2423 goto exit_unlock_free; 2424 2425 if (a[OVS_VPORT_ATTR_TYPE] && 2426 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { 2427 err = -EINVAL; 2428 goto exit_unlock_free; 2429 } 2430 2431 if (a[OVS_VPORT_ATTR_OPTIONS]) { 2432 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); 2433 if (err) 2434 goto exit_unlock_free; 2435 } 2436 2437 2438 if (a[OVS_VPORT_ATTR_UPCALL_PID]) { 2439 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID]; 2440 2441 err = ovs_vport_set_upcall_portids(vport, ids); 2442 if (err) 2443 goto exit_unlock_free; 2444 } 2445 2446 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), 2447 info->snd_portid, info->snd_seq, 0, 2448 OVS_VPORT_CMD_SET, GFP_KERNEL); 2449 BUG_ON(err < 0); 2450 2451 ovs_unlock(); 2452 ovs_notify(&dp_vport_genl_family, reply, info); 2453 return 0; 2454 2455 exit_unlock_free: 2456 ovs_unlock(); 2457 kfree_skb(reply); 2458 return err; 2459 } 2460 2461 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) 2462 { 2463 bool update_headroom = false; 2464 struct nlattr **a = info->attrs; 2465 struct sk_buff *reply; 2466 struct datapath *dp; 2467 struct vport *vport; 2468 unsigned int new_headroom; 2469 int err; 2470 2471 reply = ovs_vport_cmd_alloc_info(); 2472 if (!reply) 2473 return -ENOMEM; 2474 2475 ovs_lock(); 2476 vport = lookup_vport(sock_net(skb->sk), genl_info_userhdr(info), a); 2477 err = PTR_ERR(vport); 2478 if (IS_ERR(vport)) 2479 goto exit_unlock_free; 2480 2481 if (vport->port_no == OVSP_LOCAL) { 2482 err = -EINVAL; 2483 goto exit_unlock_free; 2484 } 2485 2486 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), 2487 info->snd_portid, info->snd_seq, 0, 2488 OVS_VPORT_CMD_DEL, GFP_KERNEL); 2489 BUG_ON(err < 0); 2490 2491 /* the vport deletion may trigger dp headroom update */ 2492 dp = vport->dp; 2493 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom) 2494 update_headroom = true; 2495 2496 netdev_reset_rx_headroom(vport->dev); 2497 ovs_dp_detach_port(vport); 2498 2499 if (update_headroom) { 2500 new_headroom = ovs_get_max_headroom(dp); 2501 2502 if (new_headroom < dp->max_headroom) 2503 ovs_update_headroom(dp, new_headroom); 2504 } 2505 ovs_unlock(); 2506 2507 ovs_notify(&dp_vport_genl_family, reply, info); 2508 return 0; 2509 2510 exit_unlock_free: 2511 ovs_unlock(); 2512 kfree_skb(reply); 2513 return err; 2514 } 2515 2516 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) 2517 { 2518 struct nlattr **a = info->attrs; 2519 struct ovs_header *ovs_header = genl_info_userhdr(info); 2520 struct sk_buff *reply; 2521 struct vport *vport; 2522 int err; 2523 2524 reply = ovs_vport_cmd_alloc_info(); 2525 if (!reply) 2526 return -ENOMEM; 2527 2528 rcu_read_lock(); 2529 vport = lookup_vport(sock_net(skb->sk), ovs_header, a); 2530 err = PTR_ERR(vport); 2531 if (IS_ERR(vport)) 2532 goto exit_unlock_free; 2533 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), 2534 info->snd_portid, info->snd_seq, 0, 2535 OVS_VPORT_CMD_GET, GFP_ATOMIC); 2536 BUG_ON(err < 0); 2537 rcu_read_unlock(); 2538 2539 return genlmsg_reply(reply, info); 2540 2541 exit_unlock_free: 2542 rcu_read_unlock(); 2543 kfree_skb(reply); 2544 return err; 2545 } 2546 2547 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 2548 { 2549 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 2550 struct datapath *dp; 2551 int bucket = cb->args[0], skip = cb->args[1]; 2552 int i, j = 0; 2553 2554 rcu_read_lock(); 2555 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); 2556 if (!dp) { 2557 rcu_read_unlock(); 2558 return -ENODEV; 2559 } 2560 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { 2561 struct vport *vport; 2562 2563 j = 0; 2564 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { 2565 if (j >= skip && 2566 ovs_vport_cmd_fill_info(vport, skb, 2567 sock_net(skb->sk), 2568 NETLINK_CB(cb->skb).portid, 2569 cb->nlh->nlmsg_seq, 2570 NLM_F_MULTI, 2571 OVS_VPORT_CMD_GET, 2572 GFP_ATOMIC) < 0) 2573 goto out; 2574 2575 j++; 2576 } 2577 skip = 0; 2578 } 2579 out: 2580 rcu_read_unlock(); 2581 2582 cb->args[0] = i; 2583 cb->args[1] = j; 2584 2585 return skb->len; 2586 } 2587 2588 static void ovs_dp_masks_rebalance(struct work_struct *work) 2589 { 2590 struct ovs_net *ovs_net = container_of(work, struct ovs_net, 2591 masks_rebalance.work); 2592 struct datapath *dp; 2593 2594 ovs_lock(); 2595 2596 list_for_each_entry(dp, &ovs_net->dps, list_node) 2597 ovs_flow_masks_rebalance(&dp->table); 2598 2599 ovs_unlock(); 2600 2601 schedule_delayed_work(&ovs_net->masks_rebalance, 2602 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL)); 2603 } 2604 2605 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { 2606 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, 2607 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, 2608 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, 2609 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, 2610 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC }, 2611 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, 2612 [OVS_VPORT_ATTR_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 0), 2613 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 }, 2614 [OVS_VPORT_ATTR_UPCALL_STATS] = { .type = NLA_NESTED }, 2615 }; 2616 2617 static const struct genl_small_ops dp_vport_genl_ops[] = { 2618 { .cmd = OVS_VPORT_CMD_NEW, 2619 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2620 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2621 .doit = ovs_vport_cmd_new 2622 }, 2623 { .cmd = OVS_VPORT_CMD_DEL, 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_del 2627 }, 2628 { .cmd = OVS_VPORT_CMD_GET, 2629 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2630 .flags = 0, /* OK for unprivileged users. */ 2631 .doit = ovs_vport_cmd_get, 2632 .dumpit = ovs_vport_cmd_dump 2633 }, 2634 { .cmd = OVS_VPORT_CMD_SET, 2635 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 2636 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 2637 .doit = ovs_vport_cmd_set, 2638 }, 2639 }; 2640 2641 struct genl_family dp_vport_genl_family __ro_after_init = { 2642 .hdrsize = sizeof(struct ovs_header), 2643 .name = OVS_VPORT_FAMILY, 2644 .version = OVS_VPORT_VERSION, 2645 .maxattr = OVS_VPORT_ATTR_MAX, 2646 .policy = vport_policy, 2647 .netnsok = true, 2648 .parallel_ops = true, 2649 .small_ops = dp_vport_genl_ops, 2650 .n_small_ops = ARRAY_SIZE(dp_vport_genl_ops), 2651 .resv_start_op = OVS_VPORT_CMD_SET + 1, 2652 .mcgrps = &ovs_dp_vport_multicast_group, 2653 .n_mcgrps = 1, 2654 .module = THIS_MODULE, 2655 }; 2656 2657 static struct genl_family * const dp_genl_families[] = { 2658 &dp_datapath_genl_family, 2659 &dp_vport_genl_family, 2660 &dp_flow_genl_family, 2661 &dp_packet_genl_family, 2662 &dp_meter_genl_family, 2663 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 2664 &dp_ct_limit_genl_family, 2665 #endif 2666 }; 2667 2668 static void dp_unregister_genl(int n_families) 2669 { 2670 int i; 2671 2672 for (i = 0; i < n_families; i++) 2673 genl_unregister_family(dp_genl_families[i]); 2674 } 2675 2676 static int __init dp_register_genl(void) 2677 { 2678 int err; 2679 int i; 2680 2681 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { 2682 2683 err = genl_register_family(dp_genl_families[i]); 2684 if (err) 2685 goto error; 2686 } 2687 2688 return 0; 2689 2690 error: 2691 dp_unregister_genl(i); 2692 return err; 2693 } 2694 2695 static int __net_init ovs_init_net(struct net *net) 2696 { 2697 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 2698 int err; 2699 2700 INIT_LIST_HEAD(&ovs_net->dps); 2701 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); 2702 INIT_DELAYED_WORK(&ovs_net->masks_rebalance, ovs_dp_masks_rebalance); 2703 2704 err = ovs_ct_init(net); 2705 if (err) 2706 return err; 2707 2708 schedule_delayed_work(&ovs_net->masks_rebalance, 2709 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL)); 2710 return 0; 2711 } 2712 2713 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet, 2714 struct list_head *head) 2715 { 2716 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 2717 struct datapath *dp; 2718 2719 list_for_each_entry(dp, &ovs_net->dps, list_node) { 2720 int i; 2721 2722 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 2723 struct vport *vport; 2724 2725 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) { 2726 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL) 2727 continue; 2728 2729 if (dev_net(vport->dev) == dnet) 2730 list_add(&vport->detach_list, head); 2731 } 2732 } 2733 } 2734 } 2735 2736 static void __net_exit ovs_exit_net(struct net *dnet) 2737 { 2738 struct datapath *dp, *dp_next; 2739 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id); 2740 struct vport *vport, *vport_next; 2741 struct net *net; 2742 LIST_HEAD(head); 2743 2744 ovs_lock(); 2745 2746 ovs_ct_exit(dnet); 2747 2748 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) 2749 __dp_destroy(dp); 2750 2751 down_read(&net_rwsem); 2752 for_each_net(net) 2753 list_vports_from_net(net, dnet, &head); 2754 up_read(&net_rwsem); 2755 2756 /* Detach all vports from given namespace. */ 2757 list_for_each_entry_safe(vport, vport_next, &head, detach_list) { 2758 list_del(&vport->detach_list); 2759 ovs_dp_detach_port(vport); 2760 } 2761 2762 ovs_unlock(); 2763 2764 cancel_delayed_work_sync(&ovs_net->masks_rebalance); 2765 cancel_work_sync(&ovs_net->dp_notify_work); 2766 } 2767 2768 static struct pernet_operations ovs_net_ops = { 2769 .init = ovs_init_net, 2770 .exit = ovs_exit_net, 2771 .id = &ovs_net_id, 2772 .size = sizeof(struct ovs_net), 2773 }; 2774 2775 static const char * const ovs_drop_reasons[] = { 2776 #define S(x) [(x) & ~SKB_DROP_REASON_SUBSYS_MASK] = (#x), 2777 OVS_DROP_REASONS(S) 2778 #undef S 2779 }; 2780 2781 static struct drop_reason_list drop_reason_list_ovs = { 2782 .reasons = ovs_drop_reasons, 2783 .n_reasons = ARRAY_SIZE(ovs_drop_reasons), 2784 }; 2785 2786 static int __init ovs_alloc_percpu_storage(void) 2787 { 2788 unsigned int cpu; 2789 2790 ovs_pcpu_storage = alloc_percpu(*ovs_pcpu_storage); 2791 if (!ovs_pcpu_storage) 2792 return -ENOMEM; 2793 2794 for_each_possible_cpu(cpu) { 2795 struct ovs_pcpu_storage *ovs_pcpu; 2796 2797 ovs_pcpu = per_cpu_ptr(ovs_pcpu_storage, cpu); 2798 local_lock_init(&ovs_pcpu->bh_lock); 2799 } 2800 return 0; 2801 } 2802 2803 static void ovs_free_percpu_storage(void) 2804 { 2805 free_percpu(ovs_pcpu_storage); 2806 } 2807 2808 static int __init dp_init(void) 2809 { 2810 int err; 2811 2812 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > 2813 sizeof_field(struct sk_buff, cb)); 2814 2815 pr_info("Open vSwitch switching datapath\n"); 2816 2817 err = ovs_alloc_percpu_storage(); 2818 if (err) 2819 goto error; 2820 2821 err = ovs_internal_dev_rtnl_link_register(); 2822 if (err) 2823 goto error; 2824 2825 err = ovs_flow_init(); 2826 if (err) 2827 goto error_unreg_rtnl_link; 2828 2829 err = ovs_vport_init(); 2830 if (err) 2831 goto error_flow_exit; 2832 2833 err = register_pernet_device(&ovs_net_ops); 2834 if (err) 2835 goto error_vport_exit; 2836 2837 err = register_netdevice_notifier(&ovs_dp_device_notifier); 2838 if (err) 2839 goto error_netns_exit; 2840 2841 err = ovs_netdev_init(); 2842 if (err) 2843 goto error_unreg_notifier; 2844 2845 err = dp_register_genl(); 2846 if (err < 0) 2847 goto error_unreg_netdev; 2848 2849 drop_reasons_register_subsys(SKB_DROP_REASON_SUBSYS_OPENVSWITCH, 2850 &drop_reason_list_ovs); 2851 2852 return 0; 2853 2854 error_unreg_netdev: 2855 ovs_netdev_exit(); 2856 error_unreg_notifier: 2857 unregister_netdevice_notifier(&ovs_dp_device_notifier); 2858 error_netns_exit: 2859 unregister_pernet_device(&ovs_net_ops); 2860 error_vport_exit: 2861 ovs_vport_exit(); 2862 error_flow_exit: 2863 ovs_flow_exit(); 2864 error_unreg_rtnl_link: 2865 ovs_internal_dev_rtnl_link_unregister(); 2866 error: 2867 ovs_free_percpu_storage(); 2868 return err; 2869 } 2870 2871 static void dp_cleanup(void) 2872 { 2873 dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); 2874 ovs_netdev_exit(); 2875 unregister_netdevice_notifier(&ovs_dp_device_notifier); 2876 unregister_pernet_device(&ovs_net_ops); 2877 drop_reasons_unregister_subsys(SKB_DROP_REASON_SUBSYS_OPENVSWITCH); 2878 rcu_barrier(); 2879 ovs_vport_exit(); 2880 ovs_flow_exit(); 2881 ovs_internal_dev_rtnl_link_unregister(); 2882 ovs_free_percpu_storage(); 2883 } 2884 2885 module_init(dp_init); 2886 module_exit(dp_cleanup); 2887 2888 MODULE_DESCRIPTION("Open vSwitch switching datapath"); 2889 MODULE_LICENSE("GPL"); 2890 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY); 2891 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY); 2892 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY); 2893 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY); 2894 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY); 2895 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY); 2896