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 <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
59 #include "flow_table.h"
60 #include "flow_netlink.h"
62 #include "vport-internal_dev.h"
63 #include "vport-netdev.h"
65 int ovs_net_id __read_mostly;
67 static struct genl_family dp_packet_genl_family;
68 static struct genl_family dp_flow_genl_family;
69 static struct genl_family dp_datapath_genl_family;
71 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
72 .name = OVS_FLOW_MCGROUP
75 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
76 .name = OVS_DATAPATH_MCGROUP
79 struct genl_multicast_group ovs_dp_vport_multicast_group = {
80 .name = OVS_VPORT_MCGROUP
83 /* Check if need to build a reply message.
84 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
85 static bool ovs_must_notify(struct genl_info *info,
86 const struct genl_multicast_group *grp)
88 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
89 netlink_has_listeners(genl_info_net(info)->genl_sock, GROUP_ID(grp));
92 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
93 struct sk_buff *skb, struct genl_info *info)
95 genl_notify(family, skb, genl_info_net(info),
96 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
102 * All writes e.g. Writes to device state (add/remove datapath, port, set
103 * operations on vports, etc.), Writes to other state (flow table
104 * modifications, set miscellaneous datapath parameters, etc.) are protected
107 * Reads are protected by RCU.
109 * There are a few special cases (mostly stats) that have their own
110 * synchronization but they nest under all of above and don't interact with
113 * The RTNL lock nests inside ovs_mutex.
116 static DEFINE_MUTEX(ovs_mutex);
120 mutex_lock(&ovs_mutex);
123 void ovs_unlock(void)
125 mutex_unlock(&ovs_mutex);
128 #ifdef CONFIG_LOCKDEP
129 int lockdep_ovsl_is_held(void)
132 return lockdep_is_held(&ovs_mutex);
138 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
139 const struct dp_upcall_info *);
140 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
141 const struct dp_upcall_info *);
143 /* Must be called with rcu_read_lock. */
144 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
146 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
157 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
158 * returned dp pointer valid. */
159 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
163 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
165 dp = get_dp_rcu(net, dp_ifindex);
171 /* Must be called with rcu_read_lock or ovs_mutex. */
172 const char *ovs_dp_name(const struct datapath *dp)
174 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
175 return vport->ops->get_name(vport);
178 static int get_dpifindex(struct datapath *dp)
185 local = ovs_vport_rcu(dp, OVSP_LOCAL);
187 ifindex = netdev_vport_priv(local)->dev->ifindex;
196 static void destroy_dp_rcu(struct rcu_head *rcu)
198 struct datapath *dp = container_of(rcu, struct datapath, rcu);
200 ovs_flow_tbl_destroy(&dp->table);
201 free_percpu(dp->stats_percpu);
202 release_net(ovs_dp_get_net(dp));
207 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
210 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
213 /* Called with ovs_mutex or RCU read lock. */
214 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
217 struct hlist_head *head;
219 head = vport_hash_bucket(dp, port_no);
220 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
221 if (vport->port_no == port_no)
227 /* Called with ovs_mutex. */
228 static struct vport *new_vport(const struct vport_parms *parms)
232 vport = ovs_vport_add(parms);
233 if (!IS_ERR(vport)) {
234 struct datapath *dp = parms->dp;
235 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
237 hlist_add_head_rcu(&vport->dp_hash_node, head);
242 void ovs_dp_detach_port(struct vport *p)
246 /* First drop references to device. */
247 hlist_del_rcu(&p->dp_hash_node);
249 /* Then destroy it. */
253 /* Must be called with rcu_read_lock. */
254 void ovs_dp_process_packet(struct sk_buff *skb)
256 const struct vport *p = OVS_CB(skb)->input_vport;
257 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
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, pkt_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, &upcall);
285 stats_counter = &stats->n_missed;
289 ovs_flow_stats_update(flow, pkt_key->tp.flags, skb);
291 sf_acts = rcu_dereference(flow->sf_acts);
292 ovs_execute_actions(dp, skb, sf_acts);
293 stats_counter = &stats->n_hit;
296 /* Update datapath statistics. */
297 u64_stats_update_begin(&stats->sync);
299 stats->n_mask_hit += n_mask_hit;
300 u64_stats_update_end(&stats->sync);
303 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
304 const struct dp_upcall_info *upcall_info)
306 struct dp_stats_percpu *stats;
309 BUG_ON(!OVS_CB(skb)->pkt_key);
311 if (upcall_info->portid == 0) {
316 if (!skb_is_gso(skb))
317 err = queue_userspace_packet(dp, skb, upcall_info);
319 err = queue_gso_packets(dp, skb, upcall_info);
326 stats = this_cpu_ptr(dp->stats_percpu);
328 u64_stats_update_begin(&stats->sync);
330 u64_stats_update_end(&stats->sync);
335 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
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;
343 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
345 return PTR_ERR(segs);
347 if (gso_type & SKB_GSO_UDP) {
348 /* The initial flow key extracted by ovs_flow_key_extract()
349 * in this case is for a first fragment, so we need to
350 * properly mark later fragments.
352 later_key = *OVS_CB(skb)->pkt_key;
353 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
356 /* Queue all of the segments. */
359 if (gso_type & SKB_GSO_UDP && skb != segs)
360 OVS_CB(skb)->pkt_key = &later_key;
362 err = queue_userspace_packet(dp, skb, upcall_info);
366 } while ((skb = skb->next));
368 /* Free all of the segments. */
376 } while ((skb = nskb));
380 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
383 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
384 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
385 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
387 /* OVS_PACKET_ATTR_USERDATA */
388 if (upcall_info->userdata)
389 size += NLA_ALIGN(upcall_info->userdata->nla_len);
391 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
392 if (upcall_info->egress_tun_info)
393 size += nla_total_size(ovs_tun_key_attr_size());
398 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
399 const struct dp_upcall_info *upcall_info)
401 struct ovs_header *upcall;
402 struct sk_buff *nskb = NULL;
403 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
404 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
406 struct genl_info info = {
407 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
408 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
410 .snd_portid = upcall_info->portid,
416 dp_ifindex = get_dpifindex(dp);
420 if (vlan_tx_tag_present(skb)) {
421 nskb = skb_clone(skb, GFP_ATOMIC);
425 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
429 vlan_set_tci(nskb, 0);
434 if (nla_attr_size(skb->len) > USHRT_MAX) {
439 /* Complete checksum if needed */
440 if (skb->ip_summed == CHECKSUM_PARTIAL &&
441 (err = skb_checksum_help(skb)))
444 /* Older versions of OVS user space enforce alignment of the last
445 * Netlink attribute to NLA_ALIGNTO which would require extensive
446 * padding logic. Only perform zerocopy if padding is not required.
448 if (dp->user_features & OVS_DP_F_UNALIGNED)
449 hlen = skb_zerocopy_headlen(skb);
453 len = upcall_msg_size(upcall_info, hlen);
454 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
460 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
461 0, upcall_info->cmd);
462 upcall->dp_ifindex = dp_ifindex;
464 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
465 err = ovs_nla_put_flow(dp, pkt_key, pkt_key, user_skb);
467 nla_nest_end(user_skb, nla);
469 if (upcall_info->userdata)
470 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
471 nla_len(upcall_info->userdata),
472 nla_data(upcall_info->userdata));
474 if (upcall_info->egress_tun_info) {
475 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
476 err = ovs_nla_put_egress_tunnel_key(user_skb,
477 upcall_info->egress_tun_info);
479 nla_nest_end(user_skb, nla);
482 /* Only reserve room for attribute header, packet data is added
483 * in skb_zerocopy() */
484 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
488 nla->nla_len = nla_attr_size(skb->len);
490 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
494 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
495 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
496 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
499 memset(skb_put(user_skb, plen), 0, plen);
502 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
504 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
515 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
517 struct ovs_header *ovs_header = info->userhdr;
518 struct nlattr **a = info->attrs;
519 struct sw_flow_actions *acts;
520 struct sk_buff *packet;
521 struct sw_flow *flow;
522 struct sw_flow_actions *sf_acts;
525 struct vport *input_vport;
530 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
531 !a[OVS_PACKET_ATTR_ACTIONS])
534 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
535 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
539 skb_reserve(packet, NET_IP_ALIGN);
541 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
543 skb_reset_mac_header(packet);
544 eth = eth_hdr(packet);
546 /* Normally, setting the skb 'protocol' field would be handled by a
547 * call to eth_type_trans(), but it assumes there's a sending
548 * device, which we may not have. */
549 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
550 packet->protocol = eth->h_proto;
552 packet->protocol = htons(ETH_P_802_2);
554 /* Build an sw_flow for sending this packet. */
555 flow = ovs_flow_alloc();
560 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
565 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
570 rcu_assign_pointer(flow->sf_acts, acts);
571 OVS_CB(packet)->pkt_key = &flow->key;
572 OVS_CB(skb)->egress_tun_info = NULL;
573 packet->priority = flow->key.phy.priority;
574 packet->mark = flow->key.phy.skb_mark;
577 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
582 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
584 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
589 OVS_CB(packet)->input_vport = input_vport;
590 sf_acts = rcu_dereference(flow->sf_acts);
593 err = ovs_execute_actions(dp, packet, sf_acts);
597 ovs_flow_free(flow, false);
603 ovs_flow_free(flow, false);
610 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
611 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
612 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
613 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
616 static struct genl_ops dp_packet_genl_ops[] = {
617 { .cmd = OVS_PACKET_CMD_EXECUTE,
618 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
619 .policy = packet_policy,
620 .doit = ovs_packet_cmd_execute
624 static struct genl_family dp_packet_genl_family = {
625 .id = GENL_ID_GENERATE,
626 .hdrsize = sizeof(struct ovs_header),
627 .name = OVS_PACKET_FAMILY,
628 .version = OVS_PACKET_VERSION,
629 .maxattr = OVS_PACKET_ATTR_MAX,
631 .parallel_ops = true,
632 .ops = dp_packet_genl_ops,
633 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
636 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
637 struct ovs_dp_megaflow_stats *mega_stats)
641 memset(mega_stats, 0, sizeof(*mega_stats));
643 stats->n_flows = ovs_flow_tbl_count(&dp->table);
644 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
646 stats->n_hit = stats->n_missed = stats->n_lost = 0;
648 for_each_possible_cpu(i) {
649 const struct dp_stats_percpu *percpu_stats;
650 struct dp_stats_percpu local_stats;
653 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
656 start = u64_stats_fetch_begin_irq(&percpu_stats->sync);
657 local_stats = *percpu_stats;
658 } while (u64_stats_fetch_retry_irq(&percpu_stats->sync, start));
660 stats->n_hit += local_stats.n_hit;
661 stats->n_missed += local_stats.n_missed;
662 stats->n_lost += local_stats.n_lost;
663 mega_stats->n_mask_hit += local_stats.n_mask_hit;
667 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
669 return NLMSG_ALIGN(sizeof(struct ovs_header))
670 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
671 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
672 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
673 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
674 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
675 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
678 /* Called with ovs_mutex or RCU read lock. */
679 static int ovs_flow_cmd_fill_match(struct datapath *dp,
680 const struct sw_flow *flow,
687 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
691 err = ovs_nla_put_flow(dp, &flow->unmasked_key,
692 &flow->unmasked_key, skb);
695 nla_nest_end(skb, nla);
697 /* Fill flow mask. */
698 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
702 err = ovs_nla_put_flow(dp, &flow->key, &flow->mask->key, skb);
705 nla_nest_end(skb, nla);
710 /* Called with ovs_mutex or RCU read lock. */
711 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
714 struct ovs_flow_stats stats;
718 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
721 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
724 if (stats.n_packets &&
725 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
728 if ((u8)ntohs(tcp_flags) &&
729 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
735 /* Called with ovs_mutex or RCU read lock. */
736 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
737 struct sk_buff *skb, int skb_orig_len)
739 struct nlattr *start;
742 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
743 * this is the first flow to be dumped into 'skb'. This is unusual for
744 * Netlink but individual action lists can be longer than
745 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
746 * The userspace caller can always fetch the actions separately if it
747 * really wants them. (Most userspace callers in fact don't care.)
749 * This can only fail for dump operations because the skb is always
750 * properly sized for single flows.
752 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
754 const struct sw_flow_actions *sf_acts;
756 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
757 err = ovs_nla_put_actions(sf_acts->actions,
758 sf_acts->actions_len, skb);
761 nla_nest_end(skb, start);
766 nla_nest_cancel(skb, start);
768 } else if (skb_orig_len) {
775 /* Called with ovs_mutex or RCU read lock. */
776 static int ovs_flow_cmd_fill_info(struct datapath *dp,
777 const struct sw_flow *flow, int dp_ifindex,
778 struct sk_buff *skb, u32 portid,
779 u32 seq, u32 flags, u8 cmd)
781 const int skb_orig_len = skb->len;
782 struct ovs_header *ovs_header;
785 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
788 ovs_header->dp_ifindex = dp_ifindex;
790 err = ovs_flow_cmd_fill_match(dp, flow, skb);
794 err = ovs_flow_cmd_fill_stats(flow, skb);
798 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
802 return genlmsg_end(skb, ovs_header);
805 genlmsg_cancel(skb, ovs_header);
809 /* May not be called with RCU read lock. */
810 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
811 struct genl_info *info,
816 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
819 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
822 return ERR_PTR(-ENOMEM);
827 /* Called with ovs_mutex. */
828 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
829 const struct sw_flow *flow,
831 struct genl_info *info, u8 cmd,
837 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
839 if (!skb || IS_ERR(skb))
842 retval = ovs_flow_cmd_fill_info(dp, flow, dp_ifindex, skb,
843 info->snd_portid, info->snd_seq, 0,
849 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
851 struct nlattr **a = info->attrs;
852 struct ovs_header *ovs_header = info->userhdr;
853 struct sw_flow *flow, *new_flow;
854 struct sw_flow_mask mask;
855 struct sk_buff *reply;
857 struct sw_flow_actions *acts;
858 struct sw_flow_match match;
861 /* Must have key and actions. */
863 if (!a[OVS_FLOW_ATTR_KEY]) {
864 OVS_NLERR("Flow key attribute not present in new flow.\n");
867 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
868 OVS_NLERR("Flow actions attribute not present in new flow.\n");
872 /* Most of the time we need to allocate a new flow, do it before
874 new_flow = ovs_flow_alloc();
875 if (IS_ERR(new_flow)) {
876 error = PTR_ERR(new_flow);
881 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
882 error = ovs_nla_get_match(&match,
883 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
887 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
889 /* Validate actions. */
890 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
893 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
897 reply = ovs_flow_cmd_alloc_info(acts, info, false);
899 error = PTR_ERR(reply);
904 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
909 /* Check if this is a duplicate flow */
910 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
912 rcu_assign_pointer(new_flow->sf_acts, acts);
914 /* Put flow in bucket. */
915 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
916 if (unlikely(error)) {
921 if (unlikely(reply)) {
922 error = ovs_flow_cmd_fill_info(dp, new_flow,
923 ovs_header->dp_ifindex,
924 reply, info->snd_portid,
931 struct sw_flow_actions *old_acts;
933 /* Bail out if we're not allowed to modify an existing flow.
934 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
935 * because Generic Netlink treats the latter as a dump
936 * request. We also accept NLM_F_EXCL in case that bug ever
939 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
944 /* The unmasked key has to be the same for flow updates. */
945 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
946 /* Look for any overlapping flow. */
947 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
953 /* Update actions. */
954 old_acts = ovsl_dereference(flow->sf_acts);
955 rcu_assign_pointer(flow->sf_acts, acts);
957 if (unlikely(reply)) {
958 error = ovs_flow_cmd_fill_info(dp, flow,
959 ovs_header->dp_ifindex,
960 reply, info->snd_portid,
967 ovs_nla_free_flow_actions(old_acts);
968 ovs_flow_free(new_flow, false);
972 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
981 ovs_flow_free(new_flow, false);
986 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
987 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
988 const struct sw_flow_key *key,
989 const struct sw_flow_mask *mask)
991 struct sw_flow_actions *acts;
992 struct sw_flow_key masked_key;
995 ovs_flow_mask_key(&masked_key, key, mask);
996 error = ovs_nla_copy_actions(a, &masked_key, &acts);
998 OVS_NLERR("Actions may not be safe on all matching packets.\n");
999 return ERR_PTR(error);
1005 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1007 struct nlattr **a = info->attrs;
1008 struct ovs_header *ovs_header = info->userhdr;
1009 struct sw_flow_key key;
1010 struct sw_flow *flow;
1011 struct sw_flow_mask mask;
1012 struct sk_buff *reply = NULL;
1013 struct datapath *dp;
1014 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1015 struct sw_flow_match match;
1020 if (!a[OVS_FLOW_ATTR_KEY]) {
1021 OVS_NLERR("Flow key attribute not present in set flow.\n");
1025 ovs_match_init(&match, &key, &mask);
1026 error = ovs_nla_get_match(&match,
1027 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1031 /* Validate actions. */
1032 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1033 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1035 error = PTR_ERR(acts);
1039 /* Can allocate before locking if have acts. */
1040 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1041 if (IS_ERR(reply)) {
1042 error = PTR_ERR(reply);
1043 goto err_kfree_acts;
1048 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1049 if (unlikely(!dp)) {
1051 goto err_unlock_ovs;
1053 /* Check that the flow exists. */
1054 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1055 if (unlikely(!flow)) {
1057 goto err_unlock_ovs;
1060 /* Update actions, if present. */
1062 old_acts = ovsl_dereference(flow->sf_acts);
1063 rcu_assign_pointer(flow->sf_acts, acts);
1065 if (unlikely(reply)) {
1066 error = ovs_flow_cmd_fill_info(dp, flow,
1067 ovs_header->dp_ifindex,
1068 reply, info->snd_portid,
1074 /* Could not alloc without acts before locking. */
1075 reply = ovs_flow_cmd_build_info(dp, flow,
1076 ovs_header->dp_ifindex,
1077 info, OVS_FLOW_CMD_NEW, false);
1078 if (unlikely(IS_ERR(reply))) {
1079 error = PTR_ERR(reply);
1080 goto err_unlock_ovs;
1085 if (a[OVS_FLOW_ATTR_CLEAR])
1086 ovs_flow_stats_clear(flow);
1090 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1092 ovs_nla_free_flow_actions(old_acts);
1104 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1106 struct nlattr **a = info->attrs;
1107 struct ovs_header *ovs_header = info->userhdr;
1108 struct sw_flow_key key;
1109 struct sk_buff *reply;
1110 struct sw_flow *flow;
1111 struct datapath *dp;
1112 struct sw_flow_match match;
1115 if (!a[OVS_FLOW_ATTR_KEY]) {
1116 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1120 ovs_match_init(&match, &key, NULL);
1121 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1126 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1132 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1138 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1139 OVS_FLOW_CMD_NEW, true);
1140 if (IS_ERR(reply)) {
1141 err = PTR_ERR(reply);
1146 return genlmsg_reply(reply, info);
1152 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1154 struct nlattr **a = info->attrs;
1155 struct ovs_header *ovs_header = info->userhdr;
1156 struct sw_flow_key key;
1157 struct sk_buff *reply;
1158 struct sw_flow *flow;
1159 struct datapath *dp;
1160 struct sw_flow_match match;
1163 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1164 ovs_match_init(&match, &key, NULL);
1165 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1171 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1172 if (unlikely(!dp)) {
1176 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1177 err = ovs_flow_tbl_flush(&dp->table);
1180 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1181 if (unlikely(!flow)) {
1186 ovs_flow_tbl_remove(&dp->table, flow);
1189 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1192 if (likely(reply)) {
1193 if (likely(!IS_ERR(reply))) {
1194 rcu_read_lock(); /* Keep RCU checker happy. */
1195 err = ovs_flow_cmd_fill_info(dp, flow,
1196 ovs_header->dp_ifindex,
1197 reply, info->snd_portid,
1202 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1204 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1205 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1210 ovs_flow_free(flow, true);
1217 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1219 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1220 struct table_instance *ti;
1221 struct datapath *dp;
1224 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1230 ti = rcu_dereference(dp->table.ti);
1232 struct sw_flow *flow;
1235 bucket = cb->args[0];
1237 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1241 if (ovs_flow_cmd_fill_info(dp, flow, ovs_header->dp_ifindex, skb,
1242 NETLINK_CB(cb->skb).portid,
1243 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1244 OVS_FLOW_CMD_NEW) < 0)
1247 cb->args[0] = bucket;
1254 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1255 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1256 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1257 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1260 static struct genl_ops dp_flow_genl_ops[] = {
1261 { .cmd = OVS_FLOW_CMD_NEW,
1262 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1263 .policy = flow_policy,
1264 .doit = ovs_flow_cmd_new
1266 { .cmd = OVS_FLOW_CMD_DEL,
1267 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1268 .policy = flow_policy,
1269 .doit = ovs_flow_cmd_del
1271 { .cmd = OVS_FLOW_CMD_GET,
1272 .flags = 0, /* OK for unprivileged users. */
1273 .policy = flow_policy,
1274 .doit = ovs_flow_cmd_get,
1275 .dumpit = ovs_flow_cmd_dump
1277 { .cmd = OVS_FLOW_CMD_SET,
1278 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1279 .policy = flow_policy,
1280 .doit = ovs_flow_cmd_set,
1284 static struct genl_family dp_flow_genl_family = {
1285 .id = GENL_ID_GENERATE,
1286 .hdrsize = sizeof(struct ovs_header),
1287 .name = OVS_FLOW_FAMILY,
1288 .version = OVS_FLOW_VERSION,
1289 .maxattr = OVS_FLOW_ATTR_MAX,
1291 .parallel_ops = true,
1292 .ops = dp_flow_genl_ops,
1293 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1294 .mcgrps = &ovs_dp_flow_multicast_group,
1298 static size_t ovs_dp_cmd_msg_size(void)
1300 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1302 msgsize += nla_total_size(IFNAMSIZ);
1303 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1304 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1305 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1310 /* Called with ovs_mutex or RCU read lock. */
1311 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1312 u32 portid, u32 seq, u32 flags, u8 cmd)
1314 struct ovs_header *ovs_header;
1315 struct ovs_dp_stats dp_stats;
1316 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1319 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1324 ovs_header->dp_ifindex = get_dpifindex(dp);
1326 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1328 goto nla_put_failure;
1330 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1331 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1333 goto nla_put_failure;
1335 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1336 sizeof(struct ovs_dp_megaflow_stats),
1337 &dp_megaflow_stats))
1338 goto nla_put_failure;
1340 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1341 goto nla_put_failure;
1343 return genlmsg_end(skb, ovs_header);
1346 genlmsg_cancel(skb, ovs_header);
1351 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1353 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1356 /* Called with rcu_read_lock or ovs_mutex. */
1357 static struct datapath *lookup_datapath(struct net *net,
1358 struct ovs_header *ovs_header,
1359 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1361 struct datapath *dp;
1363 if (!a[OVS_DP_ATTR_NAME])
1364 dp = get_dp(net, ovs_header->dp_ifindex);
1366 struct vport *vport;
1368 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1369 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1371 return dp ? dp : ERR_PTR(-ENODEV);
1374 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1376 struct datapath *dp;
1378 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1382 WARN(dp->user_features, "Dropping previously announced user features\n");
1383 dp->user_features = 0;
1386 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1388 if (a[OVS_DP_ATTR_USER_FEATURES])
1389 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1392 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1394 struct nlattr **a = info->attrs;
1395 struct vport_parms parms;
1396 struct sk_buff *reply;
1397 struct datapath *dp;
1398 struct vport *vport;
1399 struct ovs_net *ovs_net;
1403 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1406 reply = ovs_dp_cmd_alloc_info(info);
1411 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1413 goto err_free_reply;
1415 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1417 /* Allocate table. */
1418 err = ovs_flow_tbl_init(&dp->table);
1422 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1423 if (!dp->stats_percpu) {
1425 goto err_destroy_table;
1428 for_each_possible_cpu(i) {
1429 struct dp_stats_percpu *dpath_stats;
1430 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1431 u64_stats_init(&dpath_stats->sync);
1434 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1438 goto err_destroy_percpu;
1441 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1442 INIT_HLIST_HEAD(&dp->ports[i]);
1444 /* Set up our datapath device. */
1445 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1446 parms.type = OVS_VPORT_TYPE_INTERNAL;
1447 parms.options = NULL;
1449 parms.port_no = OVSP_LOCAL;
1450 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1452 ovs_dp_change(dp, a);
1454 /* So far only local changes have been made, now need the lock. */
1457 vport = new_vport(&parms);
1458 if (IS_ERR(vport)) {
1459 err = PTR_ERR(vport);
1463 if (err == -EEXIST) {
1464 /* An outdated user space instance that does not understand
1465 * the concept of user_features has attempted to create a new
1466 * datapath and is likely to reuse it. Drop all user features.
1468 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1469 ovs_dp_reset_user_features(skb, info);
1472 goto err_destroy_ports_array;
1475 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1476 info->snd_seq, 0, OVS_DP_CMD_NEW);
1479 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1480 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1484 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1487 err_destroy_ports_array:
1491 free_percpu(dp->stats_percpu);
1493 ovs_flow_tbl_destroy(&dp->table);
1495 release_net(ovs_dp_get_net(dp));
1503 /* Called with ovs_mutex. */
1504 static void __dp_destroy(struct datapath *dp)
1508 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1509 struct vport *vport;
1510 struct hlist_node *n;
1512 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1513 if (vport->port_no != OVSP_LOCAL)
1514 ovs_dp_detach_port(vport);
1517 list_del_rcu(&dp->list_node);
1519 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1520 * all ports in datapath are destroyed first before freeing datapath.
1522 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1524 /* RCU destroy the flow table */
1525 call_rcu(&dp->rcu, destroy_dp_rcu);
1528 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1530 struct sk_buff *reply;
1531 struct datapath *dp;
1534 reply = ovs_dp_cmd_alloc_info(info);
1539 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1542 goto err_unlock_free;
1544 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1545 info->snd_seq, 0, OVS_DP_CMD_DEL);
1551 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1560 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1562 struct sk_buff *reply;
1563 struct datapath *dp;
1566 reply = ovs_dp_cmd_alloc_info(info);
1571 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1574 goto err_unlock_free;
1576 ovs_dp_change(dp, info->attrs);
1578 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1579 info->snd_seq, 0, OVS_DP_CMD_NEW);
1583 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1592 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1594 struct sk_buff *reply;
1595 struct datapath *dp;
1598 reply = ovs_dp_cmd_alloc_info(info);
1603 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1606 goto err_unlock_free;
1608 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1609 info->snd_seq, 0, OVS_DP_CMD_NEW);
1613 return genlmsg_reply(reply, info);
1621 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1623 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1624 struct datapath *dp;
1625 int skip = cb->args[0];
1629 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1631 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1632 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1633 OVS_DP_CMD_NEW) < 0)
1644 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1645 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1646 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1647 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1650 static struct genl_ops dp_datapath_genl_ops[] = {
1651 { .cmd = OVS_DP_CMD_NEW,
1652 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1653 .policy = datapath_policy,
1654 .doit = ovs_dp_cmd_new
1656 { .cmd = OVS_DP_CMD_DEL,
1657 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1658 .policy = datapath_policy,
1659 .doit = ovs_dp_cmd_del
1661 { .cmd = OVS_DP_CMD_GET,
1662 .flags = 0, /* OK for unprivileged users. */
1663 .policy = datapath_policy,
1664 .doit = ovs_dp_cmd_get,
1665 .dumpit = ovs_dp_cmd_dump
1667 { .cmd = OVS_DP_CMD_SET,
1668 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1669 .policy = datapath_policy,
1670 .doit = ovs_dp_cmd_set,
1674 static struct genl_family dp_datapath_genl_family = {
1675 .id = GENL_ID_GENERATE,
1676 .hdrsize = sizeof(struct ovs_header),
1677 .name = OVS_DATAPATH_FAMILY,
1678 .version = OVS_DATAPATH_VERSION,
1679 .maxattr = OVS_DP_ATTR_MAX,
1681 .parallel_ops = true,
1682 .ops = dp_datapath_genl_ops,
1683 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1684 .mcgrps = &ovs_dp_datapath_multicast_group,
1688 /* Called with ovs_mutex or RCU read lock. */
1689 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1690 u32 portid, u32 seq, u32 flags, u8 cmd)
1692 struct ovs_header *ovs_header;
1693 struct ovs_vport_stats vport_stats;
1696 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1701 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1703 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1704 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1705 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1706 goto nla_put_failure;
1708 ovs_vport_get_stats(vport, &vport_stats);
1709 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1711 goto nla_put_failure;
1713 if (ovs_vport_get_upcall_portids(vport, skb))
1714 goto nla_put_failure;
1716 err = ovs_vport_get_options(vport, skb);
1717 if (err == -EMSGSIZE)
1720 return genlmsg_end(skb, ovs_header);
1725 genlmsg_cancel(skb, ovs_header);
1729 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1731 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1734 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1735 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1738 struct sk_buff *skb;
1741 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1743 return ERR_PTR(-ENOMEM);
1745 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1751 /* Called with ovs_mutex or RCU read lock. */
1752 static struct vport *lookup_vport(struct net *net,
1753 struct ovs_header *ovs_header,
1754 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1756 struct datapath *dp;
1757 struct vport *vport;
1759 if (a[OVS_VPORT_ATTR_NAME]) {
1760 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1762 return ERR_PTR(-ENODEV);
1763 if (ovs_header->dp_ifindex &&
1764 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1765 return ERR_PTR(-ENODEV);
1767 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1768 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1770 if (port_no >= DP_MAX_PORTS)
1771 return ERR_PTR(-EFBIG);
1773 dp = get_dp(net, ovs_header->dp_ifindex);
1775 return ERR_PTR(-ENODEV);
1777 vport = ovs_vport_ovsl_rcu(dp, port_no);
1779 return ERR_PTR(-ENODEV);
1782 return ERR_PTR(-EINVAL);
1785 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1787 struct nlattr **a = info->attrs;
1788 struct ovs_header *ovs_header = info->userhdr;
1789 struct vport_parms parms;
1790 struct sk_buff *reply;
1791 struct vport *vport;
1792 struct datapath *dp;
1796 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1797 !a[OVS_VPORT_ATTR_UPCALL_PID])
1800 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1801 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1802 if (port_no >= DP_MAX_PORTS)
1805 reply = ovs_vport_cmd_alloc_info();
1810 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1813 goto exit_unlock_free;
1816 vport = ovs_vport_ovsl(dp, port_no);
1819 goto exit_unlock_free;
1821 for (port_no = 1; ; port_no++) {
1822 if (port_no >= DP_MAX_PORTS) {
1824 goto exit_unlock_free;
1826 vport = ovs_vport_ovsl(dp, port_no);
1832 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1833 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1834 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1836 parms.port_no = port_no;
1837 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1839 vport = new_vport(&parms);
1840 err = PTR_ERR(vport);
1842 goto exit_unlock_free;
1845 if (a[OVS_VPORT_ATTR_STATS])
1846 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1848 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1849 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1853 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1862 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1864 struct nlattr **a = info->attrs;
1865 struct sk_buff *reply;
1866 struct vport *vport;
1869 reply = ovs_vport_cmd_alloc_info();
1874 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1875 err = PTR_ERR(vport);
1877 goto exit_unlock_free;
1879 if (a[OVS_VPORT_ATTR_TYPE] &&
1880 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1882 goto exit_unlock_free;
1885 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1886 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1888 goto exit_unlock_free;
1891 if (a[OVS_VPORT_ATTR_STATS])
1892 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1895 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1896 err = ovs_vport_set_upcall_portids(vport,
1897 a[OVS_VPORT_ATTR_UPCALL_PID]);
1899 goto exit_unlock_free;
1902 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1903 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1907 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1916 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1918 struct nlattr **a = info->attrs;
1919 struct sk_buff *reply;
1920 struct vport *vport;
1923 reply = ovs_vport_cmd_alloc_info();
1928 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1929 err = PTR_ERR(vport);
1931 goto exit_unlock_free;
1933 if (vport->port_no == OVSP_LOCAL) {
1935 goto exit_unlock_free;
1938 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1939 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1941 ovs_dp_detach_port(vport);
1944 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1953 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1955 struct nlattr **a = info->attrs;
1956 struct ovs_header *ovs_header = info->userhdr;
1957 struct sk_buff *reply;
1958 struct vport *vport;
1961 reply = ovs_vport_cmd_alloc_info();
1966 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1967 err = PTR_ERR(vport);
1969 goto exit_unlock_free;
1970 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1971 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1975 return genlmsg_reply(reply, info);
1983 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1985 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1986 struct datapath *dp;
1987 int bucket = cb->args[0], skip = cb->args[1];
1991 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1996 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1997 struct vport *vport;
2000 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2002 ovs_vport_cmd_fill_info(vport, skb,
2003 NETLINK_CB(cb->skb).portid,
2006 OVS_VPORT_CMD_NEW) < 0)
2022 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2023 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2024 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2025 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2026 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2027 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2028 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2031 static struct genl_ops dp_vport_genl_ops[] = {
2032 { .cmd = OVS_VPORT_CMD_NEW,
2033 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2034 .policy = vport_policy,
2035 .doit = ovs_vport_cmd_new
2037 { .cmd = OVS_VPORT_CMD_DEL,
2038 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2039 .policy = vport_policy,
2040 .doit = ovs_vport_cmd_del
2042 { .cmd = OVS_VPORT_CMD_GET,
2043 .flags = 0, /* OK for unprivileged users. */
2044 .policy = vport_policy,
2045 .doit = ovs_vport_cmd_get,
2046 .dumpit = ovs_vport_cmd_dump
2048 { .cmd = OVS_VPORT_CMD_SET,
2049 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2050 .policy = vport_policy,
2051 .doit = ovs_vport_cmd_set,
2055 struct genl_family dp_vport_genl_family = {
2056 .id = GENL_ID_GENERATE,
2057 .hdrsize = sizeof(struct ovs_header),
2058 .name = OVS_VPORT_FAMILY,
2059 .version = OVS_VPORT_VERSION,
2060 .maxattr = OVS_VPORT_ATTR_MAX,
2062 .parallel_ops = true,
2063 .ops = dp_vport_genl_ops,
2064 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2065 .mcgrps = &ovs_dp_vport_multicast_group,
2069 static struct genl_family *dp_genl_families[] = {
2070 &dp_datapath_genl_family,
2071 &dp_vport_genl_family,
2072 &dp_flow_genl_family,
2073 &dp_packet_genl_family,
2076 static void dp_unregister_genl(int n_families)
2080 for (i = 0; i < n_families; i++)
2081 genl_unregister_family(dp_genl_families[i]);
2084 static int dp_register_genl(void)
2089 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2091 err = genl_register_family(dp_genl_families[i]);
2099 dp_unregister_genl(i);
2103 static int __net_init ovs_init_net(struct net *net)
2105 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2107 INIT_LIST_HEAD(&ovs_net->dps);
2108 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2112 static void __net_exit ovs_exit_net(struct net *net)
2114 struct datapath *dp, *dp_next;
2115 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2118 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2122 cancel_work_sync(&ovs_net->dp_notify_work);
2125 static struct pernet_operations ovs_net_ops = {
2126 .init = ovs_init_net,
2127 .exit = ovs_exit_net,
2129 .size = sizeof(struct ovs_net),
2132 DEFINE_COMPAT_PNET_REG_FUNC(device);
2134 static int __init dp_init(void)
2138 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2140 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2143 err = action_fifos_init();
2147 err = ovs_flow_init();
2149 goto error_action_fifos_exit;
2151 err = ovs_vport_init();
2153 goto error_flow_exit;
2155 err = register_pernet_device(&ovs_net_ops);
2157 goto error_vport_exit;
2159 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2161 goto error_netns_exit;
2163 err = dp_register_genl();
2165 goto error_unreg_notifier;
2169 error_unreg_notifier:
2170 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2172 unregister_pernet_device(&ovs_net_ops);
2177 error_action_fifos_exit:
2178 action_fifos_exit();
2183 static void dp_cleanup(void)
2185 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2186 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2187 unregister_pernet_device(&ovs_net_ops);
2191 action_fifos_exit();
2194 module_init(dp_init);
2195 module_exit(dp_cleanup);
2197 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2198 MODULE_LICENSE("GPL");
2199 MODULE_VERSION(VERSION);