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/ethtool.h>
40 #include <linux/wait.h>
41 #include <asm/div64.h>
42 #include <linux/highmem.h>
43 #include <linux/netfilter_bridge.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/inetdevice.h>
46 #include <linux/list.h>
47 #include <linux/openvswitch.h>
48 #include <linux/rculist.h>
49 #include <linux/dmi.h>
50 #include <linux/genetlink.h>
51 #include <net/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
58 #include "flow_table.h"
59 #include "flow_netlink.h"
60 #include "vport-internal_dev.h"
61 #include "vport-netdev.h"
63 int ovs_net_id __read_mostly;
65 static struct genl_family dp_packet_genl_family;
66 static struct genl_family dp_flow_genl_family;
67 static struct genl_family dp_datapath_genl_family;
69 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
70 .name = OVS_FLOW_MCGROUP
73 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
74 .name = OVS_DATAPATH_MCGROUP
77 struct genl_multicast_group ovs_dp_vport_multicast_group = {
78 .name = OVS_VPORT_MCGROUP
81 /* Check if need to build a reply message.
82 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
83 static bool ovs_must_notify(struct genl_info *info,
84 const struct genl_multicast_group *grp)
86 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
87 netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
90 static void ovs_notify(struct genl_family *family,
91 struct sk_buff *skb, struct genl_info *info)
93 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
94 0, info->nlhdr, GFP_KERNEL);
100 * All writes e.g. Writes to device state (add/remove datapath, port, set
101 * operations on vports, etc.), Writes to other state (flow table
102 * modifications, set miscellaneous datapath parameters, etc.) are protected
105 * Reads are protected by RCU.
107 * There are a few special cases (mostly stats) that have their own
108 * synchronization but they nest under all of above and don't interact with
111 * The RTNL lock nests inside ovs_mutex.
114 static DEFINE_MUTEX(ovs_mutex);
118 mutex_lock(&ovs_mutex);
121 void ovs_unlock(void)
123 mutex_unlock(&ovs_mutex);
126 #ifdef CONFIG_LOCKDEP
127 int lockdep_ovsl_is_held(void)
130 return lockdep_is_held(&ovs_mutex);
136 static struct vport *new_vport(const struct vport_parms *);
137 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
138 const struct dp_upcall_info *);
139 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
140 const struct dp_upcall_info *);
142 /* Must be called with rcu_read_lock or ovs_mutex. */
143 static struct datapath *get_dp(struct net *net, int dp_ifindex)
145 struct datapath *dp = NULL;
146 struct net_device *dev;
149 dev = dev_get_by_index_rcu(net, dp_ifindex);
151 struct vport *vport = ovs_internal_dev_get_vport(dev);
160 /* Must be called with rcu_read_lock or ovs_mutex. */
161 static const char *ovs_dp_name(const struct datapath *dp)
163 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
164 return vport->ops->get_name(vport);
167 static int get_dpifindex(struct datapath *dp)
174 local = ovs_vport_rcu(dp, OVSP_LOCAL);
176 ifindex = netdev_vport_priv(local)->dev->ifindex;
185 static void destroy_dp_rcu(struct rcu_head *rcu)
187 struct datapath *dp = container_of(rcu, struct datapath, rcu);
189 free_percpu(dp->stats_percpu);
190 release_net(ovs_dp_get_net(dp));
195 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
198 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
201 /* Called with ovs_mutex or RCU read lock. */
202 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
205 struct hlist_head *head;
207 head = vport_hash_bucket(dp, port_no);
208 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
209 if (vport->port_no == port_no)
215 /* Called with ovs_mutex. */
216 static struct vport *new_vport(const struct vport_parms *parms)
220 vport = ovs_vport_add(parms);
221 if (!IS_ERR(vport)) {
222 struct datapath *dp = parms->dp;
223 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
225 hlist_add_head_rcu(&vport->dp_hash_node, head);
230 void ovs_dp_detach_port(struct vport *p)
234 /* First drop references to device. */
235 hlist_del_rcu(&p->dp_hash_node);
237 /* Then destroy it. */
241 /* Must be called with rcu_read_lock. */
242 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
244 struct datapath *dp = p->dp;
245 struct sw_flow *flow;
246 struct dp_stats_percpu *stats;
247 struct sw_flow_key key;
252 stats = this_cpu_ptr(dp->stats_percpu);
254 /* Extract flow from 'skb' into 'key'. */
255 error = ovs_flow_extract(skb, p->port_no, &key);
256 if (unlikely(error)) {
262 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
263 if (unlikely(!flow)) {
264 struct dp_upcall_info upcall;
266 upcall.cmd = OVS_PACKET_CMD_MISS;
268 upcall.userdata = NULL;
269 upcall.portid = p->upcall_portid;
270 ovs_dp_upcall(dp, skb, &upcall);
272 stats_counter = &stats->n_missed;
276 OVS_CB(skb)->flow = flow;
277 OVS_CB(skb)->pkt_key = &key;
279 ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
280 ovs_execute_actions(dp, skb);
281 stats_counter = &stats->n_hit;
284 /* Update datapath statistics. */
285 u64_stats_update_begin(&stats->syncp);
287 stats->n_mask_hit += n_mask_hit;
288 u64_stats_update_end(&stats->syncp);
291 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
292 const struct dp_upcall_info *upcall_info)
294 struct dp_stats_percpu *stats;
297 if (upcall_info->portid == 0) {
302 if (!skb_is_gso(skb))
303 err = queue_userspace_packet(dp, skb, upcall_info);
305 err = queue_gso_packets(dp, skb, upcall_info);
312 stats = this_cpu_ptr(dp->stats_percpu);
314 u64_stats_update_begin(&stats->syncp);
316 u64_stats_update_end(&stats->syncp);
321 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
322 const struct dp_upcall_info *upcall_info)
324 unsigned short gso_type = skb_shinfo(skb)->gso_type;
325 struct dp_upcall_info later_info;
326 struct sw_flow_key later_key;
327 struct sk_buff *segs, *nskb;
330 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
332 return PTR_ERR(segs);
334 /* Queue all of the segments. */
337 err = queue_userspace_packet(dp, skb, upcall_info);
341 if (skb == segs && gso_type & SKB_GSO_UDP) {
342 /* The initial flow key extracted by ovs_flow_extract()
343 * in this case is for a first fragment, so we need to
344 * properly mark later fragments.
346 later_key = *upcall_info->key;
347 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
349 later_info = *upcall_info;
350 later_info.key = &later_key;
351 upcall_info = &later_info;
353 } while ((skb = skb->next));
355 /* Free all of the segments. */
363 } while ((skb = nskb));
367 static size_t key_attr_size(void)
369 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
370 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
371 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
372 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
373 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
374 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
375 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
376 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
377 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
378 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
379 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
380 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
382 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
383 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
385 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
386 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
387 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
390 static size_t upcall_msg_size(const struct nlattr *userdata,
393 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
394 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
395 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
397 /* OVS_PACKET_ATTR_USERDATA */
399 size += NLA_ALIGN(userdata->nla_len);
404 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
405 const struct dp_upcall_info *upcall_info)
407 struct ovs_header *upcall;
408 struct sk_buff *nskb = NULL;
409 struct sk_buff *user_skb; /* to be queued to userspace */
411 struct genl_info info = {
412 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
413 .snd_portid = upcall_info->portid,
419 dp_ifindex = get_dpifindex(dp);
423 if (vlan_tx_tag_present(skb)) {
424 nskb = skb_clone(skb, GFP_ATOMIC);
428 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
436 if (nla_attr_size(skb->len) > USHRT_MAX) {
441 /* Complete checksum if needed */
442 if (skb->ip_summed == CHECKSUM_PARTIAL &&
443 (err = skb_checksum_help(skb)))
446 /* Older versions of OVS user space enforce alignment of the last
447 * Netlink attribute to NLA_ALIGNTO which would require extensive
448 * padding logic. Only perform zerocopy if padding is not required.
450 if (dp->user_features & OVS_DP_F_UNALIGNED)
451 hlen = skb_zerocopy_headlen(skb);
455 len = upcall_msg_size(upcall_info->userdata, hlen);
456 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
462 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
463 0, upcall_info->cmd);
464 upcall->dp_ifindex = dp_ifindex;
466 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
467 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
468 nla_nest_end(user_skb, nla);
470 if (upcall_info->userdata)
471 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
472 nla_len(upcall_info->userdata),
473 nla_data(upcall_info->userdata));
475 /* Only reserve room for attribute header, packet data is added
476 * in skb_zerocopy() */
477 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
481 nla->nla_len = nla_attr_size(skb->len);
483 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
487 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
488 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
489 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
492 memset(skb_put(user_skb, plen), 0, plen);
495 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
497 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
505 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
507 struct ovs_header *ovs_header = info->userhdr;
508 struct nlattr **a = info->attrs;
509 struct sw_flow_actions *acts;
510 struct sk_buff *packet;
511 struct sw_flow *flow;
518 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
519 !a[OVS_PACKET_ATTR_ACTIONS])
522 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
523 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
527 skb_reserve(packet, NET_IP_ALIGN);
529 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
531 skb_reset_mac_header(packet);
532 eth = eth_hdr(packet);
534 /* Normally, setting the skb 'protocol' field would be handled by a
535 * call to eth_type_trans(), but it assumes there's a sending
536 * device, which we may not have. */
537 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
538 packet->protocol = eth->h_proto;
540 packet->protocol = htons(ETH_P_802_2);
542 /* Build an sw_flow for sending this packet. */
543 flow = ovs_flow_alloc();
548 err = ovs_flow_extract(packet, -1, &flow->key);
552 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
555 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
560 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
561 &flow->key, 0, &acts);
562 rcu_assign_pointer(flow->sf_acts, acts);
566 OVS_CB(packet)->flow = flow;
567 OVS_CB(packet)->pkt_key = &flow->key;
568 packet->priority = flow->key.phy.priority;
569 packet->mark = flow->key.phy.skb_mark;
572 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
578 err = ovs_execute_actions(dp, packet);
582 ovs_flow_free(flow, false);
588 ovs_flow_free(flow, false);
595 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
596 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
597 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
598 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
601 static const struct genl_ops dp_packet_genl_ops[] = {
602 { .cmd = OVS_PACKET_CMD_EXECUTE,
603 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
604 .policy = packet_policy,
605 .doit = ovs_packet_cmd_execute
609 static struct genl_family dp_packet_genl_family = {
610 .id = GENL_ID_GENERATE,
611 .hdrsize = sizeof(struct ovs_header),
612 .name = OVS_PACKET_FAMILY,
613 .version = OVS_PACKET_VERSION,
614 .maxattr = OVS_PACKET_ATTR_MAX,
616 .parallel_ops = true,
617 .ops = dp_packet_genl_ops,
618 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
621 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
622 struct ovs_dp_megaflow_stats *mega_stats)
626 memset(mega_stats, 0, sizeof(*mega_stats));
628 stats->n_flows = ovs_flow_tbl_count(&dp->table);
629 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
631 stats->n_hit = stats->n_missed = stats->n_lost = 0;
633 for_each_possible_cpu(i) {
634 const struct dp_stats_percpu *percpu_stats;
635 struct dp_stats_percpu local_stats;
638 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
641 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
642 local_stats = *percpu_stats;
643 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
645 stats->n_hit += local_stats.n_hit;
646 stats->n_missed += local_stats.n_missed;
647 stats->n_lost += local_stats.n_lost;
648 mega_stats->n_mask_hit += local_stats.n_mask_hit;
652 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
654 return NLMSG_ALIGN(sizeof(struct ovs_header))
655 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
656 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
657 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
658 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
659 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
660 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
663 /* Called with ovs_mutex or RCU read lock. */
664 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
665 struct sk_buff *skb, u32 portid,
666 u32 seq, u32 flags, u8 cmd)
668 const int skb_orig_len = skb->len;
669 struct nlattr *start;
670 struct ovs_flow_stats stats;
673 struct ovs_header *ovs_header;
677 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
681 ovs_header->dp_ifindex = dp_ifindex;
684 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
686 goto nla_put_failure;
688 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
691 nla_nest_end(skb, nla);
693 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
695 goto nla_put_failure;
697 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
701 nla_nest_end(skb, nla);
703 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
706 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
707 goto nla_put_failure;
709 if (stats.n_packets &&
710 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
711 goto nla_put_failure;
713 if ((u8)ntohs(tcp_flags) &&
714 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
715 goto nla_put_failure;
717 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
718 * this is the first flow to be dumped into 'skb'. This is unusual for
719 * Netlink but individual action lists can be longer than
720 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
721 * The userspace caller can always fetch the actions separately if it
722 * really wants them. (Most userspace callers in fact don't care.)
724 * This can only fail for dump operations because the skb is always
725 * properly sized for single flows.
727 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
729 const struct sw_flow_actions *sf_acts;
731 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
732 err = ovs_nla_put_actions(sf_acts->actions,
733 sf_acts->actions_len, skb);
736 nla_nest_end(skb, start);
741 nla_nest_cancel(skb, start);
743 } else if (skb_orig_len)
744 goto nla_put_failure;
746 return genlmsg_end(skb, ovs_header);
751 genlmsg_cancel(skb, ovs_header);
755 /* May not be called with RCU read lock. */
756 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
757 struct genl_info *info,
762 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
765 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
767 return ERR_PTR(-ENOMEM);
772 /* Called with ovs_mutex. */
773 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
775 struct genl_info *info, u8 cmd,
781 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
783 if (!skb || IS_ERR(skb))
786 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
787 info->snd_portid, info->snd_seq, 0,
793 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
795 struct nlattr **a = info->attrs;
796 struct ovs_header *ovs_header = info->userhdr;
797 struct sw_flow *flow, *new_flow;
798 struct sw_flow_mask mask;
799 struct sk_buff *reply;
801 struct sw_flow_actions *acts;
802 struct sw_flow_match match;
805 /* Must have key and actions. */
807 if (!a[OVS_FLOW_ATTR_KEY])
809 if (!a[OVS_FLOW_ATTR_ACTIONS])
812 /* Most of the time we need to allocate a new flow, do it before
815 new_flow = ovs_flow_alloc();
816 if (IS_ERR(new_flow)) {
817 error = PTR_ERR(new_flow);
822 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
823 error = ovs_nla_get_match(&match,
824 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
828 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
830 /* Validate actions. */
831 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
832 error = PTR_ERR(acts);
836 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
839 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
843 reply = ovs_flow_cmd_alloc_info(acts, info, false);
845 error = PTR_ERR(reply);
850 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
855 /* Check if this is a duplicate flow */
856 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
858 rcu_assign_pointer(new_flow->sf_acts, acts);
860 /* Put flow in bucket. */
861 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
862 if (unlikely(error)) {
867 if (unlikely(reply)) {
868 error = ovs_flow_cmd_fill_info(new_flow,
869 ovs_header->dp_ifindex,
870 reply, info->snd_portid,
877 struct sw_flow_actions *old_acts;
879 /* Bail out if we're not allowed to modify an existing flow.
880 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
881 * because Generic Netlink treats the latter as a dump
882 * request. We also accept NLM_F_EXCL in case that bug ever
885 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
890 /* The unmasked key has to be the same for flow updates. */
891 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
892 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
898 /* Update actions. */
899 old_acts = ovsl_dereference(flow->sf_acts);
900 rcu_assign_pointer(flow->sf_acts, acts);
902 if (unlikely(reply)) {
903 error = ovs_flow_cmd_fill_info(flow,
904 ovs_header->dp_ifindex,
905 reply, info->snd_portid,
912 ovs_nla_free_flow_actions(old_acts);
913 ovs_flow_free(new_flow, false);
917 ovs_notify(&dp_flow_genl_family, reply, info);
926 ovs_flow_free(new_flow, false);
931 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
933 struct nlattr **a = info->attrs;
934 struct ovs_header *ovs_header = info->userhdr;
935 struct sw_flow_key key, masked_key;
936 struct sw_flow *flow;
937 struct sw_flow_mask mask;
938 struct sk_buff *reply = NULL;
940 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
941 struct sw_flow_match match;
946 if (!a[OVS_FLOW_ATTR_KEY])
949 ovs_match_init(&match, &key, &mask);
950 error = ovs_nla_get_match(&match,
951 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
955 /* Validate actions. */
956 if (a[OVS_FLOW_ATTR_ACTIONS]) {
957 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
958 error = PTR_ERR(acts);
962 ovs_flow_mask_key(&masked_key, &key, &mask);
963 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
964 &masked_key, 0, &acts);
966 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
971 /* Can allocate before locking if have acts. */
973 reply = ovs_flow_cmd_alloc_info(acts, info, false);
975 error = PTR_ERR(reply);
981 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
986 /* Check that the flow exists. */
987 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
988 if (unlikely(!flow)) {
993 /* Update actions, if present. */
995 old_acts = ovsl_dereference(flow->sf_acts);
996 rcu_assign_pointer(flow->sf_acts, acts);
998 if (unlikely(reply)) {
999 error = ovs_flow_cmd_fill_info(flow,
1000 ovs_header->dp_ifindex,
1001 reply, info->snd_portid,
1007 /* Could not alloc without acts before locking. */
1008 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1009 info, OVS_FLOW_CMD_NEW, false);
1010 if (unlikely(IS_ERR(reply))) {
1011 error = PTR_ERR(reply);
1012 goto err_unlock_ovs;
1017 if (a[OVS_FLOW_ATTR_CLEAR])
1018 ovs_flow_stats_clear(flow);
1022 ovs_notify(&dp_flow_genl_family, reply, info);
1024 ovs_nla_free_flow_actions(old_acts);
1037 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1039 struct nlattr **a = info->attrs;
1040 struct ovs_header *ovs_header = info->userhdr;
1041 struct sw_flow_key key;
1042 struct sk_buff *reply;
1043 struct sw_flow *flow;
1044 struct datapath *dp;
1045 struct sw_flow_match match;
1048 if (!a[OVS_FLOW_ATTR_KEY]) {
1049 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1053 ovs_match_init(&match, &key, NULL);
1054 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1059 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1065 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1071 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1072 OVS_FLOW_CMD_NEW, true);
1073 if (IS_ERR(reply)) {
1074 err = PTR_ERR(reply);
1079 return genlmsg_reply(reply, info);
1085 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1087 struct nlattr **a = info->attrs;
1088 struct ovs_header *ovs_header = info->userhdr;
1089 struct sw_flow_key key;
1090 struct sk_buff *reply;
1091 struct sw_flow *flow;
1092 struct datapath *dp;
1093 struct sw_flow_match match;
1096 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1097 ovs_match_init(&match, &key, NULL);
1098 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1104 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1105 if (unlikely(!dp)) {
1110 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1111 err = ovs_flow_tbl_flush(&dp->table);
1115 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1116 if (unlikely(!flow)) {
1121 ovs_flow_tbl_remove(&dp->table, flow);
1124 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1126 if (likely(reply)) {
1127 if (likely(!IS_ERR(reply))) {
1128 rcu_read_lock(); /*To keep RCU checker happy. */
1129 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1130 reply, info->snd_portid,
1136 ovs_notify(&dp_flow_genl_family, reply, info);
1138 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1142 ovs_flow_free(flow, true);
1149 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1151 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1152 struct table_instance *ti;
1153 struct datapath *dp;
1156 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1162 ti = rcu_dereference(dp->table.ti);
1164 struct sw_flow *flow;
1167 bucket = cb->args[0];
1169 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1173 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1174 NETLINK_CB(cb->skb).portid,
1175 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1176 OVS_FLOW_CMD_NEW) < 0)
1179 cb->args[0] = bucket;
1186 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1187 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1188 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1189 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1192 static struct genl_ops dp_flow_genl_ops[] = {
1193 { .cmd = OVS_FLOW_CMD_NEW,
1194 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1195 .policy = flow_policy,
1196 .doit = ovs_flow_cmd_new
1198 { .cmd = OVS_FLOW_CMD_DEL,
1199 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1200 .policy = flow_policy,
1201 .doit = ovs_flow_cmd_del
1203 { .cmd = OVS_FLOW_CMD_GET,
1204 .flags = 0, /* OK for unprivileged users. */
1205 .policy = flow_policy,
1206 .doit = ovs_flow_cmd_get,
1207 .dumpit = ovs_flow_cmd_dump
1209 { .cmd = OVS_FLOW_CMD_SET,
1210 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1211 .policy = flow_policy,
1212 .doit = ovs_flow_cmd_set,
1216 static struct genl_family dp_flow_genl_family = {
1217 .id = GENL_ID_GENERATE,
1218 .hdrsize = sizeof(struct ovs_header),
1219 .name = OVS_FLOW_FAMILY,
1220 .version = OVS_FLOW_VERSION,
1221 .maxattr = OVS_FLOW_ATTR_MAX,
1223 .parallel_ops = true,
1224 .ops = dp_flow_genl_ops,
1225 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1226 .mcgrps = &ovs_dp_flow_multicast_group,
1230 static size_t ovs_dp_cmd_msg_size(void)
1232 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1234 msgsize += nla_total_size(IFNAMSIZ);
1235 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1236 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1237 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1242 /* Called with ovs_mutex or RCU read lock. */
1243 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1244 u32 portid, u32 seq, u32 flags, u8 cmd)
1246 struct ovs_header *ovs_header;
1247 struct ovs_dp_stats dp_stats;
1248 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1251 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1256 ovs_header->dp_ifindex = get_dpifindex(dp);
1258 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1260 goto nla_put_failure;
1262 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1263 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1265 goto nla_put_failure;
1267 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1268 sizeof(struct ovs_dp_megaflow_stats),
1269 &dp_megaflow_stats))
1270 goto nla_put_failure;
1272 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1273 goto nla_put_failure;
1275 return genlmsg_end(skb, ovs_header);
1278 genlmsg_cancel(skb, ovs_header);
1283 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1285 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1288 /* Called with rcu_read_lock or ovs_mutex. */
1289 static struct datapath *lookup_datapath(struct net *net,
1290 struct ovs_header *ovs_header,
1291 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1293 struct datapath *dp;
1295 if (!a[OVS_DP_ATTR_NAME])
1296 dp = get_dp(net, ovs_header->dp_ifindex);
1298 struct vport *vport;
1300 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1301 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1303 return dp ? dp : ERR_PTR(-ENODEV);
1306 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1308 struct datapath *dp;
1310 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1314 WARN(dp->user_features, "Dropping previously announced user features\n");
1315 dp->user_features = 0;
1318 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1320 if (a[OVS_DP_ATTR_USER_FEATURES])
1321 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1324 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1326 struct nlattr **a = info->attrs;
1327 struct vport_parms parms;
1328 struct sk_buff *reply;
1329 struct datapath *dp;
1330 struct vport *vport;
1331 struct ovs_net *ovs_net;
1335 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1338 reply = ovs_dp_cmd_alloc_info(info);
1343 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1345 goto err_free_reply;
1347 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1349 /* Allocate table. */
1350 err = ovs_flow_tbl_init(&dp->table);
1354 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1355 if (!dp->stats_percpu) {
1357 goto err_destroy_table;
1360 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1364 goto err_destroy_percpu;
1367 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1368 INIT_HLIST_HEAD(&dp->ports[i]);
1370 /* Set up our datapath device. */
1371 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1372 parms.type = OVS_VPORT_TYPE_INTERNAL;
1373 parms.options = NULL;
1375 parms.port_no = OVSP_LOCAL;
1376 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1378 ovs_dp_change(dp, a);
1380 /* So far only local changes have been made, now need the lock. */
1383 vport = new_vport(&parms);
1384 if (IS_ERR(vport)) {
1385 err = PTR_ERR(vport);
1389 if (err == -EEXIST) {
1390 /* An outdated user space instance that does not understand
1391 * the concept of user_features has attempted to create a new
1392 * datapath and is likely to reuse it. Drop all user features.
1394 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1395 ovs_dp_reset_user_features(skb, info);
1398 goto err_destroy_ports_array;
1401 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1402 info->snd_seq, 0, OVS_DP_CMD_NEW);
1405 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1406 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1410 ovs_notify(&dp_datapath_genl_family, reply, info);
1413 err_destroy_ports_array:
1417 free_percpu(dp->stats_percpu);
1419 ovs_flow_tbl_destroy(&dp->table, false);
1421 release_net(ovs_dp_get_net(dp));
1429 /* Called with ovs_mutex. */
1430 static void __dp_destroy(struct datapath *dp)
1434 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1435 struct vport *vport;
1436 struct hlist_node *n;
1438 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1439 if (vport->port_no != OVSP_LOCAL)
1440 ovs_dp_detach_port(vport);
1443 list_del_rcu(&dp->list_node);
1445 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1446 * all ports in datapath are destroyed first before freeing datapath.
1448 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1450 /* RCU destroy the flow table */
1451 ovs_flow_tbl_destroy(&dp->table, true);
1453 call_rcu(&dp->rcu, destroy_dp_rcu);
1456 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1458 struct sk_buff *reply;
1459 struct datapath *dp;
1462 reply = ovs_dp_cmd_alloc_info(info);
1467 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1470 goto err_unlock_free;
1472 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1473 info->snd_seq, 0, OVS_DP_CMD_DEL);
1479 ovs_notify(&dp_datapath_genl_family, reply, info);
1489 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1491 struct sk_buff *reply;
1492 struct datapath *dp;
1495 reply = ovs_dp_cmd_alloc_info(info);
1500 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1503 goto err_unlock_free;
1505 ovs_dp_change(dp, info->attrs);
1507 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1508 info->snd_seq, 0, OVS_DP_CMD_NEW);
1512 ovs_notify(&dp_datapath_genl_family, reply, info);
1522 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1524 struct sk_buff *reply;
1525 struct datapath *dp;
1528 reply = ovs_dp_cmd_alloc_info(info);
1533 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1536 goto err_unlock_free;
1538 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1539 info->snd_seq, 0, OVS_DP_CMD_NEW);
1543 return genlmsg_reply(reply, info);
1551 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1553 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1554 struct datapath *dp;
1555 int skip = cb->args[0];
1559 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1561 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1562 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1563 OVS_DP_CMD_NEW) < 0)
1574 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1575 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1576 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1577 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1580 static struct genl_ops dp_datapath_genl_ops[] = {
1581 { .cmd = OVS_DP_CMD_NEW,
1582 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1583 .policy = datapath_policy,
1584 .doit = ovs_dp_cmd_new
1586 { .cmd = OVS_DP_CMD_DEL,
1587 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1588 .policy = datapath_policy,
1589 .doit = ovs_dp_cmd_del
1591 { .cmd = OVS_DP_CMD_GET,
1592 .flags = 0, /* OK for unprivileged users. */
1593 .policy = datapath_policy,
1594 .doit = ovs_dp_cmd_get,
1595 .dumpit = ovs_dp_cmd_dump
1597 { .cmd = OVS_DP_CMD_SET,
1598 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1599 .policy = datapath_policy,
1600 .doit = ovs_dp_cmd_set,
1604 static struct genl_family dp_datapath_genl_family = {
1605 .id = GENL_ID_GENERATE,
1606 .hdrsize = sizeof(struct ovs_header),
1607 .name = OVS_DATAPATH_FAMILY,
1608 .version = OVS_DATAPATH_VERSION,
1609 .maxattr = OVS_DP_ATTR_MAX,
1611 .parallel_ops = true,
1612 .ops = dp_datapath_genl_ops,
1613 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1614 .mcgrps = &ovs_dp_datapath_multicast_group,
1618 /* Called with ovs_mutex or RCU read lock. */
1619 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1620 u32 portid, u32 seq, u32 flags, u8 cmd)
1622 struct ovs_header *ovs_header;
1623 struct ovs_vport_stats vport_stats;
1626 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1631 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1633 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1634 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1635 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1636 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1637 goto nla_put_failure;
1639 ovs_vport_get_stats(vport, &vport_stats);
1640 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1642 goto nla_put_failure;
1644 err = ovs_vport_get_options(vport, skb);
1645 if (err == -EMSGSIZE)
1648 return genlmsg_end(skb, ovs_header);
1653 genlmsg_cancel(skb, ovs_header);
1657 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1659 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1662 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1663 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1666 struct sk_buff *skb;
1669 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1671 return ERR_PTR(-ENOMEM);
1673 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1679 /* Called with ovs_mutex or RCU read lock. */
1680 static struct vport *lookup_vport(struct net *net,
1681 struct ovs_header *ovs_header,
1682 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1684 struct datapath *dp;
1685 struct vport *vport;
1687 if (a[OVS_VPORT_ATTR_NAME]) {
1688 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1690 return ERR_PTR(-ENODEV);
1691 if (ovs_header->dp_ifindex &&
1692 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1693 return ERR_PTR(-ENODEV);
1695 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1696 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1698 if (port_no >= DP_MAX_PORTS)
1699 return ERR_PTR(-EFBIG);
1701 dp = get_dp(net, ovs_header->dp_ifindex);
1703 return ERR_PTR(-ENODEV);
1705 vport = ovs_vport_ovsl_rcu(dp, port_no);
1707 return ERR_PTR(-ENODEV);
1710 return ERR_PTR(-EINVAL);
1713 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1715 struct nlattr **a = info->attrs;
1716 struct ovs_header *ovs_header = info->userhdr;
1717 struct vport_parms parms;
1718 struct sk_buff *reply;
1719 struct vport *vport;
1720 struct datapath *dp;
1724 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1725 !a[OVS_VPORT_ATTR_UPCALL_PID])
1728 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1729 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1730 if (port_no >= DP_MAX_PORTS)
1733 reply = ovs_vport_cmd_alloc_info();
1738 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1741 goto exit_unlock_free;
1744 vport = ovs_vport_ovsl(dp, port_no);
1747 goto exit_unlock_free;
1749 for (port_no = 1; ; port_no++) {
1750 if (port_no >= DP_MAX_PORTS) {
1752 goto exit_unlock_free;
1754 vport = ovs_vport_ovsl(dp, port_no);
1760 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1761 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1762 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1764 parms.port_no = port_no;
1765 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1767 vport = new_vport(&parms);
1768 err = PTR_ERR(vport);
1770 goto exit_unlock_free;
1772 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1773 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1777 ovs_notify(&dp_vport_genl_family, reply, info);
1786 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1788 struct nlattr **a = info->attrs;
1789 struct sk_buff *reply;
1790 struct vport *vport;
1793 reply = ovs_vport_cmd_alloc_info();
1798 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1799 err = PTR_ERR(vport);
1801 goto exit_unlock_free;
1803 if (a[OVS_VPORT_ATTR_TYPE] &&
1804 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1806 goto exit_unlock_free;
1809 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1810 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1812 goto exit_unlock_free;
1815 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1816 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1818 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1819 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1823 ovs_notify(&dp_vport_genl_family, reply, info);
1832 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1834 struct nlattr **a = info->attrs;
1835 struct sk_buff *reply;
1836 struct vport *vport;
1839 reply = ovs_vport_cmd_alloc_info();
1844 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1845 err = PTR_ERR(vport);
1847 goto exit_unlock_free;
1849 if (vport->port_no == OVSP_LOCAL) {
1851 goto exit_unlock_free;
1854 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1855 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1857 ovs_dp_detach_port(vport);
1860 ovs_notify(&dp_vport_genl_family, reply, info);
1869 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1871 struct nlattr **a = info->attrs;
1872 struct ovs_header *ovs_header = info->userhdr;
1873 struct sk_buff *reply;
1874 struct vport *vport;
1877 reply = ovs_vport_cmd_alloc_info();
1882 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1883 err = PTR_ERR(vport);
1885 goto exit_unlock_free;
1886 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1887 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1891 return genlmsg_reply(reply, info);
1899 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1901 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1902 struct datapath *dp;
1903 int bucket = cb->args[0], skip = cb->args[1];
1907 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1912 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1913 struct vport *vport;
1916 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1918 ovs_vport_cmd_fill_info(vport, skb,
1919 NETLINK_CB(cb->skb).portid,
1922 OVS_VPORT_CMD_NEW) < 0)
1938 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1939 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1940 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1941 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1942 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1943 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1944 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1947 static struct genl_ops dp_vport_genl_ops[] = {
1948 { .cmd = OVS_VPORT_CMD_NEW,
1949 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1950 .policy = vport_policy,
1951 .doit = ovs_vport_cmd_new
1953 { .cmd = OVS_VPORT_CMD_DEL,
1954 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1955 .policy = vport_policy,
1956 .doit = ovs_vport_cmd_del
1958 { .cmd = OVS_VPORT_CMD_GET,
1959 .flags = 0, /* OK for unprivileged users. */
1960 .policy = vport_policy,
1961 .doit = ovs_vport_cmd_get,
1962 .dumpit = ovs_vport_cmd_dump
1964 { .cmd = OVS_VPORT_CMD_SET,
1965 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1966 .policy = vport_policy,
1967 .doit = ovs_vport_cmd_set,
1971 struct genl_family dp_vport_genl_family = {
1972 .id = GENL_ID_GENERATE,
1973 .hdrsize = sizeof(struct ovs_header),
1974 .name = OVS_VPORT_FAMILY,
1975 .version = OVS_VPORT_VERSION,
1976 .maxattr = OVS_VPORT_ATTR_MAX,
1978 .parallel_ops = true,
1979 .ops = dp_vport_genl_ops,
1980 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
1981 .mcgrps = &ovs_dp_vport_multicast_group,
1985 static struct genl_family * const dp_genl_families[] = {
1986 &dp_datapath_genl_family,
1987 &dp_vport_genl_family,
1988 &dp_flow_genl_family,
1989 &dp_packet_genl_family,
1992 static void dp_unregister_genl(int n_families)
1996 for (i = 0; i < n_families; i++)
1997 genl_unregister_family(dp_genl_families[i]);
2000 static int dp_register_genl(void)
2005 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2007 err = genl_register_family(dp_genl_families[i]);
2015 dp_unregister_genl(i);
2019 static int __net_init ovs_init_net(struct net *net)
2021 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2023 INIT_LIST_HEAD(&ovs_net->dps);
2024 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2028 static void __net_exit ovs_exit_net(struct net *net)
2030 struct datapath *dp, *dp_next;
2031 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2034 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2038 cancel_work_sync(&ovs_net->dp_notify_work);
2041 static struct pernet_operations ovs_net_ops = {
2042 .init = ovs_init_net,
2043 .exit = ovs_exit_net,
2045 .size = sizeof(struct ovs_net),
2048 static int __init dp_init(void)
2052 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2054 pr_info("Open vSwitch switching datapath\n");
2056 err = ovs_flow_init();
2060 err = ovs_vport_init();
2062 goto error_flow_exit;
2064 err = register_pernet_device(&ovs_net_ops);
2066 goto error_vport_exit;
2068 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2070 goto error_netns_exit;
2072 err = dp_register_genl();
2074 goto error_unreg_notifier;
2078 error_unreg_notifier:
2079 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2081 unregister_pernet_device(&ovs_net_ops);
2090 static void dp_cleanup(void)
2092 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2093 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2094 unregister_pernet_device(&ovs_net_ops);
2100 module_init(dp_init);
2101 module_exit(dp_cleanup);
2103 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2104 MODULE_LICENSE("GPL");