1 /* 2 * Copyright (c) 2007-2012 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 <linux/workqueue.h> 51 #include <net/genetlink.h> 52 #include <net/net_namespace.h> 53 #include <net/netns/generic.h> 54 55 #include "datapath.h" 56 #include "flow.h" 57 #include "vport-internal_dev.h" 58 59 /** 60 * struct ovs_net - Per net-namespace data for ovs. 61 * @dps: List of datapaths to enable dumping them all out. 62 * Protected by genl_mutex. 63 */ 64 struct ovs_net { 65 struct list_head dps; 66 }; 67 68 static int ovs_net_id __read_mostly; 69 70 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ) 71 static void rehash_flow_table(struct work_struct *work); 72 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table); 73 74 /** 75 * DOC: Locking: 76 * 77 * Writes to device state (add/remove datapath, port, set operations on vports, 78 * etc.) are protected by RTNL. 79 * 80 * Writes to other state (flow table modifications, set miscellaneous datapath 81 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside 82 * genl_mutex. 83 * 84 * Reads are protected by RCU. 85 * 86 * There are a few special cases (mostly stats) that have their own 87 * synchronization but they nest under all of above and don't interact with 88 * each other. 89 */ 90 91 static struct vport *new_vport(const struct vport_parms *); 92 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *, 93 const struct dp_upcall_info *); 94 static int queue_userspace_packet(struct net *, int dp_ifindex, 95 struct sk_buff *, 96 const struct dp_upcall_info *); 97 98 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */ 99 static struct datapath *get_dp(struct net *net, int dp_ifindex) 100 { 101 struct datapath *dp = NULL; 102 struct net_device *dev; 103 104 rcu_read_lock(); 105 dev = dev_get_by_index_rcu(net, dp_ifindex); 106 if (dev) { 107 struct vport *vport = ovs_internal_dev_get_vport(dev); 108 if (vport) 109 dp = vport->dp; 110 } 111 rcu_read_unlock(); 112 113 return dp; 114 } 115 116 /* Must be called with rcu_read_lock or RTNL lock. */ 117 const char *ovs_dp_name(const struct datapath *dp) 118 { 119 struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL); 120 return vport->ops->get_name(vport); 121 } 122 123 static int get_dpifindex(struct datapath *dp) 124 { 125 struct vport *local; 126 int ifindex; 127 128 rcu_read_lock(); 129 130 local = ovs_vport_rcu(dp, OVSP_LOCAL); 131 if (local) 132 ifindex = local->ops->get_ifindex(local); 133 else 134 ifindex = 0; 135 136 rcu_read_unlock(); 137 138 return ifindex; 139 } 140 141 static void destroy_dp_rcu(struct rcu_head *rcu) 142 { 143 struct datapath *dp = container_of(rcu, struct datapath, rcu); 144 145 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table); 146 free_percpu(dp->stats_percpu); 147 release_net(ovs_dp_get_net(dp)); 148 kfree(dp->ports); 149 kfree(dp); 150 } 151 152 static struct hlist_head *vport_hash_bucket(const struct datapath *dp, 153 u16 port_no) 154 { 155 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; 156 } 157 158 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) 159 { 160 struct vport *vport; 161 struct hlist_head *head; 162 163 head = vport_hash_bucket(dp, port_no); 164 hlist_for_each_entry_rcu(vport, head, dp_hash_node) { 165 if (vport->port_no == port_no) 166 return vport; 167 } 168 return NULL; 169 } 170 171 /* Called with RTNL lock and genl_lock. */ 172 static struct vport *new_vport(const struct vport_parms *parms) 173 { 174 struct vport *vport; 175 176 vport = ovs_vport_add(parms); 177 if (!IS_ERR(vport)) { 178 struct datapath *dp = parms->dp; 179 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); 180 181 hlist_add_head_rcu(&vport->dp_hash_node, head); 182 } 183 184 return vport; 185 } 186 187 /* Called with RTNL lock. */ 188 void ovs_dp_detach_port(struct vport *p) 189 { 190 ASSERT_RTNL(); 191 192 /* First drop references to device. */ 193 hlist_del_rcu(&p->dp_hash_node); 194 195 /* Then destroy it. */ 196 ovs_vport_del(p); 197 } 198 199 /* Must be called with rcu_read_lock. */ 200 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb) 201 { 202 struct datapath *dp = p->dp; 203 struct sw_flow *flow; 204 struct dp_stats_percpu *stats; 205 struct sw_flow_key key; 206 u64 *stats_counter; 207 int error; 208 int key_len; 209 210 stats = this_cpu_ptr(dp->stats_percpu); 211 212 /* Extract flow from 'skb' into 'key'. */ 213 error = ovs_flow_extract(skb, p->port_no, &key, &key_len); 214 if (unlikely(error)) { 215 kfree_skb(skb); 216 return; 217 } 218 219 /* Look up flow. */ 220 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len); 221 if (unlikely(!flow)) { 222 struct dp_upcall_info upcall; 223 224 upcall.cmd = OVS_PACKET_CMD_MISS; 225 upcall.key = &key; 226 upcall.userdata = NULL; 227 upcall.portid = p->upcall_portid; 228 ovs_dp_upcall(dp, skb, &upcall); 229 consume_skb(skb); 230 stats_counter = &stats->n_missed; 231 goto out; 232 } 233 234 OVS_CB(skb)->flow = flow; 235 236 stats_counter = &stats->n_hit; 237 ovs_flow_used(OVS_CB(skb)->flow, skb); 238 ovs_execute_actions(dp, skb); 239 240 out: 241 /* Update datapath statistics. */ 242 u64_stats_update_begin(&stats->sync); 243 (*stats_counter)++; 244 u64_stats_update_end(&stats->sync); 245 } 246 247 static struct genl_family dp_packet_genl_family = { 248 .id = GENL_ID_GENERATE, 249 .hdrsize = sizeof(struct ovs_header), 250 .name = OVS_PACKET_FAMILY, 251 .version = OVS_PACKET_VERSION, 252 .maxattr = OVS_PACKET_ATTR_MAX, 253 .netnsok = true 254 }; 255 256 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, 257 const struct dp_upcall_info *upcall_info) 258 { 259 struct dp_stats_percpu *stats; 260 int dp_ifindex; 261 int err; 262 263 if (upcall_info->portid == 0) { 264 err = -ENOTCONN; 265 goto err; 266 } 267 268 dp_ifindex = get_dpifindex(dp); 269 if (!dp_ifindex) { 270 err = -ENODEV; 271 goto err; 272 } 273 274 if (!skb_is_gso(skb)) 275 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); 276 else 277 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); 278 if (err) 279 goto err; 280 281 return 0; 282 283 err: 284 stats = this_cpu_ptr(dp->stats_percpu); 285 286 u64_stats_update_begin(&stats->sync); 287 stats->n_lost++; 288 u64_stats_update_end(&stats->sync); 289 290 return err; 291 } 292 293 static int queue_gso_packets(struct net *net, int dp_ifindex, 294 struct sk_buff *skb, 295 const struct dp_upcall_info *upcall_info) 296 { 297 unsigned short gso_type = skb_shinfo(skb)->gso_type; 298 struct dp_upcall_info later_info; 299 struct sw_flow_key later_key; 300 struct sk_buff *segs, *nskb; 301 int err; 302 303 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false); 304 if (IS_ERR(segs)) 305 return PTR_ERR(segs); 306 307 /* Queue all of the segments. */ 308 skb = segs; 309 do { 310 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info); 311 if (err) 312 break; 313 314 if (skb == segs && gso_type & SKB_GSO_UDP) { 315 /* The initial flow key extracted by ovs_flow_extract() 316 * in this case is for a first fragment, so we need to 317 * properly mark later fragments. 318 */ 319 later_key = *upcall_info->key; 320 later_key.ip.frag = OVS_FRAG_TYPE_LATER; 321 322 later_info = *upcall_info; 323 later_info.key = &later_key; 324 upcall_info = &later_info; 325 } 326 } while ((skb = skb->next)); 327 328 /* Free all of the segments. */ 329 skb = segs; 330 do { 331 nskb = skb->next; 332 if (err) 333 kfree_skb(skb); 334 else 335 consume_skb(skb); 336 } while ((skb = nskb)); 337 return err; 338 } 339 340 static int queue_userspace_packet(struct net *net, int dp_ifindex, 341 struct sk_buff *skb, 342 const struct dp_upcall_info *upcall_info) 343 { 344 struct ovs_header *upcall; 345 struct sk_buff *nskb = NULL; 346 struct sk_buff *user_skb; /* to be queued to userspace */ 347 struct nlattr *nla; 348 unsigned int len; 349 int err; 350 351 if (vlan_tx_tag_present(skb)) { 352 nskb = skb_clone(skb, GFP_ATOMIC); 353 if (!nskb) 354 return -ENOMEM; 355 356 nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb)); 357 if (!nskb) 358 return -ENOMEM; 359 360 nskb->vlan_tci = 0; 361 skb = nskb; 362 } 363 364 if (nla_attr_size(skb->len) > USHRT_MAX) { 365 err = -EFBIG; 366 goto out; 367 } 368 369 len = sizeof(struct ovs_header); 370 len += nla_total_size(skb->len); 371 len += nla_total_size(FLOW_BUFSIZE); 372 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION) 373 len += nla_total_size(8); 374 375 user_skb = genlmsg_new(len, GFP_ATOMIC); 376 if (!user_skb) { 377 err = -ENOMEM; 378 goto out; 379 } 380 381 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 382 0, upcall_info->cmd); 383 upcall->dp_ifindex = dp_ifindex; 384 385 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY); 386 ovs_flow_to_nlattrs(upcall_info->key, user_skb); 387 nla_nest_end(user_skb, nla); 388 389 if (upcall_info->userdata) 390 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA, 391 nla_get_u64(upcall_info->userdata)); 392 393 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len); 394 395 skb_copy_and_csum_dev(skb, nla_data(nla)); 396 397 err = genlmsg_unicast(net, user_skb, upcall_info->portid); 398 399 out: 400 kfree_skb(nskb); 401 return err; 402 } 403 404 /* Called with genl_mutex. */ 405 static int flush_flows(struct datapath *dp) 406 { 407 struct flow_table *old_table; 408 struct flow_table *new_table; 409 410 old_table = genl_dereference(dp->table); 411 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS); 412 if (!new_table) 413 return -ENOMEM; 414 415 rcu_assign_pointer(dp->table, new_table); 416 417 ovs_flow_tbl_deferred_destroy(old_table); 418 return 0; 419 } 420 421 static int validate_actions(const struct nlattr *attr, 422 const struct sw_flow_key *key, int depth); 423 424 static int validate_sample(const struct nlattr *attr, 425 const struct sw_flow_key *key, int depth) 426 { 427 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1]; 428 const struct nlattr *probability, *actions; 429 const struct nlattr *a; 430 int rem; 431 432 memset(attrs, 0, sizeof(attrs)); 433 nla_for_each_nested(a, attr, rem) { 434 int type = nla_type(a); 435 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type]) 436 return -EINVAL; 437 attrs[type] = a; 438 } 439 if (rem) 440 return -EINVAL; 441 442 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY]; 443 if (!probability || nla_len(probability) != sizeof(u32)) 444 return -EINVAL; 445 446 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS]; 447 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) 448 return -EINVAL; 449 return validate_actions(actions, key, depth + 1); 450 } 451 452 static int validate_tp_port(const struct sw_flow_key *flow_key) 453 { 454 if (flow_key->eth.type == htons(ETH_P_IP)) { 455 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst) 456 return 0; 457 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) { 458 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst) 459 return 0; 460 } 461 462 return -EINVAL; 463 } 464 465 static int validate_set(const struct nlattr *a, 466 const struct sw_flow_key *flow_key) 467 { 468 const struct nlattr *ovs_key = nla_data(a); 469 int key_type = nla_type(ovs_key); 470 471 /* There can be only one key in a action */ 472 if (nla_total_size(nla_len(ovs_key)) != nla_len(a)) 473 return -EINVAL; 474 475 if (key_type > OVS_KEY_ATTR_MAX || 476 nla_len(ovs_key) != ovs_key_lens[key_type]) 477 return -EINVAL; 478 479 switch (key_type) { 480 const struct ovs_key_ipv4 *ipv4_key; 481 const struct ovs_key_ipv6 *ipv6_key; 482 483 case OVS_KEY_ATTR_PRIORITY: 484 case OVS_KEY_ATTR_SKB_MARK: 485 case OVS_KEY_ATTR_ETHERNET: 486 break; 487 488 case OVS_KEY_ATTR_IPV4: 489 if (flow_key->eth.type != htons(ETH_P_IP)) 490 return -EINVAL; 491 492 if (!flow_key->ip.proto) 493 return -EINVAL; 494 495 ipv4_key = nla_data(ovs_key); 496 if (ipv4_key->ipv4_proto != flow_key->ip.proto) 497 return -EINVAL; 498 499 if (ipv4_key->ipv4_frag != flow_key->ip.frag) 500 return -EINVAL; 501 502 break; 503 504 case OVS_KEY_ATTR_IPV6: 505 if (flow_key->eth.type != htons(ETH_P_IPV6)) 506 return -EINVAL; 507 508 if (!flow_key->ip.proto) 509 return -EINVAL; 510 511 ipv6_key = nla_data(ovs_key); 512 if (ipv6_key->ipv6_proto != flow_key->ip.proto) 513 return -EINVAL; 514 515 if (ipv6_key->ipv6_frag != flow_key->ip.frag) 516 return -EINVAL; 517 518 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000) 519 return -EINVAL; 520 521 break; 522 523 case OVS_KEY_ATTR_TCP: 524 if (flow_key->ip.proto != IPPROTO_TCP) 525 return -EINVAL; 526 527 return validate_tp_port(flow_key); 528 529 case OVS_KEY_ATTR_UDP: 530 if (flow_key->ip.proto != IPPROTO_UDP) 531 return -EINVAL; 532 533 return validate_tp_port(flow_key); 534 535 default: 536 return -EINVAL; 537 } 538 539 return 0; 540 } 541 542 static int validate_userspace(const struct nlattr *attr) 543 { 544 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = { 545 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 }, 546 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 }, 547 }; 548 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1]; 549 int error; 550 551 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, 552 attr, userspace_policy); 553 if (error) 554 return error; 555 556 if (!a[OVS_USERSPACE_ATTR_PID] || 557 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID])) 558 return -EINVAL; 559 560 return 0; 561 } 562 563 static int validate_actions(const struct nlattr *attr, 564 const struct sw_flow_key *key, int depth) 565 { 566 const struct nlattr *a; 567 int rem, err; 568 569 if (depth >= SAMPLE_ACTION_DEPTH) 570 return -EOVERFLOW; 571 572 nla_for_each_nested(a, attr, rem) { 573 /* Expected argument lengths, (u32)-1 for variable length. */ 574 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = { 575 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32), 576 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1, 577 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan), 578 [OVS_ACTION_ATTR_POP_VLAN] = 0, 579 [OVS_ACTION_ATTR_SET] = (u32)-1, 580 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1 581 }; 582 const struct ovs_action_push_vlan *vlan; 583 int type = nla_type(a); 584 585 if (type > OVS_ACTION_ATTR_MAX || 586 (action_lens[type] != nla_len(a) && 587 action_lens[type] != (u32)-1)) 588 return -EINVAL; 589 590 switch (type) { 591 case OVS_ACTION_ATTR_UNSPEC: 592 return -EINVAL; 593 594 case OVS_ACTION_ATTR_USERSPACE: 595 err = validate_userspace(a); 596 if (err) 597 return err; 598 break; 599 600 case OVS_ACTION_ATTR_OUTPUT: 601 if (nla_get_u32(a) >= DP_MAX_PORTS) 602 return -EINVAL; 603 break; 604 605 606 case OVS_ACTION_ATTR_POP_VLAN: 607 break; 608 609 case OVS_ACTION_ATTR_PUSH_VLAN: 610 vlan = nla_data(a); 611 if (vlan->vlan_tpid != htons(ETH_P_8021Q)) 612 return -EINVAL; 613 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT))) 614 return -EINVAL; 615 break; 616 617 case OVS_ACTION_ATTR_SET: 618 err = validate_set(a, key); 619 if (err) 620 return err; 621 break; 622 623 case OVS_ACTION_ATTR_SAMPLE: 624 err = validate_sample(a, key, depth); 625 if (err) 626 return err; 627 break; 628 629 default: 630 return -EINVAL; 631 } 632 } 633 634 if (rem > 0) 635 return -EINVAL; 636 637 return 0; 638 } 639 640 static void clear_stats(struct sw_flow *flow) 641 { 642 flow->used = 0; 643 flow->tcp_flags = 0; 644 flow->packet_count = 0; 645 flow->byte_count = 0; 646 } 647 648 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) 649 { 650 struct ovs_header *ovs_header = info->userhdr; 651 struct nlattr **a = info->attrs; 652 struct sw_flow_actions *acts; 653 struct sk_buff *packet; 654 struct sw_flow *flow; 655 struct datapath *dp; 656 struct ethhdr *eth; 657 int len; 658 int err; 659 int key_len; 660 661 err = -EINVAL; 662 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || 663 !a[OVS_PACKET_ATTR_ACTIONS] || 664 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN) 665 goto err; 666 667 len = nla_len(a[OVS_PACKET_ATTR_PACKET]); 668 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); 669 err = -ENOMEM; 670 if (!packet) 671 goto err; 672 skb_reserve(packet, NET_IP_ALIGN); 673 674 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len); 675 676 skb_reset_mac_header(packet); 677 eth = eth_hdr(packet); 678 679 /* Normally, setting the skb 'protocol' field would be handled by a 680 * call to eth_type_trans(), but it assumes there's a sending 681 * device, which we may not have. */ 682 if (ntohs(eth->h_proto) >= 1536) 683 packet->protocol = eth->h_proto; 684 else 685 packet->protocol = htons(ETH_P_802_2); 686 687 /* Build an sw_flow for sending this packet. */ 688 flow = ovs_flow_alloc(); 689 err = PTR_ERR(flow); 690 if (IS_ERR(flow)) 691 goto err_kfree_skb; 692 693 err = ovs_flow_extract(packet, -1, &flow->key, &key_len); 694 if (err) 695 goto err_flow_free; 696 697 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority, 698 &flow->key.phy.skb_mark, 699 &flow->key.phy.in_port, 700 a[OVS_PACKET_ATTR_KEY]); 701 if (err) 702 goto err_flow_free; 703 704 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0); 705 if (err) 706 goto err_flow_free; 707 708 flow->hash = ovs_flow_hash(&flow->key, key_len); 709 710 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]); 711 err = PTR_ERR(acts); 712 if (IS_ERR(acts)) 713 goto err_flow_free; 714 rcu_assign_pointer(flow->sf_acts, acts); 715 716 OVS_CB(packet)->flow = flow; 717 packet->priority = flow->key.phy.priority; 718 packet->mark = flow->key.phy.skb_mark; 719 720 rcu_read_lock(); 721 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 722 err = -ENODEV; 723 if (!dp) 724 goto err_unlock; 725 726 local_bh_disable(); 727 err = ovs_execute_actions(dp, packet); 728 local_bh_enable(); 729 rcu_read_unlock(); 730 731 ovs_flow_free(flow); 732 return err; 733 734 err_unlock: 735 rcu_read_unlock(); 736 err_flow_free: 737 ovs_flow_free(flow); 738 err_kfree_skb: 739 kfree_skb(packet); 740 err: 741 return err; 742 } 743 744 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { 745 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC }, 746 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, 747 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, 748 }; 749 750 static struct genl_ops dp_packet_genl_ops[] = { 751 { .cmd = OVS_PACKET_CMD_EXECUTE, 752 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 753 .policy = packet_policy, 754 .doit = ovs_packet_cmd_execute 755 } 756 }; 757 758 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats) 759 { 760 int i; 761 struct flow_table *table = genl_dereference(dp->table); 762 763 stats->n_flows = ovs_flow_tbl_count(table); 764 765 stats->n_hit = stats->n_missed = stats->n_lost = 0; 766 for_each_possible_cpu(i) { 767 const struct dp_stats_percpu *percpu_stats; 768 struct dp_stats_percpu local_stats; 769 unsigned int start; 770 771 percpu_stats = per_cpu_ptr(dp->stats_percpu, i); 772 773 do { 774 start = u64_stats_fetch_begin_bh(&percpu_stats->sync); 775 local_stats = *percpu_stats; 776 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start)); 777 778 stats->n_hit += local_stats.n_hit; 779 stats->n_missed += local_stats.n_missed; 780 stats->n_lost += local_stats.n_lost; 781 } 782 } 783 784 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { 785 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, 786 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, 787 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, 788 }; 789 790 static struct genl_family dp_flow_genl_family = { 791 .id = GENL_ID_GENERATE, 792 .hdrsize = sizeof(struct ovs_header), 793 .name = OVS_FLOW_FAMILY, 794 .version = OVS_FLOW_VERSION, 795 .maxattr = OVS_FLOW_ATTR_MAX, 796 .netnsok = true 797 }; 798 799 static struct genl_multicast_group ovs_dp_flow_multicast_group = { 800 .name = OVS_FLOW_MCGROUP 801 }; 802 803 /* Called with genl_lock. */ 804 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp, 805 struct sk_buff *skb, u32 portid, 806 u32 seq, u32 flags, u8 cmd) 807 { 808 const int skb_orig_len = skb->len; 809 const struct sw_flow_actions *sf_acts; 810 struct ovs_flow_stats stats; 811 struct ovs_header *ovs_header; 812 struct nlattr *nla; 813 unsigned long used; 814 u8 tcp_flags; 815 int err; 816 817 sf_acts = rcu_dereference_protected(flow->sf_acts, 818 lockdep_genl_is_held()); 819 820 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd); 821 if (!ovs_header) 822 return -EMSGSIZE; 823 824 ovs_header->dp_ifindex = get_dpifindex(dp); 825 826 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY); 827 if (!nla) 828 goto nla_put_failure; 829 err = ovs_flow_to_nlattrs(&flow->key, skb); 830 if (err) 831 goto error; 832 nla_nest_end(skb, nla); 833 834 spin_lock_bh(&flow->lock); 835 used = flow->used; 836 stats.n_packets = flow->packet_count; 837 stats.n_bytes = flow->byte_count; 838 tcp_flags = flow->tcp_flags; 839 spin_unlock_bh(&flow->lock); 840 841 if (used && 842 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) 843 goto nla_put_failure; 844 845 if (stats.n_packets && 846 nla_put(skb, OVS_FLOW_ATTR_STATS, 847 sizeof(struct ovs_flow_stats), &stats)) 848 goto nla_put_failure; 849 850 if (tcp_flags && 851 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags)) 852 goto nla_put_failure; 853 854 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if 855 * this is the first flow to be dumped into 'skb'. This is unusual for 856 * Netlink but individual action lists can be longer than 857 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. 858 * The userspace caller can always fetch the actions separately if it 859 * really wants them. (Most userspace callers in fact don't care.) 860 * 861 * This can only fail for dump operations because the skb is always 862 * properly sized for single flows. 863 */ 864 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len, 865 sf_acts->actions); 866 if (err < 0 && skb_orig_len) 867 goto error; 868 869 return genlmsg_end(skb, ovs_header); 870 871 nla_put_failure: 872 err = -EMSGSIZE; 873 error: 874 genlmsg_cancel(skb, ovs_header); 875 return err; 876 } 877 878 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow) 879 { 880 const struct sw_flow_actions *sf_acts; 881 int len; 882 883 sf_acts = rcu_dereference_protected(flow->sf_acts, 884 lockdep_genl_is_held()); 885 886 /* OVS_FLOW_ATTR_KEY */ 887 len = nla_total_size(FLOW_BUFSIZE); 888 /* OVS_FLOW_ATTR_ACTIONS */ 889 len += nla_total_size(sf_acts->actions_len); 890 /* OVS_FLOW_ATTR_STATS */ 891 len += nla_total_size(sizeof(struct ovs_flow_stats)); 892 /* OVS_FLOW_ATTR_TCP_FLAGS */ 893 len += nla_total_size(1); 894 /* OVS_FLOW_ATTR_USED */ 895 len += nla_total_size(8); 896 897 len += NLMSG_ALIGN(sizeof(struct ovs_header)); 898 899 return genlmsg_new(len, GFP_KERNEL); 900 } 901 902 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, 903 struct datapath *dp, 904 u32 portid, u32 seq, u8 cmd) 905 { 906 struct sk_buff *skb; 907 int retval; 908 909 skb = ovs_flow_cmd_alloc_info(flow); 910 if (!skb) 911 return ERR_PTR(-ENOMEM); 912 913 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd); 914 BUG_ON(retval < 0); 915 return skb; 916 } 917 918 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info) 919 { 920 struct nlattr **a = info->attrs; 921 struct ovs_header *ovs_header = info->userhdr; 922 struct sw_flow_key key; 923 struct sw_flow *flow; 924 struct sk_buff *reply; 925 struct datapath *dp; 926 struct flow_table *table; 927 int error; 928 int key_len; 929 930 /* Extract key. */ 931 error = -EINVAL; 932 if (!a[OVS_FLOW_ATTR_KEY]) 933 goto error; 934 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); 935 if (error) 936 goto error; 937 938 /* Validate actions. */ 939 if (a[OVS_FLOW_ATTR_ACTIONS]) { 940 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0); 941 if (error) 942 goto error; 943 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) { 944 error = -EINVAL; 945 goto error; 946 } 947 948 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 949 error = -ENODEV; 950 if (!dp) 951 goto error; 952 953 table = genl_dereference(dp->table); 954 flow = ovs_flow_tbl_lookup(table, &key, key_len); 955 if (!flow) { 956 struct sw_flow_actions *acts; 957 958 /* Bail out if we're not allowed to create a new flow. */ 959 error = -ENOENT; 960 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET) 961 goto error; 962 963 /* Expand table, if necessary, to make room. */ 964 if (ovs_flow_tbl_need_to_expand(table)) { 965 struct flow_table *new_table; 966 967 new_table = ovs_flow_tbl_expand(table); 968 if (!IS_ERR(new_table)) { 969 rcu_assign_pointer(dp->table, new_table); 970 ovs_flow_tbl_deferred_destroy(table); 971 table = genl_dereference(dp->table); 972 } 973 } 974 975 /* Allocate flow. */ 976 flow = ovs_flow_alloc(); 977 if (IS_ERR(flow)) { 978 error = PTR_ERR(flow); 979 goto error; 980 } 981 flow->key = key; 982 clear_stats(flow); 983 984 /* Obtain actions. */ 985 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]); 986 error = PTR_ERR(acts); 987 if (IS_ERR(acts)) 988 goto error_free_flow; 989 rcu_assign_pointer(flow->sf_acts, acts); 990 991 /* Put flow in bucket. */ 992 flow->hash = ovs_flow_hash(&key, key_len); 993 ovs_flow_tbl_insert(table, flow); 994 995 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, 996 info->snd_seq, 997 OVS_FLOW_CMD_NEW); 998 } else { 999 /* We found a matching flow. */ 1000 struct sw_flow_actions *old_acts; 1001 struct nlattr *acts_attrs; 1002 1003 /* Bail out if we're not allowed to modify an existing flow. 1004 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL 1005 * because Generic Netlink treats the latter as a dump 1006 * request. We also accept NLM_F_EXCL in case that bug ever 1007 * gets fixed. 1008 */ 1009 error = -EEXIST; 1010 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW && 1011 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) 1012 goto error; 1013 1014 /* Update actions. */ 1015 old_acts = rcu_dereference_protected(flow->sf_acts, 1016 lockdep_genl_is_held()); 1017 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS]; 1018 if (acts_attrs && 1019 (old_acts->actions_len != nla_len(acts_attrs) || 1020 memcmp(old_acts->actions, nla_data(acts_attrs), 1021 old_acts->actions_len))) { 1022 struct sw_flow_actions *new_acts; 1023 1024 new_acts = ovs_flow_actions_alloc(acts_attrs); 1025 error = PTR_ERR(new_acts); 1026 if (IS_ERR(new_acts)) 1027 goto error; 1028 1029 rcu_assign_pointer(flow->sf_acts, new_acts); 1030 ovs_flow_deferred_free_acts(old_acts); 1031 } 1032 1033 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, 1034 info->snd_seq, OVS_FLOW_CMD_NEW); 1035 1036 /* Clear stats. */ 1037 if (a[OVS_FLOW_ATTR_CLEAR]) { 1038 spin_lock_bh(&flow->lock); 1039 clear_stats(flow); 1040 spin_unlock_bh(&flow->lock); 1041 } 1042 } 1043 1044 if (!IS_ERR(reply)) 1045 genl_notify(reply, genl_info_net(info), info->snd_portid, 1046 ovs_dp_flow_multicast_group.id, info->nlhdr, 1047 GFP_KERNEL); 1048 else 1049 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 1050 ovs_dp_flow_multicast_group.id, PTR_ERR(reply)); 1051 return 0; 1052 1053 error_free_flow: 1054 ovs_flow_free(flow); 1055 error: 1056 return error; 1057 } 1058 1059 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) 1060 { 1061 struct nlattr **a = info->attrs; 1062 struct ovs_header *ovs_header = info->userhdr; 1063 struct sw_flow_key key; 1064 struct sk_buff *reply; 1065 struct sw_flow *flow; 1066 struct datapath *dp; 1067 struct flow_table *table; 1068 int err; 1069 int key_len; 1070 1071 if (!a[OVS_FLOW_ATTR_KEY]) 1072 return -EINVAL; 1073 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); 1074 if (err) 1075 return err; 1076 1077 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1078 if (!dp) 1079 return -ENODEV; 1080 1081 table = genl_dereference(dp->table); 1082 flow = ovs_flow_tbl_lookup(table, &key, key_len); 1083 if (!flow) 1084 return -ENOENT; 1085 1086 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, 1087 info->snd_seq, OVS_FLOW_CMD_NEW); 1088 if (IS_ERR(reply)) 1089 return PTR_ERR(reply); 1090 1091 return genlmsg_reply(reply, info); 1092 } 1093 1094 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) 1095 { 1096 struct nlattr **a = info->attrs; 1097 struct ovs_header *ovs_header = info->userhdr; 1098 struct sw_flow_key key; 1099 struct sk_buff *reply; 1100 struct sw_flow *flow; 1101 struct datapath *dp; 1102 struct flow_table *table; 1103 int err; 1104 int key_len; 1105 1106 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1107 if (!dp) 1108 return -ENODEV; 1109 1110 if (!a[OVS_FLOW_ATTR_KEY]) 1111 return flush_flows(dp); 1112 1113 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); 1114 if (err) 1115 return err; 1116 1117 table = genl_dereference(dp->table); 1118 flow = ovs_flow_tbl_lookup(table, &key, key_len); 1119 if (!flow) 1120 return -ENOENT; 1121 1122 reply = ovs_flow_cmd_alloc_info(flow); 1123 if (!reply) 1124 return -ENOMEM; 1125 1126 ovs_flow_tbl_remove(table, flow); 1127 1128 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid, 1129 info->snd_seq, 0, OVS_FLOW_CMD_DEL); 1130 BUG_ON(err < 0); 1131 1132 ovs_flow_deferred_free(flow); 1133 1134 genl_notify(reply, genl_info_net(info), info->snd_portid, 1135 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL); 1136 return 0; 1137 } 1138 1139 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1140 { 1141 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 1142 struct datapath *dp; 1143 struct flow_table *table; 1144 1145 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1146 if (!dp) 1147 return -ENODEV; 1148 1149 table = genl_dereference(dp->table); 1150 1151 for (;;) { 1152 struct sw_flow *flow; 1153 u32 bucket, obj; 1154 1155 bucket = cb->args[0]; 1156 obj = cb->args[1]; 1157 flow = ovs_flow_tbl_next(table, &bucket, &obj); 1158 if (!flow) 1159 break; 1160 1161 if (ovs_flow_cmd_fill_info(flow, dp, skb, 1162 NETLINK_CB(cb->skb).portid, 1163 cb->nlh->nlmsg_seq, NLM_F_MULTI, 1164 OVS_FLOW_CMD_NEW) < 0) 1165 break; 1166 1167 cb->args[0] = bucket; 1168 cb->args[1] = obj; 1169 } 1170 return skb->len; 1171 } 1172 1173 static struct genl_ops dp_flow_genl_ops[] = { 1174 { .cmd = OVS_FLOW_CMD_NEW, 1175 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1176 .policy = flow_policy, 1177 .doit = ovs_flow_cmd_new_or_set 1178 }, 1179 { .cmd = OVS_FLOW_CMD_DEL, 1180 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1181 .policy = flow_policy, 1182 .doit = ovs_flow_cmd_del 1183 }, 1184 { .cmd = OVS_FLOW_CMD_GET, 1185 .flags = 0, /* OK for unprivileged users. */ 1186 .policy = flow_policy, 1187 .doit = ovs_flow_cmd_get, 1188 .dumpit = ovs_flow_cmd_dump 1189 }, 1190 { .cmd = OVS_FLOW_CMD_SET, 1191 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1192 .policy = flow_policy, 1193 .doit = ovs_flow_cmd_new_or_set, 1194 }, 1195 }; 1196 1197 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { 1198 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, 1199 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, 1200 }; 1201 1202 static struct genl_family dp_datapath_genl_family = { 1203 .id = GENL_ID_GENERATE, 1204 .hdrsize = sizeof(struct ovs_header), 1205 .name = OVS_DATAPATH_FAMILY, 1206 .version = OVS_DATAPATH_VERSION, 1207 .maxattr = OVS_DP_ATTR_MAX, 1208 .netnsok = true 1209 }; 1210 1211 static struct genl_multicast_group ovs_dp_datapath_multicast_group = { 1212 .name = OVS_DATAPATH_MCGROUP 1213 }; 1214 1215 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, 1216 u32 portid, u32 seq, u32 flags, u8 cmd) 1217 { 1218 struct ovs_header *ovs_header; 1219 struct ovs_dp_stats dp_stats; 1220 int err; 1221 1222 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, 1223 flags, cmd); 1224 if (!ovs_header) 1225 goto error; 1226 1227 ovs_header->dp_ifindex = get_dpifindex(dp); 1228 1229 rcu_read_lock(); 1230 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); 1231 rcu_read_unlock(); 1232 if (err) 1233 goto nla_put_failure; 1234 1235 get_dp_stats(dp, &dp_stats); 1236 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats)) 1237 goto nla_put_failure; 1238 1239 return genlmsg_end(skb, ovs_header); 1240 1241 nla_put_failure: 1242 genlmsg_cancel(skb, ovs_header); 1243 error: 1244 return -EMSGSIZE; 1245 } 1246 1247 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid, 1248 u32 seq, u8 cmd) 1249 { 1250 struct sk_buff *skb; 1251 int retval; 1252 1253 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1254 if (!skb) 1255 return ERR_PTR(-ENOMEM); 1256 1257 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd); 1258 if (retval < 0) { 1259 kfree_skb(skb); 1260 return ERR_PTR(retval); 1261 } 1262 return skb; 1263 } 1264 1265 /* Called with genl_mutex and optionally with RTNL lock also. */ 1266 static struct datapath *lookup_datapath(struct net *net, 1267 struct ovs_header *ovs_header, 1268 struct nlattr *a[OVS_DP_ATTR_MAX + 1]) 1269 { 1270 struct datapath *dp; 1271 1272 if (!a[OVS_DP_ATTR_NAME]) 1273 dp = get_dp(net, ovs_header->dp_ifindex); 1274 else { 1275 struct vport *vport; 1276 1277 rcu_read_lock(); 1278 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); 1279 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; 1280 rcu_read_unlock(); 1281 } 1282 return dp ? dp : ERR_PTR(-ENODEV); 1283 } 1284 1285 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) 1286 { 1287 struct nlattr **a = info->attrs; 1288 struct vport_parms parms; 1289 struct sk_buff *reply; 1290 struct datapath *dp; 1291 struct vport *vport; 1292 struct ovs_net *ovs_net; 1293 int err, i; 1294 1295 err = -EINVAL; 1296 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) 1297 goto err; 1298 1299 rtnl_lock(); 1300 1301 err = -ENOMEM; 1302 dp = kzalloc(sizeof(*dp), GFP_KERNEL); 1303 if (dp == NULL) 1304 goto err_unlock_rtnl; 1305 1306 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); 1307 1308 /* Allocate table. */ 1309 err = -ENOMEM; 1310 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS)); 1311 if (!dp->table) 1312 goto err_free_dp; 1313 1314 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu); 1315 if (!dp->stats_percpu) { 1316 err = -ENOMEM; 1317 goto err_destroy_table; 1318 } 1319 1320 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), 1321 GFP_KERNEL); 1322 if (!dp->ports) { 1323 err = -ENOMEM; 1324 goto err_destroy_percpu; 1325 } 1326 1327 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) 1328 INIT_HLIST_HEAD(&dp->ports[i]); 1329 1330 /* Set up our datapath device. */ 1331 parms.name = nla_data(a[OVS_DP_ATTR_NAME]); 1332 parms.type = OVS_VPORT_TYPE_INTERNAL; 1333 parms.options = NULL; 1334 parms.dp = dp; 1335 parms.port_no = OVSP_LOCAL; 1336 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]); 1337 1338 vport = new_vport(&parms); 1339 if (IS_ERR(vport)) { 1340 err = PTR_ERR(vport); 1341 if (err == -EBUSY) 1342 err = -EEXIST; 1343 1344 goto err_destroy_ports_array; 1345 } 1346 1347 reply = ovs_dp_cmd_build_info(dp, info->snd_portid, 1348 info->snd_seq, OVS_DP_CMD_NEW); 1349 err = PTR_ERR(reply); 1350 if (IS_ERR(reply)) 1351 goto err_destroy_local_port; 1352 1353 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); 1354 list_add_tail(&dp->list_node, &ovs_net->dps); 1355 rtnl_unlock(); 1356 1357 genl_notify(reply, genl_info_net(info), info->snd_portid, 1358 ovs_dp_datapath_multicast_group.id, info->nlhdr, 1359 GFP_KERNEL); 1360 return 0; 1361 1362 err_destroy_local_port: 1363 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL)); 1364 err_destroy_ports_array: 1365 kfree(dp->ports); 1366 err_destroy_percpu: 1367 free_percpu(dp->stats_percpu); 1368 err_destroy_table: 1369 ovs_flow_tbl_destroy(genl_dereference(dp->table)); 1370 err_free_dp: 1371 release_net(ovs_dp_get_net(dp)); 1372 kfree(dp); 1373 err_unlock_rtnl: 1374 rtnl_unlock(); 1375 err: 1376 return err; 1377 } 1378 1379 /* Called with genl_mutex. */ 1380 static void __dp_destroy(struct datapath *dp) 1381 { 1382 int i; 1383 1384 rtnl_lock(); 1385 1386 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { 1387 struct vport *vport; 1388 struct hlist_node *n; 1389 1390 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) 1391 if (vport->port_no != OVSP_LOCAL) 1392 ovs_dp_detach_port(vport); 1393 } 1394 1395 list_del(&dp->list_node); 1396 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL)); 1397 1398 /* rtnl_unlock() will wait until all the references to devices that 1399 * are pending unregistration have been dropped. We do it here to 1400 * ensure that any internal devices (which contain DP pointers) are 1401 * fully destroyed before freeing the datapath. 1402 */ 1403 rtnl_unlock(); 1404 1405 call_rcu(&dp->rcu, destroy_dp_rcu); 1406 } 1407 1408 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) 1409 { 1410 struct sk_buff *reply; 1411 struct datapath *dp; 1412 int err; 1413 1414 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1415 err = PTR_ERR(dp); 1416 if (IS_ERR(dp)) 1417 return err; 1418 1419 reply = ovs_dp_cmd_build_info(dp, info->snd_portid, 1420 info->snd_seq, OVS_DP_CMD_DEL); 1421 err = PTR_ERR(reply); 1422 if (IS_ERR(reply)) 1423 return err; 1424 1425 __dp_destroy(dp); 1426 1427 genl_notify(reply, genl_info_net(info), info->snd_portid, 1428 ovs_dp_datapath_multicast_group.id, info->nlhdr, 1429 GFP_KERNEL); 1430 1431 return 0; 1432 } 1433 1434 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) 1435 { 1436 struct sk_buff *reply; 1437 struct datapath *dp; 1438 int err; 1439 1440 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1441 if (IS_ERR(dp)) 1442 return PTR_ERR(dp); 1443 1444 reply = ovs_dp_cmd_build_info(dp, info->snd_portid, 1445 info->snd_seq, OVS_DP_CMD_NEW); 1446 if (IS_ERR(reply)) { 1447 err = PTR_ERR(reply); 1448 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 1449 ovs_dp_datapath_multicast_group.id, err); 1450 return 0; 1451 } 1452 1453 genl_notify(reply, genl_info_net(info), info->snd_portid, 1454 ovs_dp_datapath_multicast_group.id, info->nlhdr, 1455 GFP_KERNEL); 1456 1457 return 0; 1458 } 1459 1460 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) 1461 { 1462 struct sk_buff *reply; 1463 struct datapath *dp; 1464 1465 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); 1466 if (IS_ERR(dp)) 1467 return PTR_ERR(dp); 1468 1469 reply = ovs_dp_cmd_build_info(dp, info->snd_portid, 1470 info->snd_seq, OVS_DP_CMD_NEW); 1471 if (IS_ERR(reply)) 1472 return PTR_ERR(reply); 1473 1474 return genlmsg_reply(reply, info); 1475 } 1476 1477 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1478 { 1479 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); 1480 struct datapath *dp; 1481 int skip = cb->args[0]; 1482 int i = 0; 1483 1484 list_for_each_entry(dp, &ovs_net->dps, list_node) { 1485 if (i >= skip && 1486 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, 1487 cb->nlh->nlmsg_seq, NLM_F_MULTI, 1488 OVS_DP_CMD_NEW) < 0) 1489 break; 1490 i++; 1491 } 1492 1493 cb->args[0] = i; 1494 1495 return skb->len; 1496 } 1497 1498 static struct genl_ops dp_datapath_genl_ops[] = { 1499 { .cmd = OVS_DP_CMD_NEW, 1500 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1501 .policy = datapath_policy, 1502 .doit = ovs_dp_cmd_new 1503 }, 1504 { .cmd = OVS_DP_CMD_DEL, 1505 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1506 .policy = datapath_policy, 1507 .doit = ovs_dp_cmd_del 1508 }, 1509 { .cmd = OVS_DP_CMD_GET, 1510 .flags = 0, /* OK for unprivileged users. */ 1511 .policy = datapath_policy, 1512 .doit = ovs_dp_cmd_get, 1513 .dumpit = ovs_dp_cmd_dump 1514 }, 1515 { .cmd = OVS_DP_CMD_SET, 1516 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1517 .policy = datapath_policy, 1518 .doit = ovs_dp_cmd_set, 1519 }, 1520 }; 1521 1522 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { 1523 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, 1524 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, 1525 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, 1526 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, 1527 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, 1528 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, 1529 }; 1530 1531 static struct genl_family dp_vport_genl_family = { 1532 .id = GENL_ID_GENERATE, 1533 .hdrsize = sizeof(struct ovs_header), 1534 .name = OVS_VPORT_FAMILY, 1535 .version = OVS_VPORT_VERSION, 1536 .maxattr = OVS_VPORT_ATTR_MAX, 1537 .netnsok = true 1538 }; 1539 1540 struct genl_multicast_group ovs_dp_vport_multicast_group = { 1541 .name = OVS_VPORT_MCGROUP 1542 }; 1543 1544 /* Called with RTNL lock or RCU read lock. */ 1545 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, 1546 u32 portid, u32 seq, u32 flags, u8 cmd) 1547 { 1548 struct ovs_header *ovs_header; 1549 struct ovs_vport_stats vport_stats; 1550 int err; 1551 1552 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, 1553 flags, cmd); 1554 if (!ovs_header) 1555 return -EMSGSIZE; 1556 1557 ovs_header->dp_ifindex = get_dpifindex(vport->dp); 1558 1559 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || 1560 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || 1561 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) || 1562 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid)) 1563 goto nla_put_failure; 1564 1565 ovs_vport_get_stats(vport, &vport_stats); 1566 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), 1567 &vport_stats)) 1568 goto nla_put_failure; 1569 1570 err = ovs_vport_get_options(vport, skb); 1571 if (err == -EMSGSIZE) 1572 goto error; 1573 1574 return genlmsg_end(skb, ovs_header); 1575 1576 nla_put_failure: 1577 err = -EMSGSIZE; 1578 error: 1579 genlmsg_cancel(skb, ovs_header); 1580 return err; 1581 } 1582 1583 /* Called with RTNL lock or RCU read lock. */ 1584 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, 1585 u32 seq, u8 cmd) 1586 { 1587 struct sk_buff *skb; 1588 int retval; 1589 1590 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); 1591 if (!skb) 1592 return ERR_PTR(-ENOMEM); 1593 1594 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); 1595 if (retval < 0) { 1596 kfree_skb(skb); 1597 return ERR_PTR(retval); 1598 } 1599 return skb; 1600 } 1601 1602 /* Called with RTNL lock or RCU read lock. */ 1603 static struct vport *lookup_vport(struct net *net, 1604 struct ovs_header *ovs_header, 1605 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) 1606 { 1607 struct datapath *dp; 1608 struct vport *vport; 1609 1610 if (a[OVS_VPORT_ATTR_NAME]) { 1611 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); 1612 if (!vport) 1613 return ERR_PTR(-ENODEV); 1614 if (ovs_header->dp_ifindex && 1615 ovs_header->dp_ifindex != get_dpifindex(vport->dp)) 1616 return ERR_PTR(-ENODEV); 1617 return vport; 1618 } else if (a[OVS_VPORT_ATTR_PORT_NO]) { 1619 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); 1620 1621 if (port_no >= DP_MAX_PORTS) 1622 return ERR_PTR(-EFBIG); 1623 1624 dp = get_dp(net, ovs_header->dp_ifindex); 1625 if (!dp) 1626 return ERR_PTR(-ENODEV); 1627 1628 vport = ovs_vport_rtnl_rcu(dp, port_no); 1629 if (!vport) 1630 return ERR_PTR(-ENOENT); 1631 return vport; 1632 } else 1633 return ERR_PTR(-EINVAL); 1634 } 1635 1636 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) 1637 { 1638 struct nlattr **a = info->attrs; 1639 struct ovs_header *ovs_header = info->userhdr; 1640 struct vport_parms parms; 1641 struct sk_buff *reply; 1642 struct vport *vport; 1643 struct datapath *dp; 1644 u32 port_no; 1645 int err; 1646 1647 err = -EINVAL; 1648 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || 1649 !a[OVS_VPORT_ATTR_UPCALL_PID]) 1650 goto exit; 1651 1652 rtnl_lock(); 1653 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1654 err = -ENODEV; 1655 if (!dp) 1656 goto exit_unlock; 1657 1658 if (a[OVS_VPORT_ATTR_PORT_NO]) { 1659 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); 1660 1661 err = -EFBIG; 1662 if (port_no >= DP_MAX_PORTS) 1663 goto exit_unlock; 1664 1665 vport = ovs_vport_rtnl_rcu(dp, port_no); 1666 err = -EBUSY; 1667 if (vport) 1668 goto exit_unlock; 1669 } else { 1670 for (port_no = 1; ; port_no++) { 1671 if (port_no >= DP_MAX_PORTS) { 1672 err = -EFBIG; 1673 goto exit_unlock; 1674 } 1675 vport = ovs_vport_rtnl(dp, port_no); 1676 if (!vport) 1677 break; 1678 } 1679 } 1680 1681 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); 1682 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); 1683 parms.options = a[OVS_VPORT_ATTR_OPTIONS]; 1684 parms.dp = dp; 1685 parms.port_no = port_no; 1686 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); 1687 1688 vport = new_vport(&parms); 1689 err = PTR_ERR(vport); 1690 if (IS_ERR(vport)) 1691 goto exit_unlock; 1692 1693 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, 1694 OVS_VPORT_CMD_NEW); 1695 if (IS_ERR(reply)) { 1696 err = PTR_ERR(reply); 1697 ovs_dp_detach_port(vport); 1698 goto exit_unlock; 1699 } 1700 genl_notify(reply, genl_info_net(info), info->snd_portid, 1701 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL); 1702 1703 exit_unlock: 1704 rtnl_unlock(); 1705 exit: 1706 return err; 1707 } 1708 1709 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) 1710 { 1711 struct nlattr **a = info->attrs; 1712 struct sk_buff *reply; 1713 struct vport *vport; 1714 int err; 1715 1716 rtnl_lock(); 1717 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); 1718 err = PTR_ERR(vport); 1719 if (IS_ERR(vport)) 1720 goto exit_unlock; 1721 1722 err = 0; 1723 if (a[OVS_VPORT_ATTR_TYPE] && 1724 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) 1725 err = -EINVAL; 1726 1727 if (!err && a[OVS_VPORT_ATTR_OPTIONS]) 1728 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); 1729 if (err) 1730 goto exit_unlock; 1731 if (a[OVS_VPORT_ATTR_UPCALL_PID]) 1732 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); 1733 1734 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, 1735 OVS_VPORT_CMD_NEW); 1736 if (IS_ERR(reply)) { 1737 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 1738 ovs_dp_vport_multicast_group.id, PTR_ERR(reply)); 1739 goto exit_unlock; 1740 } 1741 1742 genl_notify(reply, genl_info_net(info), info->snd_portid, 1743 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL); 1744 1745 exit_unlock: 1746 rtnl_unlock(); 1747 return err; 1748 } 1749 1750 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) 1751 { 1752 struct nlattr **a = info->attrs; 1753 struct sk_buff *reply; 1754 struct vport *vport; 1755 int err; 1756 1757 rtnl_lock(); 1758 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); 1759 err = PTR_ERR(vport); 1760 if (IS_ERR(vport)) 1761 goto exit_unlock; 1762 1763 if (vport->port_no == OVSP_LOCAL) { 1764 err = -EINVAL; 1765 goto exit_unlock; 1766 } 1767 1768 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, 1769 OVS_VPORT_CMD_DEL); 1770 err = PTR_ERR(reply); 1771 if (IS_ERR(reply)) 1772 goto exit_unlock; 1773 1774 ovs_dp_detach_port(vport); 1775 1776 genl_notify(reply, genl_info_net(info), info->snd_portid, 1777 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL); 1778 1779 exit_unlock: 1780 rtnl_unlock(); 1781 return err; 1782 } 1783 1784 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) 1785 { 1786 struct nlattr **a = info->attrs; 1787 struct ovs_header *ovs_header = info->userhdr; 1788 struct sk_buff *reply; 1789 struct vport *vport; 1790 int err; 1791 1792 rcu_read_lock(); 1793 vport = lookup_vport(sock_net(skb->sk), ovs_header, a); 1794 err = PTR_ERR(vport); 1795 if (IS_ERR(vport)) 1796 goto exit_unlock; 1797 1798 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, 1799 OVS_VPORT_CMD_NEW); 1800 err = PTR_ERR(reply); 1801 if (IS_ERR(reply)) 1802 goto exit_unlock; 1803 1804 rcu_read_unlock(); 1805 1806 return genlmsg_reply(reply, info); 1807 1808 exit_unlock: 1809 rcu_read_unlock(); 1810 return err; 1811 } 1812 1813 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1814 { 1815 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 1816 struct datapath *dp; 1817 int bucket = cb->args[0], skip = cb->args[1]; 1818 int i, j = 0; 1819 1820 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1821 if (!dp) 1822 return -ENODEV; 1823 1824 rcu_read_lock(); 1825 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { 1826 struct vport *vport; 1827 1828 j = 0; 1829 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { 1830 if (j >= skip && 1831 ovs_vport_cmd_fill_info(vport, skb, 1832 NETLINK_CB(cb->skb).portid, 1833 cb->nlh->nlmsg_seq, 1834 NLM_F_MULTI, 1835 OVS_VPORT_CMD_NEW) < 0) 1836 goto out; 1837 1838 j++; 1839 } 1840 skip = 0; 1841 } 1842 out: 1843 rcu_read_unlock(); 1844 1845 cb->args[0] = i; 1846 cb->args[1] = j; 1847 1848 return skb->len; 1849 } 1850 1851 static struct genl_ops dp_vport_genl_ops[] = { 1852 { .cmd = OVS_VPORT_CMD_NEW, 1853 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1854 .policy = vport_policy, 1855 .doit = ovs_vport_cmd_new 1856 }, 1857 { .cmd = OVS_VPORT_CMD_DEL, 1858 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1859 .policy = vport_policy, 1860 .doit = ovs_vport_cmd_del 1861 }, 1862 { .cmd = OVS_VPORT_CMD_GET, 1863 .flags = 0, /* OK for unprivileged users. */ 1864 .policy = vport_policy, 1865 .doit = ovs_vport_cmd_get, 1866 .dumpit = ovs_vport_cmd_dump 1867 }, 1868 { .cmd = OVS_VPORT_CMD_SET, 1869 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1870 .policy = vport_policy, 1871 .doit = ovs_vport_cmd_set, 1872 }, 1873 }; 1874 1875 struct genl_family_and_ops { 1876 struct genl_family *family; 1877 struct genl_ops *ops; 1878 int n_ops; 1879 struct genl_multicast_group *group; 1880 }; 1881 1882 static const struct genl_family_and_ops dp_genl_families[] = { 1883 { &dp_datapath_genl_family, 1884 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops), 1885 &ovs_dp_datapath_multicast_group }, 1886 { &dp_vport_genl_family, 1887 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops), 1888 &ovs_dp_vport_multicast_group }, 1889 { &dp_flow_genl_family, 1890 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops), 1891 &ovs_dp_flow_multicast_group }, 1892 { &dp_packet_genl_family, 1893 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops), 1894 NULL }, 1895 }; 1896 1897 static void dp_unregister_genl(int n_families) 1898 { 1899 int i; 1900 1901 for (i = 0; i < n_families; i++) 1902 genl_unregister_family(dp_genl_families[i].family); 1903 } 1904 1905 static int dp_register_genl(void) 1906 { 1907 int n_registered; 1908 int err; 1909 int i; 1910 1911 n_registered = 0; 1912 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { 1913 const struct genl_family_and_ops *f = &dp_genl_families[i]; 1914 1915 err = genl_register_family_with_ops(f->family, f->ops, 1916 f->n_ops); 1917 if (err) 1918 goto error; 1919 n_registered++; 1920 1921 if (f->group) { 1922 err = genl_register_mc_group(f->family, f->group); 1923 if (err) 1924 goto error; 1925 } 1926 } 1927 1928 return 0; 1929 1930 error: 1931 dp_unregister_genl(n_registered); 1932 return err; 1933 } 1934 1935 static void rehash_flow_table(struct work_struct *work) 1936 { 1937 struct datapath *dp; 1938 struct net *net; 1939 1940 genl_lock(); 1941 rtnl_lock(); 1942 for_each_net(net) { 1943 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1944 1945 list_for_each_entry(dp, &ovs_net->dps, list_node) { 1946 struct flow_table *old_table = genl_dereference(dp->table); 1947 struct flow_table *new_table; 1948 1949 new_table = ovs_flow_tbl_rehash(old_table); 1950 if (!IS_ERR(new_table)) { 1951 rcu_assign_pointer(dp->table, new_table); 1952 ovs_flow_tbl_deferred_destroy(old_table); 1953 } 1954 } 1955 } 1956 rtnl_unlock(); 1957 genl_unlock(); 1958 1959 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL); 1960 } 1961 1962 static int __net_init ovs_init_net(struct net *net) 1963 { 1964 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1965 1966 INIT_LIST_HEAD(&ovs_net->dps); 1967 return 0; 1968 } 1969 1970 static void __net_exit ovs_exit_net(struct net *net) 1971 { 1972 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1973 struct datapath *dp, *dp_next; 1974 1975 genl_lock(); 1976 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) 1977 __dp_destroy(dp); 1978 genl_unlock(); 1979 } 1980 1981 static struct pernet_operations ovs_net_ops = { 1982 .init = ovs_init_net, 1983 .exit = ovs_exit_net, 1984 .id = &ovs_net_id, 1985 .size = sizeof(struct ovs_net), 1986 }; 1987 1988 static int __init dp_init(void) 1989 { 1990 int err; 1991 1992 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); 1993 1994 pr_info("Open vSwitch switching datapath\n"); 1995 1996 err = ovs_flow_init(); 1997 if (err) 1998 goto error; 1999 2000 err = ovs_vport_init(); 2001 if (err) 2002 goto error_flow_exit; 2003 2004 err = register_pernet_device(&ovs_net_ops); 2005 if (err) 2006 goto error_vport_exit; 2007 2008 err = register_netdevice_notifier(&ovs_dp_device_notifier); 2009 if (err) 2010 goto error_netns_exit; 2011 2012 err = dp_register_genl(); 2013 if (err < 0) 2014 goto error_unreg_notifier; 2015 2016 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL); 2017 2018 return 0; 2019 2020 error_unreg_notifier: 2021 unregister_netdevice_notifier(&ovs_dp_device_notifier); 2022 error_netns_exit: 2023 unregister_pernet_device(&ovs_net_ops); 2024 error_vport_exit: 2025 ovs_vport_exit(); 2026 error_flow_exit: 2027 ovs_flow_exit(); 2028 error: 2029 return err; 2030 } 2031 2032 static void dp_cleanup(void) 2033 { 2034 cancel_delayed_work_sync(&rehash_flow_wq); 2035 dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); 2036 unregister_netdevice_notifier(&ovs_dp_device_notifier); 2037 unregister_pernet_device(&ovs_net_ops); 2038 rcu_barrier(); 2039 ovs_vport_exit(); 2040 ovs_flow_exit(); 2041 } 2042 2043 module_init(dp_init); 2044 module_exit(dp_cleanup); 2045 2046 MODULE_DESCRIPTION("Open vSwitch switching datapath"); 2047 MODULE_LICENSE("GPL"); 2048