2 * Copyright (c) 2007-2014 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
57 #include "flow_table.h"
58 #include "flow_netlink.h"
60 #include "vport-internal_dev.h"
61 #include "vport-netdev.h"
63 int ovs_net_id __read_mostly;
65 static struct genl_family dp_packet_genl_family;
66 static struct genl_family dp_flow_genl_family;
67 static struct genl_family dp_datapath_genl_family;
69 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
70 .name = OVS_FLOW_MCGROUP
73 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
74 .name = OVS_DATAPATH_MCGROUP
77 struct genl_multicast_group ovs_dp_vport_multicast_group = {
78 .name = OVS_VPORT_MCGROUP
81 /* Check if need to build a reply message.
82 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
83 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
86 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
87 genl_has_listeners(family, genl_info_net(info)->genl_sock,
91 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
92 struct sk_buff *skb, struct genl_info *info)
94 genl_notify(family, skb, genl_info_net(info),
95 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
101 * All writes e.g. Writes to device state (add/remove datapath, port, set
102 * operations on vports, etc.), Writes to other state (flow table
103 * modifications, set miscellaneous datapath parameters, etc.) are protected
106 * Reads are protected by RCU.
108 * There are a few special cases (mostly stats) that have their own
109 * synchronization but they nest under all of above and don't interact with
112 * The RTNL lock nests inside ovs_mutex.
115 static DEFINE_MUTEX(ovs_mutex);
119 mutex_lock(&ovs_mutex);
122 void ovs_unlock(void)
124 mutex_unlock(&ovs_mutex);
127 #ifdef CONFIG_LOCKDEP
128 int lockdep_ovsl_is_held(void)
131 return lockdep_is_held(&ovs_mutex);
137 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
138 const struct sw_flow_key *,
139 const struct dp_upcall_info *);
140 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
141 const struct sw_flow_key *key,
142 const struct dp_upcall_info *);
144 /* Must be called with rcu_read_lock. */
145 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
147 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
150 struct vport *vport = ovs_internal_dev_get_vport(dev);
158 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
159 * returned dp pointer valid. */
160 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
164 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
166 dp = get_dp_rcu(net, dp_ifindex);
172 /* Must be called with rcu_read_lock or ovs_mutex. */
173 const char *ovs_dp_name(const struct datapath *dp)
175 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
176 return vport->ops->get_name(vport);
179 static int get_dpifindex(const struct datapath *dp)
186 local = ovs_vport_rcu(dp, OVSP_LOCAL);
188 ifindex = netdev_vport_priv(local)->dev->ifindex;
197 static void destroy_dp_rcu(struct rcu_head *rcu)
199 struct datapath *dp = container_of(rcu, struct datapath, rcu);
201 ovs_flow_tbl_destroy(&dp->table);
202 free_percpu(dp->stats_percpu);
203 release_net(ovs_dp_get_net(dp));
208 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
211 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
214 /* Called with ovs_mutex or RCU read lock. */
215 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
218 struct hlist_head *head;
220 head = vport_hash_bucket(dp, port_no);
221 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
222 if (vport->port_no == port_no)
228 /* Called with ovs_mutex. */
229 static struct vport *new_vport(const struct vport_parms *parms)
233 vport = ovs_vport_add(parms);
234 if (!IS_ERR(vport)) {
235 struct datapath *dp = parms->dp;
236 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
238 hlist_add_head_rcu(&vport->dp_hash_node, head);
243 void ovs_dp_detach_port(struct vport *p)
247 /* First drop references to device. */
248 hlist_del_rcu(&p->dp_hash_node);
250 /* Then destroy it. */
254 /* Must be called with rcu_read_lock. */
255 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
257 const struct vport *p = OVS_CB(skb)->input_vport;
258 struct datapath *dp = p->dp;
259 struct sw_flow *flow;
260 struct sw_flow_actions *sf_acts;
261 struct dp_stats_percpu *stats;
265 stats = this_cpu_ptr(dp->stats_percpu);
268 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
270 if (unlikely(!flow)) {
271 struct dp_upcall_info upcall;
274 upcall.cmd = OVS_PACKET_CMD_MISS;
275 upcall.userdata = NULL;
276 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
277 upcall.egress_tun_info = NULL;
279 error = ovs_dp_upcall(dp, skb, key, &upcall);
285 stats_counter = &stats->n_missed;
289 ovs_flow_stats_update(flow, key->tp.flags, skb);
291 sf_acts = rcu_dereference(flow->sf_acts);
292 ovs_execute_actions(dp, skb, key, sf_acts);
293 stats_counter = &stats->n_hit;
296 /* Update datapath statistics. */
297 u64_stats_update_begin(&stats->syncp);
299 stats->n_mask_hit += n_mask_hit;
300 u64_stats_update_end(&stats->syncp);
303 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
304 const struct sw_flow_key *key,
305 const struct dp_upcall_info *upcall_info)
307 struct dp_stats_percpu *stats;
310 if (upcall_info->portid == 0) {
315 if (!skb_is_gso(skb))
316 err = queue_userspace_packet(dp, skb, key, upcall_info);
318 err = queue_gso_packets(dp, skb, key, upcall_info);
325 stats = this_cpu_ptr(dp->stats_percpu);
327 u64_stats_update_begin(&stats->syncp);
329 u64_stats_update_end(&stats->syncp);
334 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
335 const struct sw_flow_key *key,
336 const struct dp_upcall_info *upcall_info)
338 unsigned short gso_type = skb_shinfo(skb)->gso_type;
339 struct sw_flow_key later_key;
340 struct sk_buff *segs, *nskb;
341 struct ovs_skb_cb ovs_cb;
344 ovs_cb = *OVS_CB(skb);
345 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
346 *OVS_CB(skb) = ovs_cb;
348 return PTR_ERR(segs);
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
489 * in skb_zerocopy() */
490 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
494 nla->nla_len = nla_attr_size(skb->len);
496 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
500 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
501 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
502 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
505 memset(skb_put(user_skb, plen), 0, plen);
508 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
510 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. */
556 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
557 packet->protocol = eth->h_proto;
559 packet->protocol = htons(ETH_P_802_2);
561 /* Build an sw_flow for sending this packet. */
562 flow = ovs_flow_alloc();
567 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
572 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
573 &flow->key, &acts, log);
577 rcu_assign_pointer(flow->sf_acts, acts);
578 OVS_CB(packet)->egress_tun_info = NULL;
579 packet->priority = flow->key.phy.priority;
580 packet->mark = flow->key.phy.skb_mark;
583 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
588 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
590 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
595 OVS_CB(packet)->input_vport = input_vport;
596 sf_acts = rcu_dereference(flow->sf_acts);
599 err = ovs_execute_actions(dp, packet, &flow->key, sf_acts);
603 ovs_flow_free(flow, false);
609 ovs_flow_free(flow, false);
616 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
617 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
618 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
619 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
622 static struct genl_ops dp_packet_genl_ops[] = {
623 { .cmd = OVS_PACKET_CMD_EXECUTE,
624 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
625 .policy = packet_policy,
626 .doit = ovs_packet_cmd_execute
630 static struct genl_family dp_packet_genl_family = {
631 .id = GENL_ID_GENERATE,
632 .hdrsize = sizeof(struct ovs_header),
633 .name = OVS_PACKET_FAMILY,
634 .version = OVS_PACKET_VERSION,
635 .maxattr = OVS_PACKET_ATTR_MAX,
637 .parallel_ops = true,
638 .ops = dp_packet_genl_ops,
639 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
642 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
643 struct ovs_dp_megaflow_stats *mega_stats)
647 memset(mega_stats, 0, sizeof(*mega_stats));
649 stats->n_flows = ovs_flow_tbl_count(&dp->table);
650 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
652 stats->n_hit = stats->n_missed = stats->n_lost = 0;
654 for_each_possible_cpu(i) {
655 const struct dp_stats_percpu *percpu_stats;
656 struct dp_stats_percpu local_stats;
659 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
662 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
663 local_stats = *percpu_stats;
664 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
666 stats->n_hit += local_stats.n_hit;
667 stats->n_missed += local_stats.n_missed;
668 stats->n_lost += local_stats.n_lost;
669 mega_stats->n_mask_hit += local_stats.n_mask_hit;
673 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
675 return NLMSG_ALIGN(sizeof(struct ovs_header))
676 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
677 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
678 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
679 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
680 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
681 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
684 /* Called with ovs_mutex or RCU read lock. */
685 static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
692 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
696 err = ovs_nla_put_flow(&flow->unmasked_key,
697 &flow->unmasked_key, skb);
700 nla_nest_end(skb, nla);
702 /* Fill flow mask. */
703 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
707 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
710 nla_nest_end(skb, nla);
715 /* Called with ovs_mutex or RCU read lock. */
716 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
719 struct ovs_flow_stats stats;
723 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
726 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
729 if (stats.n_packets &&
730 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
733 if ((u8)ntohs(tcp_flags) &&
734 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
740 /* Called with ovs_mutex or RCU read lock. */
741 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
742 struct sk_buff *skb, int skb_orig_len)
744 struct nlattr *start;
747 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
748 * this is the first flow to be dumped into 'skb'. This is unusual for
749 * Netlink but individual action lists can be longer than
750 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
751 * The userspace caller can always fetch the actions separately if it
752 * really wants them. (Most userspace callers in fact don't care.)
754 * This can only fail for dump operations because the skb is always
755 * properly sized for single flows.
757 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
759 const struct sw_flow_actions *sf_acts;
761 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
762 err = ovs_nla_put_actions(sf_acts->actions,
763 sf_acts->actions_len, skb);
766 nla_nest_end(skb, start);
771 nla_nest_cancel(skb, start);
773 } else if (skb_orig_len) {
780 /* Called with ovs_mutex or RCU read lock. */
781 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
782 struct sk_buff *skb, u32 portid,
783 u32 seq, u32 flags, u8 cmd)
785 const int skb_orig_len = skb->len;
786 struct ovs_header *ovs_header;
789 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
792 ovs_header->dp_ifindex = dp_ifindex;
794 err = ovs_flow_cmd_fill_match(flow, skb);
798 err = ovs_flow_cmd_fill_stats(flow, skb);
802 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
806 return genlmsg_end(skb, ovs_header);
809 genlmsg_cancel(skb, ovs_header);
813 /* May not be called with RCU read lock. */
814 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
815 struct genl_info *info,
820 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
821 GROUP_ID(&ovs_dp_flow_multicast_group)))
824 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
827 return ERR_PTR(-ENOMEM);
832 /* Called with ovs_mutex. */
833 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
834 const struct sw_flow *flow,
836 struct genl_info *info, u8 cmd,
842 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
844 if (IS_ERR_OR_NULL(skb))
847 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
848 info->snd_portid, info->snd_seq, 0,
854 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
856 struct nlattr **a = info->attrs;
857 struct ovs_header *ovs_header = info->userhdr;
858 struct sw_flow *flow, *new_flow;
859 struct sw_flow_mask mask;
860 struct sk_buff *reply;
862 struct sw_flow_actions *acts;
863 struct sw_flow_match match;
865 bool log = !a[OVS_FLOW_ATTR_PROBE];
867 /* Must have key and actions. */
869 if (!a[OVS_FLOW_ATTR_KEY]) {
870 OVS_NLERR(log, "Flow key attribute not present in new flow.");
873 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
875 "Flow actions attribute not present in new flow.");
879 /* Most of the time we need to allocate a new flow, do it before
881 new_flow = ovs_flow_alloc();
882 if (IS_ERR(new_flow)) {
883 error = PTR_ERR(new_flow);
888 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
889 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
890 a[OVS_FLOW_ATTR_MASK], log);
894 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
896 /* Validate actions. */
897 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
902 "Flow actions may not be safe on all matching packets."
907 reply = ovs_flow_cmd_alloc_info(acts, info, false);
909 error = PTR_ERR(reply);
914 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
919 /* Check if this is a duplicate flow */
920 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
922 rcu_assign_pointer(new_flow->sf_acts, acts);
924 /* Put flow in bucket. */
925 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
926 if (unlikely(error)) {
931 if (unlikely(reply)) {
932 error = ovs_flow_cmd_fill_info(new_flow,
933 ovs_header->dp_ifindex,
934 reply, info->snd_portid,
941 struct sw_flow_actions *old_acts;
943 /* Bail out if we're not allowed to modify an existing flow.
944 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
945 * because Generic Netlink treats the latter as a dump
946 * request. We also accept NLM_F_EXCL in case that bug ever
949 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
954 /* The unmasked key has to be the same for flow updates. */
955 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
956 /* Look for any overlapping flow. */
957 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
963 /* Update actions. */
964 old_acts = ovsl_dereference(flow->sf_acts);
965 rcu_assign_pointer(flow->sf_acts, acts);
967 if (unlikely(reply)) {
968 error = ovs_flow_cmd_fill_info(flow,
969 ovs_header->dp_ifindex,
970 reply, info->snd_portid,
977 ovs_nla_free_flow_actions(old_acts);
978 ovs_flow_free(new_flow, false);
982 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
991 ovs_flow_free(new_flow, false);
996 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
997 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
998 const struct sw_flow_key *key,
999 const struct sw_flow_mask *mask,
1002 struct sw_flow_actions *acts;
1003 struct sw_flow_key masked_key;
1006 ovs_flow_mask_key(&masked_key, key, mask);
1007 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
1010 "Actions may not be safe on all matching packets.");
1011 return ERR_PTR(error);
1017 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1019 struct nlattr **a = info->attrs;
1020 struct ovs_header *ovs_header = info->userhdr;
1021 struct sw_flow_key key;
1022 struct sw_flow *flow;
1023 struct sw_flow_mask mask;
1024 struct sk_buff *reply = NULL;
1025 struct datapath *dp;
1026 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1027 struct sw_flow_match match;
1029 bool log = !a[OVS_FLOW_ATTR_PROBE];
1033 if (!a[OVS_FLOW_ATTR_KEY]) {
1034 OVS_NLERR(log, "Flow key attribute not present in set flow.");
1038 ovs_match_init(&match, &key, &mask);
1039 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1040 a[OVS_FLOW_ATTR_MASK], log);
1044 /* Validate actions. */
1045 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1046 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1049 error = PTR_ERR(acts);
1053 /* Can allocate before locking if have acts. */
1054 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1055 if (IS_ERR(reply)) {
1056 error = PTR_ERR(reply);
1057 goto err_kfree_acts;
1062 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1063 if (unlikely(!dp)) {
1065 goto err_unlock_ovs;
1067 /* Check that the flow exists. */
1068 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1069 if (unlikely(!flow)) {
1071 goto err_unlock_ovs;
1074 /* Update actions, if present. */
1076 old_acts = ovsl_dereference(flow->sf_acts);
1077 rcu_assign_pointer(flow->sf_acts, acts);
1079 if (unlikely(reply)) {
1080 error = ovs_flow_cmd_fill_info(flow,
1081 ovs_header->dp_ifindex,
1082 reply, info->snd_portid,
1088 /* Could not alloc without acts before locking. */
1089 reply = ovs_flow_cmd_build_info(dp, flow,
1090 ovs_header->dp_ifindex,
1091 info, OVS_FLOW_CMD_NEW, false);
1092 if (unlikely(IS_ERR(reply))) {
1093 error = PTR_ERR(reply);
1094 goto err_unlock_ovs;
1099 if (a[OVS_FLOW_ATTR_CLEAR])
1100 ovs_flow_stats_clear(flow);
1104 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1106 ovs_nla_free_flow_actions(old_acts);
1118 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1120 struct nlattr **a = info->attrs;
1121 struct ovs_header *ovs_header = info->userhdr;
1122 struct sw_flow_key key;
1123 struct sk_buff *reply;
1124 struct sw_flow *flow;
1125 struct datapath *dp;
1126 struct sw_flow_match match;
1128 bool log = !a[OVS_FLOW_ATTR_PROBE];
1130 if (!a[OVS_FLOW_ATTR_KEY]) {
1132 "Flow get message rejected, Key attribute missing.");
1136 ovs_match_init(&match, &key, NULL);
1137 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, log);
1142 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1148 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1154 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1155 OVS_FLOW_CMD_NEW, true);
1156 if (IS_ERR(reply)) {
1157 err = PTR_ERR(reply);
1162 return genlmsg_reply(reply, info);
1168 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1170 struct nlattr **a = info->attrs;
1171 struct ovs_header *ovs_header = info->userhdr;
1172 struct sw_flow_key key;
1173 struct sk_buff *reply;
1174 struct sw_flow *flow;
1175 struct datapath *dp;
1176 struct sw_flow_match match;
1178 bool log = !a[OVS_FLOW_ATTR_PROBE];
1180 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1181 ovs_match_init(&match, &key, NULL);
1182 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1189 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1190 if (unlikely(!dp)) {
1194 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1195 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((const struct sw_flow_actions __force *)flow->sf_acts,
1210 if (likely(reply)) {
1211 if (likely(!IS_ERR(reply))) {
1212 rcu_read_lock(); /* Keep RCU checker happy. */
1213 err = ovs_flow_cmd_fill_info(flow,
1214 ovs_header->dp_ifindex,
1215 reply, info->snd_portid,
1220 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1222 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1223 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1228 ovs_flow_free(flow, true);
1235 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1237 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1238 struct table_instance *ti;
1239 struct datapath *dp;
1242 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1248 ti = rcu_dereference(dp->table.ti);
1250 struct sw_flow *flow;
1253 bucket = cb->args[0];
1255 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1259 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1260 NETLINK_CB(cb->skb).portid,
1261 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1262 OVS_FLOW_CMD_NEW) < 0)
1265 cb->args[0] = bucket;
1272 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1273 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1274 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1275 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1276 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1277 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1280 static struct genl_ops dp_flow_genl_ops[] = {
1281 { .cmd = OVS_FLOW_CMD_NEW,
1282 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1283 .policy = flow_policy,
1284 .doit = ovs_flow_cmd_new
1286 { .cmd = OVS_FLOW_CMD_DEL,
1287 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1288 .policy = flow_policy,
1289 .doit = ovs_flow_cmd_del
1291 { .cmd = OVS_FLOW_CMD_GET,
1292 .flags = 0, /* OK for unprivileged users. */
1293 .policy = flow_policy,
1294 .doit = ovs_flow_cmd_get,
1295 .dumpit = ovs_flow_cmd_dump
1297 { .cmd = OVS_FLOW_CMD_SET,
1298 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1299 .policy = flow_policy,
1300 .doit = ovs_flow_cmd_set,
1304 static struct genl_family dp_flow_genl_family = {
1305 .id = GENL_ID_GENERATE,
1306 .hdrsize = sizeof(struct ovs_header),
1307 .name = OVS_FLOW_FAMILY,
1308 .version = OVS_FLOW_VERSION,
1309 .maxattr = OVS_FLOW_ATTR_MAX,
1311 .parallel_ops = true,
1312 .ops = dp_flow_genl_ops,
1313 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1314 .mcgrps = &ovs_dp_flow_multicast_group,
1318 static size_t ovs_dp_cmd_msg_size(void)
1320 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1322 msgsize += nla_total_size(IFNAMSIZ);
1323 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1324 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1325 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1330 /* Called with ovs_mutex or RCU read lock. */
1331 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1332 u32 portid, u32 seq, u32 flags, u8 cmd)
1334 struct ovs_header *ovs_header;
1335 struct ovs_dp_stats dp_stats;
1336 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1339 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1344 ovs_header->dp_ifindex = get_dpifindex(dp);
1346 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1348 goto nla_put_failure;
1350 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1351 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1353 goto nla_put_failure;
1355 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1356 sizeof(struct ovs_dp_megaflow_stats),
1357 &dp_megaflow_stats))
1358 goto nla_put_failure;
1360 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1361 goto nla_put_failure;
1363 return genlmsg_end(skb, ovs_header);
1366 genlmsg_cancel(skb, ovs_header);
1371 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1373 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1376 /* Called with rcu_read_lock or ovs_mutex. */
1377 static struct datapath *lookup_datapath(struct net *net,
1378 const struct ovs_header *ovs_header,
1379 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1381 struct datapath *dp;
1383 if (!a[OVS_DP_ATTR_NAME])
1384 dp = get_dp(net, ovs_header->dp_ifindex);
1386 struct vport *vport;
1388 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1389 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1391 return dp ? dp : ERR_PTR(-ENODEV);
1394 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1396 struct datapath *dp;
1398 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1402 WARN(dp->user_features, "Dropping previously announced user features\n");
1403 dp->user_features = 0;
1406 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1408 if (a[OVS_DP_ATTR_USER_FEATURES])
1409 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1412 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1414 struct nlattr **a = info->attrs;
1415 struct vport_parms parms;
1416 struct sk_buff *reply;
1417 struct datapath *dp;
1418 struct vport *vport;
1419 struct ovs_net *ovs_net;
1423 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1426 reply = ovs_dp_cmd_alloc_info(info);
1431 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1433 goto err_free_reply;
1435 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1437 /* Allocate table. */
1438 err = ovs_flow_tbl_init(&dp->table);
1442 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1443 if (!dp->stats_percpu) {
1445 goto err_destroy_table;
1448 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1452 goto err_destroy_percpu;
1455 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1456 INIT_HLIST_HEAD(&dp->ports[i]);
1458 /* Set up our datapath device. */
1459 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1460 parms.type = OVS_VPORT_TYPE_INTERNAL;
1461 parms.options = NULL;
1463 parms.port_no = OVSP_LOCAL;
1464 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1466 ovs_dp_change(dp, a);
1468 /* So far only local changes have been made, now need the lock. */
1471 vport = new_vport(&parms);
1472 if (IS_ERR(vport)) {
1473 err = PTR_ERR(vport);
1477 if (err == -EEXIST) {
1478 /* An outdated user space instance that does not understand
1479 * the concept of user_features has attempted to create a new
1480 * datapath and is likely to reuse it. Drop all user features.
1482 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1483 ovs_dp_reset_user_features(skb, info);
1486 goto err_destroy_ports_array;
1489 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1490 info->snd_seq, 0, OVS_DP_CMD_NEW);
1493 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1494 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1498 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1501 err_destroy_ports_array:
1505 free_percpu(dp->stats_percpu);
1507 ovs_flow_tbl_destroy(&dp->table);
1509 release_net(ovs_dp_get_net(dp));
1517 /* Called with ovs_mutex. */
1518 static void __dp_destroy(struct datapath *dp)
1522 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1523 struct vport *vport;
1524 struct hlist_node *n;
1526 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1527 if (vport->port_no != OVSP_LOCAL)
1528 ovs_dp_detach_port(vport);
1531 list_del_rcu(&dp->list_node);
1533 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1534 * all ports in datapath are destroyed first before freeing datapath.
1536 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1538 /* RCU destroy the flow table */
1539 call_rcu(&dp->rcu, destroy_dp_rcu);
1542 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1544 struct sk_buff *reply;
1545 struct datapath *dp;
1548 reply = ovs_dp_cmd_alloc_info(info);
1553 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1556 goto err_unlock_free;
1558 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1559 info->snd_seq, 0, OVS_DP_CMD_DEL);
1565 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1574 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1576 struct sk_buff *reply;
1577 struct datapath *dp;
1580 reply = ovs_dp_cmd_alloc_info(info);
1585 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1588 goto err_unlock_free;
1590 ovs_dp_change(dp, info->attrs);
1592 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1593 info->snd_seq, 0, OVS_DP_CMD_NEW);
1597 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1606 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1608 struct sk_buff *reply;
1609 struct datapath *dp;
1612 reply = ovs_dp_cmd_alloc_info(info);
1617 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1620 goto err_unlock_free;
1622 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1623 info->snd_seq, 0, OVS_DP_CMD_NEW);
1627 return genlmsg_reply(reply, info);
1635 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1637 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1638 struct datapath *dp;
1639 int skip = cb->args[0];
1643 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1645 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1646 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1647 OVS_DP_CMD_NEW) < 0)
1658 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1659 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1660 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1661 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1664 static struct genl_ops dp_datapath_genl_ops[] = {
1665 { .cmd = OVS_DP_CMD_NEW,
1666 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1667 .policy = datapath_policy,
1668 .doit = ovs_dp_cmd_new
1670 { .cmd = OVS_DP_CMD_DEL,
1671 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1672 .policy = datapath_policy,
1673 .doit = ovs_dp_cmd_del
1675 { .cmd = OVS_DP_CMD_GET,
1676 .flags = 0, /* OK for unprivileged users. */
1677 .policy = datapath_policy,
1678 .doit = ovs_dp_cmd_get,
1679 .dumpit = ovs_dp_cmd_dump
1681 { .cmd = OVS_DP_CMD_SET,
1682 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1683 .policy = datapath_policy,
1684 .doit = ovs_dp_cmd_set,
1688 static struct genl_family dp_datapath_genl_family = {
1689 .id = GENL_ID_GENERATE,
1690 .hdrsize = sizeof(struct ovs_header),
1691 .name = OVS_DATAPATH_FAMILY,
1692 .version = OVS_DATAPATH_VERSION,
1693 .maxattr = OVS_DP_ATTR_MAX,
1695 .parallel_ops = true,
1696 .ops = dp_datapath_genl_ops,
1697 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1698 .mcgrps = &ovs_dp_datapath_multicast_group,
1702 /* Called with ovs_mutex or RCU read lock. */
1703 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1704 u32 portid, u32 seq, u32 flags, u8 cmd)
1706 struct ovs_header *ovs_header;
1707 struct ovs_vport_stats vport_stats;
1710 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1715 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1717 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1718 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1719 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1720 goto nla_put_failure;
1722 ovs_vport_get_stats(vport, &vport_stats);
1723 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1725 goto nla_put_failure;
1727 if (ovs_vport_get_upcall_portids(vport, skb))
1728 goto nla_put_failure;
1730 err = ovs_vport_get_options(vport, skb);
1731 if (err == -EMSGSIZE)
1734 return genlmsg_end(skb, ovs_header);
1739 genlmsg_cancel(skb, ovs_header);
1743 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1745 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1748 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1749 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1752 struct sk_buff *skb;
1755 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1757 return ERR_PTR(-ENOMEM);
1759 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1765 /* Called with ovs_mutex or RCU read lock. */
1766 static struct vport *lookup_vport(struct net *net,
1767 const struct ovs_header *ovs_header,
1768 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1770 struct datapath *dp;
1771 struct vport *vport;
1773 if (a[OVS_VPORT_ATTR_NAME]) {
1774 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1776 return ERR_PTR(-ENODEV);
1777 if (ovs_header->dp_ifindex &&
1778 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1779 return ERR_PTR(-ENODEV);
1781 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1782 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1784 if (port_no >= DP_MAX_PORTS)
1785 return ERR_PTR(-EFBIG);
1787 dp = get_dp(net, ovs_header->dp_ifindex);
1789 return ERR_PTR(-ENODEV);
1791 vport = ovs_vport_ovsl_rcu(dp, port_no);
1793 return ERR_PTR(-ENODEV);
1796 return ERR_PTR(-EINVAL);
1799 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1801 struct nlattr **a = info->attrs;
1802 struct ovs_header *ovs_header = info->userhdr;
1803 struct vport_parms parms;
1804 struct sk_buff *reply;
1805 struct vport *vport;
1806 struct datapath *dp;
1810 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1811 !a[OVS_VPORT_ATTR_UPCALL_PID])
1814 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1815 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1816 if (port_no >= DP_MAX_PORTS)
1819 reply = ovs_vport_cmd_alloc_info();
1824 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1827 goto exit_unlock_free;
1830 vport = ovs_vport_ovsl(dp, port_no);
1833 goto exit_unlock_free;
1835 for (port_no = 1; ; port_no++) {
1836 if (port_no >= DP_MAX_PORTS) {
1838 goto exit_unlock_free;
1840 vport = ovs_vport_ovsl(dp, port_no);
1846 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1847 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1848 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1850 parms.port_no = port_no;
1851 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1853 vport = new_vport(&parms);
1854 err = PTR_ERR(vport);
1856 goto exit_unlock_free;
1858 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1859 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1863 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1872 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1874 struct nlattr **a = info->attrs;
1875 struct sk_buff *reply;
1876 struct vport *vport;
1879 reply = ovs_vport_cmd_alloc_info();
1884 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1885 err = PTR_ERR(vport);
1887 goto exit_unlock_free;
1889 if (a[OVS_VPORT_ATTR_TYPE] &&
1890 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1892 goto exit_unlock_free;
1895 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1896 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1898 goto exit_unlock_free;
1901 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1902 err = ovs_vport_set_upcall_portids(vport,
1903 a[OVS_VPORT_ATTR_UPCALL_PID]);
1905 goto exit_unlock_free;
1908 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1909 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1913 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1922 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1924 struct nlattr **a = info->attrs;
1925 struct sk_buff *reply;
1926 struct vport *vport;
1929 reply = ovs_vport_cmd_alloc_info();
1934 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1935 err = PTR_ERR(vport);
1937 goto exit_unlock_free;
1939 if (vport->port_no == OVSP_LOCAL) {
1941 goto exit_unlock_free;
1944 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1945 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1947 ovs_dp_detach_port(vport);
1950 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1959 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1961 struct nlattr **a = info->attrs;
1962 struct ovs_header *ovs_header = info->userhdr;
1963 struct sk_buff *reply;
1964 struct vport *vport;
1967 reply = ovs_vport_cmd_alloc_info();
1972 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1973 err = PTR_ERR(vport);
1975 goto exit_unlock_free;
1976 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1977 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1981 return genlmsg_reply(reply, info);
1989 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1991 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1992 struct datapath *dp;
1993 int bucket = cb->args[0], skip = cb->args[1];
1997 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2002 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2003 struct vport *vport;
2006 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2008 ovs_vport_cmd_fill_info(vport, skb,
2009 NETLINK_CB(cb->skb).portid,
2012 OVS_VPORT_CMD_NEW) < 0)
2028 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2029 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2030 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2031 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2032 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2033 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2034 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2037 static struct genl_ops dp_vport_genl_ops[] = {
2038 { .cmd = OVS_VPORT_CMD_NEW,
2039 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2040 .policy = vport_policy,
2041 .doit = ovs_vport_cmd_new
2043 { .cmd = OVS_VPORT_CMD_DEL,
2044 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2045 .policy = vport_policy,
2046 .doit = ovs_vport_cmd_del
2048 { .cmd = OVS_VPORT_CMD_GET,
2049 .flags = 0, /* OK for unprivileged users. */
2050 .policy = vport_policy,
2051 .doit = ovs_vport_cmd_get,
2052 .dumpit = ovs_vport_cmd_dump
2054 { .cmd = OVS_VPORT_CMD_SET,
2055 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2056 .policy = vport_policy,
2057 .doit = ovs_vport_cmd_set,
2061 struct genl_family dp_vport_genl_family = {
2062 .id = GENL_ID_GENERATE,
2063 .hdrsize = sizeof(struct ovs_header),
2064 .name = OVS_VPORT_FAMILY,
2065 .version = OVS_VPORT_VERSION,
2066 .maxattr = OVS_VPORT_ATTR_MAX,
2068 .parallel_ops = true,
2069 .ops = dp_vport_genl_ops,
2070 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2071 .mcgrps = &ovs_dp_vport_multicast_group,
2075 static struct genl_family *dp_genl_families[] = {
2076 &dp_datapath_genl_family,
2077 &dp_vport_genl_family,
2078 &dp_flow_genl_family,
2079 &dp_packet_genl_family,
2082 static void dp_unregister_genl(int n_families)
2086 for (i = 0; i < n_families; i++)
2087 genl_unregister_family(dp_genl_families[i]);
2090 static int dp_register_genl(void)
2095 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2097 err = genl_register_family(dp_genl_families[i]);
2105 dp_unregister_genl(i);
2109 static int __net_init ovs_init_net(struct net *net)
2111 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2113 INIT_LIST_HEAD(&ovs_net->dps);
2114 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2118 static void __net_exit ovs_exit_net(struct net *net)
2120 struct datapath *dp, *dp_next;
2121 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2124 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2128 cancel_work_sync(&ovs_net->dp_notify_work);
2131 static struct pernet_operations ovs_net_ops = {
2132 .init = ovs_init_net,
2133 .exit = ovs_exit_net,
2135 .size = sizeof(struct ovs_net),
2138 DEFINE_COMPAT_PNET_REG_FUNC(device);
2140 static int __init dp_init(void)
2144 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2146 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2149 err = action_fifos_init();
2153 err = ovs_flow_init();
2155 goto error_action_fifos_exit;
2157 err = ovs_vport_init();
2159 goto error_flow_exit;
2161 err = register_pernet_device(&ovs_net_ops);
2163 goto error_vport_exit;
2165 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2167 goto error_netns_exit;
2169 err = dp_register_genl();
2171 goto error_unreg_notifier;
2175 error_unreg_notifier:
2176 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2178 unregister_pernet_device(&ovs_net_ops);
2183 error_action_fifos_exit:
2184 action_fifos_exit();
2189 static void dp_cleanup(void)
2191 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2192 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2193 unregister_pernet_device(&ovs_net_ops);
2197 action_fifos_exit();
2200 module_init(dp_init);
2201 module_exit(dp_cleanup);
2203 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2204 MODULE_LICENSE("GPL");
2205 MODULE_VERSION(VERSION);