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 *,
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;
280 error = ovs_dp_upcall(dp, skb, key, &upcall);
285 stats_counter = &stats->n_missed;
289 ovs_flow_stats_update(flow, key->tp.flags, skb);
290 sf_acts = rcu_dereference(flow->sf_acts);
291 ovs_execute_actions(dp, skb, sf_acts, key);
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);
352 if (gso_type & SKB_GSO_UDP) {
353 /* The initial flow key extracted by ovs_flow_key_extract()
354 * in this case is for a first fragment, so we need to
355 * properly mark later fragments.
358 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
361 /* Queue all of the segments. */
364 *OVS_CB(skb) = ovs_cb;
365 if (gso_type & SKB_GSO_UDP && skb != segs)
368 err = queue_userspace_packet(dp, skb, key, upcall_info);
372 } while ((skb = skb->next));
374 /* Free all of the segments. */
382 } while ((skb = nskb));
386 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
389 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
390 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
391 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
393 /* OVS_PACKET_ATTR_USERDATA */
394 if (upcall_info->userdata)
395 size += NLA_ALIGN(upcall_info->userdata->nla_len);
397 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
398 if (upcall_info->egress_tun_info)
399 size += nla_total_size(ovs_tun_key_attr_size());
404 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
405 const struct sw_flow_key *key,
406 const struct dp_upcall_info *upcall_info)
408 struct ovs_header *upcall;
409 struct sk_buff *nskb = NULL;
410 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
412 struct genl_info info = {
413 #ifdef HAVE_GENLMSG_NEW_UNICAST
414 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
416 .snd_portid = upcall_info->portid,
422 dp_ifindex = get_dpifindex(dp);
426 if (vlan_tx_tag_present(skb)) {
427 nskb = skb_clone(skb, GFP_ATOMIC);
431 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
435 vlan_set_tci(nskb, 0);
440 if (nla_attr_size(skb->len) > USHRT_MAX) {
445 /* Complete checksum if needed */
446 if (skb->ip_summed == CHECKSUM_PARTIAL &&
447 (err = skb_checksum_help(skb)))
450 /* Older versions of OVS user space enforce alignment of the last
451 * Netlink attribute to NLA_ALIGNTO which would require extensive
452 * padding logic. Only perform zerocopy if padding is not required.
454 if (dp->user_features & OVS_DP_F_UNALIGNED)
455 hlen = skb_zerocopy_headlen(skb);
459 len = upcall_msg_size(upcall_info, hlen);
460 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
466 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
467 0, upcall_info->cmd);
468 upcall->dp_ifindex = dp_ifindex;
470 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
471 err = ovs_nla_put_flow(key, key, user_skb);
473 nla_nest_end(user_skb, nla);
475 if (upcall_info->userdata)
476 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
477 nla_len(upcall_info->userdata),
478 nla_data(upcall_info->userdata));
480 if (upcall_info->egress_tun_info) {
481 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
482 err = ovs_nla_put_egress_tunnel_key(user_skb,
483 upcall_info->egress_tun_info);
485 nla_nest_end(user_skb, nla);
488 /* Only reserve room for attribute header, packet data is added
491 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
495 nla->nla_len = nla_attr_size(skb->len);
497 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
501 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
502 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
503 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
506 memset(skb_put(user_skb, plen), 0, plen);
509 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
511 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
521 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
523 struct ovs_header *ovs_header = info->userhdr;
524 struct nlattr **a = info->attrs;
525 struct sw_flow_actions *acts;
526 struct sk_buff *packet;
527 struct sw_flow *flow;
528 struct sw_flow_actions *sf_acts;
531 struct vport *input_vport;
534 bool log = !a[OVS_FLOW_ATTR_PROBE];
537 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
538 !a[OVS_PACKET_ATTR_ACTIONS])
541 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
542 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
546 skb_reserve(packet, NET_IP_ALIGN);
548 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
550 skb_reset_mac_header(packet);
551 eth = eth_hdr(packet);
553 /* Normally, setting the skb 'protocol' field would be handled by a
554 * call to eth_type_trans(), but it assumes there's a sending
555 * device, which we may not have.
557 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
558 packet->protocol = eth->h_proto;
560 packet->protocol = htons(ETH_P_802_2);
562 /* Build an sw_flow for sending this packet. */
563 flow = ovs_flow_alloc();
568 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
573 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
574 &flow->key, &acts, log);
578 rcu_assign_pointer(flow->sf_acts, acts);
579 OVS_CB(packet)->egress_tun_info = NULL;
580 packet->priority = flow->key.phy.priority;
581 packet->mark = flow->key.phy.skb_mark;
584 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
589 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
591 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
596 OVS_CB(packet)->input_vport = input_vport;
597 sf_acts = rcu_dereference(flow->sf_acts);
600 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
604 ovs_flow_free(flow, false);
610 ovs_flow_free(flow, false);
617 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
618 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
619 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
620 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
623 static struct genl_ops dp_packet_genl_ops[] = {
624 { .cmd = OVS_PACKET_CMD_EXECUTE,
625 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
626 .policy = packet_policy,
627 .doit = ovs_packet_cmd_execute
631 static struct genl_family dp_packet_genl_family = {
632 .id = GENL_ID_GENERATE,
633 .hdrsize = sizeof(struct ovs_header),
634 .name = OVS_PACKET_FAMILY,
635 .version = OVS_PACKET_VERSION,
636 .maxattr = OVS_PACKET_ATTR_MAX,
638 .parallel_ops = true,
639 .ops = dp_packet_genl_ops,
640 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
643 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
644 struct ovs_dp_megaflow_stats *mega_stats)
648 memset(mega_stats, 0, sizeof(*mega_stats));
650 stats->n_flows = ovs_flow_tbl_count(&dp->table);
651 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
653 stats->n_hit = stats->n_missed = stats->n_lost = 0;
655 for_each_possible_cpu(i) {
656 const struct dp_stats_percpu *percpu_stats;
657 struct dp_stats_percpu local_stats;
660 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
663 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
664 local_stats = *percpu_stats;
665 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
667 stats->n_hit += local_stats.n_hit;
668 stats->n_missed += local_stats.n_missed;
669 stats->n_lost += local_stats.n_lost;
670 mega_stats->n_mask_hit += local_stats.n_mask_hit;
674 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
676 return NLMSG_ALIGN(sizeof(struct ovs_header))
677 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
678 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
679 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
680 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
681 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
682 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
685 /* Called with ovs_mutex or RCU read lock. */
686 static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
693 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
697 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
701 nla_nest_end(skb, nla);
703 /* Fill flow mask. */
704 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
708 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
712 nla_nest_end(skb, nla);
716 /* Called with ovs_mutex or RCU read lock. */
717 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
720 struct ovs_flow_stats stats;
724 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
727 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
730 if (stats.n_packets &&
731 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
734 if ((u8)ntohs(tcp_flags) &&
735 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
741 /* Called with ovs_mutex or RCU read lock. */
742 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
743 struct sk_buff *skb, int skb_orig_len)
745 struct nlattr *start;
748 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
749 * this is the first flow to be dumped into 'skb'. This is unusual for
750 * Netlink but individual action lists can be longer than
751 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
752 * The userspace caller can always fetch the actions separately if it
753 * really wants them. (Most userspace callers in fact don't care.)
755 * This can only fail for dump operations because the skb is always
756 * properly sized for single flows.
758 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
760 const struct sw_flow_actions *sf_acts;
762 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
763 err = ovs_nla_put_actions(sf_acts->actions,
764 sf_acts->actions_len, skb);
767 nla_nest_end(skb, start);
772 nla_nest_cancel(skb, start);
774 } else if (skb_orig_len) {
781 /* Called with ovs_mutex or RCU read lock. */
782 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
783 struct sk_buff *skb, u32 portid,
784 u32 seq, u32 flags, u8 cmd)
786 const int skb_orig_len = skb->len;
787 struct ovs_header *ovs_header;
790 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
795 ovs_header->dp_ifindex = dp_ifindex;
797 err = ovs_flow_cmd_fill_match(flow, skb);
801 err = ovs_flow_cmd_fill_stats(flow, skb);
805 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
809 return genlmsg_end(skb, ovs_header);
812 genlmsg_cancel(skb, ovs_header);
816 /* May not be called with RCU read lock. */
817 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
818 struct genl_info *info,
823 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
824 GROUP_ID(&ovs_dp_flow_multicast_group)))
827 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
829 return ERR_PTR(-ENOMEM);
834 /* Called with ovs_mutex. */
835 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
837 struct genl_info *info, u8 cmd,
843 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
845 if (IS_ERR_OR_NULL(skb))
848 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
849 info->snd_portid, info->snd_seq, 0,
855 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
857 struct nlattr **a = info->attrs;
858 struct ovs_header *ovs_header = info->userhdr;
859 struct sw_flow *flow, *new_flow;
860 struct sw_flow_mask mask;
861 struct sk_buff *reply;
863 struct sw_flow_actions *acts;
864 struct sw_flow_match match;
866 bool log = !a[OVS_FLOW_ATTR_PROBE];
868 /* Must have key and actions. */
870 if (!a[OVS_FLOW_ATTR_KEY]) {
871 OVS_NLERR(log, "Flow key attr not present in new flow.");
874 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
875 OVS_NLERR(log, "Flow actions attr not present in new flow.");
879 /* Most of the time we need to allocate a new flow, do it before
882 new_flow = ovs_flow_alloc();
883 if (IS_ERR(new_flow)) {
884 error = PTR_ERR(new_flow);
889 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
890 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
891 a[OVS_FLOW_ATTR_MASK], log);
895 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
897 /* Validate actions. */
898 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
901 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
905 reply = ovs_flow_cmd_alloc_info(acts, info, false);
907 error = PTR_ERR(reply);
912 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
917 /* Check if this is a duplicate flow */
918 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
920 rcu_assign_pointer(new_flow->sf_acts, acts);
922 /* Put flow in bucket. */
923 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
924 if (unlikely(error)) {
929 if (unlikely(reply)) {
930 error = ovs_flow_cmd_fill_info(new_flow,
931 ovs_header->dp_ifindex,
932 reply, info->snd_portid,
939 struct sw_flow_actions *old_acts;
941 /* Bail out if we're not allowed to modify an existing flow.
942 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
943 * because Generic Netlink treats the latter as a dump
944 * request. We also accept NLM_F_EXCL in case that bug ever
947 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
952 /* The unmasked key has to be the same for flow updates. */
953 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
954 /* Look for any overlapping flow. */
955 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
961 /* Update actions. */
962 old_acts = ovsl_dereference(flow->sf_acts);
963 rcu_assign_pointer(flow->sf_acts, acts);
965 if (unlikely(reply)) {
966 error = ovs_flow_cmd_fill_info(flow,
967 ovs_header->dp_ifindex,
968 reply, info->snd_portid,
975 ovs_nla_free_flow_actions(old_acts);
976 ovs_flow_free(new_flow, false);
980 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
989 ovs_flow_free(new_flow, false);
994 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
995 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
996 const struct sw_flow_key *key,
997 const struct sw_flow_mask *mask,
1000 struct sw_flow_actions *acts;
1001 struct sw_flow_key masked_key;
1004 ovs_flow_mask_key(&masked_key, key, mask);
1005 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
1008 "Actions may not be safe on all matching packets");
1009 return ERR_PTR(error);
1015 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1017 struct nlattr **a = info->attrs;
1018 struct ovs_header *ovs_header = info->userhdr;
1019 struct sw_flow_key key;
1020 struct sw_flow *flow;
1021 struct sw_flow_mask mask;
1022 struct sk_buff *reply = NULL;
1023 struct datapath *dp;
1024 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1025 struct sw_flow_match match;
1027 bool log = !a[OVS_FLOW_ATTR_PROBE];
1031 if (!a[OVS_FLOW_ATTR_KEY]) {
1032 OVS_NLERR(log, "Flow key attribute not present in set flow.");
1036 ovs_match_init(&match, &key, &mask);
1037 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1038 a[OVS_FLOW_ATTR_MASK], log);
1042 /* Validate actions. */
1043 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1044 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1047 error = PTR_ERR(acts);
1051 /* Can allocate before locking if have acts. */
1052 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1053 if (IS_ERR(reply)) {
1054 error = PTR_ERR(reply);
1055 goto err_kfree_acts;
1060 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1061 if (unlikely(!dp)) {
1063 goto err_unlock_ovs;
1065 /* Check that the flow exists. */
1066 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1067 if (unlikely(!flow)) {
1069 goto err_unlock_ovs;
1072 /* Update actions, if present. */
1074 old_acts = ovsl_dereference(flow->sf_acts);
1075 rcu_assign_pointer(flow->sf_acts, acts);
1077 if (unlikely(reply)) {
1078 error = ovs_flow_cmd_fill_info(flow,
1079 ovs_header->dp_ifindex,
1080 reply, info->snd_portid,
1086 /* Could not alloc without acts before locking. */
1087 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1088 info, OVS_FLOW_CMD_NEW, false);
1089 if (unlikely(IS_ERR(reply))) {
1090 error = PTR_ERR(reply);
1091 goto err_unlock_ovs;
1096 if (a[OVS_FLOW_ATTR_CLEAR])
1097 ovs_flow_stats_clear(flow);
1101 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1103 ovs_nla_free_flow_actions(old_acts);
1116 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1118 struct nlattr **a = info->attrs;
1119 struct ovs_header *ovs_header = info->userhdr;
1120 struct sw_flow_key key;
1121 struct sk_buff *reply;
1122 struct sw_flow *flow;
1123 struct datapath *dp;
1124 struct sw_flow_match match;
1126 bool log = !a[OVS_FLOW_ATTR_PROBE];
1128 if (!a[OVS_FLOW_ATTR_KEY]) {
1130 "Flow get message rejected, Key attribute missing.");
1134 ovs_match_init(&match, &key, NULL);
1135 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, log);
1140 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1146 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1152 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1153 OVS_FLOW_CMD_NEW, true);
1154 if (IS_ERR(reply)) {
1155 err = PTR_ERR(reply);
1160 return genlmsg_reply(reply, info);
1166 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1168 struct nlattr **a = info->attrs;
1169 struct ovs_header *ovs_header = info->userhdr;
1170 struct sw_flow_key key;
1171 struct sk_buff *reply;
1172 struct sw_flow *flow;
1173 struct datapath *dp;
1174 struct sw_flow_match match;
1176 bool log = !a[OVS_FLOW_ATTR_PROBE];
1178 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1179 ovs_match_init(&match, &key, NULL);
1180 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1187 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1188 if (unlikely(!dp)) {
1193 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1194 err = ovs_flow_tbl_flush(&dp->table);
1198 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1199 if (unlikely(!flow)) {
1204 ovs_flow_tbl_remove(&dp->table, flow);
1207 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1210 if (likely(reply)) {
1211 if (likely(!IS_ERR(reply))) {
1212 rcu_read_lock(); /*To keep RCU checker happy. */
1213 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1214 reply, info->snd_portid,
1219 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1221 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1222 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1227 ovs_flow_free(flow, true);
1234 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1236 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1237 struct table_instance *ti;
1238 struct datapath *dp;
1241 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1247 ti = rcu_dereference(dp->table.ti);
1249 struct sw_flow *flow;
1252 bucket = cb->args[0];
1254 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1258 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1259 NETLINK_CB(cb->skb).portid,
1260 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1261 OVS_FLOW_CMD_NEW) < 0)
1264 cb->args[0] = bucket;
1271 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1272 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1273 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1274 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1275 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1276 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1279 static struct genl_ops dp_flow_genl_ops[] = {
1280 { .cmd = OVS_FLOW_CMD_NEW,
1281 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1282 .policy = flow_policy,
1283 .doit = ovs_flow_cmd_new
1285 { .cmd = OVS_FLOW_CMD_DEL,
1286 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1287 .policy = flow_policy,
1288 .doit = ovs_flow_cmd_del
1290 { .cmd = OVS_FLOW_CMD_GET,
1291 .flags = 0, /* OK for unprivileged users. */
1292 .policy = flow_policy,
1293 .doit = ovs_flow_cmd_get,
1294 .dumpit = ovs_flow_cmd_dump
1296 { .cmd = OVS_FLOW_CMD_SET,
1297 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1298 .policy = flow_policy,
1299 .doit = ovs_flow_cmd_set,
1303 static struct genl_family dp_flow_genl_family = {
1304 .id = GENL_ID_GENERATE,
1305 .hdrsize = sizeof(struct ovs_header),
1306 .name = OVS_FLOW_FAMILY,
1307 .version = OVS_FLOW_VERSION,
1308 .maxattr = OVS_FLOW_ATTR_MAX,
1310 .parallel_ops = true,
1311 .ops = dp_flow_genl_ops,
1312 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1313 .mcgrps = &ovs_dp_flow_multicast_group,
1317 static size_t ovs_dp_cmd_msg_size(void)
1319 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1321 msgsize += nla_total_size(IFNAMSIZ);
1322 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1323 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1324 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1329 /* Called with ovs_mutex. */
1330 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1331 u32 portid, u32 seq, u32 flags, u8 cmd)
1333 struct ovs_header *ovs_header;
1334 struct ovs_dp_stats dp_stats;
1335 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1338 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1343 ovs_header->dp_ifindex = get_dpifindex(dp);
1345 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1347 goto nla_put_failure;
1349 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1350 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1352 goto nla_put_failure;
1354 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1355 sizeof(struct ovs_dp_megaflow_stats),
1356 &dp_megaflow_stats))
1357 goto nla_put_failure;
1359 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1360 goto nla_put_failure;
1362 return genlmsg_end(skb, ovs_header);
1365 genlmsg_cancel(skb, ovs_header);
1370 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1372 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1375 /* Called with rcu_read_lock or ovs_mutex. */
1376 static struct datapath *lookup_datapath(struct net *net,
1377 const struct ovs_header *ovs_header,
1378 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1380 struct datapath *dp;
1382 if (!a[OVS_DP_ATTR_NAME])
1383 dp = get_dp(net, ovs_header->dp_ifindex);
1385 struct vport *vport;
1387 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1388 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1390 return dp ? dp : ERR_PTR(-ENODEV);
1393 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1395 struct datapath *dp;
1397 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1401 WARN(dp->user_features, "Dropping previously announced user features\n");
1402 dp->user_features = 0;
1405 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1407 if (a[OVS_DP_ATTR_USER_FEATURES])
1408 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1411 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1413 struct nlattr **a = info->attrs;
1414 struct vport_parms parms;
1415 struct sk_buff *reply;
1416 struct datapath *dp;
1417 struct vport *vport;
1418 struct ovs_net *ovs_net;
1422 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1425 reply = ovs_dp_cmd_alloc_info(info);
1430 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1432 goto err_free_reply;
1434 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1436 /* Allocate table. */
1437 err = ovs_flow_tbl_init(&dp->table);
1441 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1442 if (!dp->stats_percpu) {
1444 goto err_destroy_table;
1447 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1451 goto err_destroy_percpu;
1454 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1455 INIT_HLIST_HEAD(&dp->ports[i]);
1457 /* Set up our datapath device. */
1458 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1459 parms.type = OVS_VPORT_TYPE_INTERNAL;
1460 parms.options = NULL;
1462 parms.port_no = OVSP_LOCAL;
1463 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1465 ovs_dp_change(dp, a);
1467 /* So far only local changes have been made, now need the lock. */
1470 vport = new_vport(&parms);
1471 if (IS_ERR(vport)) {
1472 err = PTR_ERR(vport);
1476 if (err == -EEXIST) {
1477 /* An outdated user space instance that does not understand
1478 * the concept of user_features has attempted to create a new
1479 * datapath and is likely to reuse it. Drop all user features.
1481 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1482 ovs_dp_reset_user_features(skb, info);
1485 goto err_destroy_ports_array;
1488 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1489 info->snd_seq, 0, OVS_DP_CMD_NEW);
1492 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1493 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1496 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1499 err_destroy_ports_array:
1503 free_percpu(dp->stats_percpu);
1505 ovs_flow_tbl_destroy(&dp->table);
1507 release_net(ovs_dp_get_net(dp));
1515 /* Called with ovs_mutex. */
1516 static void __dp_destroy(struct datapath *dp)
1520 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1521 struct vport *vport;
1522 struct hlist_node *n;
1524 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1525 if (vport->port_no != OVSP_LOCAL)
1526 ovs_dp_detach_port(vport);
1529 list_del_rcu(&dp->list_node);
1531 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1532 * all ports in datapath are destroyed first before freeing datapath.
1534 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1536 /* RCU destroy the flow table */
1537 call_rcu(&dp->rcu, destroy_dp_rcu);
1540 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1542 struct sk_buff *reply;
1543 struct datapath *dp;
1546 reply = ovs_dp_cmd_alloc_info(info);
1551 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1554 goto err_unlock_free;
1556 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1557 info->snd_seq, 0, OVS_DP_CMD_DEL);
1563 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1572 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1574 struct sk_buff *reply;
1575 struct datapath *dp;
1578 reply = ovs_dp_cmd_alloc_info(info);
1583 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1586 goto err_unlock_free;
1588 ovs_dp_change(dp, info->attrs);
1590 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1591 info->snd_seq, 0, OVS_DP_CMD_NEW);
1595 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1604 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1606 struct sk_buff *reply;
1607 struct datapath *dp;
1610 reply = ovs_dp_cmd_alloc_info(info);
1615 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1618 goto err_unlock_free;
1620 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1621 info->snd_seq, 0, OVS_DP_CMD_NEW);
1625 return genlmsg_reply(reply, info);
1633 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1635 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1636 struct datapath *dp;
1637 int skip = cb->args[0];
1641 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1643 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1644 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1645 OVS_DP_CMD_NEW) < 0)
1656 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1657 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1658 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1659 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1662 static struct genl_ops dp_datapath_genl_ops[] = {
1663 { .cmd = OVS_DP_CMD_NEW,
1664 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1665 .policy = datapath_policy,
1666 .doit = ovs_dp_cmd_new
1668 { .cmd = OVS_DP_CMD_DEL,
1669 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1670 .policy = datapath_policy,
1671 .doit = ovs_dp_cmd_del
1673 { .cmd = OVS_DP_CMD_GET,
1674 .flags = 0, /* OK for unprivileged users. */
1675 .policy = datapath_policy,
1676 .doit = ovs_dp_cmd_get,
1677 .dumpit = ovs_dp_cmd_dump
1679 { .cmd = OVS_DP_CMD_SET,
1680 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1681 .policy = datapath_policy,
1682 .doit = ovs_dp_cmd_set,
1686 static struct genl_family dp_datapath_genl_family = {
1687 .id = GENL_ID_GENERATE,
1688 .hdrsize = sizeof(struct ovs_header),
1689 .name = OVS_DATAPATH_FAMILY,
1690 .version = OVS_DATAPATH_VERSION,
1691 .maxattr = OVS_DP_ATTR_MAX,
1693 .parallel_ops = true,
1694 .ops = dp_datapath_genl_ops,
1695 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1696 .mcgrps = &ovs_dp_datapath_multicast_group,
1700 /* Called with ovs_mutex or RCU read lock. */
1701 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1702 u32 portid, u32 seq, u32 flags, u8 cmd)
1704 struct ovs_header *ovs_header;
1705 struct ovs_vport_stats vport_stats;
1708 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1713 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1715 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1716 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1717 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1718 goto nla_put_failure;
1720 ovs_vport_get_stats(vport, &vport_stats);
1721 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1723 goto nla_put_failure;
1725 if (ovs_vport_get_upcall_portids(vport, skb))
1726 goto nla_put_failure;
1728 err = ovs_vport_get_options(vport, skb);
1729 if (err == -EMSGSIZE)
1732 return genlmsg_end(skb, ovs_header);
1737 genlmsg_cancel(skb, ovs_header);
1741 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1743 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1746 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1747 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1750 struct sk_buff *skb;
1753 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1755 return ERR_PTR(-ENOMEM);
1757 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1763 /* Called with ovs_mutex or RCU read lock. */
1764 static struct vport *lookup_vport(struct net *net,
1765 const struct ovs_header *ovs_header,
1766 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1768 struct datapath *dp;
1769 struct vport *vport;
1771 if (a[OVS_VPORT_ATTR_NAME]) {
1772 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1774 return ERR_PTR(-ENODEV);
1775 if (ovs_header->dp_ifindex &&
1776 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1777 return ERR_PTR(-ENODEV);
1779 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1780 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1782 if (port_no >= DP_MAX_PORTS)
1783 return ERR_PTR(-EFBIG);
1785 dp = get_dp(net, ovs_header->dp_ifindex);
1787 return ERR_PTR(-ENODEV);
1789 vport = ovs_vport_ovsl_rcu(dp, port_no);
1791 return ERR_PTR(-ENODEV);
1794 return ERR_PTR(-EINVAL);
1797 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1799 struct nlattr **a = info->attrs;
1800 struct ovs_header *ovs_header = info->userhdr;
1801 struct vport_parms parms;
1802 struct sk_buff *reply;
1803 struct vport *vport;
1804 struct datapath *dp;
1808 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1809 !a[OVS_VPORT_ATTR_UPCALL_PID])
1812 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1813 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1814 if (port_no >= DP_MAX_PORTS)
1817 reply = ovs_vport_cmd_alloc_info();
1822 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1825 goto exit_unlock_free;
1828 vport = ovs_vport_ovsl(dp, port_no);
1831 goto exit_unlock_free;
1833 for (port_no = 1; ; port_no++) {
1834 if (port_no >= DP_MAX_PORTS) {
1836 goto exit_unlock_free;
1838 vport = ovs_vport_ovsl(dp, port_no);
1844 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1845 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1846 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1848 parms.port_no = port_no;
1849 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1851 vport = new_vport(&parms);
1852 err = PTR_ERR(vport);
1854 goto exit_unlock_free;
1856 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1857 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1861 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1870 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1872 struct nlattr **a = info->attrs;
1873 struct sk_buff *reply;
1874 struct vport *vport;
1877 reply = ovs_vport_cmd_alloc_info();
1882 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1883 err = PTR_ERR(vport);
1885 goto exit_unlock_free;
1887 if (a[OVS_VPORT_ATTR_TYPE] &&
1888 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1890 goto exit_unlock_free;
1893 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1894 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1896 goto exit_unlock_free;
1899 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1900 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1902 err = ovs_vport_set_upcall_portids(vport, ids);
1904 goto exit_unlock_free;
1907 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1908 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1912 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1921 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1923 struct nlattr **a = info->attrs;
1924 struct sk_buff *reply;
1925 struct vport *vport;
1928 reply = ovs_vport_cmd_alloc_info();
1933 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1934 err = PTR_ERR(vport);
1936 goto exit_unlock_free;
1938 if (vport->port_no == OVSP_LOCAL) {
1940 goto exit_unlock_free;
1943 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1944 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1946 ovs_dp_detach_port(vport);
1949 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1958 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1960 struct nlattr **a = info->attrs;
1961 struct ovs_header *ovs_header = info->userhdr;
1962 struct sk_buff *reply;
1963 struct vport *vport;
1966 reply = ovs_vport_cmd_alloc_info();
1971 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1972 err = PTR_ERR(vport);
1974 goto exit_unlock_free;
1975 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1976 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1980 return genlmsg_reply(reply, info);
1988 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1990 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1991 struct datapath *dp;
1992 int bucket = cb->args[0], skip = cb->args[1];
1996 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2001 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2002 struct vport *vport;
2005 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2007 ovs_vport_cmd_fill_info(vport, skb,
2008 NETLINK_CB(cb->skb).portid,
2011 OVS_VPORT_CMD_NEW) < 0)
2027 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2028 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2029 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2030 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2031 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2032 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2033 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2036 static struct genl_ops dp_vport_genl_ops[] = {
2037 { .cmd = OVS_VPORT_CMD_NEW,
2038 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2039 .policy = vport_policy,
2040 .doit = ovs_vport_cmd_new
2042 { .cmd = OVS_VPORT_CMD_DEL,
2043 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2044 .policy = vport_policy,
2045 .doit = ovs_vport_cmd_del
2047 { .cmd = OVS_VPORT_CMD_GET,
2048 .flags = 0, /* OK for unprivileged users. */
2049 .policy = vport_policy,
2050 .doit = ovs_vport_cmd_get,
2051 .dumpit = ovs_vport_cmd_dump
2053 { .cmd = OVS_VPORT_CMD_SET,
2054 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2055 .policy = vport_policy,
2056 .doit = ovs_vport_cmd_set,
2060 struct genl_family dp_vport_genl_family = {
2061 .id = GENL_ID_GENERATE,
2062 .hdrsize = sizeof(struct ovs_header),
2063 .name = OVS_VPORT_FAMILY,
2064 .version = OVS_VPORT_VERSION,
2065 .maxattr = OVS_VPORT_ATTR_MAX,
2067 .parallel_ops = true,
2068 .ops = dp_vport_genl_ops,
2069 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2070 .mcgrps = &ovs_dp_vport_multicast_group,
2074 static struct genl_family *dp_genl_families[] = {
2075 &dp_datapath_genl_family,
2076 &dp_vport_genl_family,
2077 &dp_flow_genl_family,
2078 &dp_packet_genl_family,
2081 static void dp_unregister_genl(int n_families)
2085 for (i = 0; i < n_families; i++)
2086 genl_unregister_family(dp_genl_families[i]);
2089 static int dp_register_genl(void)
2094 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2096 err = genl_register_family(dp_genl_families[i]);
2104 dp_unregister_genl(i);
2108 static int __net_init ovs_init_net(struct net *net)
2110 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2112 INIT_LIST_HEAD(&ovs_net->dps);
2113 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2117 static void __net_exit ovs_exit_net(struct net *net)
2119 struct datapath *dp, *dp_next;
2120 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2123 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2127 cancel_work_sync(&ovs_net->dp_notify_work);
2130 static struct pernet_operations ovs_net_ops = {
2131 .init = ovs_init_net,
2132 .exit = ovs_exit_net,
2134 .size = sizeof(struct ovs_net),
2137 DEFINE_COMPAT_PNET_REG_FUNC(device);
2139 static int __init dp_init(void)
2143 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2145 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2148 err = action_fifos_init();
2152 err = ovs_flow_init();
2154 goto error_action_fifos_exit;
2156 err = ovs_vport_init();
2158 goto error_flow_exit;
2160 err = register_pernet_device(&ovs_net_ops);
2162 goto error_vport_exit;
2164 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2166 goto error_netns_exit;
2168 err = dp_register_genl();
2170 goto error_unreg_notifier;
2174 error_unreg_notifier:
2175 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2177 unregister_pernet_device(&ovs_net_ops);
2182 error_action_fifos_exit:
2183 action_fifos_exit();
2188 static void dp_cleanup(void)
2190 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2191 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2192 unregister_pernet_device(&ovs_net_ops);
2196 action_fifos_exit();
2199 module_init(dp_init);
2200 module_exit(dp_cleanup);
2202 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2203 MODULE_LICENSE("GPL");
2204 MODULE_VERSION(VERSION);