2 * Copyright (c) 2007-2014 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
59 #include "flow_table.h"
60 #include "flow_netlink.h"
62 #include "vport-internal_dev.h"
63 #include "vport-netdev.h"
65 int ovs_net_id __read_mostly;
67 static struct genl_family dp_packet_genl_family;
68 static struct genl_family dp_flow_genl_family;
69 static struct genl_family dp_datapath_genl_family;
71 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
72 .name = OVS_FLOW_MCGROUP
75 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
76 .name = OVS_DATAPATH_MCGROUP
79 struct genl_multicast_group ovs_dp_vport_multicast_group = {
80 .name = OVS_VPORT_MCGROUP
83 /* Check if need to build a reply message.
84 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
85 static bool ovs_must_notify(struct genl_info *info,
86 const struct genl_multicast_group *grp)
88 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
89 netlink_has_listeners(genl_info_net(info)->genl_sock, GROUP_ID(grp));
92 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
93 struct sk_buff *skb, struct genl_info *info)
95 genl_notify(family, skb, genl_info_net(info),
96 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
102 * All writes e.g. Writes to device state (add/remove datapath, port, set
103 * operations on vports, etc.), Writes to other state (flow table
104 * modifications, set miscellaneous datapath parameters, etc.) are protected
107 * Reads are protected by RCU.
109 * There are a few special cases (mostly stats) that have their own
110 * synchronization but they nest under all of above and don't interact with
113 * The RTNL lock nests inside ovs_mutex.
116 static DEFINE_MUTEX(ovs_mutex);
120 mutex_lock(&ovs_mutex);
123 void ovs_unlock(void)
125 mutex_unlock(&ovs_mutex);
128 #ifdef CONFIG_LOCKDEP
129 int lockdep_ovsl_is_held(void)
132 return lockdep_is_held(&ovs_mutex);
138 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
139 const struct dp_upcall_info *);
140 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
141 const struct dp_upcall_info *);
143 /* Must be called with rcu_read_lock. */
144 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
146 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
157 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
158 * returned dp pointer valid. */
159 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
163 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
165 dp = get_dp_rcu(net, dp_ifindex);
171 /* Must be called with rcu_read_lock or ovs_mutex. */
172 const char *ovs_dp_name(const struct datapath *dp)
174 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
175 return vport->ops->get_name(vport);
178 static int get_dpifindex(struct datapath *dp)
185 local = ovs_vport_rcu(dp, OVSP_LOCAL);
187 ifindex = netdev_vport_priv(local)->dev->ifindex;
196 static void destroy_dp_rcu(struct rcu_head *rcu)
198 struct datapath *dp = container_of(rcu, struct datapath, rcu);
200 ovs_flow_tbl_destroy(&dp->table);
201 free_percpu(dp->stats_percpu);
202 release_net(ovs_dp_get_net(dp));
207 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
210 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
213 /* Called with ovs_mutex or RCU read lock. */
214 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
217 struct hlist_head *head;
219 head = vport_hash_bucket(dp, port_no);
220 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
221 if (vport->port_no == port_no)
227 /* Called with ovs_mutex. */
228 static struct vport *new_vport(const struct vport_parms *parms)
232 vport = ovs_vport_add(parms);
233 if (!IS_ERR(vport)) {
234 struct datapath *dp = parms->dp;
235 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
237 hlist_add_head_rcu(&vport->dp_hash_node, head);
242 void ovs_dp_detach_port(struct vport *p)
246 /* First drop references to device. */
247 hlist_del_rcu(&p->dp_hash_node);
249 /* Then destroy it. */
253 /* Must be called with rcu_read_lock. */
254 void ovs_dp_process_packet(struct sk_buff *skb, bool recirc)
256 const struct vport *p = OVS_CB(skb)->input_vport;
257 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
258 struct datapath *dp = p->dp;
259 struct sw_flow *flow;
260 struct dp_stats_percpu *stats;
264 stats = this_cpu_ptr(dp->stats_percpu);
267 flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, skb_get_hash(skb),
269 if (unlikely(!flow)) {
270 struct dp_upcall_info upcall;
272 upcall.cmd = OVS_PACKET_CMD_MISS;
273 upcall.userdata = NULL;
274 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
275 upcall.egress_tun_info = NULL;
276 ovs_dp_upcall(dp, skb, &upcall);
278 stats_counter = &stats->n_missed;
282 OVS_CB(skb)->flow = flow;
284 ovs_flow_stats_update(OVS_CB(skb)->flow, pkt_key->tp.flags, skb);
285 ovs_execute_actions(dp, skb, recirc);
286 stats_counter = &stats->n_hit;
289 /* Update datapath statistics. */
290 u64_stats_update_begin(&stats->sync);
292 stats->n_mask_hit += n_mask_hit;
293 u64_stats_update_end(&stats->sync);
296 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
297 const struct dp_upcall_info *upcall_info)
299 struct dp_stats_percpu *stats;
302 BUG_ON(!OVS_CB(skb)->pkt_key);
304 if (upcall_info->portid == 0) {
309 if (!skb_is_gso(skb))
310 err = queue_userspace_packet(dp, skb, upcall_info);
312 err = queue_gso_packets(dp, skb, upcall_info);
319 stats = this_cpu_ptr(dp->stats_percpu);
321 u64_stats_update_begin(&stats->sync);
323 u64_stats_update_end(&stats->sync);
328 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
329 const struct dp_upcall_info *upcall_info)
331 unsigned short gso_type = skb_shinfo(skb)->gso_type;
332 struct sw_flow_key later_key;
333 struct sk_buff *segs, *nskb;
336 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
338 return PTR_ERR(segs);
340 if (gso_type & SKB_GSO_UDP) {
341 /* The initial flow key extracted by ovs_flow_key_extract()
342 * in this case is for a first fragment, so we need to
343 * properly mark later fragments.
345 later_key = *OVS_CB(skb)->pkt_key;
346 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
349 /* Queue all of the segments. */
352 if (gso_type & SKB_GSO_UDP && skb != segs)
353 OVS_CB(skb)->pkt_key = &later_key;
355 err = queue_userspace_packet(dp, skb, upcall_info);
359 } while ((skb = skb->next));
361 /* Free all of the segments. */
369 } while ((skb = nskb));
373 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
376 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
377 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
378 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
380 /* OVS_PACKET_ATTR_USERDATA */
381 if (upcall_info->userdata)
382 size += NLA_ALIGN(upcall_info->userdata->nla_len);
384 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
385 if (upcall_info->egress_tun_info)
386 size += nla_total_size(ovs_tun_key_attr_size());
391 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
392 const struct dp_upcall_info *upcall_info)
394 struct ovs_header *upcall;
395 struct sk_buff *nskb = NULL;
396 struct sk_buff *user_skb; /* to be queued to userspace */
397 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
399 struct genl_info info = {
400 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
401 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
403 .snd_portid = upcall_info->portid,
409 dp_ifindex = get_dpifindex(dp);
413 if (vlan_tx_tag_present(skb)) {
414 nskb = skb_clone(skb, GFP_ATOMIC);
418 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
422 vlan_set_tci(nskb, 0);
427 if (nla_attr_size(skb->len) > USHRT_MAX) {
432 /* Complete checksum if needed */
433 if (skb->ip_summed == CHECKSUM_PARTIAL &&
434 (err = skb_checksum_help(skb)))
437 /* Older versions of OVS user space enforce alignment of the last
438 * Netlink attribute to NLA_ALIGNTO which would require extensive
439 * padding logic. Only perform zerocopy if padding is not required.
441 if (dp->user_features & OVS_DP_F_UNALIGNED)
442 hlen = skb_zerocopy_headlen(skb);
446 len = upcall_msg_size(upcall_info, hlen);
447 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
453 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
454 0, upcall_info->cmd);
455 upcall->dp_ifindex = dp_ifindex;
457 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
458 err = ovs_nla_put_flow(dp, pkt_key, pkt_key, user_skb);
460 nla_nest_end(user_skb, nla);
462 if (upcall_info->userdata)
463 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
464 nla_len(upcall_info->userdata),
465 nla_data(upcall_info->userdata));
467 if (upcall_info->egress_tun_info) {
468 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
469 err = ovs_nla_put_egress_tunnel_key(user_skb,
470 upcall_info->egress_tun_info);
472 nla_nest_end(user_skb, nla);
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;
514 struct vport *input_vport;
519 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
520 !a[OVS_PACKET_ATTR_ACTIONS])
523 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
524 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
528 skb_reserve(packet, NET_IP_ALIGN);
530 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
532 skb_reset_mac_header(packet);
533 eth = eth_hdr(packet);
535 /* Normally, setting the skb 'protocol' field would be handled by a
536 * call to eth_type_trans(), but it assumes there's a sending
537 * device, which we may not have. */
538 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
539 packet->protocol = eth->h_proto;
541 packet->protocol = htons(ETH_P_802_2);
543 /* Build an sw_flow for sending this packet. */
544 flow = ovs_flow_alloc();
549 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
554 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
559 rcu_assign_pointer(flow->sf_acts, acts);
560 OVS_CB(packet)->flow = flow;
561 OVS_CB(packet)->pkt_key = &flow->key;
562 OVS_CB(skb)->egress_tun_info = NULL;
563 packet->priority = flow->key.phy.priority;
564 packet->mark = flow->key.phy.skb_mark;
567 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
572 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
574 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
579 OVS_CB(packet)->input_vport = input_vport;
582 err = ovs_execute_actions(dp, packet, false);
586 ovs_flow_free(flow, false);
592 ovs_flow_free(flow, false);
599 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
600 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
601 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
602 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
605 static struct genl_ops dp_packet_genl_ops[] = {
606 { .cmd = OVS_PACKET_CMD_EXECUTE,
607 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
608 .policy = packet_policy,
609 .doit = ovs_packet_cmd_execute
613 static struct genl_family dp_packet_genl_family = {
614 .id = GENL_ID_GENERATE,
615 .hdrsize = sizeof(struct ovs_header),
616 .name = OVS_PACKET_FAMILY,
617 .version = OVS_PACKET_VERSION,
618 .maxattr = OVS_PACKET_ATTR_MAX,
620 .parallel_ops = true,
621 .ops = dp_packet_genl_ops,
622 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
625 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
626 struct ovs_dp_megaflow_stats *mega_stats)
630 memset(mega_stats, 0, sizeof(*mega_stats));
632 stats->n_flows = ovs_flow_tbl_count(&dp->table);
633 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
635 stats->n_hit = stats->n_missed = stats->n_lost = 0;
637 for_each_possible_cpu(i) {
638 const struct dp_stats_percpu *percpu_stats;
639 struct dp_stats_percpu local_stats;
642 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
645 start = u64_stats_fetch_begin_irq(&percpu_stats->sync);
646 local_stats = *percpu_stats;
647 } while (u64_stats_fetch_retry_irq(&percpu_stats->sync, start));
649 stats->n_hit += local_stats.n_hit;
650 stats->n_missed += local_stats.n_missed;
651 stats->n_lost += local_stats.n_lost;
652 mega_stats->n_mask_hit += local_stats.n_mask_hit;
656 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
658 return NLMSG_ALIGN(sizeof(struct ovs_header))
659 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
660 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
661 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
662 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
663 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
664 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
667 /* Called with ovs_mutex or RCU read lock. */
668 static int ovs_flow_cmd_fill_match(struct datapath *dp,
669 const struct sw_flow *flow,
676 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
680 err = ovs_nla_put_flow(dp, &flow->unmasked_key,
681 &flow->unmasked_key, skb);
684 nla_nest_end(skb, nla);
686 /* Fill flow mask. */
687 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
691 err = ovs_nla_put_flow(dp, &flow->key, &flow->mask->key, skb);
694 nla_nest_end(skb, nla);
699 /* Called with ovs_mutex or RCU read lock. */
700 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
703 struct ovs_flow_stats stats;
707 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
710 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
713 if (stats.n_packets &&
714 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
717 if ((u8)ntohs(tcp_flags) &&
718 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
724 /* Called with ovs_mutex or RCU read lock. */
725 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
726 struct sk_buff *skb, int skb_orig_len)
728 struct nlattr *start;
731 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
732 * this is the first flow to be dumped into 'skb'. This is unusual for
733 * Netlink but individual action lists can be longer than
734 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
735 * The userspace caller can always fetch the actions separately if it
736 * really wants them. (Most userspace callers in fact don't care.)
738 * This can only fail for dump operations because the skb is always
739 * properly sized for single flows.
741 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
743 const struct sw_flow_actions *sf_acts;
745 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
746 err = ovs_nla_put_actions(sf_acts->actions,
747 sf_acts->actions_len, skb);
750 nla_nest_end(skb, start);
755 nla_nest_cancel(skb, start);
757 } else if (skb_orig_len) {
764 /* Called with ovs_mutex or RCU read lock. */
765 static int ovs_flow_cmd_fill_info(struct datapath *dp,
766 const struct sw_flow *flow, int dp_ifindex,
767 struct sk_buff *skb, u32 portid,
768 u32 seq, u32 flags, u8 cmd)
770 const int skb_orig_len = skb->len;
771 struct ovs_header *ovs_header;
774 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
777 ovs_header->dp_ifindex = dp_ifindex;
779 err = ovs_flow_cmd_fill_match(dp, flow, skb);
783 err = ovs_flow_cmd_fill_stats(flow, skb);
787 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
791 return genlmsg_end(skb, ovs_header);
794 genlmsg_cancel(skb, ovs_header);
798 /* May not be called with RCU read lock. */
799 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
800 struct genl_info *info,
805 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
808 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
811 return ERR_PTR(-ENOMEM);
816 /* Called with ovs_mutex. */
817 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
818 const struct sw_flow *flow,
820 struct genl_info *info, u8 cmd,
826 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
828 if (!skb || IS_ERR(skb))
831 retval = ovs_flow_cmd_fill_info(dp, flow, dp_ifindex, skb,
832 info->snd_portid, info->snd_seq, 0,
838 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
840 struct nlattr **a = info->attrs;
841 struct ovs_header *ovs_header = info->userhdr;
842 struct sw_flow *flow, *new_flow;
843 struct sw_flow_mask mask;
844 struct sk_buff *reply;
846 struct sw_flow_actions *acts;
847 struct sw_flow_match match;
850 /* Must have key and actions. */
852 if (!a[OVS_FLOW_ATTR_KEY]) {
853 OVS_NLERR("Flow key attribute not present in new flow.\n");
856 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
857 OVS_NLERR("Flow actions attribute not present in new flow.\n");
861 /* Most of the time we need to allocate a new flow, do it before
863 new_flow = ovs_flow_alloc();
864 if (IS_ERR(new_flow)) {
865 error = PTR_ERR(new_flow);
870 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
871 error = ovs_nla_get_match(&match,
872 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
876 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
878 /* Validate actions. */
879 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
882 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
886 reply = ovs_flow_cmd_alloc_info(acts, info, false);
888 error = PTR_ERR(reply);
893 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
898 /* Check if this is a duplicate flow */
899 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
901 rcu_assign_pointer(new_flow->sf_acts, acts);
903 /* Put flow in bucket. */
904 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
905 if (unlikely(error)) {
910 if (unlikely(reply)) {
911 error = ovs_flow_cmd_fill_info(dp, new_flow,
912 ovs_header->dp_ifindex,
913 reply, info->snd_portid,
920 struct sw_flow_actions *old_acts;
922 /* Bail out if we're not allowed to modify an existing flow.
923 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
924 * because Generic Netlink treats the latter as a dump
925 * request. We also accept NLM_F_EXCL in case that bug ever
928 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
933 /* The unmasked key has to be the same for flow updates. */
934 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
935 /* Look for any overlapping flow. */
936 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
942 /* Update actions. */
943 old_acts = ovsl_dereference(flow->sf_acts);
944 rcu_assign_pointer(flow->sf_acts, acts);
946 if (unlikely(reply)) {
947 error = ovs_flow_cmd_fill_info(dp, flow,
948 ovs_header->dp_ifindex,
949 reply, info->snd_portid,
956 ovs_nla_free_flow_actions(old_acts);
957 ovs_flow_free(new_flow, false);
961 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
970 ovs_flow_free(new_flow, false);
975 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
976 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
977 const struct sw_flow_key *key,
978 const struct sw_flow_mask *mask)
980 struct sw_flow_actions *acts;
981 struct sw_flow_key masked_key;
984 ovs_flow_mask_key(&masked_key, key, mask);
985 error = ovs_nla_copy_actions(a, &masked_key, &acts);
987 OVS_NLERR("Actions may not be safe on all matching packets.\n");
988 return ERR_PTR(error);
994 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
996 struct nlattr **a = info->attrs;
997 struct ovs_header *ovs_header = info->userhdr;
998 struct sw_flow_key key;
999 struct sw_flow *flow;
1000 struct sw_flow_mask mask;
1001 struct sk_buff *reply = NULL;
1002 struct datapath *dp;
1003 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1004 struct sw_flow_match match;
1009 if (!a[OVS_FLOW_ATTR_KEY]) {
1010 OVS_NLERR("Flow key attribute not present in set flow.\n");
1014 ovs_match_init(&match, &key, &mask);
1015 error = ovs_nla_get_match(&match,
1016 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1020 /* Validate actions. */
1021 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1022 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1024 error = PTR_ERR(acts);
1028 /* Can allocate before locking if have acts. */
1029 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1030 if (IS_ERR(reply)) {
1031 error = PTR_ERR(reply);
1032 goto err_kfree_acts;
1037 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1038 if (unlikely(!dp)) {
1040 goto err_unlock_ovs;
1042 /* Check that the flow exists. */
1043 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1044 if (unlikely(!flow)) {
1046 goto err_unlock_ovs;
1049 /* Update actions, if present. */
1051 old_acts = ovsl_dereference(flow->sf_acts);
1052 rcu_assign_pointer(flow->sf_acts, acts);
1054 if (unlikely(reply)) {
1055 error = ovs_flow_cmd_fill_info(dp, flow,
1056 ovs_header->dp_ifindex,
1057 reply, info->snd_portid,
1063 /* Could not alloc without acts before locking. */
1064 reply = ovs_flow_cmd_build_info(dp, flow,
1065 ovs_header->dp_ifindex,
1066 info, OVS_FLOW_CMD_NEW, false);
1067 if (unlikely(IS_ERR(reply))) {
1068 error = PTR_ERR(reply);
1069 goto err_unlock_ovs;
1074 if (a[OVS_FLOW_ATTR_CLEAR])
1075 ovs_flow_stats_clear(flow);
1079 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1081 ovs_nla_free_flow_actions(old_acts);
1093 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1095 struct nlattr **a = info->attrs;
1096 struct ovs_header *ovs_header = info->userhdr;
1097 struct sw_flow_key key;
1098 struct sk_buff *reply;
1099 struct sw_flow *flow;
1100 struct datapath *dp;
1101 struct sw_flow_match match;
1104 if (!a[OVS_FLOW_ATTR_KEY]) {
1105 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1109 ovs_match_init(&match, &key, NULL);
1110 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1115 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1121 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1127 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1128 OVS_FLOW_CMD_NEW, true);
1129 if (IS_ERR(reply)) {
1130 err = PTR_ERR(reply);
1135 return genlmsg_reply(reply, info);
1141 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1143 struct nlattr **a = info->attrs;
1144 struct ovs_header *ovs_header = info->userhdr;
1145 struct sw_flow_key key;
1146 struct sk_buff *reply;
1147 struct sw_flow *flow;
1148 struct datapath *dp;
1149 struct sw_flow_match match;
1152 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1153 ovs_match_init(&match, &key, NULL);
1154 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1160 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1161 if (unlikely(!dp)) {
1165 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1166 err = ovs_flow_tbl_flush(&dp->table);
1169 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1170 if (unlikely(!flow)) {
1175 ovs_flow_tbl_remove(&dp->table, flow);
1178 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1181 if (likely(reply)) {
1182 if (likely(!IS_ERR(reply))) {
1183 rcu_read_lock(); /* Keep RCU checker happy. */
1184 err = ovs_flow_cmd_fill_info(dp, flow,
1185 ovs_header->dp_ifindex,
1186 reply, info->snd_portid,
1191 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1193 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1194 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1199 ovs_flow_free(flow, true);
1206 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1208 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1209 struct table_instance *ti;
1210 struct datapath *dp;
1213 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1219 ti = rcu_dereference(dp->table.ti);
1221 struct sw_flow *flow;
1224 bucket = cb->args[0];
1226 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1230 if (ovs_flow_cmd_fill_info(dp, flow, ovs_header->dp_ifindex, skb,
1231 NETLINK_CB(cb->skb).portid,
1232 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1233 OVS_FLOW_CMD_NEW) < 0)
1236 cb->args[0] = bucket;
1243 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1244 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1245 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1246 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1249 static struct genl_ops dp_flow_genl_ops[] = {
1250 { .cmd = OVS_FLOW_CMD_NEW,
1251 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1252 .policy = flow_policy,
1253 .doit = ovs_flow_cmd_new
1255 { .cmd = OVS_FLOW_CMD_DEL,
1256 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1257 .policy = flow_policy,
1258 .doit = ovs_flow_cmd_del
1260 { .cmd = OVS_FLOW_CMD_GET,
1261 .flags = 0, /* OK for unprivileged users. */
1262 .policy = flow_policy,
1263 .doit = ovs_flow_cmd_get,
1264 .dumpit = ovs_flow_cmd_dump
1266 { .cmd = OVS_FLOW_CMD_SET,
1267 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1268 .policy = flow_policy,
1269 .doit = ovs_flow_cmd_set,
1273 static struct genl_family dp_flow_genl_family = {
1274 .id = GENL_ID_GENERATE,
1275 .hdrsize = sizeof(struct ovs_header),
1276 .name = OVS_FLOW_FAMILY,
1277 .version = OVS_FLOW_VERSION,
1278 .maxattr = OVS_FLOW_ATTR_MAX,
1280 .parallel_ops = true,
1281 .ops = dp_flow_genl_ops,
1282 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1283 .mcgrps = &ovs_dp_flow_multicast_group,
1287 static size_t ovs_dp_cmd_msg_size(void)
1289 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1291 msgsize += nla_total_size(IFNAMSIZ);
1292 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1293 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1294 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1299 /* Called with ovs_mutex or RCU read lock. */
1300 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1301 u32 portid, u32 seq, u32 flags, u8 cmd)
1303 struct ovs_header *ovs_header;
1304 struct ovs_dp_stats dp_stats;
1305 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1308 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1313 ovs_header->dp_ifindex = get_dpifindex(dp);
1315 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1317 goto nla_put_failure;
1319 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1320 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1322 goto nla_put_failure;
1324 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1325 sizeof(struct ovs_dp_megaflow_stats),
1326 &dp_megaflow_stats))
1327 goto nla_put_failure;
1329 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1330 goto nla_put_failure;
1332 return genlmsg_end(skb, ovs_header);
1335 genlmsg_cancel(skb, ovs_header);
1340 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1342 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1345 /* Called with rcu_read_lock or ovs_mutex. */
1346 static struct datapath *lookup_datapath(struct net *net,
1347 struct ovs_header *ovs_header,
1348 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1350 struct datapath *dp;
1352 if (!a[OVS_DP_ATTR_NAME])
1353 dp = get_dp(net, ovs_header->dp_ifindex);
1355 struct vport *vport;
1357 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1358 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1360 return dp ? dp : ERR_PTR(-ENODEV);
1363 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1365 struct datapath *dp;
1367 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1371 WARN(dp->user_features, "Dropping previously announced user features\n");
1372 dp->user_features = 0;
1375 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1377 if (a[OVS_DP_ATTR_USER_FEATURES])
1378 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1381 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1383 struct nlattr **a = info->attrs;
1384 struct vport_parms parms;
1385 struct sk_buff *reply;
1386 struct datapath *dp;
1387 struct vport *vport;
1388 struct ovs_net *ovs_net;
1392 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1395 reply = ovs_dp_cmd_alloc_info(info);
1400 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1402 goto err_free_reply;
1404 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1406 /* Allocate table. */
1407 err = ovs_flow_tbl_init(&dp->table);
1411 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1412 if (!dp->stats_percpu) {
1414 goto err_destroy_table;
1417 for_each_possible_cpu(i) {
1418 struct dp_stats_percpu *dpath_stats;
1419 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1420 u64_stats_init(&dpath_stats->sync);
1423 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1427 goto err_destroy_percpu;
1430 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1431 INIT_HLIST_HEAD(&dp->ports[i]);
1433 /* Set up our datapath device. */
1434 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1435 parms.type = OVS_VPORT_TYPE_INTERNAL;
1436 parms.options = NULL;
1438 parms.port_no = OVSP_LOCAL;
1439 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1441 ovs_dp_change(dp, a);
1443 /* So far only local changes have been made, now need the lock. */
1446 vport = new_vport(&parms);
1447 if (IS_ERR(vport)) {
1448 err = PTR_ERR(vport);
1452 if (err == -EEXIST) {
1453 /* An outdated user space instance that does not understand
1454 * the concept of user_features has attempted to create a new
1455 * datapath and is likely to reuse it. Drop all user features.
1457 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1458 ovs_dp_reset_user_features(skb, info);
1461 goto err_destroy_ports_array;
1464 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1465 info->snd_seq, 0, OVS_DP_CMD_NEW);
1468 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1469 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1473 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1476 err_destroy_ports_array:
1480 free_percpu(dp->stats_percpu);
1482 ovs_flow_tbl_destroy(&dp->table);
1484 release_net(ovs_dp_get_net(dp));
1492 /* Called with ovs_mutex. */
1493 static void __dp_destroy(struct datapath *dp)
1497 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1498 struct vport *vport;
1499 struct hlist_node *n;
1501 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1502 if (vport->port_no != OVSP_LOCAL)
1503 ovs_dp_detach_port(vport);
1506 list_del_rcu(&dp->list_node);
1508 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1509 * all ports in datapath are destroyed first before freeing datapath.
1511 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1513 /* RCU destroy the flow table */
1514 call_rcu(&dp->rcu, destroy_dp_rcu);
1517 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1519 struct sk_buff *reply;
1520 struct datapath *dp;
1523 reply = ovs_dp_cmd_alloc_info(info);
1528 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1531 goto err_unlock_free;
1533 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1534 info->snd_seq, 0, OVS_DP_CMD_DEL);
1540 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1549 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1551 struct sk_buff *reply;
1552 struct datapath *dp;
1555 reply = ovs_dp_cmd_alloc_info(info);
1560 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1563 goto err_unlock_free;
1565 ovs_dp_change(dp, info->attrs);
1567 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1568 info->snd_seq, 0, OVS_DP_CMD_NEW);
1572 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1581 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1583 struct sk_buff *reply;
1584 struct datapath *dp;
1587 reply = ovs_dp_cmd_alloc_info(info);
1592 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1595 goto err_unlock_free;
1597 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1598 info->snd_seq, 0, OVS_DP_CMD_NEW);
1602 return genlmsg_reply(reply, info);
1610 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1612 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1613 struct datapath *dp;
1614 int skip = cb->args[0];
1618 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1620 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1621 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1622 OVS_DP_CMD_NEW) < 0)
1633 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1634 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1635 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1636 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1639 static struct genl_ops dp_datapath_genl_ops[] = {
1640 { .cmd = OVS_DP_CMD_NEW,
1641 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1642 .policy = datapath_policy,
1643 .doit = ovs_dp_cmd_new
1645 { .cmd = OVS_DP_CMD_DEL,
1646 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1647 .policy = datapath_policy,
1648 .doit = ovs_dp_cmd_del
1650 { .cmd = OVS_DP_CMD_GET,
1651 .flags = 0, /* OK for unprivileged users. */
1652 .policy = datapath_policy,
1653 .doit = ovs_dp_cmd_get,
1654 .dumpit = ovs_dp_cmd_dump
1656 { .cmd = OVS_DP_CMD_SET,
1657 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1658 .policy = datapath_policy,
1659 .doit = ovs_dp_cmd_set,
1663 static struct genl_family dp_datapath_genl_family = {
1664 .id = GENL_ID_GENERATE,
1665 .hdrsize = sizeof(struct ovs_header),
1666 .name = OVS_DATAPATH_FAMILY,
1667 .version = OVS_DATAPATH_VERSION,
1668 .maxattr = OVS_DP_ATTR_MAX,
1670 .parallel_ops = true,
1671 .ops = dp_datapath_genl_ops,
1672 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1673 .mcgrps = &ovs_dp_datapath_multicast_group,
1677 /* Called with ovs_mutex or RCU read lock. */
1678 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1679 u32 portid, u32 seq, u32 flags, u8 cmd)
1681 struct ovs_header *ovs_header;
1682 struct ovs_vport_stats vport_stats;
1685 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1690 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1692 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1693 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1694 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1695 goto nla_put_failure;
1697 ovs_vport_get_stats(vport, &vport_stats);
1698 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1700 goto nla_put_failure;
1702 if (ovs_vport_get_upcall_portids(vport, skb))
1703 goto nla_put_failure;
1705 err = ovs_vport_get_options(vport, skb);
1706 if (err == -EMSGSIZE)
1709 return genlmsg_end(skb, ovs_header);
1714 genlmsg_cancel(skb, ovs_header);
1718 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1720 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1723 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1724 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1727 struct sk_buff *skb;
1730 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1732 return ERR_PTR(-ENOMEM);
1734 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1740 /* Called with ovs_mutex or RCU read lock. */
1741 static struct vport *lookup_vport(struct net *net,
1742 struct ovs_header *ovs_header,
1743 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1745 struct datapath *dp;
1746 struct vport *vport;
1748 if (a[OVS_VPORT_ATTR_NAME]) {
1749 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1751 return ERR_PTR(-ENODEV);
1752 if (ovs_header->dp_ifindex &&
1753 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1754 return ERR_PTR(-ENODEV);
1756 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1757 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1759 if (port_no >= DP_MAX_PORTS)
1760 return ERR_PTR(-EFBIG);
1762 dp = get_dp(net, ovs_header->dp_ifindex);
1764 return ERR_PTR(-ENODEV);
1766 vport = ovs_vport_ovsl_rcu(dp, port_no);
1768 return ERR_PTR(-ENODEV);
1771 return ERR_PTR(-EINVAL);
1774 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1776 struct nlattr **a = info->attrs;
1777 struct ovs_header *ovs_header = info->userhdr;
1778 struct vport_parms parms;
1779 struct sk_buff *reply;
1780 struct vport *vport;
1781 struct datapath *dp;
1785 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1786 !a[OVS_VPORT_ATTR_UPCALL_PID])
1789 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1790 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1791 if (port_no >= DP_MAX_PORTS)
1794 reply = ovs_vport_cmd_alloc_info();
1799 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1802 goto exit_unlock_free;
1805 vport = ovs_vport_ovsl(dp, port_no);
1808 goto exit_unlock_free;
1810 for (port_no = 1; ; port_no++) {
1811 if (port_no >= DP_MAX_PORTS) {
1813 goto exit_unlock_free;
1815 vport = ovs_vport_ovsl(dp, port_no);
1821 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1822 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1823 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1825 parms.port_no = port_no;
1826 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1828 vport = new_vport(&parms);
1829 err = PTR_ERR(vport);
1831 goto exit_unlock_free;
1834 if (a[OVS_VPORT_ATTR_STATS])
1835 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1837 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1838 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1842 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1851 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1853 struct nlattr **a = info->attrs;
1854 struct sk_buff *reply;
1855 struct vport *vport;
1858 reply = ovs_vport_cmd_alloc_info();
1863 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1864 err = PTR_ERR(vport);
1866 goto exit_unlock_free;
1868 if (a[OVS_VPORT_ATTR_TYPE] &&
1869 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1871 goto exit_unlock_free;
1874 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1875 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1877 goto exit_unlock_free;
1880 if (a[OVS_VPORT_ATTR_STATS])
1881 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1884 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1885 err = ovs_vport_set_upcall_portids(vport,
1886 a[OVS_VPORT_ATTR_UPCALL_PID]);
1888 goto exit_unlock_free;
1891 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1892 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1896 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1905 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1907 struct nlattr **a = info->attrs;
1908 struct sk_buff *reply;
1909 struct vport *vport;
1912 reply = ovs_vport_cmd_alloc_info();
1917 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1918 err = PTR_ERR(vport);
1920 goto exit_unlock_free;
1922 if (vport->port_no == OVSP_LOCAL) {
1924 goto exit_unlock_free;
1927 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1928 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1930 ovs_dp_detach_port(vport);
1933 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1942 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1944 struct nlattr **a = info->attrs;
1945 struct ovs_header *ovs_header = info->userhdr;
1946 struct sk_buff *reply;
1947 struct vport *vport;
1950 reply = ovs_vport_cmd_alloc_info();
1955 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1956 err = PTR_ERR(vport);
1958 goto exit_unlock_free;
1959 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1960 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1964 return genlmsg_reply(reply, info);
1972 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1974 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1975 struct datapath *dp;
1976 int bucket = cb->args[0], skip = cb->args[1];
1980 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1985 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1986 struct vport *vport;
1989 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1991 ovs_vport_cmd_fill_info(vport, skb,
1992 NETLINK_CB(cb->skb).portid,
1995 OVS_VPORT_CMD_NEW) < 0)
2011 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2012 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2013 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2014 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2015 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2016 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2017 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2020 static struct genl_ops dp_vport_genl_ops[] = {
2021 { .cmd = OVS_VPORT_CMD_NEW,
2022 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2023 .policy = vport_policy,
2024 .doit = ovs_vport_cmd_new
2026 { .cmd = OVS_VPORT_CMD_DEL,
2027 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2028 .policy = vport_policy,
2029 .doit = ovs_vport_cmd_del
2031 { .cmd = OVS_VPORT_CMD_GET,
2032 .flags = 0, /* OK for unprivileged users. */
2033 .policy = vport_policy,
2034 .doit = ovs_vport_cmd_get,
2035 .dumpit = ovs_vport_cmd_dump
2037 { .cmd = OVS_VPORT_CMD_SET,
2038 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2039 .policy = vport_policy,
2040 .doit = ovs_vport_cmd_set,
2044 struct genl_family dp_vport_genl_family = {
2045 .id = GENL_ID_GENERATE,
2046 .hdrsize = sizeof(struct ovs_header),
2047 .name = OVS_VPORT_FAMILY,
2048 .version = OVS_VPORT_VERSION,
2049 .maxattr = OVS_VPORT_ATTR_MAX,
2051 .parallel_ops = true,
2052 .ops = dp_vport_genl_ops,
2053 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2054 .mcgrps = &ovs_dp_vport_multicast_group,
2058 static struct genl_family *dp_genl_families[] = {
2059 &dp_datapath_genl_family,
2060 &dp_vport_genl_family,
2061 &dp_flow_genl_family,
2062 &dp_packet_genl_family,
2065 static void dp_unregister_genl(int n_families)
2069 for (i = 0; i < n_families; i++)
2070 genl_unregister_family(dp_genl_families[i]);
2073 static int dp_register_genl(void)
2078 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2080 err = genl_register_family(dp_genl_families[i]);
2088 dp_unregister_genl(i);
2092 static int __net_init ovs_init_net(struct net *net)
2094 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2096 INIT_LIST_HEAD(&ovs_net->dps);
2097 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2101 static void __net_exit ovs_exit_net(struct net *net)
2103 struct datapath *dp, *dp_next;
2104 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2107 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2111 cancel_work_sync(&ovs_net->dp_notify_work);
2114 static struct pernet_operations ovs_net_ops = {
2115 .init = ovs_init_net,
2116 .exit = ovs_exit_net,
2118 .size = sizeof(struct ovs_net),
2121 DEFINE_COMPAT_PNET_REG_FUNC(device);
2123 static int __init dp_init(void)
2127 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2129 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2132 err = ovs_flow_init();
2136 err = ovs_vport_init();
2138 goto error_flow_exit;
2140 err = register_pernet_device(&ovs_net_ops);
2142 goto error_vport_exit;
2144 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2146 goto error_netns_exit;
2148 err = dp_register_genl();
2150 goto error_unreg_notifier;
2154 error_unreg_notifier:
2155 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2157 unregister_pernet_device(&ovs_net_ops);
2166 static void dp_cleanup(void)
2168 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2169 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2170 unregister_pernet_device(&ovs_net_ops);
2176 module_init(dp_init);
2177 module_exit(dp_cleanup);
2179 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2180 MODULE_LICENSE("GPL");
2181 MODULE_VERSION(VERSION);