2 * Copyright (c) 2007-2015 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 <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
56 #include "conntrack.h"
58 #include "flow_table.h"
59 #include "flow_netlink.h"
62 #include "vport-internal_dev.h"
63 #include "vport-netdev.h"
65 int ovs_net_id __read_mostly;
66 EXPORT_SYMBOL_GPL(ovs_net_id);
68 static struct genl_family dp_packet_genl_family;
69 static struct genl_family dp_flow_genl_family;
70 static struct genl_family dp_datapath_genl_family;
72 static const struct nla_policy flow_policy[];
74 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
75 .name = OVS_FLOW_MCGROUP
78 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
79 .name = OVS_DATAPATH_MCGROUP
82 struct genl_multicast_group ovs_dp_vport_multicast_group = {
83 .name = OVS_VPORT_MCGROUP
86 /* Check if need to build a reply message.
87 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply.
89 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
92 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
93 genl_has_listeners(family, genl_info_net(info), group);
96 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
97 struct sk_buff *skb, struct genl_info *info)
99 genl_notify(family, skb, genl_info_net(info),
100 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
106 * All writes e.g. Writes to device state (add/remove datapath, port, set
107 * operations on vports, etc.), Writes to other state (flow table
108 * modifications, set miscellaneous datapath parameters, etc.) are protected
111 * Reads are protected by RCU.
113 * There are a few special cases (mostly stats) that have their own
114 * synchronization but they nest under all of above and don't interact with
117 * The RTNL lock nests inside ovs_mutex.
120 static DEFINE_MUTEX(ovs_mutex);
124 mutex_lock(&ovs_mutex);
127 void ovs_unlock(void)
129 mutex_unlock(&ovs_mutex);
132 #ifdef CONFIG_LOCKDEP
133 int lockdep_ovsl_is_held(void)
136 return lockdep_is_held(&ovs_mutex);
140 EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
143 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
144 const struct sw_flow_key *,
145 const struct dp_upcall_info *);
146 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
147 const struct sw_flow_key *,
148 const struct dp_upcall_info *);
150 /* Must be called with rcu_read_lock. */
151 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
153 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
156 struct vport *vport = ovs_internal_dev_get_vport(dev);
164 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
165 * returned dp pointer valid.
167 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
171 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
173 dp = get_dp_rcu(net, dp_ifindex);
179 /* Must be called with rcu_read_lock or ovs_mutex. */
180 const char *ovs_dp_name(const struct datapath *dp)
182 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
183 return ovs_vport_name(vport);
186 static int get_dpifindex(const struct datapath *dp)
193 local = ovs_vport_rcu(dp, OVSP_LOCAL);
195 ifindex = local->dev->ifindex;
204 static void destroy_dp_rcu(struct rcu_head *rcu)
206 struct datapath *dp = container_of(rcu, struct datapath, rcu);
208 ovs_flow_tbl_destroy(&dp->table);
209 free_percpu(dp->stats_percpu);
214 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
217 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
220 /* Called with ovs_mutex or RCU read lock. */
221 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
224 struct hlist_head *head;
226 head = vport_hash_bucket(dp, port_no);
227 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
228 if (vport->port_no == port_no)
234 /* Called with ovs_mutex. */
235 static struct vport *new_vport(const struct vport_parms *parms)
239 vport = ovs_vport_add(parms);
240 if (!IS_ERR(vport)) {
241 struct datapath *dp = parms->dp;
242 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
244 hlist_add_head_rcu(&vport->dp_hash_node, head);
249 void ovs_dp_detach_port(struct vport *p)
253 /* First drop references to device. */
254 hlist_del_rcu(&p->dp_hash_node);
256 /* Then destroy it. */
260 /* Must be called with rcu_read_lock. */
261 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
263 const struct vport *p = OVS_CB(skb)->input_vport;
264 struct datapath *dp = p->dp;
265 struct sw_flow *flow;
266 struct sw_flow_actions *sf_acts;
267 struct dp_stats_percpu *stats;
271 stats = this_cpu_ptr(dp->stats_percpu);
274 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
276 if (unlikely(!flow)) {
277 struct dp_upcall_info upcall;
280 memset(&upcall, 0, sizeof(upcall));
281 upcall.cmd = OVS_PACKET_CMD_MISS;
282 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
283 upcall.mru = OVS_CB(skb)->mru;
284 error = ovs_dp_upcall(dp, skb, key, &upcall);
289 stats_counter = &stats->n_missed;
293 ovs_flow_stats_update(flow, key->tp.flags, skb);
294 sf_acts = rcu_dereference(flow->sf_acts);
295 ovs_execute_actions(dp, skb, sf_acts, key);
297 stats_counter = &stats->n_hit;
300 /* Update datapath statistics. */
301 u64_stats_update_begin(&stats->syncp);
303 stats->n_mask_hit += n_mask_hit;
304 u64_stats_update_end(&stats->syncp);
307 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
308 const struct sw_flow_key *key,
309 const struct dp_upcall_info *upcall_info)
311 struct dp_stats_percpu *stats;
314 if (upcall_info->portid == 0) {
319 if (!skb_is_gso(skb))
320 err = queue_userspace_packet(dp, skb, key, upcall_info);
322 err = queue_gso_packets(dp, skb, key, upcall_info);
329 stats = this_cpu_ptr(dp->stats_percpu);
331 u64_stats_update_begin(&stats->syncp);
333 u64_stats_update_end(&stats->syncp);
338 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
339 const struct sw_flow_key *key,
340 const struct dp_upcall_info *upcall_info)
342 unsigned short gso_type = skb_shinfo(skb)->gso_type;
343 struct sw_flow_key later_key;
344 struct sk_buff *segs, *nskb;
345 struct ovs_skb_cb ovs_cb;
348 ovs_cb = *OVS_CB(skb);
349 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
350 *OVS_CB(skb) = ovs_cb;
352 return PTR_ERR(segs);
356 if (gso_type & SKB_GSO_UDP) {
357 /* The initial flow key extracted by ovs_flow_key_extract()
358 * in this case is for a first fragment, so we need to
359 * properly mark later fragments.
362 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
365 /* Queue all of the segments. */
368 *OVS_CB(skb) = ovs_cb;
369 if (gso_type & SKB_GSO_UDP && skb != segs)
372 err = queue_userspace_packet(dp, skb, key, upcall_info);
376 } while ((skb = skb->next));
378 /* Free all of the segments. */
386 } while ((skb = nskb));
390 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
393 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
394 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
395 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
397 /* OVS_PACKET_ATTR_USERDATA */
398 if (upcall_info->userdata)
399 size += NLA_ALIGN(upcall_info->userdata->nla_len);
401 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
402 if (upcall_info->egress_tun_info)
403 size += nla_total_size(ovs_tun_key_attr_size());
405 /* OVS_PACKET_ATTR_ACTIONS */
406 if (upcall_info->actions_len)
407 size += nla_total_size(upcall_info->actions_len);
409 /* OVS_PACKET_ATTR_MRU */
410 if (upcall_info->mru)
411 size += nla_total_size(sizeof(upcall_info->mru));
416 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
418 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
419 size_t plen = NLA_ALIGN(skb->len) - skb->len;
422 memset(skb_put(skb, plen), 0, plen);
426 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
427 const struct sw_flow_key *key,
428 const struct dp_upcall_info *upcall_info)
430 struct ovs_header *upcall;
431 struct sk_buff *nskb = NULL;
432 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
434 struct genl_info info = {
435 #ifdef HAVE_GENLMSG_NEW_UNICAST
436 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
438 .snd_portid = upcall_info->portid,
444 dp_ifindex = get_dpifindex(dp);
448 if (skb_vlan_tag_present(skb)) {
449 nskb = skb_clone(skb, GFP_ATOMIC);
453 nskb = vlan_insert_tag_set_proto(nskb, nskb->vlan_proto, skb_vlan_tag_get(nskb));
457 vlan_set_tci(nskb, 0);
462 if (nla_attr_size(skb->len) > USHRT_MAX) {
467 /* Complete checksum if needed */
468 if (skb->ip_summed == CHECKSUM_PARTIAL &&
469 (err = skb_checksum_help(skb)))
472 /* Older versions of OVS user space enforce alignment of the last
473 * Netlink attribute to NLA_ALIGNTO which would require extensive
474 * padding logic. Only perform zerocopy if padding is not required.
476 if (dp->user_features & OVS_DP_F_UNALIGNED)
477 hlen = skb_zerocopy_headlen(skb);
481 len = upcall_msg_size(upcall_info, hlen);
482 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
488 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
489 0, upcall_info->cmd);
490 upcall->dp_ifindex = dp_ifindex;
492 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
495 if (upcall_info->userdata)
496 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
497 nla_len(upcall_info->userdata),
498 nla_data(upcall_info->userdata));
501 if (upcall_info->egress_tun_info) {
502 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
503 err = ovs_nla_put_egress_tunnel_key(user_skb,
504 upcall_info->egress_tun_info,
505 upcall_info->egress_tun_opts);
507 nla_nest_end(user_skb, nla);
510 if (upcall_info->actions_len) {
511 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
512 err = ovs_nla_put_actions(upcall_info->actions,
513 upcall_info->actions_len,
516 nla_nest_end(user_skb, nla);
518 nla_nest_cancel(user_skb, nla);
521 /* Add OVS_PACKET_ATTR_MRU */
522 if (upcall_info->mru) {
523 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
528 pad_packet(dp, user_skb);
531 /* Only reserve room for attribute header, packet data is added
534 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
538 nla->nla_len = nla_attr_size(skb->len);
540 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
544 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
545 pad_packet(dp, user_skb);
547 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
549 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
559 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
561 struct ovs_header *ovs_header = info->userhdr;
562 struct net *net = sock_net(skb->sk);
563 struct nlattr **a = info->attrs;
564 struct sw_flow_actions *acts;
565 struct sk_buff *packet;
566 struct sw_flow *flow;
567 struct sw_flow_actions *sf_acts;
570 struct vport *input_vport;
574 bool log = !a[OVS_PACKET_ATTR_PROBE];
577 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
578 !a[OVS_PACKET_ATTR_ACTIONS])
581 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
582 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
586 skb_reserve(packet, NET_IP_ALIGN);
588 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
590 skb_reset_mac_header(packet);
591 eth = eth_hdr(packet);
593 /* Normally, setting the skb 'protocol' field would be handled by a
594 * call to eth_type_trans(), but it assumes there's a sending
595 * device, which we may not have.
597 if (eth_proto_is_802_3(eth->h_proto))
598 packet->protocol = eth->h_proto;
600 packet->protocol = htons(ETH_P_802_2);
602 /* Set packet's mru */
603 if (a[OVS_PACKET_ATTR_MRU]) {
604 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
605 packet->ignore_df = 1;
607 OVS_CB(packet)->mru = mru;
609 /* Build an sw_flow for sending this packet. */
610 flow = ovs_flow_alloc();
615 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
616 packet, &flow->key, log);
620 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
621 &flow->key, &acts, log);
625 rcu_assign_pointer(flow->sf_acts, acts);
626 packet->priority = flow->key.phy.priority;
627 packet->mark = flow->key.phy.skb_mark;
630 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
635 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
637 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
642 packet->dev = input_vport->dev;
643 OVS_CB(packet)->input_vport = input_vport;
644 sf_acts = rcu_dereference(flow->sf_acts);
647 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
651 ovs_flow_free(flow, false);
657 ovs_flow_free(flow, false);
664 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
665 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
666 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
667 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
668 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
669 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
672 static struct genl_ops dp_packet_genl_ops[] = {
673 { .cmd = OVS_PACKET_CMD_EXECUTE,
674 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
675 .policy = packet_policy,
676 .doit = ovs_packet_cmd_execute
680 static struct genl_family dp_packet_genl_family = {
681 .id = GENL_ID_GENERATE,
682 .hdrsize = sizeof(struct ovs_header),
683 .name = OVS_PACKET_FAMILY,
684 .version = OVS_PACKET_VERSION,
685 .maxattr = OVS_PACKET_ATTR_MAX,
687 .parallel_ops = true,
688 .ops = dp_packet_genl_ops,
689 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
692 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
693 struct ovs_dp_megaflow_stats *mega_stats)
697 memset(mega_stats, 0, sizeof(*mega_stats));
699 stats->n_flows = ovs_flow_tbl_count(&dp->table);
700 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
702 stats->n_hit = stats->n_missed = stats->n_lost = 0;
704 for_each_possible_cpu(i) {
705 const struct dp_stats_percpu *percpu_stats;
706 struct dp_stats_percpu local_stats;
709 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
712 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
713 local_stats = *percpu_stats;
714 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
716 stats->n_hit += local_stats.n_hit;
717 stats->n_missed += local_stats.n_missed;
718 stats->n_lost += local_stats.n_lost;
719 mega_stats->n_mask_hit += local_stats.n_mask_hit;
723 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
725 return ovs_identifier_is_ufid(sfid) &&
726 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
729 static bool should_fill_mask(uint32_t ufid_flags)
731 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
734 static bool should_fill_actions(uint32_t ufid_flags)
736 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
739 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
740 const struct sw_flow_id *sfid,
743 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
745 /* OVS_FLOW_ATTR_UFID */
746 if (sfid && ovs_identifier_is_ufid(sfid))
747 len += nla_total_size(sfid->ufid_len);
749 /* OVS_FLOW_ATTR_KEY */
750 if (!sfid || should_fill_key(sfid, ufid_flags))
751 len += nla_total_size(ovs_key_attr_size());
753 /* OVS_FLOW_ATTR_MASK */
754 if (should_fill_mask(ufid_flags))
755 len += nla_total_size(ovs_key_attr_size());
757 /* OVS_FLOW_ATTR_ACTIONS */
758 if (should_fill_actions(ufid_flags))
759 len += nla_total_size(acts->orig_len);
762 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
763 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
764 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
767 /* Called with ovs_mutex or RCU read lock. */
768 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
771 struct ovs_flow_stats stats;
775 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
778 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
781 if (stats.n_packets &&
782 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
785 if ((u8)ntohs(tcp_flags) &&
786 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
792 /* Called with ovs_mutex or RCU read lock. */
793 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
794 struct sk_buff *skb, int skb_orig_len)
796 struct nlattr *start;
799 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
800 * this is the first flow to be dumped into 'skb'. This is unusual for
801 * Netlink but individual action lists can be longer than
802 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
803 * The userspace caller can always fetch the actions separately if it
804 * really wants them. (Most userspace callers in fact don't care.)
806 * This can only fail for dump operations because the skb is always
807 * properly sized for single flows.
809 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
811 const struct sw_flow_actions *sf_acts;
813 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
814 err = ovs_nla_put_actions(sf_acts->actions,
815 sf_acts->actions_len, skb);
818 nla_nest_end(skb, start);
823 nla_nest_cancel(skb, start);
825 } else if (skb_orig_len) {
832 /* Called with ovs_mutex or RCU read lock. */
833 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
834 struct sk_buff *skb, u32 portid,
835 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
837 const int skb_orig_len = skb->len;
838 struct ovs_header *ovs_header;
841 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
846 ovs_header->dp_ifindex = dp_ifindex;
848 err = ovs_nla_put_identifier(flow, skb);
852 if (should_fill_key(&flow->id, ufid_flags)) {
853 err = ovs_nla_put_masked_key(flow, skb);
858 if (should_fill_mask(ufid_flags)) {
859 err = ovs_nla_put_mask(flow, skb);
864 err = ovs_flow_cmd_fill_stats(flow, skb);
868 if (should_fill_actions(ufid_flags)) {
869 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
874 genlmsg_end(skb, ovs_header);
878 genlmsg_cancel(skb, ovs_header);
882 /* May not be called with RCU read lock. */
883 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
884 const struct sw_flow_id *sfid,
885 struct genl_info *info,
892 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
893 GROUP_ID(&ovs_dp_flow_multicast_group)))
896 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
897 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
899 return ERR_PTR(-ENOMEM);
904 /* Called with ovs_mutex. */
905 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
907 struct genl_info *info, u8 cmd,
908 bool always, u32 ufid_flags)
913 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
914 &flow->id, info, always, ufid_flags);
915 if (IS_ERR_OR_NULL(skb))
918 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
919 info->snd_portid, info->snd_seq, 0,
925 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
927 struct net *net = sock_net(skb->sk);
928 struct nlattr **a = info->attrs;
929 struct ovs_header *ovs_header = info->userhdr;
930 struct sw_flow *flow = NULL, *new_flow;
931 struct sw_flow_mask mask;
932 struct sk_buff *reply;
934 struct sw_flow_key key;
935 struct sw_flow_actions *acts;
936 struct sw_flow_match match;
937 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
939 bool log = !a[OVS_FLOW_ATTR_PROBE];
941 /* Must have key and actions. */
943 if (!a[OVS_FLOW_ATTR_KEY]) {
944 OVS_NLERR(log, "Flow key attr not present in new flow.");
947 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
948 OVS_NLERR(log, "Flow actions attr not present in new flow.");
952 /* Most of the time we need to allocate a new flow, do it before
955 new_flow = ovs_flow_alloc();
956 if (IS_ERR(new_flow)) {
957 error = PTR_ERR(new_flow);
962 ovs_match_init(&match, &key, &mask);
963 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
964 a[OVS_FLOW_ATTR_MASK], log);
968 ovs_flow_mask_key(&new_flow->key, &key, true, &mask);
970 /* Extract flow identifier. */
971 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
976 /* Validate actions. */
977 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
978 &new_flow->key, &acts, log);
980 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
984 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
987 error = PTR_ERR(reply);
992 dp = get_dp(net, ovs_header->dp_ifindex);
998 /* Check if this is a duplicate flow */
999 if (ovs_identifier_is_ufid(&new_flow->id))
1000 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
1002 flow = ovs_flow_tbl_lookup(&dp->table, &key);
1003 if (likely(!flow)) {
1004 rcu_assign_pointer(new_flow->sf_acts, acts);
1006 /* Put flow in bucket. */
1007 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1008 if (unlikely(error)) {
1010 goto err_unlock_ovs;
1013 if (unlikely(reply)) {
1014 error = ovs_flow_cmd_fill_info(new_flow,
1015 ovs_header->dp_ifindex,
1016 reply, info->snd_portid,
1024 struct sw_flow_actions *old_acts;
1026 /* Bail out if we're not allowed to modify an existing flow.
1027 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1028 * because Generic Netlink treats the latter as a dump
1029 * request. We also accept NLM_F_EXCL in case that bug ever
1032 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1035 goto err_unlock_ovs;
1037 /* The flow identifier has to be the same for flow updates.
1038 * Look for any overlapping flow.
1040 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1041 if (ovs_identifier_is_key(&flow->id))
1042 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1044 else /* UFID matches but key is different */
1048 goto err_unlock_ovs;
1051 /* Update actions. */
1052 old_acts = ovsl_dereference(flow->sf_acts);
1053 rcu_assign_pointer(flow->sf_acts, acts);
1055 if (unlikely(reply)) {
1056 error = ovs_flow_cmd_fill_info(flow,
1057 ovs_header->dp_ifindex,
1058 reply, info->snd_portid,
1066 ovs_nla_free_flow_actions_rcu(old_acts);
1067 ovs_flow_free(new_flow, false);
1071 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1078 ovs_nla_free_flow_actions(acts);
1080 ovs_flow_free(new_flow, false);
1085 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1086 static struct sw_flow_actions *get_flow_actions(struct net *net,
1087 const struct nlattr *a,
1088 const struct sw_flow_key *key,
1089 const struct sw_flow_mask *mask,
1092 struct sw_flow_actions *acts;
1093 struct sw_flow_key masked_key;
1096 ovs_flow_mask_key(&masked_key, key, true, mask);
1097 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1100 "Actions may not be safe on all matching packets");
1101 return ERR_PTR(error);
1107 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1109 struct net *net = sock_net(skb->sk);
1110 struct nlattr **a = info->attrs;
1111 struct ovs_header *ovs_header = info->userhdr;
1112 struct sw_flow_key key;
1113 struct sw_flow *flow;
1114 struct sw_flow_mask mask;
1115 struct sk_buff *reply = NULL;
1116 struct datapath *dp;
1117 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1118 struct sw_flow_match match;
1119 struct sw_flow_id sfid;
1120 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1122 bool log = !a[OVS_FLOW_ATTR_PROBE];
1127 if (!a[OVS_FLOW_ATTR_KEY]) {
1128 OVS_NLERR(log, "Flow key attribute not present in set flow.");
1132 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1133 ovs_match_init(&match, &key, &mask);
1134 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1135 a[OVS_FLOW_ATTR_MASK], log);
1139 /* Validate actions. */
1140 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1141 acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], &key,
1144 error = PTR_ERR(acts);
1148 /* Can allocate before locking if have acts. */
1149 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1151 if (IS_ERR(reply)) {
1152 error = PTR_ERR(reply);
1153 goto err_kfree_acts;
1158 dp = get_dp(net, ovs_header->dp_ifindex);
1159 if (unlikely(!dp)) {
1161 goto err_unlock_ovs;
1163 /* Check that the flow exists. */
1165 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1167 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1168 if (unlikely(!flow)) {
1170 goto err_unlock_ovs;
1173 /* Update actions, if present. */
1175 old_acts = ovsl_dereference(flow->sf_acts);
1176 rcu_assign_pointer(flow->sf_acts, acts);
1178 if (unlikely(reply)) {
1179 error = ovs_flow_cmd_fill_info(flow,
1180 ovs_header->dp_ifindex,
1181 reply, info->snd_portid,
1188 /* Could not alloc without acts before locking. */
1189 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1190 info, OVS_FLOW_CMD_NEW, false,
1193 if (unlikely(IS_ERR(reply))) {
1194 error = PTR_ERR(reply);
1195 goto err_unlock_ovs;
1200 if (a[OVS_FLOW_ATTR_CLEAR])
1201 ovs_flow_stats_clear(flow);
1205 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1207 ovs_nla_free_flow_actions_rcu(old_acts);
1215 ovs_nla_free_flow_actions(acts);
1220 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1222 struct nlattr **a = info->attrs;
1223 struct ovs_header *ovs_header = info->userhdr;
1224 struct net *net = sock_net(skb->sk);
1225 struct sw_flow_key key;
1226 struct sk_buff *reply;
1227 struct sw_flow *flow;
1228 struct datapath *dp;
1229 struct sw_flow_match match;
1230 struct sw_flow_id ufid;
1231 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1233 bool log = !a[OVS_FLOW_ATTR_PROBE];
1236 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1237 if (a[OVS_FLOW_ATTR_KEY]) {
1238 ovs_match_init(&match, &key, NULL);
1239 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1241 } else if (!ufid_present) {
1243 "Flow get message rejected, Key attribute missing.");
1250 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1257 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1259 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1265 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1266 OVS_FLOW_CMD_NEW, true, ufid_flags);
1267 if (IS_ERR(reply)) {
1268 err = PTR_ERR(reply);
1273 return genlmsg_reply(reply, info);
1279 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1281 struct nlattr **a = info->attrs;
1282 struct ovs_header *ovs_header = info->userhdr;
1283 struct net *net = sock_net(skb->sk);
1284 struct sw_flow_key key;
1285 struct sk_buff *reply;
1286 struct sw_flow *flow = NULL;
1287 struct datapath *dp;
1288 struct sw_flow_match match;
1289 struct sw_flow_id ufid;
1290 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1292 bool log = !a[OVS_FLOW_ATTR_PROBE];
1295 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1296 if (a[OVS_FLOW_ATTR_KEY]) {
1297 ovs_match_init(&match, &key, NULL);
1298 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1305 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1306 if (unlikely(!dp)) {
1311 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1312 err = ovs_flow_tbl_flush(&dp->table);
1317 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1319 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1320 if (unlikely(!flow)) {
1325 ovs_flow_tbl_remove(&dp->table, flow);
1328 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1329 &flow->id, info, false, ufid_flags);
1331 if (likely(reply)) {
1332 if (likely(!IS_ERR(reply))) {
1333 rcu_read_lock(); /*To keep RCU checker happy. */
1334 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1335 reply, info->snd_portid,
1341 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1343 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1344 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1349 ovs_flow_free(flow, true);
1356 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1358 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1359 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1360 struct table_instance *ti;
1361 struct datapath *dp;
1365 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1366 OVS_FLOW_ATTR_MAX, flow_policy);
1369 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1372 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1378 ti = rcu_dereference(dp->table.ti);
1380 struct sw_flow *flow;
1383 bucket = cb->args[0];
1385 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1389 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1390 NETLINK_CB(cb->skb).portid,
1391 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1392 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1395 cb->args[0] = bucket;
1402 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1403 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1404 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1405 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1406 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1407 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1408 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1409 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1412 static struct genl_ops dp_flow_genl_ops[] = {
1413 { .cmd = OVS_FLOW_CMD_NEW,
1414 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1415 .policy = flow_policy,
1416 .doit = ovs_flow_cmd_new
1418 { .cmd = OVS_FLOW_CMD_DEL,
1419 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1420 .policy = flow_policy,
1421 .doit = ovs_flow_cmd_del
1423 { .cmd = OVS_FLOW_CMD_GET,
1424 .flags = 0, /* OK for unprivileged users. */
1425 .policy = flow_policy,
1426 .doit = ovs_flow_cmd_get,
1427 .dumpit = ovs_flow_cmd_dump
1429 { .cmd = OVS_FLOW_CMD_SET,
1430 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1431 .policy = flow_policy,
1432 .doit = ovs_flow_cmd_set,
1436 static struct genl_family dp_flow_genl_family = {
1437 .id = GENL_ID_GENERATE,
1438 .hdrsize = sizeof(struct ovs_header),
1439 .name = OVS_FLOW_FAMILY,
1440 .version = OVS_FLOW_VERSION,
1441 .maxattr = OVS_FLOW_ATTR_MAX,
1443 .parallel_ops = true,
1444 .ops = dp_flow_genl_ops,
1445 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1446 .mcgrps = &ovs_dp_flow_multicast_group,
1450 static size_t ovs_dp_cmd_msg_size(void)
1452 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1454 msgsize += nla_total_size(IFNAMSIZ);
1455 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1456 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1457 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1462 /* Called with ovs_mutex. */
1463 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1464 u32 portid, u32 seq, u32 flags, u8 cmd)
1466 struct ovs_header *ovs_header;
1467 struct ovs_dp_stats dp_stats;
1468 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1471 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1476 ovs_header->dp_ifindex = get_dpifindex(dp);
1478 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1480 goto nla_put_failure;
1482 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1483 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1485 goto nla_put_failure;
1487 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1488 sizeof(struct ovs_dp_megaflow_stats),
1489 &dp_megaflow_stats))
1490 goto nla_put_failure;
1492 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1493 goto nla_put_failure;
1495 genlmsg_end(skb, ovs_header);
1499 genlmsg_cancel(skb, ovs_header);
1504 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1506 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1509 /* Called with rcu_read_lock or ovs_mutex. */
1510 static struct datapath *lookup_datapath(struct net *net,
1511 const struct ovs_header *ovs_header,
1512 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1514 struct datapath *dp;
1516 if (!a[OVS_DP_ATTR_NAME])
1517 dp = get_dp(net, ovs_header->dp_ifindex);
1519 struct vport *vport;
1521 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1522 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1524 return dp ? dp : ERR_PTR(-ENODEV);
1527 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1529 struct datapath *dp;
1531 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1535 WARN(dp->user_features, "Dropping previously announced user features\n");
1536 dp->user_features = 0;
1539 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1541 if (a[OVS_DP_ATTR_USER_FEATURES])
1542 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1545 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1547 struct nlattr **a = info->attrs;
1548 struct vport_parms parms;
1549 struct sk_buff *reply;
1550 struct datapath *dp;
1551 struct vport *vport;
1552 struct ovs_net *ovs_net;
1556 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1559 reply = ovs_dp_cmd_alloc_info(info);
1564 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1566 goto err_free_reply;
1568 ovs_dp_set_net(dp, sock_net(skb->sk));
1570 /* Allocate table. */
1571 err = ovs_flow_tbl_init(&dp->table);
1575 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1576 if (!dp->stats_percpu) {
1578 goto err_destroy_table;
1581 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1585 goto err_destroy_percpu;
1588 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1589 INIT_HLIST_HEAD(&dp->ports[i]);
1591 /* Set up our datapath device. */
1592 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1593 parms.type = OVS_VPORT_TYPE_INTERNAL;
1594 parms.options = NULL;
1596 parms.port_no = OVSP_LOCAL;
1597 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1599 ovs_dp_change(dp, a);
1601 /* So far only local changes have been made, now need the lock. */
1604 vport = new_vport(&parms);
1605 if (IS_ERR(vport)) {
1606 err = PTR_ERR(vport);
1610 if (err == -EEXIST) {
1611 /* An outdated user space instance that does not understand
1612 * the concept of user_features has attempted to create a new
1613 * datapath and is likely to reuse it. Drop all user features.
1615 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1616 ovs_dp_reset_user_features(skb, info);
1619 goto err_destroy_ports_array;
1622 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1623 info->snd_seq, 0, OVS_DP_CMD_NEW);
1626 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1627 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1631 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1634 err_destroy_ports_array:
1638 free_percpu(dp->stats_percpu);
1640 ovs_flow_tbl_destroy(&dp->table);
1649 /* Called with ovs_mutex. */
1650 static void __dp_destroy(struct datapath *dp)
1654 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1655 struct vport *vport;
1656 struct hlist_node *n;
1658 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1659 if (vport->port_no != OVSP_LOCAL)
1660 ovs_dp_detach_port(vport);
1663 list_del_rcu(&dp->list_node);
1665 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1666 * all ports in datapath are destroyed first before freeing datapath.
1668 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1670 /* RCU destroy the flow table */
1671 call_rcu(&dp->rcu, destroy_dp_rcu);
1674 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1676 struct sk_buff *reply;
1677 struct datapath *dp;
1680 reply = ovs_dp_cmd_alloc_info(info);
1685 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1688 goto err_unlock_free;
1690 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1691 info->snd_seq, 0, OVS_DP_CMD_DEL);
1697 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1706 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1708 struct sk_buff *reply;
1709 struct datapath *dp;
1712 reply = ovs_dp_cmd_alloc_info(info);
1717 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1720 goto err_unlock_free;
1722 ovs_dp_change(dp, info->attrs);
1724 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1725 info->snd_seq, 0, OVS_DP_CMD_NEW);
1730 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1739 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1741 struct sk_buff *reply;
1742 struct datapath *dp;
1745 reply = ovs_dp_cmd_alloc_info(info);
1750 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1753 goto err_unlock_free;
1755 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1756 info->snd_seq, 0, OVS_DP_CMD_NEW);
1760 return genlmsg_reply(reply, info);
1768 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1770 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1771 struct datapath *dp;
1772 int skip = cb->args[0];
1776 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1778 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1779 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1780 OVS_DP_CMD_NEW) < 0)
1791 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1792 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1793 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1794 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1797 static struct genl_ops dp_datapath_genl_ops[] = {
1798 { .cmd = OVS_DP_CMD_NEW,
1799 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1800 .policy = datapath_policy,
1801 .doit = ovs_dp_cmd_new
1803 { .cmd = OVS_DP_CMD_DEL,
1804 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1805 .policy = datapath_policy,
1806 .doit = ovs_dp_cmd_del
1808 { .cmd = OVS_DP_CMD_GET,
1809 .flags = 0, /* OK for unprivileged users. */
1810 .policy = datapath_policy,
1811 .doit = ovs_dp_cmd_get,
1812 .dumpit = ovs_dp_cmd_dump
1814 { .cmd = OVS_DP_CMD_SET,
1815 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1816 .policy = datapath_policy,
1817 .doit = ovs_dp_cmd_set,
1821 static struct genl_family dp_datapath_genl_family = {
1822 .id = GENL_ID_GENERATE,
1823 .hdrsize = sizeof(struct ovs_header),
1824 .name = OVS_DATAPATH_FAMILY,
1825 .version = OVS_DATAPATH_VERSION,
1826 .maxattr = OVS_DP_ATTR_MAX,
1828 .parallel_ops = true,
1829 .ops = dp_datapath_genl_ops,
1830 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1831 .mcgrps = &ovs_dp_datapath_multicast_group,
1835 /* Called with ovs_mutex or RCU read lock. */
1836 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1837 u32 portid, u32 seq, u32 flags, u8 cmd)
1839 struct ovs_header *ovs_header;
1840 struct ovs_vport_stats vport_stats;
1843 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1848 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1850 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1851 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1852 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1853 ovs_vport_name(vport)))
1854 goto nla_put_failure;
1856 ovs_vport_get_stats(vport, &vport_stats);
1857 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1859 goto nla_put_failure;
1861 if (ovs_vport_get_upcall_portids(vport, skb))
1862 goto nla_put_failure;
1864 err = ovs_vport_get_options(vport, skb);
1865 if (err == -EMSGSIZE)
1868 genlmsg_end(skb, ovs_header);
1874 genlmsg_cancel(skb, ovs_header);
1878 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1880 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1883 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1884 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1887 struct sk_buff *skb;
1890 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1892 return ERR_PTR(-ENOMEM);
1894 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1900 /* Called with ovs_mutex or RCU read lock. */
1901 static struct vport *lookup_vport(struct net *net,
1902 const struct ovs_header *ovs_header,
1903 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1905 struct datapath *dp;
1906 struct vport *vport;
1908 if (a[OVS_VPORT_ATTR_NAME]) {
1909 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1911 return ERR_PTR(-ENODEV);
1912 if (ovs_header->dp_ifindex &&
1913 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1914 return ERR_PTR(-ENODEV);
1916 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1917 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1919 if (port_no >= DP_MAX_PORTS)
1920 return ERR_PTR(-EFBIG);
1922 dp = get_dp(net, ovs_header->dp_ifindex);
1924 return ERR_PTR(-ENODEV);
1926 vport = ovs_vport_ovsl_rcu(dp, port_no);
1928 return ERR_PTR(-ENODEV);
1931 return ERR_PTR(-EINVAL);
1934 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1936 struct nlattr **a = info->attrs;
1937 struct ovs_header *ovs_header = info->userhdr;
1938 struct vport_parms parms;
1939 struct sk_buff *reply;
1940 struct vport *vport;
1941 struct datapath *dp;
1945 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1946 !a[OVS_VPORT_ATTR_UPCALL_PID])
1949 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1950 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1951 if (port_no >= DP_MAX_PORTS)
1954 reply = ovs_vport_cmd_alloc_info();
1960 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1963 goto exit_unlock_free;
1966 vport = ovs_vport_ovsl(dp, port_no);
1969 goto exit_unlock_free;
1971 for (port_no = 1; ; port_no++) {
1972 if (port_no >= DP_MAX_PORTS) {
1974 goto exit_unlock_free;
1976 vport = ovs_vport_ovsl(dp, port_no);
1982 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1983 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1984 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1986 parms.port_no = port_no;
1987 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1989 vport = new_vport(&parms);
1990 err = PTR_ERR(vport);
1991 if (IS_ERR(vport)) {
1994 goto exit_unlock_free;
1997 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1998 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2002 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2011 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2013 struct nlattr **a = info->attrs;
2014 struct sk_buff *reply;
2015 struct vport *vport;
2018 reply = ovs_vport_cmd_alloc_info();
2023 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2024 err = PTR_ERR(vport);
2026 goto exit_unlock_free;
2028 if (a[OVS_VPORT_ATTR_TYPE] &&
2029 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2031 goto exit_unlock_free;
2034 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2035 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2037 goto exit_unlock_free;
2040 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2041 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2043 err = ovs_vport_set_upcall_portids(vport, ids);
2045 goto exit_unlock_free;
2048 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2049 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2053 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2062 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2064 struct nlattr **a = info->attrs;
2065 struct sk_buff *reply;
2066 struct vport *vport;
2069 reply = ovs_vport_cmd_alloc_info();
2074 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2075 err = PTR_ERR(vport);
2077 goto exit_unlock_free;
2079 if (vport->port_no == OVSP_LOCAL) {
2081 goto exit_unlock_free;
2084 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2085 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2087 ovs_dp_detach_port(vport);
2090 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2099 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2101 struct nlattr **a = info->attrs;
2102 struct ovs_header *ovs_header = info->userhdr;
2103 struct sk_buff *reply;
2104 struct vport *vport;
2107 reply = ovs_vport_cmd_alloc_info();
2112 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2113 err = PTR_ERR(vport);
2115 goto exit_unlock_free;
2116 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2117 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2121 return genlmsg_reply(reply, info);
2129 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2131 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2132 struct datapath *dp;
2133 int bucket = cb->args[0], skip = cb->args[1];
2137 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2142 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2143 struct vport *vport;
2146 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2148 ovs_vport_cmd_fill_info(vport, skb,
2149 NETLINK_CB(cb->skb).portid,
2152 OVS_VPORT_CMD_NEW) < 0)
2168 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2169 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2170 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2171 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2172 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2173 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2174 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2177 static struct genl_ops dp_vport_genl_ops[] = {
2178 { .cmd = OVS_VPORT_CMD_NEW,
2179 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2180 .policy = vport_policy,
2181 .doit = ovs_vport_cmd_new
2183 { .cmd = OVS_VPORT_CMD_DEL,
2184 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2185 .policy = vport_policy,
2186 .doit = ovs_vport_cmd_del
2188 { .cmd = OVS_VPORT_CMD_GET,
2189 .flags = 0, /* OK for unprivileged users. */
2190 .policy = vport_policy,
2191 .doit = ovs_vport_cmd_get,
2192 .dumpit = ovs_vport_cmd_dump
2194 { .cmd = OVS_VPORT_CMD_SET,
2195 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2196 .policy = vport_policy,
2197 .doit = ovs_vport_cmd_set,
2201 struct genl_family dp_vport_genl_family = {
2202 .id = GENL_ID_GENERATE,
2203 .hdrsize = sizeof(struct ovs_header),
2204 .name = OVS_VPORT_FAMILY,
2205 .version = OVS_VPORT_VERSION,
2206 .maxattr = OVS_VPORT_ATTR_MAX,
2208 .parallel_ops = true,
2209 .ops = dp_vport_genl_ops,
2210 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2211 .mcgrps = &ovs_dp_vport_multicast_group,
2215 static struct genl_family *dp_genl_families[] = {
2216 &dp_datapath_genl_family,
2217 &dp_vport_genl_family,
2218 &dp_flow_genl_family,
2219 &dp_packet_genl_family,
2222 static void dp_unregister_genl(int n_families)
2226 for (i = 0; i < n_families; i++)
2227 genl_unregister_family(dp_genl_families[i]);
2230 static int dp_register_genl(void)
2235 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2237 err = genl_register_family(dp_genl_families[i]);
2245 dp_unregister_genl(i);
2249 static int __net_init ovs_init_net(struct net *net)
2251 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2253 INIT_LIST_HEAD(&ovs_net->dps);
2254 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2259 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2260 struct list_head *head)
2262 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2263 struct datapath *dp;
2265 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2268 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2269 struct vport *vport;
2271 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2273 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2276 if (dev_net(vport->dev) == dnet)
2277 list_add(&vport->detach_list, head);
2283 static void __net_exit ovs_exit_net(struct net *dnet)
2285 struct datapath *dp, *dp_next;
2286 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2287 struct vport *vport, *vport_next;
2293 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2298 list_vports_from_net(net, dnet, &head);
2301 /* Detach all vports from given namespace. */
2302 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2303 list_del(&vport->detach_list);
2304 ovs_dp_detach_port(vport);
2309 cancel_work_sync(&ovs_net->dp_notify_work);
2312 static struct pernet_operations ovs_net_ops = {
2313 .init = ovs_init_net,
2314 .exit = ovs_exit_net,
2316 .size = sizeof(struct ovs_net),
2319 DEFINE_COMPAT_PNET_REG_FUNC(device);
2321 static int __init dp_init(void)
2325 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2327 pr_info("Open vSwitch switching datapath %s\n", VERSION);
2329 err = compat_init();
2333 err = action_fifos_init();
2335 goto error_compat_exit;
2337 err = ovs_internal_dev_rtnl_link_register();
2339 goto error_action_fifos_exit;
2341 err = ovs_flow_init();
2343 goto error_unreg_rtnl_link;
2345 err = ovs_vport_init();
2347 goto error_flow_exit;
2349 err = register_pernet_device(&ovs_net_ops);
2351 goto error_vport_exit;
2353 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2355 goto error_netns_exit;
2357 err = ovs_netdev_init();
2359 goto error_unreg_notifier;
2361 err = dp_register_genl();
2363 goto error_unreg_netdev;
2369 error_unreg_notifier:
2370 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2372 unregister_pernet_device(&ovs_net_ops);
2377 error_unreg_rtnl_link:
2378 ovs_internal_dev_rtnl_link_unregister();
2379 error_action_fifos_exit:
2380 action_fifos_exit();
2387 static void dp_cleanup(void)
2389 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2391 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2392 unregister_pernet_device(&ovs_net_ops);
2396 ovs_internal_dev_rtnl_link_unregister();
2397 action_fifos_exit();
2401 module_init(dp_init);
2402 module_exit(dp_cleanup);
2404 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2405 MODULE_LICENSE("GPL");
2406 MODULE_VERSION(VERSION);