2 * Copyright (c) 2007-2013 Nicira, Inc.
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.
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.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/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
59 #include "flow_netlink.h"
61 #include "vport-internal_dev.h"
62 #include "vport-netdev.h"
64 int ovs_net_id __read_mostly;
66 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
67 struct genl_multicast_group *grp)
69 genl_notify(skb, genl_info_net(info), info->snd_portid,
70 grp->id, info->nlhdr, GFP_KERNEL);
76 * All writes e.g. Writes to device state (add/remove datapath, port, set
77 * operations on vports, etc.), Writes to other state (flow table
78 * modifications, set miscellaneous datapath parameters, etc.) are protected
81 * Reads are protected by RCU.
83 * There are a few special cases (mostly stats) that have their own
84 * synchronization but they nest under all of above and don't interact with
87 * The RTNL lock nests inside ovs_mutex.
90 static DEFINE_MUTEX(ovs_mutex);
94 mutex_lock(&ovs_mutex);
99 mutex_unlock(&ovs_mutex);
102 #ifdef CONFIG_LOCKDEP
103 int lockdep_ovsl_is_held(void)
106 return lockdep_is_held(&ovs_mutex);
112 static struct vport *new_vport(const struct vport_parms *);
113 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
114 const struct dp_upcall_info *);
115 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
116 const struct dp_upcall_info *);
118 /* Must be called with rcu_read_lock or ovs_mutex. */
119 static struct datapath *get_dp(struct net *net, int dp_ifindex)
121 struct datapath *dp = NULL;
122 struct net_device *dev;
125 dev = dev_get_by_index_rcu(net, dp_ifindex);
127 struct vport *vport = ovs_internal_dev_get_vport(dev);
136 /* Must be called with rcu_read_lock or ovs_mutex. */
137 const char *ovs_dp_name(const struct datapath *dp)
139 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
140 return vport->ops->get_name(vport);
143 static int get_dpifindex(struct datapath *dp)
150 local = ovs_vport_rcu(dp, OVSP_LOCAL);
152 ifindex = netdev_vport_priv(local)->dev->ifindex;
161 static void destroy_dp_rcu(struct rcu_head *rcu)
163 struct datapath *dp = container_of(rcu, struct datapath, rcu);
165 ovs_flow_tbl_destroy(&dp->table);
166 free_percpu(dp->stats_percpu);
167 release_net(ovs_dp_get_net(dp));
172 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
175 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
178 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
181 struct hlist_head *head;
183 head = vport_hash_bucket(dp, port_no);
184 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
185 if (vport->port_no == port_no)
191 /* Called with ovs_mutex. */
192 static struct vport *new_vport(const struct vport_parms *parms)
196 vport = ovs_vport_add(parms);
197 if (!IS_ERR(vport)) {
198 struct datapath *dp = parms->dp;
199 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
201 hlist_add_head_rcu(&vport->dp_hash_node, head);
206 void ovs_dp_detach_port(struct vport *p)
210 /* First drop references to device. */
211 hlist_del_rcu(&p->dp_hash_node);
213 /* Then destroy it. */
217 /* Must be called with rcu_read_lock. */
218 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
220 struct datapath *dp = p->dp;
221 struct sw_flow *flow;
222 struct dp_stats_percpu *stats;
223 struct sw_flow_key key;
228 stats = this_cpu_ptr(dp->stats_percpu);
230 /* Extract flow from 'skb' into 'key'. */
231 error = ovs_flow_extract(skb, p->port_no, &key);
232 if (unlikely(error)) {
238 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
239 if (unlikely(!flow)) {
240 struct dp_upcall_info upcall;
242 upcall.cmd = OVS_PACKET_CMD_MISS;
244 upcall.userdata = NULL;
245 upcall.portid = p->upcall_portid;
246 ovs_dp_upcall(dp, skb, &upcall);
248 stats_counter = &stats->n_missed;
252 OVS_CB(skb)->flow = flow;
253 OVS_CB(skb)->pkt_key = &key;
255 ovs_flow_stats_update(OVS_CB(skb)->flow, skb);
256 ovs_execute_actions(dp, skb);
257 stats_counter = &stats->n_hit;
260 /* Update datapath statistics. */
261 u64_stats_update_begin(&stats->sync);
263 stats->n_mask_hit += n_mask_hit;
264 u64_stats_update_end(&stats->sync);
267 static struct genl_family dp_packet_genl_family = {
268 .id = GENL_ID_GENERATE,
269 .hdrsize = sizeof(struct ovs_header),
270 .name = OVS_PACKET_FAMILY,
271 .version = OVS_PACKET_VERSION,
272 .maxattr = OVS_PACKET_ATTR_MAX,
277 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
278 const struct dp_upcall_info *upcall_info)
280 struct dp_stats_percpu *stats;
283 if (upcall_info->portid == 0) {
288 if (!skb_is_gso(skb))
289 err = queue_userspace_packet(dp, skb, upcall_info);
291 err = queue_gso_packets(dp, skb, upcall_info);
298 stats = this_cpu_ptr(dp->stats_percpu);
300 u64_stats_update_begin(&stats->sync);
302 u64_stats_update_end(&stats->sync);
307 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
308 const struct dp_upcall_info *upcall_info)
310 unsigned short gso_type = skb_shinfo(skb)->gso_type;
311 struct dp_upcall_info later_info;
312 struct sw_flow_key later_key;
313 struct sk_buff *segs, *nskb;
316 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
318 return PTR_ERR(segs);
320 /* Queue all of the segments. */
323 err = queue_userspace_packet(dp, skb, upcall_info);
327 if (skb == segs && gso_type & SKB_GSO_UDP) {
328 /* The initial flow key extracted by ovs_flow_extract()
329 * in this case is for a first fragment, so we need to
330 * properly mark later fragments.
332 later_key = *upcall_info->key;
333 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
335 later_info = *upcall_info;
336 later_info.key = &later_key;
337 upcall_info = &later_info;
339 } while ((skb = skb->next));
341 /* Free all of the segments. */
349 } while ((skb = nskb));
353 static size_t key_attr_size(void)
355 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
356 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
357 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
358 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
359 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
360 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
361 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
362 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
363 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
364 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
365 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
366 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
367 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
368 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
369 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
370 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
371 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
372 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
373 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
376 static size_t upcall_msg_size(const struct sk_buff *skb,
377 const struct nlattr *userdata)
379 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
380 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
381 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
383 /* OVS_PACKET_ATTR_USERDATA */
385 size += NLA_ALIGN(userdata->nla_len);
390 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
391 const struct dp_upcall_info *upcall_info)
393 struct ovs_header *upcall;
394 struct sk_buff *nskb = NULL;
395 struct sk_buff *user_skb; /* to be queued to userspace */
397 struct genl_info info = {
398 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
399 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
401 .snd_portid = upcall_info->portid,
406 dp_ifindex = get_dpifindex(dp);
410 if (vlan_tx_tag_present(skb)) {
411 nskb = skb_clone(skb, GFP_ATOMIC);
415 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
419 vlan_set_tci(nskb, 0);
424 if (nla_attr_size(skb->len) > USHRT_MAX) {
429 len = upcall_msg_size(skb, upcall_info->userdata);
430 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
436 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
437 0, upcall_info->cmd);
438 upcall->dp_ifindex = dp_ifindex;
440 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
441 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
442 nla_nest_end(user_skb, nla);
444 if (upcall_info->userdata)
445 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
446 nla_len(upcall_info->userdata),
447 nla_data(upcall_info->userdata));
449 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
451 skb_copy_and_csum_dev(skb, nla_data(nla));
453 genlmsg_end(user_skb, upcall);
454 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
461 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
463 struct ovs_header *ovs_header = info->userhdr;
464 struct nlattr **a = info->attrs;
465 struct sw_flow_actions *acts;
466 struct sk_buff *packet;
467 struct sw_flow *flow;
474 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
475 !a[OVS_PACKET_ATTR_ACTIONS])
478 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
479 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
483 skb_reserve(packet, NET_IP_ALIGN);
485 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
487 skb_reset_mac_header(packet);
488 eth = eth_hdr(packet);
490 /* Normally, setting the skb 'protocol' field would be handled by a
491 * call to eth_type_trans(), but it assumes there's a sending
492 * device, which we may not have. */
493 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
494 packet->protocol = eth->h_proto;
496 packet->protocol = htons(ETH_P_802_2);
498 /* Build an sw_flow for sending this packet. */
499 flow = ovs_flow_alloc(false);
504 err = ovs_flow_extract(packet, -1, &flow->key);
508 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
511 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
516 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
517 &flow->key, 0, &acts);
518 rcu_assign_pointer(flow->sf_acts, acts);
522 OVS_CB(packet)->flow = flow;
523 OVS_CB(packet)->pkt_key = &flow->key;
524 packet->priority = flow->key.phy.priority;
525 packet->mark = flow->key.phy.skb_mark;
528 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
534 err = ovs_execute_actions(dp, packet);
538 ovs_flow_free(flow, false);
544 ovs_flow_free(flow, false);
551 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
552 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
553 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
554 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
557 static struct genl_ops dp_packet_genl_ops[] = {
558 { .cmd = OVS_PACKET_CMD_EXECUTE,
559 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
560 .policy = packet_policy,
561 .doit = ovs_packet_cmd_execute
565 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
566 struct ovs_dp_megaflow_stats *mega_stats)
570 memset(mega_stats, 0, sizeof(*mega_stats));
572 stats->n_flows = ovs_flow_tbl_count(&dp->table);
573 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
575 stats->n_hit = stats->n_missed = stats->n_lost = 0;
577 for_each_possible_cpu(i) {
578 const struct dp_stats_percpu *percpu_stats;
579 struct dp_stats_percpu local_stats;
582 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
585 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
586 local_stats = *percpu_stats;
587 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
589 stats->n_hit += local_stats.n_hit;
590 stats->n_missed += local_stats.n_missed;
591 stats->n_lost += local_stats.n_lost;
592 mega_stats->n_mask_hit += local_stats.n_mask_hit;
596 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
597 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
598 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
599 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
602 static struct genl_family dp_flow_genl_family = {
603 .id = GENL_ID_GENERATE,
604 .hdrsize = sizeof(struct ovs_header),
605 .name = OVS_FLOW_FAMILY,
606 .version = OVS_FLOW_VERSION,
607 .maxattr = OVS_FLOW_ATTR_MAX,
612 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
613 .name = OVS_FLOW_MCGROUP
616 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
618 return NLMSG_ALIGN(sizeof(struct ovs_header))
619 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
620 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
621 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
622 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
623 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
624 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
627 /* Called with ovs_mutex. */
628 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
629 struct sk_buff *skb, u32 portid,
630 u32 seq, u32 flags, u8 cmd)
632 const int skb_orig_len = skb->len;
633 struct nlattr *start;
634 struct ovs_flow_stats stats;
637 struct ovs_header *ovs_header;
641 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
645 ovs_header->dp_ifindex = get_dpifindex(dp);
648 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
650 goto nla_put_failure;
652 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
655 nla_nest_end(skb, nla);
657 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
659 goto nla_put_failure;
661 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
665 nla_nest_end(skb, nla);
667 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
669 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
670 goto nla_put_failure;
672 if (stats.n_packets &&
673 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
674 goto nla_put_failure;
676 if ((u8)ntohs(tcp_flags) &&
677 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
678 goto nla_put_failure;
680 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
681 * this is the first flow to be dumped into 'skb'. This is unusual for
682 * Netlink but individual action lists can be longer than
683 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
684 * The userspace caller can always fetch the actions separately if it
685 * really wants them. (Most userspace callers in fact don't care.)
687 * This can only fail for dump operations because the skb is always
688 * properly sized for single flows.
690 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
692 const struct sw_flow_actions *sf_acts;
694 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
696 err = ovs_nla_put_actions(sf_acts->actions,
697 sf_acts->actions_len, skb);
699 nla_nest_end(skb, start);
704 nla_nest_cancel(skb, start);
706 } else if (skb_orig_len)
707 goto nla_put_failure;
709 return genlmsg_end(skb, ovs_header);
714 genlmsg_cancel(skb, ovs_header);
718 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow,
719 struct genl_info *info)
723 len = ovs_flow_cmd_msg_size(ovsl_dereference(flow->sf_acts));
725 return genlmsg_new_unicast(len, info, GFP_KERNEL);
728 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
730 struct genl_info *info,
736 skb = ovs_flow_cmd_alloc_info(flow, info);
738 return ERR_PTR(-ENOMEM);
740 retval = ovs_flow_cmd_fill_info(flow, dp, skb, info->snd_portid,
741 info->snd_seq, 0, cmd);
746 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
748 struct nlattr **a = info->attrs;
749 struct ovs_header *ovs_header = info->userhdr;
750 struct sw_flow_key key, masked_key;
751 struct sw_flow *flow = NULL;
752 struct sw_flow_mask mask;
753 struct sk_buff *reply;
755 struct sw_flow_actions *acts = NULL;
756 struct sw_flow_match match;
762 if (!a[OVS_FLOW_ATTR_KEY])
765 ovs_match_init(&match, &key, &mask);
766 error = ovs_nla_get_match(&match, &exact_5tuple,
767 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
771 /* Validate actions. */
772 if (a[OVS_FLOW_ATTR_ACTIONS]) {
773 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
774 error = PTR_ERR(acts);
778 ovs_flow_mask_key(&masked_key, &key, &mask);
779 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
780 &masked_key, 0, &acts);
782 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
785 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
791 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
796 /* Check if this is a duplicate flow */
797 flow = ovs_flow_tbl_lookup(&dp->table, &key);
799 /* Bail out if we're not allowed to create a new flow. */
801 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
805 flow = ovs_flow_alloc(!exact_5tuple);
807 error = PTR_ERR(flow);
811 flow->key = masked_key;
812 flow->unmasked_key = key;
813 rcu_assign_pointer(flow->sf_acts, acts);
815 /* Put flow in bucket. */
816 error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
822 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
824 /* We found a matching flow. */
825 struct sw_flow_actions *old_acts;
827 /* Bail out if we're not allowed to modify an existing flow.
828 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
829 * because Generic Netlink treats the latter as a dump
830 * request. We also accept NLM_F_EXCL in case that bug ever
834 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
835 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
838 /* The unmasked key has to be the same for flow updates. */
840 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
841 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
845 /* Update actions. */
846 old_acts = ovsl_dereference(flow->sf_acts);
847 rcu_assign_pointer(flow->sf_acts, acts);
848 ovs_nla_free_flow_actions(old_acts);
850 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
853 if (a[OVS_FLOW_ATTR_CLEAR])
854 ovs_flow_stats_clear(flow);
859 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
861 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
862 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
866 ovs_flow_free(flow, false);
875 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
877 struct nlattr **a = info->attrs;
878 struct ovs_header *ovs_header = info->userhdr;
879 struct sw_flow_key key;
880 struct sk_buff *reply;
881 struct sw_flow *flow;
883 struct sw_flow_match match;
886 if (!a[OVS_FLOW_ATTR_KEY]) {
887 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
891 ovs_match_init(&match, &key, NULL);
892 err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
897 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
903 flow = ovs_flow_tbl_lookup(&dp->table, &key);
904 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
909 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
911 err = PTR_ERR(reply);
916 return genlmsg_reply(reply, info);
922 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
924 struct nlattr **a = info->attrs;
925 struct ovs_header *ovs_header = info->userhdr;
926 struct sw_flow_key key;
927 struct sk_buff *reply;
928 struct sw_flow *flow;
930 struct sw_flow_match match;
934 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
940 if (!a[OVS_FLOW_ATTR_KEY]) {
941 err = ovs_flow_tbl_flush(&dp->table);
945 ovs_match_init(&match, &key, NULL);
946 err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
950 flow = ovs_flow_tbl_lookup(&dp->table, &key);
951 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
956 reply = ovs_flow_cmd_alloc_info(flow, info);
962 ovs_flow_tbl_remove(&dp->table, flow);
964 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
965 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
968 ovs_flow_free(flow, true);
971 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
978 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
980 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
981 struct table_instance *ti;
985 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
991 ti = rcu_dereference(dp->table.ti);
993 struct sw_flow *flow;
996 bucket = cb->args[0];
998 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1002 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1003 NETLINK_CB(cb->skb).portid,
1004 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1005 OVS_FLOW_CMD_NEW) < 0)
1008 cb->args[0] = bucket;
1015 static struct genl_ops dp_flow_genl_ops[] = {
1016 { .cmd = OVS_FLOW_CMD_NEW,
1017 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1018 .policy = flow_policy,
1019 .doit = ovs_flow_cmd_new_or_set
1021 { .cmd = OVS_FLOW_CMD_DEL,
1022 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1023 .policy = flow_policy,
1024 .doit = ovs_flow_cmd_del
1026 { .cmd = OVS_FLOW_CMD_GET,
1027 .flags = 0, /* OK for unprivileged users. */
1028 .policy = flow_policy,
1029 .doit = ovs_flow_cmd_get,
1030 .dumpit = ovs_flow_cmd_dump
1032 { .cmd = OVS_FLOW_CMD_SET,
1033 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1034 .policy = flow_policy,
1035 .doit = ovs_flow_cmd_new_or_set,
1039 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1040 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1041 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1042 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1045 static struct genl_family dp_datapath_genl_family = {
1046 .id = GENL_ID_GENERATE,
1047 .hdrsize = sizeof(struct ovs_header),
1048 .name = OVS_DATAPATH_FAMILY,
1049 .version = OVS_DATAPATH_VERSION,
1050 .maxattr = OVS_DP_ATTR_MAX,
1055 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1056 .name = OVS_DATAPATH_MCGROUP
1059 static size_t ovs_dp_cmd_msg_size(void)
1061 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1063 msgsize += nla_total_size(IFNAMSIZ);
1064 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1065 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1070 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1071 u32 portid, u32 seq, u32 flags, u8 cmd)
1073 struct ovs_header *ovs_header;
1074 struct ovs_dp_stats dp_stats;
1075 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1078 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1083 ovs_header->dp_ifindex = get_dpifindex(dp);
1086 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1089 goto nla_put_failure;
1091 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1092 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1094 goto nla_put_failure;
1096 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1097 sizeof(struct ovs_dp_megaflow_stats),
1098 &dp_megaflow_stats))
1099 goto nla_put_failure;
1101 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1102 goto nla_put_failure;
1104 return genlmsg_end(skb, ovs_header);
1107 genlmsg_cancel(skb, ovs_header);
1112 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp,
1113 struct genl_info *info, u8 cmd)
1115 struct sk_buff *skb;
1118 skb = genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1120 return ERR_PTR(-ENOMEM);
1122 retval = ovs_dp_cmd_fill_info(dp, skb, info->snd_portid, info->snd_seq, 0, cmd);
1125 return ERR_PTR(retval);
1130 /* Called with ovs_mutex. */
1131 static struct datapath *lookup_datapath(struct net *net,
1132 struct ovs_header *ovs_header,
1133 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1135 struct datapath *dp;
1137 if (!a[OVS_DP_ATTR_NAME])
1138 dp = get_dp(net, ovs_header->dp_ifindex);
1140 struct vport *vport;
1143 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1144 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1147 return dp ? dp : ERR_PTR(-ENODEV);
1150 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1152 struct datapath *dp;
1154 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1158 WARN(dp->user_features, "Dropping previously announced user features\n");
1159 dp->user_features = 0;
1162 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1164 if (a[OVS_DP_ATTR_USER_FEATURES])
1165 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1168 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1170 struct nlattr **a = info->attrs;
1171 struct vport_parms parms;
1172 struct sk_buff *reply;
1173 struct datapath *dp;
1174 struct vport *vport;
1175 struct ovs_net *ovs_net;
1179 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1185 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1187 goto err_unlock_ovs;
1189 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1191 /* Allocate table. */
1192 err = ovs_flow_tbl_init(&dp->table);
1196 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1197 if (!dp->stats_percpu) {
1199 goto err_destroy_table;
1202 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1206 goto err_destroy_percpu;
1209 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1210 INIT_HLIST_HEAD(&dp->ports[i]);
1212 /* Set up our datapath device. */
1213 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1214 parms.type = OVS_VPORT_TYPE_INTERNAL;
1215 parms.options = NULL;
1217 parms.port_no = OVSP_LOCAL;
1218 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1220 ovs_dp_change(dp, a);
1222 vport = new_vport(&parms);
1223 if (IS_ERR(vport)) {
1224 err = PTR_ERR(vport);
1228 if (err == -EEXIST) {
1229 /* An outdated user space instance that does not understand
1230 * the concept of user_features has attempted to create a new
1231 * datapath and is likely to reuse it. Drop all user features.
1233 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1234 ovs_dp_reset_user_features(skb, info);
1237 goto err_destroy_ports_array;
1240 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
1241 err = PTR_ERR(reply);
1243 goto err_destroy_local_port;
1245 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1246 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1250 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1253 err_destroy_local_port:
1254 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1255 err_destroy_ports_array:
1258 free_percpu(dp->stats_percpu);
1260 ovs_flow_tbl_destroy(&dp->table);
1262 release_net(ovs_dp_get_net(dp));
1270 /* Called with ovs_mutex. */
1271 static void __dp_destroy(struct datapath *dp)
1275 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1276 struct vport *vport;
1277 struct hlist_node *n;
1279 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1280 if (vport->port_no != OVSP_LOCAL)
1281 ovs_dp_detach_port(vport);
1284 list_del_rcu(&dp->list_node);
1286 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1287 * all port in datapath are destroyed first before freeing datapath.
1289 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1291 call_rcu(&dp->rcu, destroy_dp_rcu);
1294 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1296 struct sk_buff *reply;
1297 struct datapath *dp;
1301 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1306 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_DEL);
1307 err = PTR_ERR(reply);
1314 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1322 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1324 struct sk_buff *reply;
1325 struct datapath *dp;
1329 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1334 ovs_dp_change(dp, info->attrs);
1336 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
1337 if (IS_ERR(reply)) {
1338 err = PTR_ERR(reply);
1339 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1340 ovs_dp_datapath_multicast_group.id, err);
1346 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1354 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1356 struct sk_buff *reply;
1357 struct datapath *dp;
1361 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1367 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
1368 if (IS_ERR(reply)) {
1369 err = PTR_ERR(reply);
1374 return genlmsg_reply(reply, info);
1381 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1383 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1384 struct datapath *dp;
1385 int skip = cb->args[0];
1389 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1391 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1392 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1393 OVS_DP_CMD_NEW) < 0)
1404 static struct genl_ops dp_datapath_genl_ops[] = {
1405 { .cmd = OVS_DP_CMD_NEW,
1406 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1407 .policy = datapath_policy,
1408 .doit = ovs_dp_cmd_new
1410 { .cmd = OVS_DP_CMD_DEL,
1411 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1412 .policy = datapath_policy,
1413 .doit = ovs_dp_cmd_del
1415 { .cmd = OVS_DP_CMD_GET,
1416 .flags = 0, /* OK for unprivileged users. */
1417 .policy = datapath_policy,
1418 .doit = ovs_dp_cmd_get,
1419 .dumpit = ovs_dp_cmd_dump
1421 { .cmd = OVS_DP_CMD_SET,
1422 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1423 .policy = datapath_policy,
1424 .doit = ovs_dp_cmd_set,
1428 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1429 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1430 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1431 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1432 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1433 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1434 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1437 static struct genl_family dp_vport_genl_family = {
1438 .id = GENL_ID_GENERATE,
1439 .hdrsize = sizeof(struct ovs_header),
1440 .name = OVS_VPORT_FAMILY,
1441 .version = OVS_VPORT_VERSION,
1442 .maxattr = OVS_VPORT_ATTR_MAX,
1447 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1448 .name = OVS_VPORT_MCGROUP
1451 /* Called with ovs_mutex or RCU read lock. */
1452 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1453 u32 portid, u32 seq, u32 flags, u8 cmd)
1455 struct ovs_header *ovs_header;
1456 struct ovs_vport_stats vport_stats;
1459 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1464 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1466 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1467 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1468 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1469 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1470 goto nla_put_failure;
1472 ovs_vport_get_stats(vport, &vport_stats);
1473 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1475 goto nla_put_failure;
1477 err = ovs_vport_get_options(vport, skb);
1478 if (err == -EMSGSIZE)
1481 return genlmsg_end(skb, ovs_header);
1486 genlmsg_cancel(skb, ovs_header);
1490 /* Called with ovs_mutex or RCU read lock. */
1491 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1494 struct sk_buff *skb;
1497 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1499 return ERR_PTR(-ENOMEM);
1501 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1507 /* Called with ovs_mutex or RCU read lock. */
1508 static struct vport *lookup_vport(struct net *net,
1509 struct ovs_header *ovs_header,
1510 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1512 struct datapath *dp;
1513 struct vport *vport;
1515 if (a[OVS_VPORT_ATTR_NAME]) {
1516 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1518 return ERR_PTR(-ENODEV);
1519 if (ovs_header->dp_ifindex &&
1520 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1521 return ERR_PTR(-ENODEV);
1523 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1524 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1526 if (port_no >= DP_MAX_PORTS)
1527 return ERR_PTR(-EFBIG);
1529 dp = get_dp(net, ovs_header->dp_ifindex);
1531 return ERR_PTR(-ENODEV);
1533 vport = ovs_vport_ovsl_rcu(dp, port_no);
1535 return ERR_PTR(-ENODEV);
1538 return ERR_PTR(-EINVAL);
1541 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1543 struct nlattr **a = info->attrs;
1544 struct ovs_header *ovs_header = info->userhdr;
1545 struct vport_parms parms;
1546 struct sk_buff *reply;
1547 struct vport *vport;
1548 struct datapath *dp;
1553 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1554 !a[OVS_VPORT_ATTR_UPCALL_PID])
1558 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1563 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1564 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1567 if (port_no >= DP_MAX_PORTS)
1570 vport = ovs_vport_ovsl(dp, port_no);
1575 for (port_no = 1; ; port_no++) {
1576 if (port_no >= DP_MAX_PORTS) {
1580 vport = ovs_vport_ovsl(dp, port_no);
1586 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1587 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1588 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1590 parms.port_no = port_no;
1591 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1593 vport = new_vport(&parms);
1594 err = PTR_ERR(vport);
1599 if (a[OVS_VPORT_ATTR_STATS])
1600 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1602 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1604 if (IS_ERR(reply)) {
1605 err = PTR_ERR(reply);
1606 ovs_dp_detach_port(vport);
1610 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1618 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1620 struct nlattr **a = info->attrs;
1621 struct sk_buff *reply;
1622 struct vport *vport;
1626 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1627 err = PTR_ERR(vport);
1631 if (a[OVS_VPORT_ATTR_TYPE] &&
1632 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1637 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1643 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1644 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1649 if (a[OVS_VPORT_ATTR_STATS])
1650 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1652 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1653 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1655 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1656 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1660 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1670 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1672 struct nlattr **a = info->attrs;
1673 struct sk_buff *reply;
1674 struct vport *vport;
1678 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1679 err = PTR_ERR(vport);
1683 if (vport->port_no == OVSP_LOCAL) {
1688 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1689 info->snd_seq, OVS_VPORT_CMD_DEL);
1690 err = PTR_ERR(reply);
1695 ovs_dp_detach_port(vport);
1697 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1704 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1706 struct nlattr **a = info->attrs;
1707 struct ovs_header *ovs_header = info->userhdr;
1708 struct sk_buff *reply;
1709 struct vport *vport;
1713 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1714 err = PTR_ERR(vport);
1718 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1719 info->snd_seq, OVS_VPORT_CMD_NEW);
1720 err = PTR_ERR(reply);
1726 return genlmsg_reply(reply, info);
1733 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1735 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1736 struct datapath *dp;
1737 int bucket = cb->args[0], skip = cb->args[1];
1740 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1745 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1746 struct vport *vport;
1749 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1751 ovs_vport_cmd_fill_info(vport, skb,
1752 NETLINK_CB(cb->skb).portid,
1755 OVS_VPORT_CMD_NEW) < 0)
1771 static struct genl_ops dp_vport_genl_ops[] = {
1772 { .cmd = OVS_VPORT_CMD_NEW,
1773 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1774 .policy = vport_policy,
1775 .doit = ovs_vport_cmd_new
1777 { .cmd = OVS_VPORT_CMD_DEL,
1778 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1779 .policy = vport_policy,
1780 .doit = ovs_vport_cmd_del
1782 { .cmd = OVS_VPORT_CMD_GET,
1783 .flags = 0, /* OK for unprivileged users. */
1784 .policy = vport_policy,
1785 .doit = ovs_vport_cmd_get,
1786 .dumpit = ovs_vport_cmd_dump
1788 { .cmd = OVS_VPORT_CMD_SET,
1789 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1790 .policy = vport_policy,
1791 .doit = ovs_vport_cmd_set,
1795 struct genl_family_and_ops {
1796 struct genl_family *family;
1797 struct genl_ops *ops;
1799 struct genl_multicast_group *group;
1802 static const struct genl_family_and_ops dp_genl_families[] = {
1803 { &dp_datapath_genl_family,
1804 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1805 &ovs_dp_datapath_multicast_group },
1806 { &dp_vport_genl_family,
1807 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1808 &ovs_dp_vport_multicast_group },
1809 { &dp_flow_genl_family,
1810 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1811 &ovs_dp_flow_multicast_group },
1812 { &dp_packet_genl_family,
1813 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1817 static void dp_unregister_genl(int n_families)
1821 for (i = 0; i < n_families; i++)
1822 genl_unregister_family(dp_genl_families[i].family);
1825 static int dp_register_genl(void)
1832 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1833 const struct genl_family_and_ops *f = &dp_genl_families[i];
1835 err = genl_register_family_with_ops(f->family, f->ops,
1842 err = genl_register_mc_group(f->family, f->group);
1851 dp_unregister_genl(n_registered);
1855 static int __net_init ovs_init_net(struct net *net)
1857 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1859 INIT_LIST_HEAD(&ovs_net->dps);
1860 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
1864 static void __net_exit ovs_exit_net(struct net *net)
1866 struct datapath *dp, *dp_next;
1867 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1870 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1874 cancel_work_sync(&ovs_net->dp_notify_work);
1877 static struct pernet_operations ovs_net_ops = {
1878 .init = ovs_init_net,
1879 .exit = ovs_exit_net,
1881 .size = sizeof(struct ovs_net),
1884 DEFINE_COMPAT_PNET_REG_FUNC(device);
1886 static int __init dp_init(void)
1890 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
1892 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
1895 err = ovs_flow_init();
1899 err = ovs_vport_init();
1901 goto error_flow_exit;
1903 err = register_pernet_device(&ovs_net_ops);
1905 goto error_vport_exit;
1907 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1909 goto error_netns_exit;
1911 err = dp_register_genl();
1913 goto error_unreg_notifier;
1917 error_unreg_notifier:
1918 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1920 unregister_pernet_device(&ovs_net_ops);
1929 static void dp_cleanup(void)
1931 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1932 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1933 unregister_pernet_device(&ovs_net_ops);
1939 module_init(dp_init);
1940 module_exit(dp_cleanup);
1942 MODULE_DESCRIPTION("Open vSwitch switching datapath");
1943 MODULE_LICENSE("GPL");
1944 MODULE_VERSION(VERSION);