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 or ovs_mutex. */
144 static struct datapath *get_dp(struct net *net, int dp_ifindex)
146 struct datapath *dp = NULL;
147 struct net_device *dev;
150 dev = dev_get_by_index_rcu(net, dp_ifindex);
152 struct vport *vport = ovs_internal_dev_get_vport(dev);
161 /* Must be called with rcu_read_lock or ovs_mutex. */
162 const char *ovs_dp_name(const struct datapath *dp)
164 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
165 return vport->ops->get_name(vport);
168 static int get_dpifindex(struct datapath *dp)
175 local = ovs_vport_rcu(dp, OVSP_LOCAL);
177 ifindex = netdev_vport_priv(local)->dev->ifindex;
186 static void destroy_dp_rcu(struct rcu_head *rcu)
188 struct datapath *dp = container_of(rcu, struct datapath, rcu);
190 free_percpu(dp->stats_percpu);
191 release_net(ovs_dp_get_net(dp));
196 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
199 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
202 /* Called with ovs_mutex or RCU read lock. */
203 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
206 struct hlist_head *head;
208 head = vport_hash_bucket(dp, port_no);
209 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
210 if (vport->port_no == port_no)
216 /* Called with ovs_mutex. */
217 static struct vport *new_vport(const struct vport_parms *parms)
221 vport = ovs_vport_add(parms);
222 if (!IS_ERR(vport)) {
223 struct datapath *dp = parms->dp;
224 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
226 hlist_add_head_rcu(&vport->dp_hash_node, head);
231 void ovs_dp_detach_port(struct vport *p)
235 /* First drop references to device. */
236 hlist_del_rcu(&p->dp_hash_node);
238 /* Then destroy it. */
242 void ovs_dp_process_packet_with_key(struct sk_buff *skb,
243 struct sw_flow_key *pkt_key)
245 const struct vport *p = OVS_CB(skb)->input_vport;
246 struct datapath *dp = p->dp;
247 struct sw_flow *flow;
248 struct dp_stats_percpu *stats;
252 stats = this_cpu_ptr(dp->stats_percpu);
255 flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, &n_mask_hit);
256 if (unlikely(!flow)) {
257 struct dp_upcall_info upcall;
259 upcall.cmd = OVS_PACKET_CMD_MISS;
260 upcall.key = pkt_key;
261 upcall.userdata = NULL;
262 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
263 ovs_dp_upcall(dp, skb, &upcall);
265 stats_counter = &stats->n_missed;
269 OVS_CB(skb)->pkt_key = pkt_key;
270 OVS_CB(skb)->flow = flow;
272 ovs_flow_stats_update(OVS_CB(skb)->flow, pkt_key->tp.flags, skb);
273 ovs_execute_actions(dp, skb);
274 stats_counter = &stats->n_hit;
277 /* Update datapath statistics. */
278 u64_stats_update_begin(&stats->sync);
280 stats->n_mask_hit += n_mask_hit;
281 u64_stats_update_end(&stats->sync);
284 /* Must be called with rcu_read_lock. */
285 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
288 struct sw_flow_key key;
290 OVS_CB(skb)->input_vport = p;
292 /* Extract flow from 'skb' into 'key'. */
293 error = ovs_flow_extract(skb, p->port_no, &key);
294 if (unlikely(error)) {
299 ovs_dp_process_packet_with_key(skb, &key);
302 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
303 const struct dp_upcall_info *upcall_info)
305 struct dp_stats_percpu *stats;
308 if (upcall_info->portid == 0) {
313 if (!skb_is_gso(skb))
314 err = queue_userspace_packet(dp, skb, upcall_info);
316 err = queue_gso_packets(dp, skb, upcall_info);
323 stats = this_cpu_ptr(dp->stats_percpu);
325 u64_stats_update_begin(&stats->sync);
327 u64_stats_update_end(&stats->sync);
332 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
333 const struct dp_upcall_info *upcall_info)
335 unsigned short gso_type = skb_shinfo(skb)->gso_type;
336 struct dp_upcall_info later_info;
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 /* Queue all of the segments. */
348 err = queue_userspace_packet(dp, skb, upcall_info);
352 if (skb == segs && gso_type & SKB_GSO_UDP) {
353 /* The initial flow key extracted by ovs_flow_extract()
354 * in this case is for a first fragment, so we need to
355 * properly mark later fragments.
357 later_key = *upcall_info->key;
358 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
360 later_info = *upcall_info;
361 later_info.key = &later_key;
362 upcall_info = &later_info;
364 } while ((skb = skb->next));
366 /* Free all of the segments. */
374 } while ((skb = nskb));
378 static size_t key_attr_size(void)
380 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
381 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
382 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
383 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
384 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
385 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
386 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
387 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
388 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
389 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
390 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
391 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
392 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
393 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
394 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
395 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
396 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
397 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
398 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
401 static size_t upcall_msg_size(const struct nlattr *userdata,
404 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
405 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
406 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
408 /* OVS_PACKET_ATTR_USERDATA */
410 size += NLA_ALIGN(userdata->nla_len);
415 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
416 const struct dp_upcall_info *upcall_info)
418 struct ovs_header *upcall;
419 struct sk_buff *nskb = NULL;
420 struct sk_buff *user_skb; /* to be queued to userspace */
422 struct genl_info info = {
423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
424 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
426 .snd_portid = upcall_info->portid,
432 dp_ifindex = get_dpifindex(dp);
436 if (vlan_tx_tag_present(skb)) {
437 nskb = skb_clone(skb, GFP_ATOMIC);
441 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
445 vlan_set_tci(nskb, 0);
450 if (nla_attr_size(skb->len) > USHRT_MAX) {
455 /* Complete checksum if needed */
456 if (skb->ip_summed == CHECKSUM_PARTIAL &&
457 (err = skb_checksum_help(skb)))
460 /* Older versions of OVS user space enforce alignment of the last
461 * Netlink attribute to NLA_ALIGNTO which would require extensive
462 * padding logic. Only perform zerocopy if padding is not required.
464 if (dp->user_features & OVS_DP_F_UNALIGNED)
465 hlen = skb_zerocopy_headlen(skb);
469 len = upcall_msg_size(upcall_info->userdata, hlen);
470 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
476 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
477 0, upcall_info->cmd);
478 upcall->dp_ifindex = dp_ifindex;
480 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
481 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
482 nla_nest_end(user_skb, nla);
484 if (upcall_info->userdata)
485 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
486 nla_len(upcall_info->userdata),
487 nla_data(upcall_info->userdata));
489 /* Only reserve room for attribute header, packet data is added
490 * in skb_zerocopy() */
491 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
495 nla->nla_len = nla_attr_size(skb->len);
497 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
501 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
502 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
503 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
506 memset(skb_put(user_skb, plen), 0, plen);
509 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
511 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
519 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
521 struct ovs_header *ovs_header = info->userhdr;
522 struct nlattr **a = info->attrs;
523 struct sw_flow_actions *acts;
524 struct sk_buff *packet;
525 struct sw_flow *flow;
528 struct vport *input_vport;
533 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
534 !a[OVS_PACKET_ATTR_ACTIONS])
537 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
538 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
542 skb_reserve(packet, NET_IP_ALIGN);
544 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
546 skb_reset_mac_header(packet);
547 eth = eth_hdr(packet);
549 /* Normally, setting the skb 'protocol' field would be handled by a
550 * call to eth_type_trans(), but it assumes there's a sending
551 * device, which we may not have. */
552 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
553 packet->protocol = eth->h_proto;
555 packet->protocol = htons(ETH_P_802_2);
557 /* Build an sw_flow for sending this packet. */
558 flow = ovs_flow_alloc();
563 err = ovs_flow_extract(packet, -1, &flow->key);
567 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
570 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
575 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
576 &flow->key, 0, &acts);
577 rcu_assign_pointer(flow->sf_acts, acts);
581 OVS_CB(packet)->flow = flow;
582 OVS_CB(packet)->pkt_key = &flow->key;
583 packet->priority = flow->key.phy.priority;
584 packet->mark = flow->key.phy.skb_mark;
587 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
592 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
594 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
599 OVS_CB(packet)->input_vport = input_vport;
602 err = ovs_execute_actions(dp, packet);
606 ovs_flow_free(flow, false);
612 ovs_flow_free(flow, false);
619 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
620 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
621 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
622 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
625 static struct genl_ops dp_packet_genl_ops[] = {
626 { .cmd = OVS_PACKET_CMD_EXECUTE,
627 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
628 .policy = packet_policy,
629 .doit = ovs_packet_cmd_execute
633 static struct genl_family dp_packet_genl_family = {
634 .id = GENL_ID_GENERATE,
635 .hdrsize = sizeof(struct ovs_header),
636 .name = OVS_PACKET_FAMILY,
637 .version = OVS_PACKET_VERSION,
638 .maxattr = OVS_PACKET_ATTR_MAX,
640 .parallel_ops = true,
641 .ops = dp_packet_genl_ops,
642 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
645 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
646 struct ovs_dp_megaflow_stats *mega_stats)
650 memset(mega_stats, 0, sizeof(*mega_stats));
652 stats->n_flows = ovs_flow_tbl_count(&dp->table);
653 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
655 stats->n_hit = stats->n_missed = stats->n_lost = 0;
657 for_each_possible_cpu(i) {
658 const struct dp_stats_percpu *percpu_stats;
659 struct dp_stats_percpu local_stats;
662 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
665 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
666 local_stats = *percpu_stats;
667 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
669 stats->n_hit += local_stats.n_hit;
670 stats->n_missed += local_stats.n_missed;
671 stats->n_lost += local_stats.n_lost;
672 mega_stats->n_mask_hit += local_stats.n_mask_hit;
676 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
678 return NLMSG_ALIGN(sizeof(struct ovs_header))
679 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
680 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
681 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
682 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
683 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
684 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
687 /* Called with ovs_mutex or RCU read lock. */
688 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
689 struct sk_buff *skb, u32 portid,
690 u32 seq, u32 flags, u8 cmd)
692 const int skb_orig_len = skb->len;
693 struct nlattr *start;
694 struct ovs_flow_stats stats;
697 struct ovs_header *ovs_header;
701 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
705 ovs_header->dp_ifindex = dp_ifindex;
708 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
710 goto nla_put_failure;
712 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
715 nla_nest_end(skb, nla);
717 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
719 goto nla_put_failure;
721 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
725 nla_nest_end(skb, nla);
727 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
730 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
731 goto nla_put_failure;
733 if (stats.n_packets &&
734 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
735 goto nla_put_failure;
737 if ((u8)ntohs(tcp_flags) &&
738 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
739 goto nla_put_failure;
741 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
742 * this is the first flow to be dumped into 'skb'. This is unusual for
743 * Netlink but individual action lists can be longer than
744 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
745 * The userspace caller can always fetch the actions separately if it
746 * really wants them. (Most userspace callers in fact don't care.)
748 * This can only fail for dump operations because the skb is always
749 * properly sized for single flows.
751 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
753 const struct sw_flow_actions *sf_acts;
755 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
756 err = ovs_nla_put_actions(sf_acts->actions,
757 sf_acts->actions_len, skb);
760 nla_nest_end(skb, start);
765 nla_nest_cancel(skb, start);
767 } else if (skb_orig_len)
768 goto nla_put_failure;
770 return genlmsg_end(skb, ovs_header);
775 genlmsg_cancel(skb, ovs_header);
779 /* May not be called with RCU read lock. */
780 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
781 struct genl_info *info,
786 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
789 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
792 return ERR_PTR(-ENOMEM);
797 /* Called with ovs_mutex. */
798 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
800 struct genl_info *info, u8 cmd,
806 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
808 if (!skb || IS_ERR(skb))
811 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
812 info->snd_portid, info->snd_seq, 0,
818 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
820 struct nlattr **a = info->attrs;
821 struct ovs_header *ovs_header = info->userhdr;
822 struct sw_flow *flow, *new_flow;
823 struct sw_flow_mask mask;
824 struct sk_buff *reply;
826 struct sw_flow_actions *acts;
827 struct sw_flow_match match;
830 /* Must have key and actions. */
832 if (!a[OVS_FLOW_ATTR_KEY])
834 if (!a[OVS_FLOW_ATTR_ACTIONS])
837 /* Most of the time we need to allocate a new flow, do it before
839 new_flow = ovs_flow_alloc();
840 if (IS_ERR(new_flow)) {
841 error = PTR_ERR(new_flow);
846 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
847 error = ovs_nla_get_match(&match,
848 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
852 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
854 /* Validate actions. */
855 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
856 error = PTR_ERR(acts);
860 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
863 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
867 reply = ovs_flow_cmd_alloc_info(acts, info, false);
869 error = PTR_ERR(reply);
874 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
879 /* Check if this is a duplicate flow */
880 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
882 rcu_assign_pointer(new_flow->sf_acts, acts);
884 /* Put flow in bucket. */
885 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
886 if (unlikely(error)) {
891 if (unlikely(reply)) {
892 error = ovs_flow_cmd_fill_info(new_flow,
893 ovs_header->dp_ifindex,
894 reply, info->snd_portid,
901 struct sw_flow_actions *old_acts;
903 /* Bail out if we're not allowed to modify an existing flow.
904 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
905 * because Generic Netlink treats the latter as a dump
906 * request. We also accept NLM_F_EXCL in case that bug ever
909 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
914 /* The unmasked key has to be the same for flow updates. */
915 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
919 /* Update actions. */
920 old_acts = ovsl_dereference(flow->sf_acts);
921 rcu_assign_pointer(flow->sf_acts, acts);
923 if (unlikely(reply)) {
924 error = ovs_flow_cmd_fill_info(flow,
925 ovs_header->dp_ifindex,
926 reply, info->snd_portid,
933 ovs_nla_free_flow_actions(old_acts);
934 ovs_flow_free(new_flow, false);
938 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
947 ovs_flow_free(new_flow, false);
952 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
954 struct nlattr **a = info->attrs;
955 struct ovs_header *ovs_header = info->userhdr;
956 struct sw_flow_key key, masked_key;
957 struct sw_flow *flow;
958 struct sw_flow_mask mask;
959 struct sk_buff *reply = NULL;
961 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
962 struct sw_flow_match match;
967 if (!a[OVS_FLOW_ATTR_KEY])
970 ovs_match_init(&match, &key, &mask);
971 error = ovs_nla_get_match(&match,
972 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
976 /* Validate actions. */
977 if (a[OVS_FLOW_ATTR_ACTIONS]) {
978 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
979 error = PTR_ERR(acts);
983 ovs_flow_mask_key(&masked_key, &key, &mask);
984 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
985 &masked_key, 0, &acts);
987 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
992 /* Can allocate before locking if have acts. */
994 reply = ovs_flow_cmd_alloc_info(acts, info, false);
996 error = PTR_ERR(reply);
1002 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1003 if (unlikely(!dp)) {
1005 goto err_unlock_ovs;
1007 /* Check that the flow exists. */
1008 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1009 if (unlikely(!flow)) {
1011 goto err_unlock_ovs;
1013 /* The unmasked key has to be the same for flow updates. */
1014 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
1016 goto err_unlock_ovs;
1018 /* Update actions, if present. */
1020 old_acts = ovsl_dereference(flow->sf_acts);
1021 rcu_assign_pointer(flow->sf_acts, acts);
1023 if (unlikely(reply)) {
1024 error = ovs_flow_cmd_fill_info(flow,
1025 ovs_header->dp_ifindex,
1026 reply, info->snd_portid,
1032 /* Could not alloc without acts before locking. */
1033 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1034 info, OVS_FLOW_CMD_NEW, false);
1035 if (unlikely(IS_ERR(reply))) {
1036 error = PTR_ERR(reply);
1037 goto err_unlock_ovs;
1042 if (a[OVS_FLOW_ATTR_CLEAR])
1043 ovs_flow_stats_clear(flow);
1047 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1049 ovs_nla_free_flow_actions(old_acts);
1061 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1063 struct nlattr **a = info->attrs;
1064 struct ovs_header *ovs_header = info->userhdr;
1065 struct sw_flow_key key;
1066 struct sk_buff *reply;
1067 struct sw_flow *flow;
1068 struct datapath *dp;
1069 struct sw_flow_match match;
1072 if (!a[OVS_FLOW_ATTR_KEY]) {
1073 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1077 ovs_match_init(&match, &key, NULL);
1078 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1083 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1089 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1090 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
1095 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1096 OVS_FLOW_CMD_NEW, true);
1097 if (IS_ERR(reply)) {
1098 err = PTR_ERR(reply);
1103 return genlmsg_reply(reply, info);
1109 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1111 struct nlattr **a = info->attrs;
1112 struct ovs_header *ovs_header = info->userhdr;
1113 struct sw_flow_key key;
1114 struct sk_buff *reply;
1115 struct sw_flow *flow;
1116 struct datapath *dp;
1117 struct sw_flow_match match;
1120 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1121 ovs_match_init(&match, &key, NULL);
1122 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1128 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1129 if (unlikely(!dp)) {
1133 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1134 err = ovs_flow_tbl_flush(&dp->table);
1137 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1138 if (unlikely(!flow || !ovs_flow_cmp_unmasked_key(flow, &match))) {
1143 ovs_flow_tbl_remove(&dp->table, flow);
1146 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1149 if (likely(reply)) {
1150 if (likely(!IS_ERR(reply))) {
1151 rcu_read_lock(); /* Keep RCU checker happy. */
1152 err = ovs_flow_cmd_fill_info(flow,
1153 ovs_header->dp_ifindex,
1154 reply, info->snd_portid,
1159 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1161 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1162 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1167 ovs_flow_free(flow, true);
1174 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1176 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1177 struct table_instance *ti;
1178 struct datapath *dp;
1181 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1187 ti = rcu_dereference(dp->table.ti);
1189 struct sw_flow *flow;
1192 bucket = cb->args[0];
1194 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1198 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1199 NETLINK_CB(cb->skb).portid,
1200 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1201 OVS_FLOW_CMD_NEW) < 0)
1204 cb->args[0] = bucket;
1211 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1212 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1213 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1214 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1217 static struct genl_ops dp_flow_genl_ops[] = {
1218 { .cmd = OVS_FLOW_CMD_NEW,
1219 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1220 .policy = flow_policy,
1221 .doit = ovs_flow_cmd_new
1223 { .cmd = OVS_FLOW_CMD_DEL,
1224 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1225 .policy = flow_policy,
1226 .doit = ovs_flow_cmd_del
1228 { .cmd = OVS_FLOW_CMD_GET,
1229 .flags = 0, /* OK for unprivileged users. */
1230 .policy = flow_policy,
1231 .doit = ovs_flow_cmd_get,
1232 .dumpit = ovs_flow_cmd_dump
1234 { .cmd = OVS_FLOW_CMD_SET,
1235 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1236 .policy = flow_policy,
1237 .doit = ovs_flow_cmd_set,
1241 static struct genl_family dp_flow_genl_family = {
1242 .id = GENL_ID_GENERATE,
1243 .hdrsize = sizeof(struct ovs_header),
1244 .name = OVS_FLOW_FAMILY,
1245 .version = OVS_FLOW_VERSION,
1246 .maxattr = OVS_FLOW_ATTR_MAX,
1248 .parallel_ops = true,
1249 .ops = dp_flow_genl_ops,
1250 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1251 .mcgrps = &ovs_dp_flow_multicast_group,
1255 static size_t ovs_dp_cmd_msg_size(void)
1257 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1259 msgsize += nla_total_size(IFNAMSIZ);
1260 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1261 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1262 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1267 /* Called with ovs_mutex or RCU read lock. */
1268 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1269 u32 portid, u32 seq, u32 flags, u8 cmd)
1271 struct ovs_header *ovs_header;
1272 struct ovs_dp_stats dp_stats;
1273 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1276 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1281 ovs_header->dp_ifindex = get_dpifindex(dp);
1283 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1285 goto nla_put_failure;
1287 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1288 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1290 goto nla_put_failure;
1292 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1293 sizeof(struct ovs_dp_megaflow_stats),
1294 &dp_megaflow_stats))
1295 goto nla_put_failure;
1297 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1298 goto nla_put_failure;
1300 return genlmsg_end(skb, ovs_header);
1303 genlmsg_cancel(skb, ovs_header);
1308 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1310 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1313 /* Called with rcu_read_lock or ovs_mutex. */
1314 static struct datapath *lookup_datapath(struct net *net,
1315 struct ovs_header *ovs_header,
1316 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1318 struct datapath *dp;
1320 if (!a[OVS_DP_ATTR_NAME])
1321 dp = get_dp(net, ovs_header->dp_ifindex);
1323 struct vport *vport;
1325 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1326 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1328 return dp ? dp : ERR_PTR(-ENODEV);
1331 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1333 struct datapath *dp;
1335 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1339 WARN(dp->user_features, "Dropping previously announced user features\n");
1340 dp->user_features = 0;
1343 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1345 if (a[OVS_DP_ATTR_USER_FEATURES])
1346 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1349 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1351 struct nlattr **a = info->attrs;
1352 struct vport_parms parms;
1353 struct sk_buff *reply;
1354 struct datapath *dp;
1355 struct vport *vport;
1356 struct ovs_net *ovs_net;
1360 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1363 reply = ovs_dp_cmd_alloc_info(info);
1368 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1370 goto err_free_reply;
1372 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1374 /* Allocate table. */
1375 err = ovs_flow_tbl_init(&dp->table);
1379 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1380 if (!dp->stats_percpu) {
1382 goto err_destroy_table;
1385 for_each_possible_cpu(i) {
1386 struct dp_stats_percpu *dpath_stats;
1387 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1388 u64_stats_init(&dpath_stats->sync);
1391 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1395 goto err_destroy_percpu;
1398 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1399 INIT_HLIST_HEAD(&dp->ports[i]);
1401 /* Set up our datapath device. */
1402 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1403 parms.type = OVS_VPORT_TYPE_INTERNAL;
1404 parms.options = NULL;
1406 parms.port_no = OVSP_LOCAL;
1407 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1409 ovs_dp_change(dp, a);
1411 /* So far only local changes have been made, now need the lock. */
1414 vport = new_vport(&parms);
1415 if (IS_ERR(vport)) {
1416 err = PTR_ERR(vport);
1420 if (err == -EEXIST) {
1421 /* An outdated user space instance that does not understand
1422 * the concept of user_features has attempted to create a new
1423 * datapath and is likely to reuse it. Drop all user features.
1425 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1426 ovs_dp_reset_user_features(skb, info);
1429 goto err_destroy_ports_array;
1432 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1433 info->snd_seq, 0, OVS_DP_CMD_NEW);
1436 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1437 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1441 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1444 err_destroy_ports_array:
1448 free_percpu(dp->stats_percpu);
1450 ovs_flow_tbl_destroy(&dp->table, false);
1452 release_net(ovs_dp_get_net(dp));
1460 /* Called with ovs_mutex. */
1461 static void __dp_destroy(struct datapath *dp)
1465 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1466 struct vport *vport;
1467 struct hlist_node *n;
1469 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1470 if (vport->port_no != OVSP_LOCAL)
1471 ovs_dp_detach_port(vport);
1474 list_del_rcu(&dp->list_node);
1476 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1477 * all ports in datapath are destroyed first before freeing datapath.
1479 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1481 /* RCU destroy the flow table */
1482 ovs_flow_tbl_destroy(&dp->table, true);
1484 call_rcu(&dp->rcu, destroy_dp_rcu);
1487 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1489 struct sk_buff *reply;
1490 struct datapath *dp;
1493 reply = ovs_dp_cmd_alloc_info(info);
1498 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1501 goto err_unlock_free;
1503 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1504 info->snd_seq, 0, OVS_DP_CMD_DEL);
1510 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1519 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1521 struct sk_buff *reply;
1522 struct datapath *dp;
1525 reply = ovs_dp_cmd_alloc_info(info);
1530 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1533 goto err_unlock_free;
1535 ovs_dp_change(dp, info->attrs);
1537 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1538 info->snd_seq, 0, OVS_DP_CMD_NEW);
1542 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1551 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1553 struct sk_buff *reply;
1554 struct datapath *dp;
1557 reply = ovs_dp_cmd_alloc_info(info);
1562 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1565 goto err_unlock_free;
1567 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1568 info->snd_seq, 0, OVS_DP_CMD_NEW);
1572 return genlmsg_reply(reply, info);
1580 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1582 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1583 struct datapath *dp;
1584 int skip = cb->args[0];
1588 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1590 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1591 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1592 OVS_DP_CMD_NEW) < 0)
1603 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1604 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1605 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1606 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1609 static struct genl_ops dp_datapath_genl_ops[] = {
1610 { .cmd = OVS_DP_CMD_NEW,
1611 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1612 .policy = datapath_policy,
1613 .doit = ovs_dp_cmd_new
1615 { .cmd = OVS_DP_CMD_DEL,
1616 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1617 .policy = datapath_policy,
1618 .doit = ovs_dp_cmd_del
1620 { .cmd = OVS_DP_CMD_GET,
1621 .flags = 0, /* OK for unprivileged users. */
1622 .policy = datapath_policy,
1623 .doit = ovs_dp_cmd_get,
1624 .dumpit = ovs_dp_cmd_dump
1626 { .cmd = OVS_DP_CMD_SET,
1627 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1628 .policy = datapath_policy,
1629 .doit = ovs_dp_cmd_set,
1633 static struct genl_family dp_datapath_genl_family = {
1634 .id = GENL_ID_GENERATE,
1635 .hdrsize = sizeof(struct ovs_header),
1636 .name = OVS_DATAPATH_FAMILY,
1637 .version = OVS_DATAPATH_VERSION,
1638 .maxattr = OVS_DP_ATTR_MAX,
1640 .parallel_ops = true,
1641 .ops = dp_datapath_genl_ops,
1642 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1643 .mcgrps = &ovs_dp_datapath_multicast_group,
1647 /* Called with ovs_mutex or RCU read lock. */
1648 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1649 u32 portid, u32 seq, u32 flags, u8 cmd)
1651 struct ovs_header *ovs_header;
1652 struct ovs_vport_stats vport_stats;
1655 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1660 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1662 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1663 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1664 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1665 goto nla_put_failure;
1667 ovs_vport_get_stats(vport, &vport_stats);
1668 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1670 goto nla_put_failure;
1672 if (ovs_vport_get_upcall_portids(vport, skb))
1673 goto nla_put_failure;
1675 err = ovs_vport_get_options(vport, skb);
1676 if (err == -EMSGSIZE)
1679 return genlmsg_end(skb, ovs_header);
1684 genlmsg_cancel(skb, ovs_header);
1688 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1690 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1693 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1694 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1697 struct sk_buff *skb;
1700 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1702 return ERR_PTR(-ENOMEM);
1704 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1710 /* Called with ovs_mutex or RCU read lock. */
1711 static struct vport *lookup_vport(struct net *net,
1712 struct ovs_header *ovs_header,
1713 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1715 struct datapath *dp;
1716 struct vport *vport;
1718 if (a[OVS_VPORT_ATTR_NAME]) {
1719 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1721 return ERR_PTR(-ENODEV);
1722 if (ovs_header->dp_ifindex &&
1723 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1724 return ERR_PTR(-ENODEV);
1726 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1727 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1729 if (port_no >= DP_MAX_PORTS)
1730 return ERR_PTR(-EFBIG);
1732 dp = get_dp(net, ovs_header->dp_ifindex);
1734 return ERR_PTR(-ENODEV);
1736 vport = ovs_vport_ovsl_rcu(dp, port_no);
1738 return ERR_PTR(-ENODEV);
1741 return ERR_PTR(-EINVAL);
1744 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1746 struct nlattr **a = info->attrs;
1747 struct ovs_header *ovs_header = info->userhdr;
1748 struct vport_parms parms;
1749 struct sk_buff *reply;
1750 struct vport *vport;
1751 struct datapath *dp;
1755 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1756 !a[OVS_VPORT_ATTR_UPCALL_PID])
1759 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1760 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1761 if (port_no >= DP_MAX_PORTS)
1764 reply = ovs_vport_cmd_alloc_info();
1769 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1772 goto exit_unlock_free;
1775 vport = ovs_vport_ovsl(dp, port_no);
1778 goto exit_unlock_free;
1780 for (port_no = 1; ; port_no++) {
1781 if (port_no >= DP_MAX_PORTS) {
1783 goto exit_unlock_free;
1785 vport = ovs_vport_ovsl(dp, port_no);
1791 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1792 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1793 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1795 parms.port_no = port_no;
1796 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1798 vport = new_vport(&parms);
1799 err = PTR_ERR(vport);
1801 goto exit_unlock_free;
1804 if (a[OVS_VPORT_ATTR_STATS])
1805 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1807 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1808 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1812 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1821 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1823 struct nlattr **a = info->attrs;
1824 struct sk_buff *reply;
1825 struct vport *vport;
1828 reply = ovs_vport_cmd_alloc_info();
1833 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1834 err = PTR_ERR(vport);
1836 goto exit_unlock_free;
1838 if (a[OVS_VPORT_ATTR_TYPE] &&
1839 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1841 goto exit_unlock_free;
1844 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1845 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1847 goto exit_unlock_free;
1850 if (a[OVS_VPORT_ATTR_STATS])
1851 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1854 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1855 err = ovs_vport_set_upcall_portids(vport,
1856 a[OVS_VPORT_ATTR_UPCALL_PID]);
1858 goto exit_unlock_free;
1861 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1862 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1866 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1875 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1877 struct nlattr **a = info->attrs;
1878 struct sk_buff *reply;
1879 struct vport *vport;
1882 reply = ovs_vport_cmd_alloc_info();
1887 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1888 err = PTR_ERR(vport);
1890 goto exit_unlock_free;
1892 if (vport->port_no == OVSP_LOCAL) {
1894 goto exit_unlock_free;
1897 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1898 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1900 ovs_dp_detach_port(vport);
1903 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1912 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1914 struct nlattr **a = info->attrs;
1915 struct ovs_header *ovs_header = info->userhdr;
1916 struct sk_buff *reply;
1917 struct vport *vport;
1920 reply = ovs_vport_cmd_alloc_info();
1925 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1926 err = PTR_ERR(vport);
1928 goto exit_unlock_free;
1929 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1930 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1934 return genlmsg_reply(reply, info);
1942 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1944 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1945 struct datapath *dp;
1946 int bucket = cb->args[0], skip = cb->args[1];
1950 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1955 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1956 struct vport *vport;
1959 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1961 ovs_vport_cmd_fill_info(vport, skb,
1962 NETLINK_CB(cb->skb).portid,
1965 OVS_VPORT_CMD_NEW) < 0)
1981 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1982 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1983 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1984 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1985 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1986 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1987 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1990 static struct genl_ops dp_vport_genl_ops[] = {
1991 { .cmd = OVS_VPORT_CMD_NEW,
1992 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1993 .policy = vport_policy,
1994 .doit = ovs_vport_cmd_new
1996 { .cmd = OVS_VPORT_CMD_DEL,
1997 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1998 .policy = vport_policy,
1999 .doit = ovs_vport_cmd_del
2001 { .cmd = OVS_VPORT_CMD_GET,
2002 .flags = 0, /* OK for unprivileged users. */
2003 .policy = vport_policy,
2004 .doit = ovs_vport_cmd_get,
2005 .dumpit = ovs_vport_cmd_dump
2007 { .cmd = OVS_VPORT_CMD_SET,
2008 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2009 .policy = vport_policy,
2010 .doit = ovs_vport_cmd_set,
2014 struct genl_family dp_vport_genl_family = {
2015 .id = GENL_ID_GENERATE,
2016 .hdrsize = sizeof(struct ovs_header),
2017 .name = OVS_VPORT_FAMILY,
2018 .version = OVS_VPORT_VERSION,
2019 .maxattr = OVS_VPORT_ATTR_MAX,
2021 .parallel_ops = true,
2022 .ops = dp_vport_genl_ops,
2023 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2024 .mcgrps = &ovs_dp_vport_multicast_group,
2028 static struct genl_family *dp_genl_families[] = {
2029 &dp_datapath_genl_family,
2030 &dp_vport_genl_family,
2031 &dp_flow_genl_family,
2032 &dp_packet_genl_family,
2035 static void dp_unregister_genl(int n_families)
2039 for (i = 0; i < n_families; i++)
2040 genl_unregister_family(dp_genl_families[i]);
2043 static int dp_register_genl(void)
2048 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2050 err = genl_register_family(dp_genl_families[i]);
2058 dp_unregister_genl(i);
2062 static int __net_init ovs_init_net(struct net *net)
2064 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2066 INIT_LIST_HEAD(&ovs_net->dps);
2067 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2071 static void __net_exit ovs_exit_net(struct net *net)
2073 struct datapath *dp, *dp_next;
2074 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2077 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2081 cancel_work_sync(&ovs_net->dp_notify_work);
2084 static struct pernet_operations ovs_net_ops = {
2085 .init = ovs_init_net,
2086 .exit = ovs_exit_net,
2088 .size = sizeof(struct ovs_net),
2091 DEFINE_COMPAT_PNET_REG_FUNC(device);
2093 static int __init dp_init(void)
2097 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2099 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2102 err = ovs_flow_init();
2106 err = ovs_vport_init();
2108 goto error_flow_exit;
2110 err = register_pernet_device(&ovs_net_ops);
2112 goto error_vport_exit;
2114 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2116 goto error_netns_exit;
2118 err = dp_register_genl();
2120 goto error_unreg_notifier;
2124 error_unreg_notifier:
2125 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2127 unregister_pernet_device(&ovs_net_ops);
2136 static void dp_cleanup(void)
2138 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2139 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2140 unregister_pernet_device(&ovs_net_ops);
2146 module_init(dp_init);
2147 module_exit(dp_cleanup);
2149 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2150 MODULE_LICENSE("GPL");
2151 MODULE_VERSION(VERSION);