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_info *info,
84 const struct genl_multicast_group *grp)
86 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
87 netlink_has_listeners(genl_info_net(info)->genl_sock, GROUP_ID(grp));
90 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
91 struct sk_buff *skb, struct genl_info *info)
93 genl_notify(family, skb, genl_info_net(info),
94 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
100 * All writes e.g. Writes to device state (add/remove datapath, port, set
101 * operations on vports, etc.), Writes to other state (flow table
102 * modifications, set miscellaneous datapath parameters, etc.) are protected
105 * Reads are protected by RCU.
107 * There are a few special cases (mostly stats) that have their own
108 * synchronization but they nest under all of above and don't interact with
111 * The RTNL lock nests inside ovs_mutex.
114 static DEFINE_MUTEX(ovs_mutex);
118 mutex_lock(&ovs_mutex);
121 void ovs_unlock(void)
123 mutex_unlock(&ovs_mutex);
126 #ifdef CONFIG_LOCKDEP
127 int lockdep_ovsl_is_held(void)
130 return lockdep_is_held(&ovs_mutex);
136 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
137 const struct dp_upcall_info *);
138 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
139 const struct dp_upcall_info *);
141 /* Must be called with rcu_read_lock. */
142 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
144 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
147 struct vport *vport = ovs_internal_dev_get_vport(dev);
155 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
156 * returned dp pointer valid. */
157 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
161 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
163 dp = get_dp_rcu(net, dp_ifindex);
169 /* Must be called with rcu_read_lock or ovs_mutex. */
170 const char *ovs_dp_name(const struct datapath *dp)
172 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
173 return vport->ops->get_name(vport);
176 static int get_dpifindex(struct datapath *dp)
183 local = ovs_vport_rcu(dp, OVSP_LOCAL);
185 ifindex = netdev_vport_priv(local)->dev->ifindex;
194 static void destroy_dp_rcu(struct rcu_head *rcu)
196 struct datapath *dp = container_of(rcu, struct datapath, rcu);
198 ovs_flow_tbl_destroy(&dp->table);
199 free_percpu(dp->stats_percpu);
200 release_net(ovs_dp_get_net(dp));
205 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
208 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
211 /* Called with ovs_mutex or RCU read lock. */
212 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
215 struct hlist_head *head;
217 head = vport_hash_bucket(dp, port_no);
218 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
219 if (vport->port_no == port_no)
225 /* Called with ovs_mutex. */
226 static struct vport *new_vport(const struct vport_parms *parms)
230 vport = ovs_vport_add(parms);
231 if (!IS_ERR(vport)) {
232 struct datapath *dp = parms->dp;
233 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
235 hlist_add_head_rcu(&vport->dp_hash_node, head);
240 void ovs_dp_detach_port(struct vport *p)
244 /* First drop references to device. */
245 hlist_del_rcu(&p->dp_hash_node);
247 /* Then destroy it. */
251 /* Must be called with rcu_read_lock. */
252 void ovs_dp_process_packet(struct sk_buff *skb)
254 const struct vport *p = OVS_CB(skb)->input_vport;
255 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
256 struct datapath *dp = p->dp;
257 struct sw_flow *flow;
258 struct sw_flow_actions *sf_acts;
259 struct dp_stats_percpu *stats;
263 stats = this_cpu_ptr(dp->stats_percpu);
266 flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, skb_get_hash(skb),
268 if (unlikely(!flow)) {
269 struct dp_upcall_info upcall;
272 upcall.cmd = OVS_PACKET_CMD_MISS;
273 upcall.userdata = NULL;
274 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
275 upcall.egress_tun_info = NULL;
277 error = ovs_dp_upcall(dp, skb, &upcall);
283 stats_counter = &stats->n_missed;
287 ovs_flow_stats_update(flow, pkt_key->tp.flags, skb);
289 sf_acts = rcu_dereference(flow->sf_acts);
290 ovs_execute_actions(dp, skb, sf_acts);
291 stats_counter = &stats->n_hit;
294 /* Update datapath statistics. */
295 u64_stats_update_begin(&stats->syncp);
297 stats->n_mask_hit += n_mask_hit;
298 u64_stats_update_end(&stats->syncp);
301 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
302 const struct dp_upcall_info *upcall_info)
304 struct dp_stats_percpu *stats;
307 BUG_ON(!OVS_CB(skb)->pkt_key);
309 if (upcall_info->portid == 0) {
314 if (!skb_is_gso(skb))
315 err = queue_userspace_packet(dp, skb, upcall_info);
317 err = queue_gso_packets(dp, skb, upcall_info);
324 stats = this_cpu_ptr(dp->stats_percpu);
326 u64_stats_update_begin(&stats->syncp);
328 u64_stats_update_end(&stats->syncp);
333 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
334 const struct dp_upcall_info *upcall_info)
336 unsigned short gso_type = skb_shinfo(skb)->gso_type;
337 struct sw_flow_key later_key;
338 struct sk_buff *segs, *nskb;
341 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
343 return PTR_ERR(segs);
345 if (gso_type & SKB_GSO_UDP) {
346 /* The initial flow key extracted by ovs_flow_key_extract()
347 * in this case is for a first fragment, so we need to
348 * properly mark later fragments.
350 later_key = *OVS_CB(skb)->pkt_key;
351 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
354 /* Queue all of the segments. */
357 if (gso_type & SKB_GSO_UDP && skb != segs)
358 OVS_CB(skb)->pkt_key = &later_key;
360 err = queue_userspace_packet(dp, skb, upcall_info);
364 } while ((skb = skb->next));
366 /* Free all of the segments. */
374 } while ((skb = nskb));
378 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
381 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
382 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
383 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
385 /* OVS_PACKET_ATTR_USERDATA */
386 if (upcall_info->userdata)
387 size += NLA_ALIGN(upcall_info->userdata->nla_len);
389 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
390 if (upcall_info->egress_tun_info)
391 size += nla_total_size(ovs_tun_key_attr_size());
396 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
397 const struct dp_upcall_info *upcall_info)
399 struct ovs_header *upcall;
400 struct sk_buff *nskb = NULL;
401 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
402 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
404 struct genl_info info = {
405 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
406 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
408 .snd_portid = upcall_info->portid,
414 dp_ifindex = get_dpifindex(dp);
418 if (vlan_tx_tag_present(skb)) {
419 nskb = skb_clone(skb, GFP_ATOMIC);
423 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
427 vlan_set_tci(nskb, 0);
432 if (nla_attr_size(skb->len) > USHRT_MAX) {
437 /* Complete checksum if needed */
438 if (skb->ip_summed == CHECKSUM_PARTIAL &&
439 (err = skb_checksum_help(skb)))
442 /* Older versions of OVS user space enforce alignment of the last
443 * Netlink attribute to NLA_ALIGNTO which would require extensive
444 * padding logic. Only perform zerocopy if padding is not required.
446 if (dp->user_features & OVS_DP_F_UNALIGNED)
447 hlen = skb_zerocopy_headlen(skb);
451 len = upcall_msg_size(upcall_info, hlen);
452 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
458 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
459 0, upcall_info->cmd);
460 upcall->dp_ifindex = dp_ifindex;
462 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
463 err = ovs_nla_put_flow(pkt_key, pkt_key, user_skb);
465 nla_nest_end(user_skb, nla);
467 if (upcall_info->userdata)
468 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
469 nla_len(upcall_info->userdata),
470 nla_data(upcall_info->userdata));
472 if (upcall_info->egress_tun_info) {
473 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
474 err = ovs_nla_put_egress_tunnel_key(user_skb,
475 upcall_info->egress_tun_info);
477 nla_nest_end(user_skb, nla);
480 /* Only reserve room for attribute header, packet data is added
481 * in skb_zerocopy() */
482 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
486 nla->nla_len = nla_attr_size(skb->len);
488 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
492 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
493 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
494 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
497 memset(skb_put(user_skb, plen), 0, plen);
500 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
502 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
513 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
515 struct ovs_header *ovs_header = info->userhdr;
516 struct nlattr **a = info->attrs;
517 struct sw_flow_actions *acts;
518 struct sk_buff *packet;
519 struct sw_flow *flow;
520 struct sw_flow_actions *sf_acts;
523 struct vport *input_vport;
528 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
529 !a[OVS_PACKET_ATTR_ACTIONS])
532 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
533 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
537 skb_reserve(packet, NET_IP_ALIGN);
539 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
541 skb_reset_mac_header(packet);
542 eth = eth_hdr(packet);
544 /* Normally, setting the skb 'protocol' field would be handled by a
545 * call to eth_type_trans(), but it assumes there's a sending
546 * device, which we may not have. */
547 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
548 packet->protocol = eth->h_proto;
550 packet->protocol = htons(ETH_P_802_2);
552 /* Build an sw_flow for sending this packet. */
553 flow = ovs_flow_alloc();
558 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
563 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
568 rcu_assign_pointer(flow->sf_acts, acts);
569 OVS_CB(packet)->pkt_key = &flow->key;
570 OVS_CB(packet)->egress_tun_info = NULL;
571 packet->priority = flow->key.phy.priority;
572 packet->mark = flow->key.phy.skb_mark;
575 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
580 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
582 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
587 OVS_CB(packet)->input_vport = input_vport;
588 sf_acts = rcu_dereference(flow->sf_acts);
591 err = ovs_execute_actions(dp, packet, sf_acts);
595 ovs_flow_free(flow, false);
601 ovs_flow_free(flow, false);
608 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
609 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
610 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
611 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
614 static struct genl_ops dp_packet_genl_ops[] = {
615 { .cmd = OVS_PACKET_CMD_EXECUTE,
616 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
617 .policy = packet_policy,
618 .doit = ovs_packet_cmd_execute
622 static struct genl_family dp_packet_genl_family = {
623 .id = GENL_ID_GENERATE,
624 .hdrsize = sizeof(struct ovs_header),
625 .name = OVS_PACKET_FAMILY,
626 .version = OVS_PACKET_VERSION,
627 .maxattr = OVS_PACKET_ATTR_MAX,
629 .parallel_ops = true,
630 .ops = dp_packet_genl_ops,
631 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
634 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
635 struct ovs_dp_megaflow_stats *mega_stats)
639 memset(mega_stats, 0, sizeof(*mega_stats));
641 stats->n_flows = ovs_flow_tbl_count(&dp->table);
642 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
644 stats->n_hit = stats->n_missed = stats->n_lost = 0;
646 for_each_possible_cpu(i) {
647 const struct dp_stats_percpu *percpu_stats;
648 struct dp_stats_percpu local_stats;
651 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
654 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
655 local_stats = *percpu_stats;
656 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
658 stats->n_hit += local_stats.n_hit;
659 stats->n_missed += local_stats.n_missed;
660 stats->n_lost += local_stats.n_lost;
661 mega_stats->n_mask_hit += local_stats.n_mask_hit;
665 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
667 return NLMSG_ALIGN(sizeof(struct ovs_header))
668 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
669 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
670 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
671 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
672 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
673 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
676 /* Called with ovs_mutex or RCU read lock. */
677 static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
684 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
688 err = ovs_nla_put_flow(&flow->unmasked_key,
689 &flow->unmasked_key, skb);
692 nla_nest_end(skb, nla);
694 /* Fill flow mask. */
695 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
699 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
702 nla_nest_end(skb, nla);
707 /* Called with ovs_mutex or RCU read lock. */
708 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
711 struct ovs_flow_stats stats;
715 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
718 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
721 if (stats.n_packets &&
722 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
725 if ((u8)ntohs(tcp_flags) &&
726 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
732 /* Called with ovs_mutex or RCU read lock. */
733 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
734 struct sk_buff *skb, int skb_orig_len)
736 struct nlattr *start;
739 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
740 * this is the first flow to be dumped into 'skb'. This is unusual for
741 * Netlink but individual action lists can be longer than
742 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
743 * The userspace caller can always fetch the actions separately if it
744 * really wants them. (Most userspace callers in fact don't care.)
746 * This can only fail for dump operations because the skb is always
747 * properly sized for single flows.
749 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
751 const struct sw_flow_actions *sf_acts;
753 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
754 err = ovs_nla_put_actions(sf_acts->actions,
755 sf_acts->actions_len, skb);
758 nla_nest_end(skb, start);
763 nla_nest_cancel(skb, start);
765 } else if (skb_orig_len) {
772 /* Called with ovs_mutex or RCU read lock. */
773 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
774 struct sk_buff *skb, u32 portid,
775 u32 seq, u32 flags, u8 cmd)
777 const int skb_orig_len = skb->len;
778 struct ovs_header *ovs_header;
781 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
784 ovs_header->dp_ifindex = dp_ifindex;
786 err = ovs_flow_cmd_fill_match(flow, skb);
790 err = ovs_flow_cmd_fill_stats(flow, skb);
794 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
798 return genlmsg_end(skb, ovs_header);
801 genlmsg_cancel(skb, ovs_header);
805 /* May not be called with RCU read lock. */
806 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
807 struct genl_info *info,
812 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
815 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
818 return ERR_PTR(-ENOMEM);
823 /* Called with ovs_mutex. */
824 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
825 const struct sw_flow *flow,
827 struct genl_info *info, u8 cmd,
833 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
835 if (IS_ERR_OR_NULL(skb))
838 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
839 info->snd_portid, info->snd_seq, 0,
845 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
847 struct nlattr **a = info->attrs;
848 struct ovs_header *ovs_header = info->userhdr;
849 struct sw_flow *flow, *new_flow;
850 struct sw_flow_mask mask;
851 struct sk_buff *reply;
853 struct sw_flow_actions *acts;
854 struct sw_flow_match match;
857 /* Must have key and actions. */
859 if (!a[OVS_FLOW_ATTR_KEY]) {
860 OVS_NLERR("Flow key attribute not present in new flow.\n");
863 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
864 OVS_NLERR("Flow actions attribute not present in new flow.\n");
868 /* Most of the time we need to allocate a new flow, do it before
870 new_flow = ovs_flow_alloc();
871 if (IS_ERR(new_flow)) {
872 error = PTR_ERR(new_flow);
877 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
878 error = ovs_nla_get_match(&match,
879 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
883 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
885 /* Validate actions. */
886 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
889 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
893 reply = ovs_flow_cmd_alloc_info(acts, info, false);
895 error = PTR_ERR(reply);
900 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
905 /* Check if this is a duplicate flow */
906 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
908 rcu_assign_pointer(new_flow->sf_acts, acts);
910 /* Put flow in bucket. */
911 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
912 if (unlikely(error)) {
917 if (unlikely(reply)) {
918 error = ovs_flow_cmd_fill_info(new_flow,
919 ovs_header->dp_ifindex,
920 reply, info->snd_portid,
927 struct sw_flow_actions *old_acts;
929 /* Bail out if we're not allowed to modify an existing flow.
930 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
931 * because Generic Netlink treats the latter as a dump
932 * request. We also accept NLM_F_EXCL in case that bug ever
935 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
940 /* The unmasked key has to be the same for flow updates. */
941 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
942 /* Look for any overlapping flow. */
943 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
949 /* Update actions. */
950 old_acts = ovsl_dereference(flow->sf_acts);
951 rcu_assign_pointer(flow->sf_acts, acts);
953 if (unlikely(reply)) {
954 error = ovs_flow_cmd_fill_info(flow,
955 ovs_header->dp_ifindex,
956 reply, info->snd_portid,
963 ovs_nla_free_flow_actions(old_acts);
964 ovs_flow_free(new_flow, false);
968 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
977 ovs_flow_free(new_flow, false);
982 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
983 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
984 const struct sw_flow_key *key,
985 const struct sw_flow_mask *mask)
987 struct sw_flow_actions *acts;
988 struct sw_flow_key masked_key;
991 ovs_flow_mask_key(&masked_key, key, mask);
992 error = ovs_nla_copy_actions(a, &masked_key, &acts);
994 OVS_NLERR("Actions may not be safe on all matching packets.\n");
995 return ERR_PTR(error);
1001 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1003 struct nlattr **a = info->attrs;
1004 struct ovs_header *ovs_header = info->userhdr;
1005 struct sw_flow_key key;
1006 struct sw_flow *flow;
1007 struct sw_flow_mask mask;
1008 struct sk_buff *reply = NULL;
1009 struct datapath *dp;
1010 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1011 struct sw_flow_match match;
1016 if (!a[OVS_FLOW_ATTR_KEY]) {
1017 OVS_NLERR("Flow key attribute not present in set flow.\n");
1021 ovs_match_init(&match, &key, &mask);
1022 error = ovs_nla_get_match(&match,
1023 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1027 /* Validate actions. */
1028 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1029 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1031 error = PTR_ERR(acts);
1035 /* Can allocate before locking if have acts. */
1036 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1037 if (IS_ERR(reply)) {
1038 error = PTR_ERR(reply);
1039 goto err_kfree_acts;
1044 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1045 if (unlikely(!dp)) {
1047 goto err_unlock_ovs;
1049 /* Check that the flow exists. */
1050 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1051 if (unlikely(!flow)) {
1053 goto err_unlock_ovs;
1056 /* Update actions, if present. */
1058 old_acts = ovsl_dereference(flow->sf_acts);
1059 rcu_assign_pointer(flow->sf_acts, acts);
1061 if (unlikely(reply)) {
1062 error = ovs_flow_cmd_fill_info(flow,
1063 ovs_header->dp_ifindex,
1064 reply, info->snd_portid,
1070 /* Could not alloc without acts before locking. */
1071 reply = ovs_flow_cmd_build_info(dp, flow,
1072 ovs_header->dp_ifindex,
1073 info, OVS_FLOW_CMD_NEW, false);
1074 if (unlikely(IS_ERR(reply))) {
1075 error = PTR_ERR(reply);
1076 goto err_unlock_ovs;
1081 if (a[OVS_FLOW_ATTR_CLEAR])
1082 ovs_flow_stats_clear(flow);
1086 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1088 ovs_nla_free_flow_actions(old_acts);
1100 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1102 struct nlattr **a = info->attrs;
1103 struct ovs_header *ovs_header = info->userhdr;
1104 struct sw_flow_key key;
1105 struct sk_buff *reply;
1106 struct sw_flow *flow;
1107 struct datapath *dp;
1108 struct sw_flow_match match;
1111 if (!a[OVS_FLOW_ATTR_KEY]) {
1112 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1116 ovs_match_init(&match, &key, NULL);
1117 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1122 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1128 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1134 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1135 OVS_FLOW_CMD_NEW, true);
1136 if (IS_ERR(reply)) {
1137 err = PTR_ERR(reply);
1142 return genlmsg_reply(reply, info);
1148 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1150 struct nlattr **a = info->attrs;
1151 struct ovs_header *ovs_header = info->userhdr;
1152 struct sw_flow_key key;
1153 struct sk_buff *reply;
1154 struct sw_flow *flow;
1155 struct datapath *dp;
1156 struct sw_flow_match match;
1159 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1160 ovs_match_init(&match, &key, NULL);
1161 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1167 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1168 if (unlikely(!dp)) {
1172 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1173 err = ovs_flow_tbl_flush(&dp->table);
1176 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1177 if (unlikely(!flow)) {
1182 ovs_flow_tbl_remove(&dp->table, flow);
1185 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1188 if (likely(reply)) {
1189 if (likely(!IS_ERR(reply))) {
1190 rcu_read_lock(); /* Keep RCU checker happy. */
1191 err = ovs_flow_cmd_fill_info(flow,
1192 ovs_header->dp_ifindex,
1193 reply, info->snd_portid,
1198 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1200 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1201 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1206 ovs_flow_free(flow, true);
1213 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1215 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1216 struct table_instance *ti;
1217 struct datapath *dp;
1220 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1226 ti = rcu_dereference(dp->table.ti);
1228 struct sw_flow *flow;
1231 bucket = cb->args[0];
1233 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1237 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1238 NETLINK_CB(cb->skb).portid,
1239 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1240 OVS_FLOW_CMD_NEW) < 0)
1243 cb->args[0] = bucket;
1250 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1251 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1252 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1253 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1256 static struct genl_ops dp_flow_genl_ops[] = {
1257 { .cmd = OVS_FLOW_CMD_NEW,
1258 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1259 .policy = flow_policy,
1260 .doit = ovs_flow_cmd_new
1262 { .cmd = OVS_FLOW_CMD_DEL,
1263 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1264 .policy = flow_policy,
1265 .doit = ovs_flow_cmd_del
1267 { .cmd = OVS_FLOW_CMD_GET,
1268 .flags = 0, /* OK for unprivileged users. */
1269 .policy = flow_policy,
1270 .doit = ovs_flow_cmd_get,
1271 .dumpit = ovs_flow_cmd_dump
1273 { .cmd = OVS_FLOW_CMD_SET,
1274 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1275 .policy = flow_policy,
1276 .doit = ovs_flow_cmd_set,
1280 static struct genl_family dp_flow_genl_family = {
1281 .id = GENL_ID_GENERATE,
1282 .hdrsize = sizeof(struct ovs_header),
1283 .name = OVS_FLOW_FAMILY,
1284 .version = OVS_FLOW_VERSION,
1285 .maxattr = OVS_FLOW_ATTR_MAX,
1287 .parallel_ops = true,
1288 .ops = dp_flow_genl_ops,
1289 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1290 .mcgrps = &ovs_dp_flow_multicast_group,
1294 static size_t ovs_dp_cmd_msg_size(void)
1296 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1298 msgsize += nla_total_size(IFNAMSIZ);
1299 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1300 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1301 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1306 /* Called with ovs_mutex or RCU read lock. */
1307 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1308 u32 portid, u32 seq, u32 flags, u8 cmd)
1310 struct ovs_header *ovs_header;
1311 struct ovs_dp_stats dp_stats;
1312 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1315 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1320 ovs_header->dp_ifindex = get_dpifindex(dp);
1322 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1324 goto nla_put_failure;
1326 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1327 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1329 goto nla_put_failure;
1331 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1332 sizeof(struct ovs_dp_megaflow_stats),
1333 &dp_megaflow_stats))
1334 goto nla_put_failure;
1336 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1337 goto nla_put_failure;
1339 return genlmsg_end(skb, ovs_header);
1342 genlmsg_cancel(skb, ovs_header);
1347 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1349 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1352 /* Called with rcu_read_lock or ovs_mutex. */
1353 static struct datapath *lookup_datapath(struct net *net,
1354 struct ovs_header *ovs_header,
1355 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1357 struct datapath *dp;
1359 if (!a[OVS_DP_ATTR_NAME])
1360 dp = get_dp(net, ovs_header->dp_ifindex);
1362 struct vport *vport;
1364 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1365 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1367 return dp ? dp : ERR_PTR(-ENODEV);
1370 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1372 struct datapath *dp;
1374 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1378 WARN(dp->user_features, "Dropping previously announced user features\n");
1379 dp->user_features = 0;
1382 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1384 if (a[OVS_DP_ATTR_USER_FEATURES])
1385 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1388 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1390 struct nlattr **a = info->attrs;
1391 struct vport_parms parms;
1392 struct sk_buff *reply;
1393 struct datapath *dp;
1394 struct vport *vport;
1395 struct ovs_net *ovs_net;
1399 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1402 reply = ovs_dp_cmd_alloc_info(info);
1407 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1409 goto err_free_reply;
1411 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1413 /* Allocate table. */
1414 err = ovs_flow_tbl_init(&dp->table);
1418 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1419 if (!dp->stats_percpu) {
1421 goto err_destroy_table;
1424 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1428 goto err_destroy_percpu;
1431 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1432 INIT_HLIST_HEAD(&dp->ports[i]);
1434 /* Set up our datapath device. */
1435 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1436 parms.type = OVS_VPORT_TYPE_INTERNAL;
1437 parms.options = NULL;
1439 parms.port_no = OVSP_LOCAL;
1440 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1442 ovs_dp_change(dp, a);
1444 /* So far only local changes have been made, now need the lock. */
1447 vport = new_vport(&parms);
1448 if (IS_ERR(vport)) {
1449 err = PTR_ERR(vport);
1453 if (err == -EEXIST) {
1454 /* An outdated user space instance that does not understand
1455 * the concept of user_features has attempted to create a new
1456 * datapath and is likely to reuse it. Drop all user features.
1458 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1459 ovs_dp_reset_user_features(skb, info);
1462 goto err_destroy_ports_array;
1465 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1466 info->snd_seq, 0, OVS_DP_CMD_NEW);
1469 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1470 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1474 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1477 err_destroy_ports_array:
1481 free_percpu(dp->stats_percpu);
1483 ovs_flow_tbl_destroy(&dp->table);
1485 release_net(ovs_dp_get_net(dp));
1493 /* Called with ovs_mutex. */
1494 static void __dp_destroy(struct datapath *dp)
1498 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1499 struct vport *vport;
1500 struct hlist_node *n;
1502 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1503 if (vport->port_no != OVSP_LOCAL)
1504 ovs_dp_detach_port(vport);
1507 list_del_rcu(&dp->list_node);
1509 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1510 * all ports in datapath are destroyed first before freeing datapath.
1512 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1514 /* RCU destroy the flow table */
1515 call_rcu(&dp->rcu, destroy_dp_rcu);
1518 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1520 struct sk_buff *reply;
1521 struct datapath *dp;
1524 reply = ovs_dp_cmd_alloc_info(info);
1529 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1532 goto err_unlock_free;
1534 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1535 info->snd_seq, 0, OVS_DP_CMD_DEL);
1541 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1550 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1552 struct sk_buff *reply;
1553 struct datapath *dp;
1556 reply = ovs_dp_cmd_alloc_info(info);
1561 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1564 goto err_unlock_free;
1566 ovs_dp_change(dp, info->attrs);
1568 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1569 info->snd_seq, 0, OVS_DP_CMD_NEW);
1573 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1582 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1584 struct sk_buff *reply;
1585 struct datapath *dp;
1588 reply = ovs_dp_cmd_alloc_info(info);
1593 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1596 goto err_unlock_free;
1598 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1599 info->snd_seq, 0, OVS_DP_CMD_NEW);
1603 return genlmsg_reply(reply, info);
1611 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1613 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1614 struct datapath *dp;
1615 int skip = cb->args[0];
1619 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1621 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1622 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1623 OVS_DP_CMD_NEW) < 0)
1634 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1635 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1636 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1637 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1640 static struct genl_ops dp_datapath_genl_ops[] = {
1641 { .cmd = OVS_DP_CMD_NEW,
1642 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1643 .policy = datapath_policy,
1644 .doit = ovs_dp_cmd_new
1646 { .cmd = OVS_DP_CMD_DEL,
1647 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1648 .policy = datapath_policy,
1649 .doit = ovs_dp_cmd_del
1651 { .cmd = OVS_DP_CMD_GET,
1652 .flags = 0, /* OK for unprivileged users. */
1653 .policy = datapath_policy,
1654 .doit = ovs_dp_cmd_get,
1655 .dumpit = ovs_dp_cmd_dump
1657 { .cmd = OVS_DP_CMD_SET,
1658 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1659 .policy = datapath_policy,
1660 .doit = ovs_dp_cmd_set,
1664 static struct genl_family dp_datapath_genl_family = {
1665 .id = GENL_ID_GENERATE,
1666 .hdrsize = sizeof(struct ovs_header),
1667 .name = OVS_DATAPATH_FAMILY,
1668 .version = OVS_DATAPATH_VERSION,
1669 .maxattr = OVS_DP_ATTR_MAX,
1671 .parallel_ops = true,
1672 .ops = dp_datapath_genl_ops,
1673 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1674 .mcgrps = &ovs_dp_datapath_multicast_group,
1678 /* Called with ovs_mutex or RCU read lock. */
1679 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1680 u32 portid, u32 seq, u32 flags, u8 cmd)
1682 struct ovs_header *ovs_header;
1683 struct ovs_vport_stats vport_stats;
1686 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1691 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1693 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1694 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1695 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1696 goto nla_put_failure;
1698 ovs_vport_get_stats(vport, &vport_stats);
1699 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1701 goto nla_put_failure;
1703 if (ovs_vport_get_upcall_portids(vport, skb))
1704 goto nla_put_failure;
1706 err = ovs_vport_get_options(vport, skb);
1707 if (err == -EMSGSIZE)
1710 return genlmsg_end(skb, ovs_header);
1715 genlmsg_cancel(skb, ovs_header);
1719 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1721 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1724 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1725 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1728 struct sk_buff *skb;
1731 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1733 return ERR_PTR(-ENOMEM);
1735 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1741 /* Called with ovs_mutex or RCU read lock. */
1742 static struct vport *lookup_vport(struct net *net,
1743 struct ovs_header *ovs_header,
1744 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1746 struct datapath *dp;
1747 struct vport *vport;
1749 if (a[OVS_VPORT_ATTR_NAME]) {
1750 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1752 return ERR_PTR(-ENODEV);
1753 if (ovs_header->dp_ifindex &&
1754 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1755 return ERR_PTR(-ENODEV);
1757 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1758 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1760 if (port_no >= DP_MAX_PORTS)
1761 return ERR_PTR(-EFBIG);
1763 dp = get_dp(net, ovs_header->dp_ifindex);
1765 return ERR_PTR(-ENODEV);
1767 vport = ovs_vport_ovsl_rcu(dp, port_no);
1769 return ERR_PTR(-ENODEV);
1772 return ERR_PTR(-EINVAL);
1775 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1777 struct nlattr **a = info->attrs;
1778 struct ovs_header *ovs_header = info->userhdr;
1779 struct vport_parms parms;
1780 struct sk_buff *reply;
1781 struct vport *vport;
1782 struct datapath *dp;
1786 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1787 !a[OVS_VPORT_ATTR_UPCALL_PID])
1790 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1791 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1792 if (port_no >= DP_MAX_PORTS)
1795 reply = ovs_vport_cmd_alloc_info();
1800 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1803 goto exit_unlock_free;
1806 vport = ovs_vport_ovsl(dp, port_no);
1809 goto exit_unlock_free;
1811 for (port_no = 1; ; port_no++) {
1812 if (port_no >= DP_MAX_PORTS) {
1814 goto exit_unlock_free;
1816 vport = ovs_vport_ovsl(dp, port_no);
1822 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1823 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1824 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1826 parms.port_no = port_no;
1827 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1829 vport = new_vport(&parms);
1830 err = PTR_ERR(vport);
1832 goto exit_unlock_free;
1834 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1835 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1839 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1848 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1850 struct nlattr **a = info->attrs;
1851 struct sk_buff *reply;
1852 struct vport *vport;
1855 reply = ovs_vport_cmd_alloc_info();
1860 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1861 err = PTR_ERR(vport);
1863 goto exit_unlock_free;
1865 if (a[OVS_VPORT_ATTR_TYPE] &&
1866 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1868 goto exit_unlock_free;
1871 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1872 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1874 goto exit_unlock_free;
1877 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1878 err = ovs_vport_set_upcall_portids(vport,
1879 a[OVS_VPORT_ATTR_UPCALL_PID]);
1881 goto exit_unlock_free;
1884 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1885 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1889 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1898 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1900 struct nlattr **a = info->attrs;
1901 struct sk_buff *reply;
1902 struct vport *vport;
1905 reply = ovs_vport_cmd_alloc_info();
1910 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1911 err = PTR_ERR(vport);
1913 goto exit_unlock_free;
1915 if (vport->port_no == OVSP_LOCAL) {
1917 goto exit_unlock_free;
1920 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1921 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1923 ovs_dp_detach_port(vport);
1926 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1935 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1937 struct nlattr **a = info->attrs;
1938 struct ovs_header *ovs_header = info->userhdr;
1939 struct sk_buff *reply;
1940 struct vport *vport;
1943 reply = ovs_vport_cmd_alloc_info();
1948 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1949 err = PTR_ERR(vport);
1951 goto exit_unlock_free;
1952 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1953 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1957 return genlmsg_reply(reply, info);
1965 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1967 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1968 struct datapath *dp;
1969 int bucket = cb->args[0], skip = cb->args[1];
1973 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1978 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1979 struct vport *vport;
1982 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1984 ovs_vport_cmd_fill_info(vport, skb,
1985 NETLINK_CB(cb->skb).portid,
1988 OVS_VPORT_CMD_NEW) < 0)
2004 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2005 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2006 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2007 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2008 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2009 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2010 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2013 static struct genl_ops dp_vport_genl_ops[] = {
2014 { .cmd = OVS_VPORT_CMD_NEW,
2015 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2016 .policy = vport_policy,
2017 .doit = ovs_vport_cmd_new
2019 { .cmd = OVS_VPORT_CMD_DEL,
2020 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2021 .policy = vport_policy,
2022 .doit = ovs_vport_cmd_del
2024 { .cmd = OVS_VPORT_CMD_GET,
2025 .flags = 0, /* OK for unprivileged users. */
2026 .policy = vport_policy,
2027 .doit = ovs_vport_cmd_get,
2028 .dumpit = ovs_vport_cmd_dump
2030 { .cmd = OVS_VPORT_CMD_SET,
2031 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2032 .policy = vport_policy,
2033 .doit = ovs_vport_cmd_set,
2037 struct genl_family dp_vport_genl_family = {
2038 .id = GENL_ID_GENERATE,
2039 .hdrsize = sizeof(struct ovs_header),
2040 .name = OVS_VPORT_FAMILY,
2041 .version = OVS_VPORT_VERSION,
2042 .maxattr = OVS_VPORT_ATTR_MAX,
2044 .parallel_ops = true,
2045 .ops = dp_vport_genl_ops,
2046 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2047 .mcgrps = &ovs_dp_vport_multicast_group,
2051 static struct genl_family *dp_genl_families[] = {
2052 &dp_datapath_genl_family,
2053 &dp_vport_genl_family,
2054 &dp_flow_genl_family,
2055 &dp_packet_genl_family,
2058 static void dp_unregister_genl(int n_families)
2062 for (i = 0; i < n_families; i++)
2063 genl_unregister_family(dp_genl_families[i]);
2066 static int dp_register_genl(void)
2071 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2073 err = genl_register_family(dp_genl_families[i]);
2081 dp_unregister_genl(i);
2085 static int __net_init ovs_init_net(struct net *net)
2087 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2089 INIT_LIST_HEAD(&ovs_net->dps);
2090 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2094 static void __net_exit ovs_exit_net(struct net *net)
2096 struct datapath *dp, *dp_next;
2097 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2100 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2104 cancel_work_sync(&ovs_net->dp_notify_work);
2107 static struct pernet_operations ovs_net_ops = {
2108 .init = ovs_init_net,
2109 .exit = ovs_exit_net,
2111 .size = sizeof(struct ovs_net),
2114 DEFINE_COMPAT_PNET_REG_FUNC(device);
2116 static int __init dp_init(void)
2120 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2122 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2125 err = action_fifos_init();
2129 err = ovs_flow_init();
2131 goto error_action_fifos_exit;
2133 err = ovs_vport_init();
2135 goto error_flow_exit;
2137 err = register_pernet_device(&ovs_net_ops);
2139 goto error_vport_exit;
2141 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2143 goto error_netns_exit;
2145 err = dp_register_genl();
2147 goto error_unreg_notifier;
2151 error_unreg_notifier:
2152 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2154 unregister_pernet_device(&ovs_net_ops);
2159 error_action_fifos_exit:
2160 action_fifos_exit();
2165 static void dp_cleanup(void)
2167 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2168 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2169 unregister_pernet_device(&ovs_net_ops);
2173 action_fifos_exit();
2176 module_init(dp_init);
2177 module_exit(dp_cleanup);
2179 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2180 MODULE_LICENSE("GPL");
2181 MODULE_VERSION(VERSION);