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.
84 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
87 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
88 genl_has_listeners(family, genl_info_net(info)->genl_sock,
92 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
93 struct sk_buff *skb, struct genl_info *info)
95 genl_notify(family, skb, genl_info_net(info),
96 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
102 * All writes e.g. Writes to device state (add/remove datapath, port, set
103 * operations on vports, etc.), Writes to other state (flow table
104 * modifications, set miscellaneous datapath parameters, etc.) are protected
107 * Reads are protected by RCU.
109 * There are a few special cases (mostly stats) that have their own
110 * synchronization but they nest under all of above and don't interact with
113 * The RTNL lock nests inside ovs_mutex.
116 static DEFINE_MUTEX(ovs_mutex);
120 mutex_lock(&ovs_mutex);
123 void ovs_unlock(void)
125 mutex_unlock(&ovs_mutex);
128 #ifdef CONFIG_LOCKDEP
129 int lockdep_ovsl_is_held(void)
132 return lockdep_is_held(&ovs_mutex);
138 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
139 const struct sw_flow_key *,
140 const struct dp_upcall_info *);
141 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
142 const struct sw_flow_key *key,
143 const struct dp_upcall_info *);
145 /* Must be called with rcu_read_lock. */
146 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
148 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
151 struct vport *vport = ovs_internal_dev_get_vport(dev);
159 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
160 * returned dp pointer valid.
162 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
166 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
168 dp = get_dp_rcu(net, dp_ifindex);
174 /* Must be called with rcu_read_lock or ovs_mutex. */
175 const char *ovs_dp_name(const struct datapath *dp)
177 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
178 return vport->ops->get_name(vport);
181 static int get_dpifindex(const struct datapath *dp)
188 local = ovs_vport_rcu(dp, OVSP_LOCAL);
190 ifindex = netdev_vport_priv(local)->dev->ifindex;
199 static void destroy_dp_rcu(struct rcu_head *rcu)
201 struct datapath *dp = container_of(rcu, struct datapath, rcu);
203 ovs_flow_tbl_destroy(&dp->table);
204 free_percpu(dp->stats_percpu);
205 release_net(ovs_dp_get_net(dp));
210 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
213 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
216 /* Called with ovs_mutex or RCU read lock. */
217 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
220 struct hlist_head *head;
222 head = vport_hash_bucket(dp, port_no);
223 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
224 if (vport->port_no == port_no)
230 /* Called with ovs_mutex. */
231 static struct vport *new_vport(const struct vport_parms *parms)
235 vport = ovs_vport_add(parms);
236 if (!IS_ERR(vport)) {
237 struct datapath *dp = parms->dp;
238 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
240 hlist_add_head_rcu(&vport->dp_hash_node, head);
245 void ovs_dp_detach_port(struct vport *p)
249 /* First drop references to device. */
250 hlist_del_rcu(&p->dp_hash_node);
252 /* Then destroy it. */
256 /* Must be called with rcu_read_lock. */
257 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
259 const struct vport *p = OVS_CB(skb)->input_vport;
260 struct datapath *dp = p->dp;
261 struct sw_flow *flow;
262 struct sw_flow_actions *sf_acts;
263 struct dp_stats_percpu *stats;
267 stats = this_cpu_ptr(dp->stats_percpu);
270 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
272 if (unlikely(!flow)) {
273 struct dp_upcall_info upcall;
276 upcall.cmd = OVS_PACKET_CMD_MISS;
277 upcall.userdata = NULL;
278 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
279 upcall.egress_tun_info = NULL;
281 error = ovs_dp_upcall(dp, skb, key, &upcall);
287 stats_counter = &stats->n_missed;
291 ovs_flow_stats_update(flow, key->tp.flags, skb);
293 sf_acts = rcu_dereference(flow->sf_acts);
294 ovs_execute_actions(dp, skb, key, sf_acts);
295 stats_counter = &stats->n_hit;
298 /* Update datapath statistics. */
299 u64_stats_update_begin(&stats->syncp);
301 stats->n_mask_hit += n_mask_hit;
302 u64_stats_update_end(&stats->syncp);
305 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
306 const struct sw_flow_key *key,
307 const struct dp_upcall_info *upcall_info)
309 struct dp_stats_percpu *stats;
312 if (upcall_info->portid == 0) {
317 if (!skb_is_gso(skb))
318 err = queue_userspace_packet(dp, skb, key, upcall_info);
320 err = queue_gso_packets(dp, skb, key, upcall_info);
327 stats = this_cpu_ptr(dp->stats_percpu);
329 u64_stats_update_begin(&stats->syncp);
331 u64_stats_update_end(&stats->syncp);
336 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
337 const struct sw_flow_key *key,
338 const struct dp_upcall_info *upcall_info)
340 unsigned short gso_type = skb_shinfo(skb)->gso_type;
341 struct sw_flow_key later_key;
342 struct sk_buff *segs, *nskb;
343 struct ovs_skb_cb ovs_cb;
346 ovs_cb = *OVS_CB(skb);
347 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
348 *OVS_CB(skb) = ovs_cb;
350 return PTR_ERR(segs);
354 if (gso_type & SKB_GSO_UDP) {
355 /* The initial flow key extracted by ovs_flow_key_extract()
356 * in this case is for a first fragment, so we need to
357 * properly mark later fragments.
360 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
363 /* Queue all of the segments. */
366 *OVS_CB(skb) = ovs_cb;
367 if (gso_type & SKB_GSO_UDP && skb != segs)
370 err = queue_userspace_packet(dp, skb, key, upcall_info);
374 } while ((skb = skb->next));
376 /* Free all of the segments. */
384 } while ((skb = nskb));
388 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
391 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
392 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
393 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
395 /* OVS_PACKET_ATTR_USERDATA */
396 if (upcall_info->userdata)
397 size += NLA_ALIGN(upcall_info->userdata->nla_len);
399 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
400 if (upcall_info->egress_tun_info)
401 size += nla_total_size(ovs_tun_key_attr_size());
406 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
407 const struct sw_flow_key *key,
408 const struct dp_upcall_info *upcall_info)
410 struct ovs_header *upcall;
411 struct sk_buff *nskb = NULL;
412 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
414 struct genl_info info = {
415 #ifdef HAVE_GENLMSG_NEW_UNICAST
416 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
418 .snd_portid = upcall_info->portid,
424 dp_ifindex = get_dpifindex(dp);
428 if (vlan_tx_tag_present(skb)) {
429 nskb = skb_clone(skb, GFP_ATOMIC);
433 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
437 vlan_set_tci(nskb, 0);
442 if (nla_attr_size(skb->len) > USHRT_MAX) {
447 /* Complete checksum if needed */
448 if (skb->ip_summed == CHECKSUM_PARTIAL &&
449 (err = skb_checksum_help(skb)))
452 /* Older versions of OVS user space enforce alignment of the last
453 * Netlink attribute to NLA_ALIGNTO which would require extensive
454 * padding logic. Only perform zerocopy if padding is not required.
456 if (dp->user_features & OVS_DP_F_UNALIGNED)
457 hlen = skb_zerocopy_headlen(skb);
461 len = upcall_msg_size(upcall_info, hlen);
462 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
468 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
469 0, upcall_info->cmd);
470 upcall->dp_ifindex = dp_ifindex;
472 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
473 err = ovs_nla_put_flow(key, key, user_skb);
475 nla_nest_end(user_skb, nla);
477 if (upcall_info->userdata)
478 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
479 nla_len(upcall_info->userdata),
480 nla_data(upcall_info->userdata));
482 if (upcall_info->egress_tun_info) {
483 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
484 err = ovs_nla_put_egress_tunnel_key(user_skb,
485 upcall_info->egress_tun_info);
487 nla_nest_end(user_skb, nla);
490 /* Only reserve room for attribute header, packet data is added
493 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
497 nla->nla_len = nla_attr_size(skb->len);
499 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
503 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
504 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
505 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
508 memset(skb_put(user_skb, plen), 0, plen);
511 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
513 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
524 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
526 struct ovs_header *ovs_header = info->userhdr;
527 struct nlattr **a = info->attrs;
528 struct sw_flow_actions *acts;
529 struct sk_buff *packet;
530 struct sw_flow *flow;
531 struct sw_flow_actions *sf_acts;
534 struct vport *input_vport;
537 bool log = !a[OVS_FLOW_ATTR_PROBE];
540 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
541 !a[OVS_PACKET_ATTR_ACTIONS])
544 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
545 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
549 skb_reserve(packet, NET_IP_ALIGN);
551 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
553 skb_reset_mac_header(packet);
554 eth = eth_hdr(packet);
556 /* Normally, setting the skb 'protocol' field would be handled by a
557 * call to eth_type_trans(), but it assumes there's a sending
558 * device, which we may not have.
560 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
561 packet->protocol = eth->h_proto;
563 packet->protocol = htons(ETH_P_802_2);
565 /* Build an sw_flow for sending this packet. */
566 flow = ovs_flow_alloc();
571 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
576 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
577 &flow->key, &acts, log);
581 rcu_assign_pointer(flow->sf_acts, acts);
582 OVS_CB(packet)->egress_tun_info = NULL;
583 packet->priority = flow->key.phy.priority;
584 packet->mark = flow->key.phy.skb_mark;
587 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
592 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
594 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
599 OVS_CB(packet)->input_vport = input_vport;
600 sf_acts = rcu_dereference(flow->sf_acts);
603 err = ovs_execute_actions(dp, packet, &flow->key, sf_acts);
607 ovs_flow_free(flow, false);
613 ovs_flow_free(flow, false);
620 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
621 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
622 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
623 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
626 static struct genl_ops dp_packet_genl_ops[] = {
627 { .cmd = OVS_PACKET_CMD_EXECUTE,
628 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
629 .policy = packet_policy,
630 .doit = ovs_packet_cmd_execute
634 static struct genl_family dp_packet_genl_family = {
635 .id = GENL_ID_GENERATE,
636 .hdrsize = sizeof(struct ovs_header),
637 .name = OVS_PACKET_FAMILY,
638 .version = OVS_PACKET_VERSION,
639 .maxattr = OVS_PACKET_ATTR_MAX,
641 .parallel_ops = true,
642 .ops = dp_packet_genl_ops,
643 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
646 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
647 struct ovs_dp_megaflow_stats *mega_stats)
651 memset(mega_stats, 0, sizeof(*mega_stats));
653 stats->n_flows = ovs_flow_tbl_count(&dp->table);
654 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
656 stats->n_hit = stats->n_missed = stats->n_lost = 0;
658 for_each_possible_cpu(i) {
659 const struct dp_stats_percpu *percpu_stats;
660 struct dp_stats_percpu local_stats;
663 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
666 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
667 local_stats = *percpu_stats;
668 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
670 stats->n_hit += local_stats.n_hit;
671 stats->n_missed += local_stats.n_missed;
672 stats->n_lost += local_stats.n_lost;
673 mega_stats->n_mask_hit += local_stats.n_mask_hit;
677 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
679 return NLMSG_ALIGN(sizeof(struct ovs_header))
680 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
681 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
682 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
683 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
684 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
685 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
688 /* Called with ovs_mutex or RCU read lock. */
689 static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
696 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
700 err = ovs_nla_put_flow(&flow->unmasked_key,
701 &flow->unmasked_key, skb);
704 nla_nest_end(skb, nla);
706 /* Fill flow mask. */
707 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
711 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
714 nla_nest_end(skb, nla);
719 /* Called with ovs_mutex or RCU read lock. */
720 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
723 struct ovs_flow_stats stats;
727 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
730 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
733 if (stats.n_packets &&
734 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
737 if ((u8)ntohs(tcp_flags) &&
738 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
744 /* Called with ovs_mutex or RCU read lock. */
745 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
746 struct sk_buff *skb, int skb_orig_len)
748 struct nlattr *start;
751 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
752 * this is the first flow to be dumped into 'skb'. This is unusual for
753 * Netlink but individual action lists can be longer than
754 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
755 * The userspace caller can always fetch the actions separately if it
756 * really wants them. (Most userspace callers in fact don't care.)
758 * This can only fail for dump operations because the skb is always
759 * properly sized for single flows.
761 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
763 const struct sw_flow_actions *sf_acts;
765 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
766 err = ovs_nla_put_actions(sf_acts->actions,
767 sf_acts->actions_len, skb);
770 nla_nest_end(skb, start);
775 nla_nest_cancel(skb, start);
777 } else if (skb_orig_len) {
784 /* Called with ovs_mutex or RCU read lock. */
785 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
786 struct sk_buff *skb, u32 portid,
787 u32 seq, u32 flags, u8 cmd)
789 const int skb_orig_len = skb->len;
790 struct ovs_header *ovs_header;
793 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
796 ovs_header->dp_ifindex = dp_ifindex;
798 err = ovs_flow_cmd_fill_match(flow, skb);
802 err = ovs_flow_cmd_fill_stats(flow, skb);
806 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
810 return genlmsg_end(skb, ovs_header);
813 genlmsg_cancel(skb, ovs_header);
817 /* May not be called with RCU read lock. */
818 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
819 struct genl_info *info,
824 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
825 GROUP_ID(&ovs_dp_flow_multicast_group)))
828 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
831 return ERR_PTR(-ENOMEM);
836 /* Called with ovs_mutex. */
837 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
838 const struct sw_flow *flow,
840 struct genl_info *info, u8 cmd,
846 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
848 if (IS_ERR_OR_NULL(skb))
851 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
852 info->snd_portid, info->snd_seq, 0,
858 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
860 struct nlattr **a = info->attrs;
861 struct ovs_header *ovs_header = info->userhdr;
862 struct sw_flow *flow, *new_flow;
863 struct sw_flow_mask mask;
864 struct sk_buff *reply;
866 struct sw_flow_actions *acts;
867 struct sw_flow_match match;
869 bool log = !a[OVS_FLOW_ATTR_PROBE];
871 /* Must have key and actions. */
873 if (!a[OVS_FLOW_ATTR_KEY]) {
874 OVS_NLERR(log, "Flow key attribute not present in new flow.");
877 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
879 "Flow actions attribute not present in new flow.");
883 /* Most of the time we need to allocate a new flow, do it before
886 new_flow = ovs_flow_alloc();
887 if (IS_ERR(new_flow)) {
888 error = PTR_ERR(new_flow);
893 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
894 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
895 a[OVS_FLOW_ATTR_MASK], log);
899 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
901 /* Validate actions. */
902 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
907 "Flow actions may not be safe on all matching packets."
912 reply = ovs_flow_cmd_alloc_info(acts, info, false);
914 error = PTR_ERR(reply);
919 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
924 /* Check if this is a duplicate flow */
925 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
927 rcu_assign_pointer(new_flow->sf_acts, acts);
929 /* Put flow in bucket. */
930 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
931 if (unlikely(error)) {
936 if (unlikely(reply)) {
937 error = ovs_flow_cmd_fill_info(new_flow,
938 ovs_header->dp_ifindex,
939 reply, info->snd_portid,
946 struct sw_flow_actions *old_acts;
948 /* Bail out if we're not allowed to modify an existing flow.
949 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
950 * because Generic Netlink treats the latter as a dump
951 * request. We also accept NLM_F_EXCL in case that bug ever
954 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
959 /* The unmasked key has to be the same for flow updates. */
960 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
961 /* Look for any overlapping flow. */
962 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
968 /* Update actions. */
969 old_acts = ovsl_dereference(flow->sf_acts);
970 rcu_assign_pointer(flow->sf_acts, acts);
972 if (unlikely(reply)) {
973 error = ovs_flow_cmd_fill_info(flow,
974 ovs_header->dp_ifindex,
975 reply, info->snd_portid,
982 ovs_nla_free_flow_actions(old_acts);
983 ovs_flow_free(new_flow, false);
987 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
996 ovs_flow_free(new_flow, false);
1001 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1002 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1003 const struct sw_flow_key *key,
1004 const struct sw_flow_mask *mask,
1007 struct sw_flow_actions *acts;
1008 struct sw_flow_key masked_key;
1011 ovs_flow_mask_key(&masked_key, key, mask);
1012 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
1015 "Actions may not be safe on all matching packets.");
1016 return ERR_PTR(error);
1022 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1024 struct nlattr **a = info->attrs;
1025 struct ovs_header *ovs_header = info->userhdr;
1026 struct sw_flow_key key;
1027 struct sw_flow *flow;
1028 struct sw_flow_mask mask;
1029 struct sk_buff *reply = NULL;
1030 struct datapath *dp;
1031 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1032 struct sw_flow_match match;
1034 bool log = !a[OVS_FLOW_ATTR_PROBE];
1038 if (!a[OVS_FLOW_ATTR_KEY]) {
1039 OVS_NLERR(log, "Flow key attribute not present in set flow.");
1043 ovs_match_init(&match, &key, &mask);
1044 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1045 a[OVS_FLOW_ATTR_MASK], log);
1049 /* Validate actions. */
1050 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1051 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1054 error = PTR_ERR(acts);
1058 /* Can allocate before locking if have acts. */
1059 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1060 if (IS_ERR(reply)) {
1061 error = PTR_ERR(reply);
1062 goto err_kfree_acts;
1067 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1068 if (unlikely(!dp)) {
1070 goto err_unlock_ovs;
1072 /* Check that the flow exists. */
1073 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1074 if (unlikely(!flow)) {
1076 goto err_unlock_ovs;
1079 /* Update actions, if present. */
1081 old_acts = ovsl_dereference(flow->sf_acts);
1082 rcu_assign_pointer(flow->sf_acts, acts);
1084 if (unlikely(reply)) {
1085 error = ovs_flow_cmd_fill_info(flow,
1086 ovs_header->dp_ifindex,
1087 reply, info->snd_portid,
1093 /* Could not alloc without acts before locking. */
1094 reply = ovs_flow_cmd_build_info(dp, flow,
1095 ovs_header->dp_ifindex,
1096 info, OVS_FLOW_CMD_NEW, false);
1097 if (unlikely(IS_ERR(reply))) {
1098 error = PTR_ERR(reply);
1099 goto err_unlock_ovs;
1104 if (a[OVS_FLOW_ATTR_CLEAR])
1105 ovs_flow_stats_clear(flow);
1109 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1111 ovs_nla_free_flow_actions(old_acts);
1123 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1125 struct nlattr **a = info->attrs;
1126 struct ovs_header *ovs_header = info->userhdr;
1127 struct sw_flow_key key;
1128 struct sk_buff *reply;
1129 struct sw_flow *flow;
1130 struct datapath *dp;
1131 struct sw_flow_match match;
1133 bool log = !a[OVS_FLOW_ATTR_PROBE];
1135 if (!a[OVS_FLOW_ATTR_KEY]) {
1137 "Flow get message rejected, Key attribute missing.");
1141 ovs_match_init(&match, &key, NULL);
1142 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, log);
1147 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1153 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1159 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1160 OVS_FLOW_CMD_NEW, true);
1161 if (IS_ERR(reply)) {
1162 err = PTR_ERR(reply);
1167 return genlmsg_reply(reply, info);
1173 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1175 struct nlattr **a = info->attrs;
1176 struct ovs_header *ovs_header = info->userhdr;
1177 struct sw_flow_key key;
1178 struct sk_buff *reply;
1179 struct sw_flow *flow;
1180 struct datapath *dp;
1181 struct sw_flow_match match;
1183 bool log = !a[OVS_FLOW_ATTR_PROBE];
1185 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1186 ovs_match_init(&match, &key, NULL);
1187 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1194 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1195 if (unlikely(!dp)) {
1199 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1200 err = ovs_flow_tbl_flush(&dp->table);
1203 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1204 if (unlikely(!flow)) {
1209 ovs_flow_tbl_remove(&dp->table, flow);
1212 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1215 if (likely(reply)) {
1216 if (likely(!IS_ERR(reply))) {
1217 rcu_read_lock(); /* Keep RCU checker happy. */
1218 err = ovs_flow_cmd_fill_info(flow,
1219 ovs_header->dp_ifindex,
1220 reply, info->snd_portid,
1225 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1227 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1228 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1233 ovs_flow_free(flow, true);
1240 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1242 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1243 struct table_instance *ti;
1244 struct datapath *dp;
1247 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1253 ti = rcu_dereference(dp->table.ti);
1255 struct sw_flow *flow;
1258 bucket = cb->args[0];
1260 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1264 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1265 NETLINK_CB(cb->skb).portid,
1266 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1267 OVS_FLOW_CMD_NEW) < 0)
1270 cb->args[0] = bucket;
1277 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1278 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1279 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1280 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1281 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1282 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1285 static struct genl_ops dp_flow_genl_ops[] = {
1286 { .cmd = OVS_FLOW_CMD_NEW,
1287 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1288 .policy = flow_policy,
1289 .doit = ovs_flow_cmd_new
1291 { .cmd = OVS_FLOW_CMD_DEL,
1292 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1293 .policy = flow_policy,
1294 .doit = ovs_flow_cmd_del
1296 { .cmd = OVS_FLOW_CMD_GET,
1297 .flags = 0, /* OK for unprivileged users. */
1298 .policy = flow_policy,
1299 .doit = ovs_flow_cmd_get,
1300 .dumpit = ovs_flow_cmd_dump
1302 { .cmd = OVS_FLOW_CMD_SET,
1303 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1304 .policy = flow_policy,
1305 .doit = ovs_flow_cmd_set,
1309 static struct genl_family dp_flow_genl_family = {
1310 .id = GENL_ID_GENERATE,
1311 .hdrsize = sizeof(struct ovs_header),
1312 .name = OVS_FLOW_FAMILY,
1313 .version = OVS_FLOW_VERSION,
1314 .maxattr = OVS_FLOW_ATTR_MAX,
1316 .parallel_ops = true,
1317 .ops = dp_flow_genl_ops,
1318 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1319 .mcgrps = &ovs_dp_flow_multicast_group,
1323 static size_t ovs_dp_cmd_msg_size(void)
1325 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1327 msgsize += nla_total_size(IFNAMSIZ);
1328 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1329 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1330 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1335 /* Called with ovs_mutex. */
1336 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1337 u32 portid, u32 seq, u32 flags, u8 cmd)
1339 struct ovs_header *ovs_header;
1340 struct ovs_dp_stats dp_stats;
1341 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1344 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1349 ovs_header->dp_ifindex = get_dpifindex(dp);
1351 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1353 goto nla_put_failure;
1355 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1356 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1358 goto nla_put_failure;
1360 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1361 sizeof(struct ovs_dp_megaflow_stats),
1362 &dp_megaflow_stats))
1363 goto nla_put_failure;
1365 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1366 goto nla_put_failure;
1368 return genlmsg_end(skb, ovs_header);
1371 genlmsg_cancel(skb, ovs_header);
1376 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1378 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1381 /* Called with rcu_read_lock or ovs_mutex. */
1382 static struct datapath *lookup_datapath(struct net *net,
1383 const struct ovs_header *ovs_header,
1384 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1386 struct datapath *dp;
1388 if (!a[OVS_DP_ATTR_NAME])
1389 dp = get_dp(net, ovs_header->dp_ifindex);
1391 struct vport *vport;
1393 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1394 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1396 return dp ? dp : ERR_PTR(-ENODEV);
1399 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1401 struct datapath *dp;
1403 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1407 WARN(dp->user_features, "Dropping previously announced user features\n");
1408 dp->user_features = 0;
1411 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1413 if (a[OVS_DP_ATTR_USER_FEATURES])
1414 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1417 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1419 struct nlattr **a = info->attrs;
1420 struct vport_parms parms;
1421 struct sk_buff *reply;
1422 struct datapath *dp;
1423 struct vport *vport;
1424 struct ovs_net *ovs_net;
1428 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1431 reply = ovs_dp_cmd_alloc_info(info);
1436 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1438 goto err_free_reply;
1440 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1442 /* Allocate table. */
1443 err = ovs_flow_tbl_init(&dp->table);
1447 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1448 if (!dp->stats_percpu) {
1450 goto err_destroy_table;
1453 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1457 goto err_destroy_percpu;
1460 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1461 INIT_HLIST_HEAD(&dp->ports[i]);
1463 /* Set up our datapath device. */
1464 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1465 parms.type = OVS_VPORT_TYPE_INTERNAL;
1466 parms.options = NULL;
1468 parms.port_no = OVSP_LOCAL;
1469 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1471 ovs_dp_change(dp, a);
1473 /* So far only local changes have been made, now need the lock. */
1476 vport = new_vport(&parms);
1477 if (IS_ERR(vport)) {
1478 err = PTR_ERR(vport);
1482 if (err == -EEXIST) {
1483 /* An outdated user space instance that does not understand
1484 * the concept of user_features has attempted to create a new
1485 * datapath and is likely to reuse it. Drop all user features.
1487 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1488 ovs_dp_reset_user_features(skb, info);
1491 goto err_destroy_ports_array;
1494 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1495 info->snd_seq, 0, OVS_DP_CMD_NEW);
1498 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1499 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1503 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1506 err_destroy_ports_array:
1510 free_percpu(dp->stats_percpu);
1512 ovs_flow_tbl_destroy(&dp->table);
1514 release_net(ovs_dp_get_net(dp));
1522 /* Called with ovs_mutex. */
1523 static void __dp_destroy(struct datapath *dp)
1527 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1528 struct vport *vport;
1529 struct hlist_node *n;
1531 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1532 if (vport->port_no != OVSP_LOCAL)
1533 ovs_dp_detach_port(vport);
1536 list_del_rcu(&dp->list_node);
1538 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1539 * all ports in datapath are destroyed first before freeing datapath.
1541 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1543 /* RCU destroy the flow table */
1544 call_rcu(&dp->rcu, destroy_dp_rcu);
1547 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1549 struct sk_buff *reply;
1550 struct datapath *dp;
1553 reply = ovs_dp_cmd_alloc_info(info);
1558 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1561 goto err_unlock_free;
1563 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1564 info->snd_seq, 0, OVS_DP_CMD_DEL);
1570 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1579 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1581 struct sk_buff *reply;
1582 struct datapath *dp;
1585 reply = ovs_dp_cmd_alloc_info(info);
1590 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1593 goto err_unlock_free;
1595 ovs_dp_change(dp, info->attrs);
1597 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1598 info->snd_seq, 0, OVS_DP_CMD_NEW);
1602 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1611 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1613 struct sk_buff *reply;
1614 struct datapath *dp;
1617 reply = ovs_dp_cmd_alloc_info(info);
1622 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1625 goto err_unlock_free;
1627 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1628 info->snd_seq, 0, OVS_DP_CMD_NEW);
1632 return genlmsg_reply(reply, info);
1640 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1642 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1643 struct datapath *dp;
1644 int skip = cb->args[0];
1648 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1650 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1651 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1652 OVS_DP_CMD_NEW) < 0)
1663 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1664 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1665 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1666 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1669 static struct genl_ops dp_datapath_genl_ops[] = {
1670 { .cmd = OVS_DP_CMD_NEW,
1671 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1672 .policy = datapath_policy,
1673 .doit = ovs_dp_cmd_new
1675 { .cmd = OVS_DP_CMD_DEL,
1676 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1677 .policy = datapath_policy,
1678 .doit = ovs_dp_cmd_del
1680 { .cmd = OVS_DP_CMD_GET,
1681 .flags = 0, /* OK for unprivileged users. */
1682 .policy = datapath_policy,
1683 .doit = ovs_dp_cmd_get,
1684 .dumpit = ovs_dp_cmd_dump
1686 { .cmd = OVS_DP_CMD_SET,
1687 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1688 .policy = datapath_policy,
1689 .doit = ovs_dp_cmd_set,
1693 static struct genl_family dp_datapath_genl_family = {
1694 .id = GENL_ID_GENERATE,
1695 .hdrsize = sizeof(struct ovs_header),
1696 .name = OVS_DATAPATH_FAMILY,
1697 .version = OVS_DATAPATH_VERSION,
1698 .maxattr = OVS_DP_ATTR_MAX,
1700 .parallel_ops = true,
1701 .ops = dp_datapath_genl_ops,
1702 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1703 .mcgrps = &ovs_dp_datapath_multicast_group,
1707 /* Called with ovs_mutex or RCU read lock. */
1708 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1709 u32 portid, u32 seq, u32 flags, u8 cmd)
1711 struct ovs_header *ovs_header;
1712 struct ovs_vport_stats vport_stats;
1715 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1720 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1722 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1723 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1724 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1725 goto nla_put_failure;
1727 ovs_vport_get_stats(vport, &vport_stats);
1728 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1730 goto nla_put_failure;
1732 if (ovs_vport_get_upcall_portids(vport, skb))
1733 goto nla_put_failure;
1735 err = ovs_vport_get_options(vport, skb);
1736 if (err == -EMSGSIZE)
1739 return genlmsg_end(skb, ovs_header);
1744 genlmsg_cancel(skb, ovs_header);
1748 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1750 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1753 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1754 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1757 struct sk_buff *skb;
1760 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1762 return ERR_PTR(-ENOMEM);
1764 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1770 /* Called with ovs_mutex or RCU read lock. */
1771 static struct vport *lookup_vport(struct net *net,
1772 const struct ovs_header *ovs_header,
1773 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1775 struct datapath *dp;
1776 struct vport *vport;
1778 if (a[OVS_VPORT_ATTR_NAME]) {
1779 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1781 return ERR_PTR(-ENODEV);
1782 if (ovs_header->dp_ifindex &&
1783 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1784 return ERR_PTR(-ENODEV);
1786 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1787 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1789 if (port_no >= DP_MAX_PORTS)
1790 return ERR_PTR(-EFBIG);
1792 dp = get_dp(net, ovs_header->dp_ifindex);
1794 return ERR_PTR(-ENODEV);
1796 vport = ovs_vport_ovsl_rcu(dp, port_no);
1798 return ERR_PTR(-ENODEV);
1801 return ERR_PTR(-EINVAL);
1804 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1806 struct nlattr **a = info->attrs;
1807 struct ovs_header *ovs_header = info->userhdr;
1808 struct vport_parms parms;
1809 struct sk_buff *reply;
1810 struct vport *vport;
1811 struct datapath *dp;
1815 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1816 !a[OVS_VPORT_ATTR_UPCALL_PID])
1819 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1820 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1821 if (port_no >= DP_MAX_PORTS)
1824 reply = ovs_vport_cmd_alloc_info();
1829 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1832 goto exit_unlock_free;
1835 vport = ovs_vport_ovsl(dp, port_no);
1838 goto exit_unlock_free;
1840 for (port_no = 1; ; port_no++) {
1841 if (port_no >= DP_MAX_PORTS) {
1843 goto exit_unlock_free;
1845 vport = ovs_vport_ovsl(dp, port_no);
1851 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1852 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1853 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1855 parms.port_no = port_no;
1856 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1858 vport = new_vport(&parms);
1859 err = PTR_ERR(vport);
1861 goto exit_unlock_free;
1863 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1864 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1868 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1877 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1879 struct nlattr **a = info->attrs;
1880 struct sk_buff *reply;
1881 struct vport *vport;
1884 reply = ovs_vport_cmd_alloc_info();
1889 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1890 err = PTR_ERR(vport);
1892 goto exit_unlock_free;
1894 if (a[OVS_VPORT_ATTR_TYPE] &&
1895 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1897 goto exit_unlock_free;
1900 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1901 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1903 goto exit_unlock_free;
1906 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1907 err = ovs_vport_set_upcall_portids(vport,
1908 a[OVS_VPORT_ATTR_UPCALL_PID]);
1910 goto exit_unlock_free;
1913 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1914 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1918 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1927 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1929 struct nlattr **a = info->attrs;
1930 struct sk_buff *reply;
1931 struct vport *vport;
1934 reply = ovs_vport_cmd_alloc_info();
1939 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1940 err = PTR_ERR(vport);
1942 goto exit_unlock_free;
1944 if (vport->port_no == OVSP_LOCAL) {
1946 goto exit_unlock_free;
1949 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1950 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1952 ovs_dp_detach_port(vport);
1955 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1964 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1966 struct nlattr **a = info->attrs;
1967 struct ovs_header *ovs_header = info->userhdr;
1968 struct sk_buff *reply;
1969 struct vport *vport;
1972 reply = ovs_vport_cmd_alloc_info();
1977 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1978 err = PTR_ERR(vport);
1980 goto exit_unlock_free;
1981 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1982 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1986 return genlmsg_reply(reply, info);
1994 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1996 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1997 struct datapath *dp;
1998 int bucket = cb->args[0], skip = cb->args[1];
2002 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2007 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2008 struct vport *vport;
2011 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2013 ovs_vport_cmd_fill_info(vport, skb,
2014 NETLINK_CB(cb->skb).portid,
2017 OVS_VPORT_CMD_NEW) < 0)
2033 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2034 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2035 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2036 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2037 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2038 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2039 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2042 static struct genl_ops dp_vport_genl_ops[] = {
2043 { .cmd = OVS_VPORT_CMD_NEW,
2044 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2045 .policy = vport_policy,
2046 .doit = ovs_vport_cmd_new
2048 { .cmd = OVS_VPORT_CMD_DEL,
2049 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2050 .policy = vport_policy,
2051 .doit = ovs_vport_cmd_del
2053 { .cmd = OVS_VPORT_CMD_GET,
2054 .flags = 0, /* OK for unprivileged users. */
2055 .policy = vport_policy,
2056 .doit = ovs_vport_cmd_get,
2057 .dumpit = ovs_vport_cmd_dump
2059 { .cmd = OVS_VPORT_CMD_SET,
2060 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2061 .policy = vport_policy,
2062 .doit = ovs_vport_cmd_set,
2066 struct genl_family dp_vport_genl_family = {
2067 .id = GENL_ID_GENERATE,
2068 .hdrsize = sizeof(struct ovs_header),
2069 .name = OVS_VPORT_FAMILY,
2070 .version = OVS_VPORT_VERSION,
2071 .maxattr = OVS_VPORT_ATTR_MAX,
2073 .parallel_ops = true,
2074 .ops = dp_vport_genl_ops,
2075 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2076 .mcgrps = &ovs_dp_vport_multicast_group,
2080 static struct genl_family *dp_genl_families[] = {
2081 &dp_datapath_genl_family,
2082 &dp_vport_genl_family,
2083 &dp_flow_genl_family,
2084 &dp_packet_genl_family,
2087 static void dp_unregister_genl(int n_families)
2091 for (i = 0; i < n_families; i++)
2092 genl_unregister_family(dp_genl_families[i]);
2095 static int dp_register_genl(void)
2100 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2102 err = genl_register_family(dp_genl_families[i]);
2110 dp_unregister_genl(i);
2114 static int __net_init ovs_init_net(struct net *net)
2116 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2118 INIT_LIST_HEAD(&ovs_net->dps);
2119 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2123 static void __net_exit ovs_exit_net(struct net *net)
2125 struct datapath *dp, *dp_next;
2126 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2129 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2133 cancel_work_sync(&ovs_net->dp_notify_work);
2136 static struct pernet_operations ovs_net_ops = {
2137 .init = ovs_init_net,
2138 .exit = ovs_exit_net,
2140 .size = sizeof(struct ovs_net),
2143 DEFINE_COMPAT_PNET_REG_FUNC(device);
2145 static int __init dp_init(void)
2149 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2151 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2154 err = action_fifos_init();
2158 err = ovs_flow_init();
2160 goto error_action_fifos_exit;
2162 err = ovs_vport_init();
2164 goto error_flow_exit;
2166 err = register_pernet_device(&ovs_net_ops);
2168 goto error_vport_exit;
2170 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2172 goto error_netns_exit;
2174 err = dp_register_genl();
2176 goto error_unreg_notifier;
2180 error_unreg_notifier:
2181 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2183 unregister_pernet_device(&ovs_net_ops);
2188 error_action_fifos_exit:
2189 action_fifos_exit();
2194 static void dp_cleanup(void)
2196 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2197 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2198 unregister_pernet_device(&ovs_net_ops);
2202 action_fifos_exit();
2205 module_init(dp_init);
2206 module_exit(dp_cleanup);
2208 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2209 MODULE_LICENSE("GPL");
2210 MODULE_VERSION(VERSION);