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