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