2 * Copyright (c) 2007-2014 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 <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
57 #include "flow_table.h"
58 #include "flow_netlink.h"
60 #include "vport-internal_dev.h"
61 #include "vport-netdev.h"
63 int ovs_net_id __read_mostly;
65 static struct genl_family dp_packet_genl_family;
66 static struct genl_family dp_flow_genl_family;
67 static struct genl_family dp_datapath_genl_family;
69 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
70 .name = OVS_FLOW_MCGROUP
73 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
74 .name = OVS_DATAPATH_MCGROUP
77 struct genl_multicast_group ovs_dp_vport_multicast_group = {
78 .name = OVS_VPORT_MCGROUP
81 /* Check if need to build a reply message.
82 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
83 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
86 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
87 genl_has_listeners(family, genl_info_net(info)->genl_sock,
91 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
92 struct sk_buff *skb, struct genl_info *info)
94 genl_notify(family, skb, genl_info_net(info),
95 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
101 * All writes e.g. Writes to device state (add/remove datapath, port, set
102 * operations on vports, etc.), Writes to other state (flow table
103 * modifications, set miscellaneous datapath parameters, etc.) are protected
106 * Reads are protected by RCU.
108 * There are a few special cases (mostly stats) that have their own
109 * synchronization but they nest under all of above and don't interact with
112 * The RTNL lock nests inside ovs_mutex.
115 static DEFINE_MUTEX(ovs_mutex);
119 mutex_lock(&ovs_mutex);
122 void ovs_unlock(void)
124 mutex_unlock(&ovs_mutex);
127 #ifdef CONFIG_LOCKDEP
128 int lockdep_ovsl_is_held(void)
131 return lockdep_is_held(&ovs_mutex);
137 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
138 const struct sw_flow_key *,
139 const struct dp_upcall_info *);
140 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
141 const struct sw_flow_key *key,
142 const struct dp_upcall_info *);
144 /* Must be called with rcu_read_lock. */
145 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
147 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
150 struct vport *vport = ovs_internal_dev_get_vport(dev);
158 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
159 * returned dp pointer valid. */
160 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
164 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
166 dp = get_dp_rcu(net, dp_ifindex);
172 /* Must be called with rcu_read_lock or ovs_mutex. */
173 const char *ovs_dp_name(const struct datapath *dp)
175 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
176 return vport->ops->get_name(vport);
179 static int get_dpifindex(const struct datapath *dp)
186 local = ovs_vport_rcu(dp, OVSP_LOCAL);
188 ifindex = netdev_vport_priv(local)->dev->ifindex;
197 static void destroy_dp_rcu(struct rcu_head *rcu)
199 struct datapath *dp = container_of(rcu, struct datapath, rcu);
201 ovs_flow_tbl_destroy(&dp->table);
202 free_percpu(dp->stats_percpu);
203 release_net(ovs_dp_get_net(dp));
208 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
211 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
214 /* Called with ovs_mutex or RCU read lock. */
215 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
218 struct hlist_head *head;
220 head = vport_hash_bucket(dp, port_no);
221 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
222 if (vport->port_no == port_no)
228 /* Called with ovs_mutex. */
229 static struct vport *new_vport(const struct vport_parms *parms)
233 vport = ovs_vport_add(parms);
234 if (!IS_ERR(vport)) {
235 struct datapath *dp = parms->dp;
236 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
238 hlist_add_head_rcu(&vport->dp_hash_node, head);
243 void ovs_dp_detach_port(struct vport *p)
247 /* First drop references to device. */
248 hlist_del_rcu(&p->dp_hash_node);
250 /* Then destroy it. */
254 /* Must be called with rcu_read_lock. */
255 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
257 const struct vport *p = OVS_CB(skb)->input_vport;
258 struct datapath *dp = p->dp;
259 struct sw_flow *flow;
260 struct sw_flow_actions *sf_acts;
261 struct dp_stats_percpu *stats;
265 stats = this_cpu_ptr(dp->stats_percpu);
268 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
270 if (unlikely(!flow)) {
271 struct dp_upcall_info upcall;
274 upcall.cmd = OVS_PACKET_CMD_MISS;
275 upcall.userdata = NULL;
276 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
277 upcall.egress_tun_info = NULL;
279 error = ovs_dp_upcall(dp, skb, key, &upcall);
285 stats_counter = &stats->n_missed;
289 ovs_flow_stats_update(flow, key->tp.flags, skb);
291 sf_acts = rcu_dereference(flow->sf_acts);
292 ovs_execute_actions(dp, skb, key, sf_acts);
293 stats_counter = &stats->n_hit;
296 /* Update datapath statistics. */
297 u64_stats_update_begin(&stats->syncp);
299 stats->n_mask_hit += n_mask_hit;
300 u64_stats_update_end(&stats->syncp);
303 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
304 const struct sw_flow_key *key,
305 const struct dp_upcall_info *upcall_info)
307 struct dp_stats_percpu *stats;
310 if (upcall_info->portid == 0) {
315 if (!skb_is_gso(skb))
316 err = queue_userspace_packet(dp, skb, key, upcall_info);
318 err = queue_gso_packets(dp, skb, key, upcall_info);
325 stats = this_cpu_ptr(dp->stats_percpu);
327 u64_stats_update_begin(&stats->syncp);
329 u64_stats_update_end(&stats->syncp);
334 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
335 const struct sw_flow_key *key,
336 const struct dp_upcall_info *upcall_info)
338 unsigned short gso_type = skb_shinfo(skb)->gso_type;
339 struct sw_flow_key later_key;
340 struct sk_buff *segs, *nskb;
341 struct ovs_skb_cb ovs_cb;
344 ovs_cb = *OVS_CB(skb);
345 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
346 *OVS_CB(skb) = ovs_cb;
348 return PTR_ERR(segs);
350 if (gso_type & SKB_GSO_UDP) {
351 /* The initial flow key extracted by ovs_flow_key_extract()
352 * in this case is for a first fragment, so we need to
353 * properly mark later fragments.
356 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
359 /* Queue all of the segments. */
362 *OVS_CB(skb) = ovs_cb;
363 if (gso_type & SKB_GSO_UDP && skb != segs)
366 err = queue_userspace_packet(dp, skb, key, upcall_info);
370 } while ((skb = skb->next));
372 /* Free all of the segments. */
380 } while ((skb = nskb));
384 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
387 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
388 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
389 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
391 /* OVS_PACKET_ATTR_USERDATA */
392 if (upcall_info->userdata)
393 size += NLA_ALIGN(upcall_info->userdata->nla_len);
395 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
396 if (upcall_info->egress_tun_info)
397 size += nla_total_size(ovs_tun_key_attr_size());
402 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
403 const struct sw_flow_key *key,
404 const struct dp_upcall_info *upcall_info)
406 struct ovs_header *upcall;
407 struct sk_buff *nskb = NULL;
408 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
410 struct genl_info info = {
411 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
412 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
414 .snd_portid = upcall_info->portid,
420 dp_ifindex = get_dpifindex(dp);
424 if (vlan_tx_tag_present(skb)) {
425 nskb = skb_clone(skb, GFP_ATOMIC);
429 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
433 vlan_set_tci(nskb, 0);
438 if (nla_attr_size(skb->len) > USHRT_MAX) {
443 /* Complete checksum if needed */
444 if (skb->ip_summed == CHECKSUM_PARTIAL &&
445 (err = skb_checksum_help(skb)))
448 /* Older versions of OVS user space enforce alignment of the last
449 * Netlink attribute to NLA_ALIGNTO which would require extensive
450 * padding logic. Only perform zerocopy if padding is not required.
452 if (dp->user_features & OVS_DP_F_UNALIGNED)
453 hlen = skb_zerocopy_headlen(skb);
457 len = upcall_msg_size(upcall_info, hlen);
458 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
464 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
465 0, upcall_info->cmd);
466 upcall->dp_ifindex = dp_ifindex;
468 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
469 err = ovs_nla_put_flow(key, key, user_skb);
471 nla_nest_end(user_skb, nla);
473 if (upcall_info->userdata)
474 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
475 nla_len(upcall_info->userdata),
476 nla_data(upcall_info->userdata));
478 if (upcall_info->egress_tun_info) {
479 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
480 err = ovs_nla_put_egress_tunnel_key(user_skb,
481 upcall_info->egress_tun_info);
483 nla_nest_end(user_skb, nla);
486 /* Only reserve room for attribute header, packet data is added
487 * in skb_zerocopy() */
488 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
492 nla->nla_len = nla_attr_size(skb->len);
494 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
498 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
499 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
500 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
503 memset(skb_put(user_skb, plen), 0, plen);
506 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
508 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
519 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
521 struct ovs_header *ovs_header = info->userhdr;
522 struct nlattr **a = info->attrs;
523 struct sw_flow_actions *acts;
524 struct sk_buff *packet;
525 struct sw_flow *flow;
526 struct sw_flow_actions *sf_acts;
529 struct vport *input_vport;
534 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
535 !a[OVS_PACKET_ATTR_ACTIONS])
538 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
539 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
543 skb_reserve(packet, NET_IP_ALIGN);
545 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
547 skb_reset_mac_header(packet);
548 eth = eth_hdr(packet);
550 /* Normally, setting the skb 'protocol' field would be handled by a
551 * call to eth_type_trans(), but it assumes there's a sending
552 * device, which we may not have. */
553 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
554 packet->protocol = eth->h_proto;
556 packet->protocol = htons(ETH_P_802_2);
558 /* Build an sw_flow for sending this packet. */
559 flow = ovs_flow_alloc();
564 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
569 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
574 rcu_assign_pointer(flow->sf_acts, acts);
575 OVS_CB(packet)->egress_tun_info = NULL;
576 packet->priority = flow->key.phy.priority;
577 packet->mark = flow->key.phy.skb_mark;
580 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
585 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
587 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
592 OVS_CB(packet)->input_vport = input_vport;
593 sf_acts = rcu_dereference(flow->sf_acts);
596 err = ovs_execute_actions(dp, packet, &flow->key, sf_acts);
600 ovs_flow_free(flow, false);
606 ovs_flow_free(flow, false);
613 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
614 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
615 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
616 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
619 static struct genl_ops dp_packet_genl_ops[] = {
620 { .cmd = OVS_PACKET_CMD_EXECUTE,
621 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
622 .policy = packet_policy,
623 .doit = ovs_packet_cmd_execute
627 static struct genl_family dp_packet_genl_family = {
628 .id = GENL_ID_GENERATE,
629 .hdrsize = sizeof(struct ovs_header),
630 .name = OVS_PACKET_FAMILY,
631 .version = OVS_PACKET_VERSION,
632 .maxattr = OVS_PACKET_ATTR_MAX,
634 .parallel_ops = true,
635 .ops = dp_packet_genl_ops,
636 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
639 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
640 struct ovs_dp_megaflow_stats *mega_stats)
644 memset(mega_stats, 0, sizeof(*mega_stats));
646 stats->n_flows = ovs_flow_tbl_count(&dp->table);
647 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
649 stats->n_hit = stats->n_missed = stats->n_lost = 0;
651 for_each_possible_cpu(i) {
652 const struct dp_stats_percpu *percpu_stats;
653 struct dp_stats_percpu local_stats;
656 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
659 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
660 local_stats = *percpu_stats;
661 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
663 stats->n_hit += local_stats.n_hit;
664 stats->n_missed += local_stats.n_missed;
665 stats->n_lost += local_stats.n_lost;
666 mega_stats->n_mask_hit += local_stats.n_mask_hit;
670 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
672 return NLMSG_ALIGN(sizeof(struct ovs_header))
673 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
674 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
675 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
676 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
677 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
678 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
681 /* Called with ovs_mutex or RCU read lock. */
682 static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
689 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
693 err = ovs_nla_put_flow(&flow->unmasked_key,
694 &flow->unmasked_key, skb);
697 nla_nest_end(skb, nla);
699 /* Fill flow mask. */
700 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
704 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
707 nla_nest_end(skb, nla);
712 /* Called with ovs_mutex or RCU read lock. */
713 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
716 struct ovs_flow_stats stats;
720 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
723 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
726 if (stats.n_packets &&
727 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
730 if ((u8)ntohs(tcp_flags) &&
731 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
737 /* Called with ovs_mutex or RCU read lock. */
738 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
739 struct sk_buff *skb, int skb_orig_len)
741 struct nlattr *start;
744 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
745 * this is the first flow to be dumped into 'skb'. This is unusual for
746 * Netlink but individual action lists can be longer than
747 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
748 * The userspace caller can always fetch the actions separately if it
749 * really wants them. (Most userspace callers in fact don't care.)
751 * This can only fail for dump operations because the skb is always
752 * properly sized for single flows.
754 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
756 const struct sw_flow_actions *sf_acts;
758 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
759 err = ovs_nla_put_actions(sf_acts->actions,
760 sf_acts->actions_len, skb);
763 nla_nest_end(skb, start);
768 nla_nest_cancel(skb, start);
770 } else if (skb_orig_len) {
777 /* Called with ovs_mutex or RCU read lock. */
778 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
779 struct sk_buff *skb, u32 portid,
780 u32 seq, u32 flags, u8 cmd)
782 const int skb_orig_len = skb->len;
783 struct ovs_header *ovs_header;
786 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
789 ovs_header->dp_ifindex = dp_ifindex;
791 err = ovs_flow_cmd_fill_match(flow, skb);
795 err = ovs_flow_cmd_fill_stats(flow, skb);
799 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
803 return genlmsg_end(skb, ovs_header);
806 genlmsg_cancel(skb, ovs_header);
810 /* May not be called with RCU read lock. */
811 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
812 struct genl_info *info,
817 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
818 GROUP_ID(&ovs_dp_flow_multicast_group)))
821 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
824 return ERR_PTR(-ENOMEM);
829 /* Called with ovs_mutex. */
830 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
831 const struct sw_flow *flow,
833 struct genl_info *info, u8 cmd,
839 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
841 if (IS_ERR_OR_NULL(skb))
844 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
845 info->snd_portid, info->snd_seq, 0,
851 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
853 struct nlattr **a = info->attrs;
854 struct ovs_header *ovs_header = info->userhdr;
855 struct sw_flow *flow, *new_flow;
856 struct sw_flow_mask mask;
857 struct sk_buff *reply;
859 struct sw_flow_actions *acts;
860 struct sw_flow_match match;
863 /* Must have key and actions. */
865 if (!a[OVS_FLOW_ATTR_KEY]) {
866 OVS_NLERR("Flow key attribute not present in new flow.\n");
869 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
870 OVS_NLERR("Flow actions attribute not present in new flow.\n");
874 /* Most of the time we need to allocate a new flow, do it before
876 new_flow = ovs_flow_alloc();
877 if (IS_ERR(new_flow)) {
878 error = PTR_ERR(new_flow);
883 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
884 error = ovs_nla_get_match(&match,
885 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
889 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
891 /* Validate actions. */
892 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
895 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
899 reply = ovs_flow_cmd_alloc_info(acts, info, false);
901 error = PTR_ERR(reply);
906 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
911 /* Check if this is a duplicate flow */
912 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
914 rcu_assign_pointer(new_flow->sf_acts, acts);
916 /* Put flow in bucket. */
917 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
918 if (unlikely(error)) {
923 if (unlikely(reply)) {
924 error = ovs_flow_cmd_fill_info(new_flow,
925 ovs_header->dp_ifindex,
926 reply, info->snd_portid,
933 struct sw_flow_actions *old_acts;
935 /* Bail out if we're not allowed to modify an existing flow.
936 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
937 * because Generic Netlink treats the latter as a dump
938 * request. We also accept NLM_F_EXCL in case that bug ever
941 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
946 /* The unmasked key has to be the same for flow updates. */
947 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
948 /* Look for any overlapping flow. */
949 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
955 /* Update actions. */
956 old_acts = ovsl_dereference(flow->sf_acts);
957 rcu_assign_pointer(flow->sf_acts, acts);
959 if (unlikely(reply)) {
960 error = ovs_flow_cmd_fill_info(flow,
961 ovs_header->dp_ifindex,
962 reply, info->snd_portid,
969 ovs_nla_free_flow_actions(old_acts);
970 ovs_flow_free(new_flow, false);
974 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
983 ovs_flow_free(new_flow, false);
988 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
989 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
990 const struct sw_flow_key *key,
991 const struct sw_flow_mask *mask)
993 struct sw_flow_actions *acts;
994 struct sw_flow_key masked_key;
997 ovs_flow_mask_key(&masked_key, key, mask);
998 error = ovs_nla_copy_actions(a, &masked_key, &acts);
1000 OVS_NLERR("Actions may not be safe on all matching packets.\n");
1001 return ERR_PTR(error);
1007 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1009 struct nlattr **a = info->attrs;
1010 struct ovs_header *ovs_header = info->userhdr;
1011 struct sw_flow_key key;
1012 struct sw_flow *flow;
1013 struct sw_flow_mask mask;
1014 struct sk_buff *reply = NULL;
1015 struct datapath *dp;
1016 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1017 struct sw_flow_match match;
1022 if (!a[OVS_FLOW_ATTR_KEY]) {
1023 OVS_NLERR("Flow key attribute not present in set flow.\n");
1027 ovs_match_init(&match, &key, &mask);
1028 error = ovs_nla_get_match(&match,
1029 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1033 /* Validate actions. */
1034 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1035 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1037 error = PTR_ERR(acts);
1041 /* Can allocate before locking if have acts. */
1042 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1043 if (IS_ERR(reply)) {
1044 error = PTR_ERR(reply);
1045 goto err_kfree_acts;
1050 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1051 if (unlikely(!dp)) {
1053 goto err_unlock_ovs;
1055 /* Check that the flow exists. */
1056 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1057 if (unlikely(!flow)) {
1059 goto err_unlock_ovs;
1062 /* Update actions, if present. */
1064 old_acts = ovsl_dereference(flow->sf_acts);
1065 rcu_assign_pointer(flow->sf_acts, acts);
1067 if (unlikely(reply)) {
1068 error = ovs_flow_cmd_fill_info(flow,
1069 ovs_header->dp_ifindex,
1070 reply, info->snd_portid,
1076 /* Could not alloc without acts before locking. */
1077 reply = ovs_flow_cmd_build_info(dp, flow,
1078 ovs_header->dp_ifindex,
1079 info, OVS_FLOW_CMD_NEW, false);
1080 if (unlikely(IS_ERR(reply))) {
1081 error = PTR_ERR(reply);
1082 goto err_unlock_ovs;
1087 if (a[OVS_FLOW_ATTR_CLEAR])
1088 ovs_flow_stats_clear(flow);
1092 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1094 ovs_nla_free_flow_actions(old_acts);
1106 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1108 struct nlattr **a = info->attrs;
1109 struct ovs_header *ovs_header = info->userhdr;
1110 struct sw_flow_key key;
1111 struct sk_buff *reply;
1112 struct sw_flow *flow;
1113 struct datapath *dp;
1114 struct sw_flow_match match;
1117 if (!a[OVS_FLOW_ATTR_KEY]) {
1118 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1122 ovs_match_init(&match, &key, NULL);
1123 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1128 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1134 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1140 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1141 OVS_FLOW_CMD_NEW, true);
1142 if (IS_ERR(reply)) {
1143 err = PTR_ERR(reply);
1148 return genlmsg_reply(reply, info);
1154 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1156 struct nlattr **a = info->attrs;
1157 struct ovs_header *ovs_header = info->userhdr;
1158 struct sw_flow_key key;
1159 struct sk_buff *reply;
1160 struct sw_flow *flow;
1161 struct datapath *dp;
1162 struct sw_flow_match match;
1165 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1166 ovs_match_init(&match, &key, NULL);
1167 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1173 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1174 if (unlikely(!dp)) {
1178 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1179 err = ovs_flow_tbl_flush(&dp->table);
1182 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1183 if (unlikely(!flow)) {
1188 ovs_flow_tbl_remove(&dp->table, flow);
1191 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1194 if (likely(reply)) {
1195 if (likely(!IS_ERR(reply))) {
1196 rcu_read_lock(); /* Keep RCU checker happy. */
1197 err = ovs_flow_cmd_fill_info(flow,
1198 ovs_header->dp_ifindex,
1199 reply, info->snd_portid,
1204 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1206 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1207 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1212 ovs_flow_free(flow, true);
1219 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1221 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1222 struct table_instance *ti;
1223 struct datapath *dp;
1226 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1232 ti = rcu_dereference(dp->table.ti);
1234 struct sw_flow *flow;
1237 bucket = cb->args[0];
1239 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1243 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1244 NETLINK_CB(cb->skb).portid,
1245 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1246 OVS_FLOW_CMD_NEW) < 0)
1249 cb->args[0] = bucket;
1256 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1257 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1258 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1259 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1262 static struct genl_ops dp_flow_genl_ops[] = {
1263 { .cmd = OVS_FLOW_CMD_NEW,
1264 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1265 .policy = flow_policy,
1266 .doit = ovs_flow_cmd_new
1268 { .cmd = OVS_FLOW_CMD_DEL,
1269 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1270 .policy = flow_policy,
1271 .doit = ovs_flow_cmd_del
1273 { .cmd = OVS_FLOW_CMD_GET,
1274 .flags = 0, /* OK for unprivileged users. */
1275 .policy = flow_policy,
1276 .doit = ovs_flow_cmd_get,
1277 .dumpit = ovs_flow_cmd_dump
1279 { .cmd = OVS_FLOW_CMD_SET,
1280 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1281 .policy = flow_policy,
1282 .doit = ovs_flow_cmd_set,
1286 static struct genl_family dp_flow_genl_family = {
1287 .id = GENL_ID_GENERATE,
1288 .hdrsize = sizeof(struct ovs_header),
1289 .name = OVS_FLOW_FAMILY,
1290 .version = OVS_FLOW_VERSION,
1291 .maxattr = OVS_FLOW_ATTR_MAX,
1293 .parallel_ops = true,
1294 .ops = dp_flow_genl_ops,
1295 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1296 .mcgrps = &ovs_dp_flow_multicast_group,
1300 static size_t ovs_dp_cmd_msg_size(void)
1302 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1304 msgsize += nla_total_size(IFNAMSIZ);
1305 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1306 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1307 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1312 /* Called with ovs_mutex or RCU read lock. */
1313 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1314 u32 portid, u32 seq, u32 flags, u8 cmd)
1316 struct ovs_header *ovs_header;
1317 struct ovs_dp_stats dp_stats;
1318 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1321 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1326 ovs_header->dp_ifindex = get_dpifindex(dp);
1328 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1330 goto nla_put_failure;
1332 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1333 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1335 goto nla_put_failure;
1337 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1338 sizeof(struct ovs_dp_megaflow_stats),
1339 &dp_megaflow_stats))
1340 goto nla_put_failure;
1342 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1343 goto nla_put_failure;
1345 return genlmsg_end(skb, ovs_header);
1348 genlmsg_cancel(skb, ovs_header);
1353 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1355 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1358 /* Called with rcu_read_lock or ovs_mutex. */
1359 static struct datapath *lookup_datapath(struct net *net,
1360 const struct ovs_header *ovs_header,
1361 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1363 struct datapath *dp;
1365 if (!a[OVS_DP_ATTR_NAME])
1366 dp = get_dp(net, ovs_header->dp_ifindex);
1368 struct vport *vport;
1370 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1371 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1373 return dp ? dp : ERR_PTR(-ENODEV);
1376 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1378 struct datapath *dp;
1380 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1384 WARN(dp->user_features, "Dropping previously announced user features\n");
1385 dp->user_features = 0;
1388 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1390 if (a[OVS_DP_ATTR_USER_FEATURES])
1391 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1394 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1396 struct nlattr **a = info->attrs;
1397 struct vport_parms parms;
1398 struct sk_buff *reply;
1399 struct datapath *dp;
1400 struct vport *vport;
1401 struct ovs_net *ovs_net;
1405 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1408 reply = ovs_dp_cmd_alloc_info(info);
1413 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1415 goto err_free_reply;
1417 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1419 /* Allocate table. */
1420 err = ovs_flow_tbl_init(&dp->table);
1424 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1425 if (!dp->stats_percpu) {
1427 goto err_destroy_table;
1430 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1434 goto err_destroy_percpu;
1437 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1438 INIT_HLIST_HEAD(&dp->ports[i]);
1440 /* Set up our datapath device. */
1441 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1442 parms.type = OVS_VPORT_TYPE_INTERNAL;
1443 parms.options = NULL;
1445 parms.port_no = OVSP_LOCAL;
1446 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1448 ovs_dp_change(dp, a);
1450 /* So far only local changes have been made, now need the lock. */
1453 vport = new_vport(&parms);
1454 if (IS_ERR(vport)) {
1455 err = PTR_ERR(vport);
1459 if (err == -EEXIST) {
1460 /* An outdated user space instance that does not understand
1461 * the concept of user_features has attempted to create a new
1462 * datapath and is likely to reuse it. Drop all user features.
1464 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1465 ovs_dp_reset_user_features(skb, info);
1468 goto err_destroy_ports_array;
1471 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1472 info->snd_seq, 0, OVS_DP_CMD_NEW);
1475 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1476 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1480 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1483 err_destroy_ports_array:
1487 free_percpu(dp->stats_percpu);
1489 ovs_flow_tbl_destroy(&dp->table);
1491 release_net(ovs_dp_get_net(dp));
1499 /* Called with ovs_mutex. */
1500 static void __dp_destroy(struct datapath *dp)
1504 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1505 struct vport *vport;
1506 struct hlist_node *n;
1508 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1509 if (vport->port_no != OVSP_LOCAL)
1510 ovs_dp_detach_port(vport);
1513 list_del_rcu(&dp->list_node);
1515 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1516 * all ports in datapath are destroyed first before freeing datapath.
1518 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1520 /* RCU destroy the flow table */
1521 call_rcu(&dp->rcu, destroy_dp_rcu);
1524 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1526 struct sk_buff *reply;
1527 struct datapath *dp;
1530 reply = ovs_dp_cmd_alloc_info(info);
1535 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1538 goto err_unlock_free;
1540 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1541 info->snd_seq, 0, OVS_DP_CMD_DEL);
1547 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1556 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1558 struct sk_buff *reply;
1559 struct datapath *dp;
1562 reply = ovs_dp_cmd_alloc_info(info);
1567 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1570 goto err_unlock_free;
1572 ovs_dp_change(dp, info->attrs);
1574 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1575 info->snd_seq, 0, OVS_DP_CMD_NEW);
1579 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1588 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1590 struct sk_buff *reply;
1591 struct datapath *dp;
1594 reply = ovs_dp_cmd_alloc_info(info);
1599 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1602 goto err_unlock_free;
1604 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1605 info->snd_seq, 0, OVS_DP_CMD_NEW);
1609 return genlmsg_reply(reply, info);
1617 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1619 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1620 struct datapath *dp;
1621 int skip = cb->args[0];
1625 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1627 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1628 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1629 OVS_DP_CMD_NEW) < 0)
1640 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1641 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1642 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1643 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1646 static struct genl_ops dp_datapath_genl_ops[] = {
1647 { .cmd = OVS_DP_CMD_NEW,
1648 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1649 .policy = datapath_policy,
1650 .doit = ovs_dp_cmd_new
1652 { .cmd = OVS_DP_CMD_DEL,
1653 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1654 .policy = datapath_policy,
1655 .doit = ovs_dp_cmd_del
1657 { .cmd = OVS_DP_CMD_GET,
1658 .flags = 0, /* OK for unprivileged users. */
1659 .policy = datapath_policy,
1660 .doit = ovs_dp_cmd_get,
1661 .dumpit = ovs_dp_cmd_dump
1663 { .cmd = OVS_DP_CMD_SET,
1664 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1665 .policy = datapath_policy,
1666 .doit = ovs_dp_cmd_set,
1670 static struct genl_family dp_datapath_genl_family = {
1671 .id = GENL_ID_GENERATE,
1672 .hdrsize = sizeof(struct ovs_header),
1673 .name = OVS_DATAPATH_FAMILY,
1674 .version = OVS_DATAPATH_VERSION,
1675 .maxattr = OVS_DP_ATTR_MAX,
1677 .parallel_ops = true,
1678 .ops = dp_datapath_genl_ops,
1679 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1680 .mcgrps = &ovs_dp_datapath_multicast_group,
1684 /* Called with ovs_mutex or RCU read lock. */
1685 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1686 u32 portid, u32 seq, u32 flags, u8 cmd)
1688 struct ovs_header *ovs_header;
1689 struct ovs_vport_stats vport_stats;
1692 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1697 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1699 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1700 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1701 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1702 goto nla_put_failure;
1704 ovs_vport_get_stats(vport, &vport_stats);
1705 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1707 goto nla_put_failure;
1709 if (ovs_vport_get_upcall_portids(vport, skb))
1710 goto nla_put_failure;
1712 err = ovs_vport_get_options(vport, skb);
1713 if (err == -EMSGSIZE)
1716 return genlmsg_end(skb, ovs_header);
1721 genlmsg_cancel(skb, ovs_header);
1725 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1727 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1730 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1731 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1734 struct sk_buff *skb;
1737 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1739 return ERR_PTR(-ENOMEM);
1741 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1747 /* Called with ovs_mutex or RCU read lock. */
1748 static struct vport *lookup_vport(struct net *net,
1749 const struct ovs_header *ovs_header,
1750 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1752 struct datapath *dp;
1753 struct vport *vport;
1755 if (a[OVS_VPORT_ATTR_NAME]) {
1756 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1758 return ERR_PTR(-ENODEV);
1759 if (ovs_header->dp_ifindex &&
1760 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1761 return ERR_PTR(-ENODEV);
1763 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1764 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1766 if (port_no >= DP_MAX_PORTS)
1767 return ERR_PTR(-EFBIG);
1769 dp = get_dp(net, ovs_header->dp_ifindex);
1771 return ERR_PTR(-ENODEV);
1773 vport = ovs_vport_ovsl_rcu(dp, port_no);
1775 return ERR_PTR(-ENODEV);
1778 return ERR_PTR(-EINVAL);
1781 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1783 struct nlattr **a = info->attrs;
1784 struct ovs_header *ovs_header = info->userhdr;
1785 struct vport_parms parms;
1786 struct sk_buff *reply;
1787 struct vport *vport;
1788 struct datapath *dp;
1792 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1793 !a[OVS_VPORT_ATTR_UPCALL_PID])
1796 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1797 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1798 if (port_no >= DP_MAX_PORTS)
1801 reply = ovs_vport_cmd_alloc_info();
1806 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1809 goto exit_unlock_free;
1812 vport = ovs_vport_ovsl(dp, port_no);
1815 goto exit_unlock_free;
1817 for (port_no = 1; ; port_no++) {
1818 if (port_no >= DP_MAX_PORTS) {
1820 goto exit_unlock_free;
1822 vport = ovs_vport_ovsl(dp, port_no);
1828 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1829 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1830 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1832 parms.port_no = port_no;
1833 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1835 vport = new_vport(&parms);
1836 err = PTR_ERR(vport);
1838 goto exit_unlock_free;
1840 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1841 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1845 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1854 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1856 struct nlattr **a = info->attrs;
1857 struct sk_buff *reply;
1858 struct vport *vport;
1861 reply = ovs_vport_cmd_alloc_info();
1866 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1867 err = PTR_ERR(vport);
1869 goto exit_unlock_free;
1871 if (a[OVS_VPORT_ATTR_TYPE] &&
1872 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1874 goto exit_unlock_free;
1877 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1878 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1880 goto exit_unlock_free;
1883 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1884 err = ovs_vport_set_upcall_portids(vport,
1885 a[OVS_VPORT_ATTR_UPCALL_PID]);
1887 goto exit_unlock_free;
1890 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1891 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1895 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1904 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1906 struct nlattr **a = info->attrs;
1907 struct sk_buff *reply;
1908 struct vport *vport;
1911 reply = ovs_vport_cmd_alloc_info();
1916 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1917 err = PTR_ERR(vport);
1919 goto exit_unlock_free;
1921 if (vport->port_no == OVSP_LOCAL) {
1923 goto exit_unlock_free;
1926 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1927 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1929 ovs_dp_detach_port(vport);
1932 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1941 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1943 struct nlattr **a = info->attrs;
1944 struct ovs_header *ovs_header = info->userhdr;
1945 struct sk_buff *reply;
1946 struct vport *vport;
1949 reply = ovs_vport_cmd_alloc_info();
1954 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1955 err = PTR_ERR(vport);
1957 goto exit_unlock_free;
1958 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1959 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1963 return genlmsg_reply(reply, info);
1971 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1973 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1974 struct datapath *dp;
1975 int bucket = cb->args[0], skip = cb->args[1];
1979 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1984 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1985 struct vport *vport;
1988 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1990 ovs_vport_cmd_fill_info(vport, skb,
1991 NETLINK_CB(cb->skb).portid,
1994 OVS_VPORT_CMD_NEW) < 0)
2010 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2011 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2012 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2013 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2014 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2015 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2016 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2019 static struct genl_ops dp_vport_genl_ops[] = {
2020 { .cmd = OVS_VPORT_CMD_NEW,
2021 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2022 .policy = vport_policy,
2023 .doit = ovs_vport_cmd_new
2025 { .cmd = OVS_VPORT_CMD_DEL,
2026 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2027 .policy = vport_policy,
2028 .doit = ovs_vport_cmd_del
2030 { .cmd = OVS_VPORT_CMD_GET,
2031 .flags = 0, /* OK for unprivileged users. */
2032 .policy = vport_policy,
2033 .doit = ovs_vport_cmd_get,
2034 .dumpit = ovs_vport_cmd_dump
2036 { .cmd = OVS_VPORT_CMD_SET,
2037 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2038 .policy = vport_policy,
2039 .doit = ovs_vport_cmd_set,
2043 struct genl_family dp_vport_genl_family = {
2044 .id = GENL_ID_GENERATE,
2045 .hdrsize = sizeof(struct ovs_header),
2046 .name = OVS_VPORT_FAMILY,
2047 .version = OVS_VPORT_VERSION,
2048 .maxattr = OVS_VPORT_ATTR_MAX,
2050 .parallel_ops = true,
2051 .ops = dp_vport_genl_ops,
2052 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2053 .mcgrps = &ovs_dp_vport_multicast_group,
2057 static struct genl_family *dp_genl_families[] = {
2058 &dp_datapath_genl_family,
2059 &dp_vport_genl_family,
2060 &dp_flow_genl_family,
2061 &dp_packet_genl_family,
2064 static void dp_unregister_genl(int n_families)
2068 for (i = 0; i < n_families; i++)
2069 genl_unregister_family(dp_genl_families[i]);
2072 static int dp_register_genl(void)
2077 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2079 err = genl_register_family(dp_genl_families[i]);
2087 dp_unregister_genl(i);
2091 static int __net_init ovs_init_net(struct net *net)
2093 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2095 INIT_LIST_HEAD(&ovs_net->dps);
2096 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2100 static void __net_exit ovs_exit_net(struct net *net)
2102 struct datapath *dp, *dp_next;
2103 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2106 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2110 cancel_work_sync(&ovs_net->dp_notify_work);
2113 static struct pernet_operations ovs_net_ops = {
2114 .init = ovs_init_net,
2115 .exit = ovs_exit_net,
2117 .size = sizeof(struct ovs_net),
2120 DEFINE_COMPAT_PNET_REG_FUNC(device);
2122 static int __init dp_init(void)
2126 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2128 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2131 err = action_fifos_init();
2135 err = ovs_flow_init();
2137 goto error_action_fifos_exit;
2139 err = ovs_vport_init();
2141 goto error_flow_exit;
2143 err = register_pernet_device(&ovs_net_ops);
2145 goto error_vport_exit;
2147 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2149 goto error_netns_exit;
2151 err = dp_register_genl();
2153 goto error_unreg_notifier;
2157 error_unreg_notifier:
2158 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2160 unregister_pernet_device(&ovs_net_ops);
2165 error_action_fifos_exit:
2166 action_fifos_exit();
2171 static void dp_cleanup(void)
2173 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2174 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2175 unregister_pernet_device(&ovs_net_ops);
2179 action_fifos_exit();
2182 module_init(dp_init);
2183 module_exit(dp_cleanup);
2185 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2186 MODULE_LICENSE("GPL");
2187 MODULE_VERSION(VERSION);