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 <net/genetlink.h>
51 #include <net/net_namespace.h>
52 #include <net/netns/generic.h>
56 #include "flow_table.h"
57 #include "flow_netlink.h"
58 #include "vport-internal_dev.h"
59 #include "vport-netdev.h"
61 int ovs_net_id __read_mostly;
63 static struct genl_family dp_packet_genl_family;
64 static struct genl_family dp_flow_genl_family;
65 static struct genl_family dp_datapath_genl_family;
67 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
68 .name = OVS_FLOW_MCGROUP,
71 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
72 .name = OVS_DATAPATH_MCGROUP,
75 static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
76 .name = OVS_VPORT_MCGROUP,
79 /* Check if need to build a reply message.
80 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
81 static bool ovs_must_notify(struct genl_info *info,
82 const struct genl_multicast_group *grp)
84 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
85 netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
88 static void ovs_notify(struct genl_family *family,
89 struct sk_buff *skb, struct genl_info *info)
91 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
92 0, info->nlhdr, GFP_KERNEL);
98 * All writes e.g. Writes to device state (add/remove datapath, port, set
99 * operations on vports, etc.), Writes to other state (flow table
100 * modifications, set miscellaneous datapath parameters, etc.) are protected
103 * Reads are protected by RCU.
105 * There are a few special cases (mostly stats) that have their own
106 * synchronization but they nest under all of above and don't interact with
109 * The RTNL lock nests inside ovs_mutex.
112 static DEFINE_MUTEX(ovs_mutex);
116 mutex_lock(&ovs_mutex);
119 void ovs_unlock(void)
121 mutex_unlock(&ovs_mutex);
124 #ifdef CONFIG_LOCKDEP
125 int lockdep_ovsl_is_held(void)
128 return lockdep_is_held(&ovs_mutex);
134 static struct vport *new_vport(const struct vport_parms *);
135 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
136 const struct dp_upcall_info *);
137 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
138 const struct dp_upcall_info *);
140 /* Must be called with rcu_read_lock or ovs_mutex. */
141 static struct datapath *get_dp(struct net *net, int dp_ifindex)
143 struct datapath *dp = NULL;
144 struct net_device *dev;
147 dev = dev_get_by_index_rcu(net, dp_ifindex);
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
158 /* Must be called with rcu_read_lock or ovs_mutex. */
159 static const char *ovs_dp_name(const struct datapath *dp)
161 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
162 return vport->ops->get_name(vport);
165 static int get_dpifindex(struct datapath *dp)
172 local = ovs_vport_rcu(dp, OVSP_LOCAL);
174 ifindex = netdev_vport_priv(local)->dev->ifindex;
183 static void destroy_dp_rcu(struct rcu_head *rcu)
185 struct datapath *dp = container_of(rcu, struct datapath, rcu);
187 free_percpu(dp->stats_percpu);
188 release_net(ovs_dp_get_net(dp));
193 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
196 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
199 /* Called with ovs_mutex or RCU read lock. */
200 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
203 struct hlist_head *head;
205 head = vport_hash_bucket(dp, port_no);
206 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
207 if (vport->port_no == port_no)
213 /* Called with ovs_mutex. */
214 static struct vport *new_vport(const struct vport_parms *parms)
218 vport = ovs_vport_add(parms);
219 if (!IS_ERR(vport)) {
220 struct datapath *dp = parms->dp;
221 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
223 hlist_add_head_rcu(&vport->dp_hash_node, head);
228 void ovs_dp_detach_port(struct vport *p)
232 /* First drop references to device. */
233 hlist_del_rcu(&p->dp_hash_node);
235 /* Then destroy it. */
239 /* Must be called with rcu_read_lock. */
240 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
242 struct datapath *dp = p->dp;
243 struct sw_flow *flow;
244 struct dp_stats_percpu *stats;
245 struct sw_flow_key key;
250 stats = this_cpu_ptr(dp->stats_percpu);
252 /* Extract flow from 'skb' into 'key'. */
253 error = ovs_flow_extract(skb, p->port_no, &key);
254 if (unlikely(error)) {
260 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
261 if (unlikely(!flow)) {
262 struct dp_upcall_info upcall;
264 upcall.cmd = OVS_PACKET_CMD_MISS;
266 upcall.userdata = NULL;
267 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
268 error = ovs_dp_upcall(dp, skb, &upcall);
273 stats_counter = &stats->n_missed;
277 OVS_CB(skb)->flow = flow;
279 ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
280 ovs_execute_actions(dp, skb, &key);
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 = NULL; /* 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 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
469 nla_nest_end(user_skb, nla);
471 if (upcall_info->userdata)
472 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
473 nla_len(upcall_info->userdata),
474 nla_data(upcall_info->userdata));
476 /* Only reserve room for attribute header, packet data is added
477 * in skb_zerocopy() */
478 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
482 nla->nla_len = nla_attr_size(skb->len);
484 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
488 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
489 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
490 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
493 memset(skb_put(user_skb, plen), 0, plen);
496 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
498 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
508 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
510 struct ovs_header *ovs_header = info->userhdr;
511 struct nlattr **a = info->attrs;
512 struct sw_flow_actions *acts;
513 struct sk_buff *packet;
514 struct sw_flow *flow;
521 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
522 !a[OVS_PACKET_ATTR_ACTIONS])
525 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
526 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
530 skb_reserve(packet, NET_IP_ALIGN);
532 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
534 skb_reset_mac_header(packet);
535 eth = eth_hdr(packet);
537 /* Normally, setting the skb 'protocol' field would be handled by a
538 * call to eth_type_trans(), but it assumes there's a sending
539 * device, which we may not have. */
540 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
541 packet->protocol = eth->h_proto;
543 packet->protocol = htons(ETH_P_802_2);
545 /* Build an sw_flow for sending this packet. */
546 flow = ovs_flow_alloc();
551 err = ovs_flow_extract(packet, -1, &flow->key);
555 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
558 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
563 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
564 &flow->key, 0, &acts);
565 rcu_assign_pointer(flow->sf_acts, acts);
569 OVS_CB(packet)->flow = flow;
570 packet->priority = flow->key.phy.priority;
571 packet->mark = flow->key.phy.skb_mark;
574 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
580 err = ovs_execute_actions(dp, packet, &flow->key);
584 ovs_flow_free(flow, false);
590 ovs_flow_free(flow, false);
597 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
598 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
599 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
600 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
603 static const struct genl_ops dp_packet_genl_ops[] = {
604 { .cmd = OVS_PACKET_CMD_EXECUTE,
605 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
606 .policy = packet_policy,
607 .doit = ovs_packet_cmd_execute
611 static struct genl_family dp_packet_genl_family = {
612 .id = GENL_ID_GENERATE,
613 .hdrsize = sizeof(struct ovs_header),
614 .name = OVS_PACKET_FAMILY,
615 .version = OVS_PACKET_VERSION,
616 .maxattr = OVS_PACKET_ATTR_MAX,
618 .parallel_ops = true,
619 .ops = dp_packet_genl_ops,
620 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
623 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
624 struct ovs_dp_megaflow_stats *mega_stats)
628 memset(mega_stats, 0, sizeof(*mega_stats));
630 stats->n_flows = ovs_flow_tbl_count(&dp->table);
631 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
633 stats->n_hit = stats->n_missed = stats->n_lost = 0;
635 for_each_possible_cpu(i) {
636 const struct dp_stats_percpu *percpu_stats;
637 struct dp_stats_percpu local_stats;
640 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
643 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
644 local_stats = *percpu_stats;
645 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
647 stats->n_hit += local_stats.n_hit;
648 stats->n_missed += local_stats.n_missed;
649 stats->n_lost += local_stats.n_lost;
650 mega_stats->n_mask_hit += local_stats.n_mask_hit;
654 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
656 return NLMSG_ALIGN(sizeof(struct ovs_header))
657 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
658 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
659 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
660 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
661 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
662 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
665 /* Called with ovs_mutex or RCU read lock. */
666 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
667 struct sk_buff *skb, u32 portid,
668 u32 seq, u32 flags, u8 cmd)
670 const int skb_orig_len = skb->len;
671 struct nlattr *start;
672 struct ovs_flow_stats stats;
675 struct ovs_header *ovs_header;
679 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
683 ovs_header->dp_ifindex = dp_ifindex;
686 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
688 goto nla_put_failure;
690 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
693 nla_nest_end(skb, nla);
695 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
697 goto nla_put_failure;
699 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
703 nla_nest_end(skb, nla);
705 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
708 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
709 goto nla_put_failure;
711 if (stats.n_packets &&
712 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
713 goto nla_put_failure;
715 if ((u8)ntohs(tcp_flags) &&
716 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
717 goto nla_put_failure;
719 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
720 * this is the first flow to be dumped into 'skb'. This is unusual for
721 * Netlink but individual action lists can be longer than
722 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
723 * The userspace caller can always fetch the actions separately if it
724 * really wants them. (Most userspace callers in fact don't care.)
726 * This can only fail for dump operations because the skb is always
727 * properly sized for single flows.
729 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
731 const struct sw_flow_actions *sf_acts;
733 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
734 err = ovs_nla_put_actions(sf_acts->actions,
735 sf_acts->actions_len, skb);
738 nla_nest_end(skb, start);
743 nla_nest_cancel(skb, start);
745 } else if (skb_orig_len)
746 goto nla_put_failure;
748 return genlmsg_end(skb, ovs_header);
753 genlmsg_cancel(skb, ovs_header);
757 /* May not be called with RCU read lock. */
758 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
759 struct genl_info *info,
764 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
767 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
769 return ERR_PTR(-ENOMEM);
774 /* Called with ovs_mutex. */
775 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
777 struct genl_info *info, u8 cmd,
783 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
785 if (IS_ERR_OR_NULL(skb))
788 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
789 info->snd_portid, info->snd_seq, 0,
795 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
797 struct nlattr **a = info->attrs;
798 struct ovs_header *ovs_header = info->userhdr;
799 struct sw_flow *flow, *new_flow;
800 struct sw_flow_mask mask;
801 struct sk_buff *reply;
803 struct sw_flow_actions *acts;
804 struct sw_flow_match match;
807 /* Must have key and actions. */
809 if (!a[OVS_FLOW_ATTR_KEY])
811 if (!a[OVS_FLOW_ATTR_ACTIONS])
814 /* Most of the time we need to allocate a new flow, do it before
817 new_flow = ovs_flow_alloc();
818 if (IS_ERR(new_flow)) {
819 error = PTR_ERR(new_flow);
824 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
825 error = ovs_nla_get_match(&match,
826 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
830 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
832 /* Validate actions. */
833 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
834 error = PTR_ERR(acts);
838 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
841 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
845 reply = ovs_flow_cmd_alloc_info(acts, info, false);
847 error = PTR_ERR(reply);
852 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
857 /* Check if this is a duplicate flow */
858 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
860 rcu_assign_pointer(new_flow->sf_acts, acts);
862 /* Put flow in bucket. */
863 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
864 if (unlikely(error)) {
869 if (unlikely(reply)) {
870 error = ovs_flow_cmd_fill_info(new_flow,
871 ovs_header->dp_ifindex,
872 reply, info->snd_portid,
879 struct sw_flow_actions *old_acts;
881 /* Bail out if we're not allowed to modify an existing flow.
882 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
883 * because Generic Netlink treats the latter as a dump
884 * request. We also accept NLM_F_EXCL in case that bug ever
887 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
892 /* The unmasked key has to be the same for flow updates. */
893 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
894 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
900 /* Update actions. */
901 old_acts = ovsl_dereference(flow->sf_acts);
902 rcu_assign_pointer(flow->sf_acts, acts);
904 if (unlikely(reply)) {
905 error = ovs_flow_cmd_fill_info(flow,
906 ovs_header->dp_ifindex,
907 reply, info->snd_portid,
914 ovs_nla_free_flow_actions(old_acts);
915 ovs_flow_free(new_flow, false);
919 ovs_notify(&dp_flow_genl_family, reply, info);
928 ovs_flow_free(new_flow, false);
933 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
935 struct nlattr **a = info->attrs;
936 struct ovs_header *ovs_header = info->userhdr;
937 struct sw_flow_key key, masked_key;
938 struct sw_flow *flow;
939 struct sw_flow_mask mask;
940 struct sk_buff *reply = NULL;
942 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
943 struct sw_flow_match match;
948 if (!a[OVS_FLOW_ATTR_KEY])
951 ovs_match_init(&match, &key, &mask);
952 error = ovs_nla_get_match(&match,
953 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
957 /* Validate actions. */
958 if (a[OVS_FLOW_ATTR_ACTIONS]) {
959 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
960 error = PTR_ERR(acts);
964 ovs_flow_mask_key(&masked_key, &key, &mask);
965 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
966 &masked_key, 0, &acts);
968 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
973 /* Can allocate before locking if have acts. */
975 reply = ovs_flow_cmd_alloc_info(acts, info, false);
977 error = PTR_ERR(reply);
983 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
988 /* Check that the flow exists. */
989 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
990 if (unlikely(!flow)) {
995 /* Update actions, if present. */
997 old_acts = ovsl_dereference(flow->sf_acts);
998 rcu_assign_pointer(flow->sf_acts, acts);
1000 if (unlikely(reply)) {
1001 error = ovs_flow_cmd_fill_info(flow,
1002 ovs_header->dp_ifindex,
1003 reply, info->snd_portid,
1009 /* Could not alloc without acts before locking. */
1010 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1011 info, OVS_FLOW_CMD_NEW, false);
1012 if (unlikely(IS_ERR(reply))) {
1013 error = PTR_ERR(reply);
1014 goto err_unlock_ovs;
1019 if (a[OVS_FLOW_ATTR_CLEAR])
1020 ovs_flow_stats_clear(flow);
1024 ovs_notify(&dp_flow_genl_family, reply, info);
1026 ovs_nla_free_flow_actions(old_acts);
1039 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1041 struct nlattr **a = info->attrs;
1042 struct ovs_header *ovs_header = info->userhdr;
1043 struct sw_flow_key key;
1044 struct sk_buff *reply;
1045 struct sw_flow *flow;
1046 struct datapath *dp;
1047 struct sw_flow_match match;
1050 if (!a[OVS_FLOW_ATTR_KEY]) {
1051 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1055 ovs_match_init(&match, &key, NULL);
1056 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1061 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1067 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1073 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1074 OVS_FLOW_CMD_NEW, true);
1075 if (IS_ERR(reply)) {
1076 err = PTR_ERR(reply);
1081 return genlmsg_reply(reply, info);
1087 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1089 struct nlattr **a = info->attrs;
1090 struct ovs_header *ovs_header = info->userhdr;
1091 struct sw_flow_key key;
1092 struct sk_buff *reply;
1093 struct sw_flow *flow;
1094 struct datapath *dp;
1095 struct sw_flow_match match;
1098 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1099 ovs_match_init(&match, &key, NULL);
1100 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1106 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1107 if (unlikely(!dp)) {
1112 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1113 err = ovs_flow_tbl_flush(&dp->table);
1117 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1118 if (unlikely(!flow)) {
1123 ovs_flow_tbl_remove(&dp->table, flow);
1126 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1128 if (likely(reply)) {
1129 if (likely(!IS_ERR(reply))) {
1130 rcu_read_lock(); /*To keep RCU checker happy. */
1131 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1132 reply, info->snd_portid,
1138 ovs_notify(&dp_flow_genl_family, reply, info);
1140 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1144 ovs_flow_free(flow, true);
1151 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1153 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1154 struct table_instance *ti;
1155 struct datapath *dp;
1158 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1164 ti = rcu_dereference(dp->table.ti);
1166 struct sw_flow *flow;
1169 bucket = cb->args[0];
1171 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1175 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1176 NETLINK_CB(cb->skb).portid,
1177 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1178 OVS_FLOW_CMD_NEW) < 0)
1181 cb->args[0] = bucket;
1188 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1189 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1190 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1191 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1194 static const struct genl_ops dp_flow_genl_ops[] = {
1195 { .cmd = OVS_FLOW_CMD_NEW,
1196 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1197 .policy = flow_policy,
1198 .doit = ovs_flow_cmd_new
1200 { .cmd = OVS_FLOW_CMD_DEL,
1201 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1202 .policy = flow_policy,
1203 .doit = ovs_flow_cmd_del
1205 { .cmd = OVS_FLOW_CMD_GET,
1206 .flags = 0, /* OK for unprivileged users. */
1207 .policy = flow_policy,
1208 .doit = ovs_flow_cmd_get,
1209 .dumpit = ovs_flow_cmd_dump
1211 { .cmd = OVS_FLOW_CMD_SET,
1212 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1213 .policy = flow_policy,
1214 .doit = ovs_flow_cmd_set,
1218 static struct genl_family dp_flow_genl_family = {
1219 .id = GENL_ID_GENERATE,
1220 .hdrsize = sizeof(struct ovs_header),
1221 .name = OVS_FLOW_FAMILY,
1222 .version = OVS_FLOW_VERSION,
1223 .maxattr = OVS_FLOW_ATTR_MAX,
1225 .parallel_ops = true,
1226 .ops = dp_flow_genl_ops,
1227 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1228 .mcgrps = &ovs_dp_flow_multicast_group,
1232 static size_t ovs_dp_cmd_msg_size(void)
1234 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1236 msgsize += nla_total_size(IFNAMSIZ);
1237 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1238 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1239 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1244 /* Called with ovs_mutex or RCU read lock. */
1245 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1246 u32 portid, u32 seq, u32 flags, u8 cmd)
1248 struct ovs_header *ovs_header;
1249 struct ovs_dp_stats dp_stats;
1250 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1253 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1258 ovs_header->dp_ifindex = get_dpifindex(dp);
1260 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1262 goto nla_put_failure;
1264 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1265 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1267 goto nla_put_failure;
1269 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1270 sizeof(struct ovs_dp_megaflow_stats),
1271 &dp_megaflow_stats))
1272 goto nla_put_failure;
1274 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1275 goto nla_put_failure;
1277 return genlmsg_end(skb, ovs_header);
1280 genlmsg_cancel(skb, ovs_header);
1285 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1287 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1290 /* Called with rcu_read_lock or ovs_mutex. */
1291 static struct datapath *lookup_datapath(struct net *net,
1292 struct ovs_header *ovs_header,
1293 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1295 struct datapath *dp;
1297 if (!a[OVS_DP_ATTR_NAME])
1298 dp = get_dp(net, ovs_header->dp_ifindex);
1300 struct vport *vport;
1302 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1303 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1305 return dp ? dp : ERR_PTR(-ENODEV);
1308 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1310 struct datapath *dp;
1312 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1316 WARN(dp->user_features, "Dropping previously announced user features\n");
1317 dp->user_features = 0;
1320 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1322 if (a[OVS_DP_ATTR_USER_FEATURES])
1323 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1326 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1328 struct nlattr **a = info->attrs;
1329 struct vport_parms parms;
1330 struct sk_buff *reply;
1331 struct datapath *dp;
1332 struct vport *vport;
1333 struct ovs_net *ovs_net;
1337 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1340 reply = ovs_dp_cmd_alloc_info(info);
1345 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1347 goto err_free_reply;
1349 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1351 /* Allocate table. */
1352 err = ovs_flow_tbl_init(&dp->table);
1356 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1357 if (!dp->stats_percpu) {
1359 goto err_destroy_table;
1362 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1366 goto err_destroy_percpu;
1369 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1370 INIT_HLIST_HEAD(&dp->ports[i]);
1372 /* Set up our datapath device. */
1373 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1374 parms.type = OVS_VPORT_TYPE_INTERNAL;
1375 parms.options = NULL;
1377 parms.port_no = OVSP_LOCAL;
1378 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1380 ovs_dp_change(dp, a);
1382 /* So far only local changes have been made, now need the lock. */
1385 vport = new_vport(&parms);
1386 if (IS_ERR(vport)) {
1387 err = PTR_ERR(vport);
1391 if (err == -EEXIST) {
1392 /* An outdated user space instance that does not understand
1393 * the concept of user_features has attempted to create a new
1394 * datapath and is likely to reuse it. Drop all user features.
1396 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1397 ovs_dp_reset_user_features(skb, info);
1400 goto err_destroy_ports_array;
1403 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1404 info->snd_seq, 0, OVS_DP_CMD_NEW);
1407 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1408 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1412 ovs_notify(&dp_datapath_genl_family, reply, info);
1415 err_destroy_ports_array:
1419 free_percpu(dp->stats_percpu);
1421 ovs_flow_tbl_destroy(&dp->table, false);
1423 release_net(ovs_dp_get_net(dp));
1431 /* Called with ovs_mutex. */
1432 static void __dp_destroy(struct datapath *dp)
1436 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1437 struct vport *vport;
1438 struct hlist_node *n;
1440 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1441 if (vport->port_no != OVSP_LOCAL)
1442 ovs_dp_detach_port(vport);
1445 list_del_rcu(&dp->list_node);
1447 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1448 * all ports in datapath are destroyed first before freeing datapath.
1450 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1452 /* RCU destroy the flow table */
1453 ovs_flow_tbl_destroy(&dp->table, true);
1455 call_rcu(&dp->rcu, destroy_dp_rcu);
1458 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1460 struct sk_buff *reply;
1461 struct datapath *dp;
1464 reply = ovs_dp_cmd_alloc_info(info);
1469 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1472 goto err_unlock_free;
1474 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1475 info->snd_seq, 0, OVS_DP_CMD_DEL);
1481 ovs_notify(&dp_datapath_genl_family, reply, info);
1491 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1493 struct sk_buff *reply;
1494 struct datapath *dp;
1497 reply = ovs_dp_cmd_alloc_info(info);
1502 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1505 goto err_unlock_free;
1507 ovs_dp_change(dp, info->attrs);
1509 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1510 info->snd_seq, 0, OVS_DP_CMD_NEW);
1514 ovs_notify(&dp_datapath_genl_family, reply, info);
1524 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1526 struct sk_buff *reply;
1527 struct datapath *dp;
1530 reply = ovs_dp_cmd_alloc_info(info);
1535 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1538 goto err_unlock_free;
1540 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1541 info->snd_seq, 0, OVS_DP_CMD_NEW);
1545 return genlmsg_reply(reply, info);
1553 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1555 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1556 struct datapath *dp;
1557 int skip = cb->args[0];
1561 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1563 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1564 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1565 OVS_DP_CMD_NEW) < 0)
1576 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1577 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1578 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1579 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1582 static const struct genl_ops dp_datapath_genl_ops[] = {
1583 { .cmd = OVS_DP_CMD_NEW,
1584 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1585 .policy = datapath_policy,
1586 .doit = ovs_dp_cmd_new
1588 { .cmd = OVS_DP_CMD_DEL,
1589 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1590 .policy = datapath_policy,
1591 .doit = ovs_dp_cmd_del
1593 { .cmd = OVS_DP_CMD_GET,
1594 .flags = 0, /* OK for unprivileged users. */
1595 .policy = datapath_policy,
1596 .doit = ovs_dp_cmd_get,
1597 .dumpit = ovs_dp_cmd_dump
1599 { .cmd = OVS_DP_CMD_SET,
1600 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1601 .policy = datapath_policy,
1602 .doit = ovs_dp_cmd_set,
1606 static struct genl_family dp_datapath_genl_family = {
1607 .id = GENL_ID_GENERATE,
1608 .hdrsize = sizeof(struct ovs_header),
1609 .name = OVS_DATAPATH_FAMILY,
1610 .version = OVS_DATAPATH_VERSION,
1611 .maxattr = OVS_DP_ATTR_MAX,
1613 .parallel_ops = true,
1614 .ops = dp_datapath_genl_ops,
1615 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1616 .mcgrps = &ovs_dp_datapath_multicast_group,
1620 /* Called with ovs_mutex or RCU read lock. */
1621 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1622 u32 portid, u32 seq, u32 flags, u8 cmd)
1624 struct ovs_header *ovs_header;
1625 struct ovs_vport_stats vport_stats;
1628 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1633 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1635 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1636 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1637 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1638 vport->ops->get_name(vport)))
1639 goto nla_put_failure;
1641 ovs_vport_get_stats(vport, &vport_stats);
1642 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1644 goto nla_put_failure;
1646 if (ovs_vport_get_upcall_portids(vport, skb))
1647 goto nla_put_failure;
1649 err = ovs_vport_get_options(vport, skb);
1650 if (err == -EMSGSIZE)
1653 return genlmsg_end(skb, ovs_header);
1658 genlmsg_cancel(skb, ovs_header);
1662 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1664 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1667 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1668 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1671 struct sk_buff *skb;
1674 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1676 return ERR_PTR(-ENOMEM);
1678 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1684 /* Called with ovs_mutex or RCU read lock. */
1685 static struct vport *lookup_vport(struct net *net,
1686 struct ovs_header *ovs_header,
1687 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1689 struct datapath *dp;
1690 struct vport *vport;
1692 if (a[OVS_VPORT_ATTR_NAME]) {
1693 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1695 return ERR_PTR(-ENODEV);
1696 if (ovs_header->dp_ifindex &&
1697 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1698 return ERR_PTR(-ENODEV);
1700 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1701 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1703 if (port_no >= DP_MAX_PORTS)
1704 return ERR_PTR(-EFBIG);
1706 dp = get_dp(net, ovs_header->dp_ifindex);
1708 return ERR_PTR(-ENODEV);
1710 vport = ovs_vport_ovsl_rcu(dp, port_no);
1712 return ERR_PTR(-ENODEV);
1715 return ERR_PTR(-EINVAL);
1718 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1720 struct nlattr **a = info->attrs;
1721 struct ovs_header *ovs_header = info->userhdr;
1722 struct vport_parms parms;
1723 struct sk_buff *reply;
1724 struct vport *vport;
1725 struct datapath *dp;
1729 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1730 !a[OVS_VPORT_ATTR_UPCALL_PID])
1733 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1734 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1735 if (port_no >= DP_MAX_PORTS)
1738 reply = ovs_vport_cmd_alloc_info();
1743 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1746 goto exit_unlock_free;
1749 vport = ovs_vport_ovsl(dp, port_no);
1752 goto exit_unlock_free;
1754 for (port_no = 1; ; port_no++) {
1755 if (port_no >= DP_MAX_PORTS) {
1757 goto exit_unlock_free;
1759 vport = ovs_vport_ovsl(dp, port_no);
1765 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1766 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1767 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1769 parms.port_no = port_no;
1770 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1772 vport = new_vport(&parms);
1773 err = PTR_ERR(vport);
1775 goto exit_unlock_free;
1777 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1778 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1782 ovs_notify(&dp_vport_genl_family, reply, info);
1791 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1793 struct nlattr **a = info->attrs;
1794 struct sk_buff *reply;
1795 struct vport *vport;
1798 reply = ovs_vport_cmd_alloc_info();
1803 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1804 err = PTR_ERR(vport);
1806 goto exit_unlock_free;
1808 if (a[OVS_VPORT_ATTR_TYPE] &&
1809 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1811 goto exit_unlock_free;
1814 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1815 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1817 goto exit_unlock_free;
1821 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1822 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1824 err = ovs_vport_set_upcall_portids(vport, ids);
1826 goto exit_unlock_free;
1829 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1830 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1834 ovs_notify(&dp_vport_genl_family, reply, info);
1843 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1845 struct nlattr **a = info->attrs;
1846 struct sk_buff *reply;
1847 struct vport *vport;
1850 reply = ovs_vport_cmd_alloc_info();
1855 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1856 err = PTR_ERR(vport);
1858 goto exit_unlock_free;
1860 if (vport->port_no == OVSP_LOCAL) {
1862 goto exit_unlock_free;
1865 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1866 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1868 ovs_dp_detach_port(vport);
1871 ovs_notify(&dp_vport_genl_family, reply, info);
1880 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1882 struct nlattr **a = info->attrs;
1883 struct ovs_header *ovs_header = info->userhdr;
1884 struct sk_buff *reply;
1885 struct vport *vport;
1888 reply = ovs_vport_cmd_alloc_info();
1893 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1894 err = PTR_ERR(vport);
1896 goto exit_unlock_free;
1897 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1898 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1902 return genlmsg_reply(reply, info);
1910 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1912 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1913 struct datapath *dp;
1914 int bucket = cb->args[0], skip = cb->args[1];
1918 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1923 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1924 struct vport *vport;
1927 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1929 ovs_vport_cmd_fill_info(vport, skb,
1930 NETLINK_CB(cb->skb).portid,
1933 OVS_VPORT_CMD_NEW) < 0)
1949 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1950 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1951 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1952 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1953 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1954 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1955 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1958 static const struct genl_ops dp_vport_genl_ops[] = {
1959 { .cmd = OVS_VPORT_CMD_NEW,
1960 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1961 .policy = vport_policy,
1962 .doit = ovs_vport_cmd_new
1964 { .cmd = OVS_VPORT_CMD_DEL,
1965 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1966 .policy = vport_policy,
1967 .doit = ovs_vport_cmd_del
1969 { .cmd = OVS_VPORT_CMD_GET,
1970 .flags = 0, /* OK for unprivileged users. */
1971 .policy = vport_policy,
1972 .doit = ovs_vport_cmd_get,
1973 .dumpit = ovs_vport_cmd_dump
1975 { .cmd = OVS_VPORT_CMD_SET,
1976 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1977 .policy = vport_policy,
1978 .doit = ovs_vport_cmd_set,
1982 struct genl_family dp_vport_genl_family = {
1983 .id = GENL_ID_GENERATE,
1984 .hdrsize = sizeof(struct ovs_header),
1985 .name = OVS_VPORT_FAMILY,
1986 .version = OVS_VPORT_VERSION,
1987 .maxattr = OVS_VPORT_ATTR_MAX,
1989 .parallel_ops = true,
1990 .ops = dp_vport_genl_ops,
1991 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
1992 .mcgrps = &ovs_dp_vport_multicast_group,
1996 static struct genl_family * const dp_genl_families[] = {
1997 &dp_datapath_genl_family,
1998 &dp_vport_genl_family,
1999 &dp_flow_genl_family,
2000 &dp_packet_genl_family,
2003 static void dp_unregister_genl(int n_families)
2007 for (i = 0; i < n_families; i++)
2008 genl_unregister_family(dp_genl_families[i]);
2011 static int dp_register_genl(void)
2016 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2018 err = genl_register_family(dp_genl_families[i]);
2026 dp_unregister_genl(i);
2030 static int __net_init ovs_init_net(struct net *net)
2032 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2034 INIT_LIST_HEAD(&ovs_net->dps);
2035 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2039 static void __net_exit ovs_exit_net(struct net *net)
2041 struct datapath *dp, *dp_next;
2042 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2045 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2049 cancel_work_sync(&ovs_net->dp_notify_work);
2052 static struct pernet_operations ovs_net_ops = {
2053 .init = ovs_init_net,
2054 .exit = ovs_exit_net,
2056 .size = sizeof(struct ovs_net),
2059 static int __init dp_init(void)
2063 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2065 pr_info("Open vSwitch switching datapath\n");
2067 err = ovs_internal_dev_rtnl_link_register();
2071 err = ovs_flow_init();
2073 goto error_unreg_rtnl_link;
2075 err = ovs_vport_init();
2077 goto error_flow_exit;
2079 err = register_pernet_device(&ovs_net_ops);
2081 goto error_vport_exit;
2083 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2085 goto error_netns_exit;
2087 err = dp_register_genl();
2089 goto error_unreg_notifier;
2093 error_unreg_notifier:
2094 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2096 unregister_pernet_device(&ovs_net_ops);
2101 error_unreg_rtnl_link:
2102 ovs_internal_dev_rtnl_link_unregister();
2107 static void dp_cleanup(void)
2109 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2110 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2111 unregister_pernet_device(&ovs_net_ops);
2115 ovs_internal_dev_rtnl_link_unregister();
2118 module_init(dp_init);
2119 module_exit(dp_cleanup);
2121 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2122 MODULE_LICENSE("GPL");