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 <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
57 #include "flow_table.h"
58 #include "flow_netlink.h"
60 #include "vport-internal_dev.h"
61 #include "vport-netdev.h"
63 int ovs_net_id __read_mostly;
64 EXPORT_SYMBOL_GPL(ovs_net_id);
66 static struct genl_family dp_packet_genl_family;
67 static struct genl_family dp_flow_genl_family;
68 static struct genl_family dp_datapath_genl_family;
70 static const struct nla_policy flow_policy[];
72 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
73 .name = OVS_FLOW_MCGROUP
76 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
77 .name = OVS_DATAPATH_MCGROUP
80 struct genl_multicast_group ovs_dp_vport_multicast_group = {
81 .name = OVS_VPORT_MCGROUP
84 /* Check if need to build a reply message.
85 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply.
87 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
90 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
91 genl_has_listeners(family, genl_info_net(info), group);
94 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
95 struct sk_buff *skb, struct genl_info *info)
97 genl_notify(family, skb, genl_info_net(info),
98 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
104 * All writes e.g. Writes to device state (add/remove datapath, port, set
105 * operations on vports, etc.), Writes to other state (flow table
106 * modifications, set miscellaneous datapath parameters, etc.) are protected
109 * Reads are protected by RCU.
111 * There are a few special cases (mostly stats) that have their own
112 * synchronization but they nest under all of above and don't interact with
115 * The RTNL lock nests inside ovs_mutex.
118 static DEFINE_MUTEX(ovs_mutex);
122 mutex_lock(&ovs_mutex);
125 void ovs_unlock(void)
127 mutex_unlock(&ovs_mutex);
130 #ifdef CONFIG_LOCKDEP
131 int lockdep_ovsl_is_held(void)
134 return lockdep_is_held(&ovs_mutex);
138 EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
141 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
142 const struct sw_flow_key *,
143 const struct dp_upcall_info *);
144 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
145 const struct sw_flow_key *,
146 const struct dp_upcall_info *);
148 /* Must be called with rcu_read_lock. */
149 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
151 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
154 struct vport *vport = ovs_internal_dev_get_vport(dev);
162 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
163 * returned dp pointer valid.
165 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
169 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
171 dp = get_dp_rcu(net, dp_ifindex);
177 /* Must be called with rcu_read_lock or ovs_mutex. */
178 const char *ovs_dp_name(const struct datapath *dp)
180 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
181 return vport->ops->get_name(vport);
184 static int get_dpifindex(const struct datapath *dp)
191 local = ovs_vport_rcu(dp, OVSP_LOCAL);
193 ifindex = netdev_vport_priv(local)->dev->ifindex;
202 static void destroy_dp_rcu(struct rcu_head *rcu)
204 struct datapath *dp = container_of(rcu, struct datapath, rcu);
206 ovs_flow_tbl_destroy(&dp->table);
207 free_percpu(dp->stats_percpu);
208 release_net(ovs_dp_get_net(dp));
213 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
216 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
219 /* Called with ovs_mutex or RCU read lock. */
220 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
223 struct hlist_head *head;
225 head = vport_hash_bucket(dp, port_no);
226 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
227 if (vport->port_no == port_no)
233 /* Called with ovs_mutex. */
234 static struct vport *new_vport(const struct vport_parms *parms)
238 vport = ovs_vport_add(parms);
239 if (!IS_ERR(vport)) {
240 struct datapath *dp = parms->dp;
241 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
243 hlist_add_head_rcu(&vport->dp_hash_node, head);
248 void ovs_dp_detach_port(struct vport *p)
252 /* First drop references to device. */
253 hlist_del_rcu(&p->dp_hash_node);
255 /* Then destroy it. */
259 /* Must be called with rcu_read_lock. */
260 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
262 const struct vport *p = OVS_CB(skb)->input_vport;
263 struct datapath *dp = p->dp;
264 struct sw_flow *flow;
265 struct sw_flow_actions *sf_acts;
266 struct dp_stats_percpu *stats;
270 stats = this_cpu_ptr(dp->stats_percpu);
273 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
275 if (unlikely(!flow)) {
276 struct dp_upcall_info upcall;
279 upcall.cmd = OVS_PACKET_CMD_MISS;
280 upcall.userdata = NULL;
281 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
282 upcall.egress_tun_info = NULL;
283 error = ovs_dp_upcall(dp, skb, key, &upcall);
288 stats_counter = &stats->n_missed;
292 ovs_flow_stats_update(flow, key->tp.flags, skb);
293 sf_acts = rcu_dereference(flow->sf_acts);
294 ovs_execute_actions(dp, skb, sf_acts, key);
296 stats_counter = &stats->n_hit;
299 /* Update datapath statistics. */
300 u64_stats_update_begin(&stats->syncp);
302 stats->n_mask_hit += n_mask_hit;
303 u64_stats_update_end(&stats->syncp);
306 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
307 const struct sw_flow_key *key,
308 const struct dp_upcall_info *upcall_info)
310 struct dp_stats_percpu *stats;
313 if (upcall_info->portid == 0) {
318 if (!skb_is_gso(skb))
319 err = queue_userspace_packet(dp, skb, key, upcall_info);
321 err = queue_gso_packets(dp, skb, key, upcall_info);
328 stats = this_cpu_ptr(dp->stats_percpu);
330 u64_stats_update_begin(&stats->syncp);
332 u64_stats_update_end(&stats->syncp);
337 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
338 const struct sw_flow_key *key,
339 const struct dp_upcall_info *upcall_info)
341 unsigned short gso_type = skb_shinfo(skb)->gso_type;
342 struct sw_flow_key later_key;
343 struct sk_buff *segs, *nskb;
344 struct ovs_skb_cb ovs_cb;
347 ovs_cb = *OVS_CB(skb);
348 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
349 *OVS_CB(skb) = ovs_cb;
351 return PTR_ERR(segs);
355 if (gso_type & SKB_GSO_UDP) {
356 /* The initial flow key extracted by ovs_flow_key_extract()
357 * in this case is for a first fragment, so we need to
358 * properly mark later fragments.
361 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
364 /* Queue all of the segments. */
367 *OVS_CB(skb) = ovs_cb;
368 if (gso_type & SKB_GSO_UDP && skb != segs)
371 err = queue_userspace_packet(dp, skb, key, upcall_info);
375 } while ((skb = skb->next));
377 /* Free all of the segments. */
385 } while ((skb = nskb));
389 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
392 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
393 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
394 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
396 /* OVS_PACKET_ATTR_USERDATA */
397 if (upcall_info->userdata)
398 size += NLA_ALIGN(upcall_info->userdata->nla_len);
400 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
401 if (upcall_info->egress_tun_info)
402 size += nla_total_size(ovs_tun_key_attr_size());
407 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
408 const struct sw_flow_key *key,
409 const struct dp_upcall_info *upcall_info)
411 struct ovs_header *upcall;
412 struct sk_buff *nskb = NULL;
413 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
415 struct genl_info info = {
416 #ifdef HAVE_GENLMSG_NEW_UNICAST
417 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
419 .snd_portid = upcall_info->portid,
425 dp_ifindex = get_dpifindex(dp);
429 if (skb_vlan_tag_present(skb)) {
430 nskb = skb_clone(skb, GFP_ATOMIC);
434 nskb = vlan_insert_tag_set_proto(nskb, nskb->vlan_proto, skb_vlan_tag_get(nskb));
438 vlan_set_tci(nskb, 0);
443 if (nla_attr_size(skb->len) > USHRT_MAX) {
448 /* Complete checksum if needed */
449 if (skb->ip_summed == CHECKSUM_PARTIAL &&
450 (err = skb_checksum_help(skb)))
453 /* Older versions of OVS user space enforce alignment of the last
454 * Netlink attribute to NLA_ALIGNTO which would require extensive
455 * padding logic. Only perform zerocopy if padding is not required.
457 if (dp->user_features & OVS_DP_F_UNALIGNED)
458 hlen = skb_zerocopy_headlen(skb);
462 len = upcall_msg_size(upcall_info, hlen);
463 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
469 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
470 0, upcall_info->cmd);
471 upcall->dp_ifindex = dp_ifindex;
473 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
476 if (upcall_info->userdata)
477 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
478 nla_len(upcall_info->userdata),
479 nla_data(upcall_info->userdata));
481 if (upcall_info->egress_tun_info) {
482 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
483 err = ovs_nla_put_egress_tunnel_key(user_skb,
484 upcall_info->egress_tun_info);
486 nla_nest_end(user_skb, nla);
489 /* Only reserve room for attribute header, packet data is added
492 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
496 nla->nla_len = nla_attr_size(skb->len);
498 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
502 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
503 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
504 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
507 memset(skb_put(user_skb, plen), 0, plen);
510 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
512 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
522 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
524 struct ovs_header *ovs_header = info->userhdr;
525 struct nlattr **a = info->attrs;
526 struct sw_flow_actions *acts;
527 struct sk_buff *packet;
528 struct sw_flow *flow;
529 struct sw_flow_actions *sf_acts;
532 struct vport *input_vport;
535 bool log = !a[OVS_PACKET_ATTR_PROBE];
538 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
539 !a[OVS_PACKET_ATTR_ACTIONS])
542 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
543 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
547 skb_reserve(packet, NET_IP_ALIGN);
549 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
551 skb_reset_mac_header(packet);
552 eth = eth_hdr(packet);
554 /* Normally, setting the skb 'protocol' field would be handled by a
555 * call to eth_type_trans(), but it assumes there's a sending
556 * device, which we may not have.
558 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
559 packet->protocol = eth->h_proto;
561 packet->protocol = htons(ETH_P_802_2);
563 /* Build an sw_flow for sending this packet. */
564 flow = ovs_flow_alloc();
569 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
574 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
575 &flow->key, &acts, log);
579 rcu_assign_pointer(flow->sf_acts, acts);
580 OVS_CB(packet)->egress_tun_info = NULL;
581 packet->priority = flow->key.phy.priority;
582 packet->mark = flow->key.phy.skb_mark;
585 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
590 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
592 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
597 OVS_CB(packet)->input_vport = input_vport;
598 sf_acts = rcu_dereference(flow->sf_acts);
601 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
605 ovs_flow_free(flow, false);
611 ovs_flow_free(flow, false);
618 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
619 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
620 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
621 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
622 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
625 static struct genl_ops dp_packet_genl_ops[] = {
626 { .cmd = OVS_PACKET_CMD_EXECUTE,
627 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
628 .policy = packet_policy,
629 .doit = ovs_packet_cmd_execute
633 static struct genl_family dp_packet_genl_family = {
634 .id = GENL_ID_GENERATE,
635 .hdrsize = sizeof(struct ovs_header),
636 .name = OVS_PACKET_FAMILY,
637 .version = OVS_PACKET_VERSION,
638 .maxattr = OVS_PACKET_ATTR_MAX,
640 .parallel_ops = true,
641 .ops = dp_packet_genl_ops,
642 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
645 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
646 struct ovs_dp_megaflow_stats *mega_stats)
650 memset(mega_stats, 0, sizeof(*mega_stats));
652 stats->n_flows = ovs_flow_tbl_count(&dp->table);
653 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
655 stats->n_hit = stats->n_missed = stats->n_lost = 0;
657 for_each_possible_cpu(i) {
658 const struct dp_stats_percpu *percpu_stats;
659 struct dp_stats_percpu local_stats;
662 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
665 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
666 local_stats = *percpu_stats;
667 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
669 stats->n_hit += local_stats.n_hit;
670 stats->n_missed += local_stats.n_missed;
671 stats->n_lost += local_stats.n_lost;
672 mega_stats->n_mask_hit += local_stats.n_mask_hit;
676 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
678 return ovs_identifier_is_ufid(sfid) &&
679 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
682 static bool should_fill_mask(uint32_t ufid_flags)
684 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
687 static bool should_fill_actions(uint32_t ufid_flags)
689 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
692 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
693 const struct sw_flow_id *sfid,
696 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
698 /* OVS_FLOW_ATTR_UFID */
699 if (sfid && ovs_identifier_is_ufid(sfid))
700 len += nla_total_size(sfid->ufid_len);
702 /* OVS_FLOW_ATTR_KEY */
703 if (!sfid || should_fill_key(sfid, ufid_flags))
704 len += nla_total_size(ovs_key_attr_size());
706 /* OVS_FLOW_ATTR_MASK */
707 if (should_fill_mask(ufid_flags))
708 len += nla_total_size(ovs_key_attr_size());
710 /* OVS_FLOW_ATTR_ACTIONS */
711 if (should_fill_actions(ufid_flags))
712 len += nla_total_size(acts->actions_len);
715 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
716 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
717 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
720 /* Called with ovs_mutex or RCU read lock. */
721 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
724 struct ovs_flow_stats stats;
728 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
731 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
734 if (stats.n_packets &&
735 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
738 if ((u8)ntohs(tcp_flags) &&
739 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
745 /* Called with ovs_mutex or RCU read lock. */
746 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
747 struct sk_buff *skb, int skb_orig_len)
749 struct nlattr *start;
752 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
753 * this is the first flow to be dumped into 'skb'. This is unusual for
754 * Netlink but individual action lists can be longer than
755 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
756 * The userspace caller can always fetch the actions separately if it
757 * really wants them. (Most userspace callers in fact don't care.)
759 * This can only fail for dump operations because the skb is always
760 * properly sized for single flows.
762 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
764 const struct sw_flow_actions *sf_acts;
766 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
767 err = ovs_nla_put_actions(sf_acts->actions,
768 sf_acts->actions_len, skb);
771 nla_nest_end(skb, start);
776 nla_nest_cancel(skb, start);
778 } else if (skb_orig_len) {
785 /* Called with ovs_mutex or RCU read lock. */
786 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
787 struct sk_buff *skb, u32 portid,
788 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
790 const int skb_orig_len = skb->len;
791 struct ovs_header *ovs_header;
794 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
799 ovs_header->dp_ifindex = dp_ifindex;
801 err = ovs_nla_put_identifier(flow, skb);
805 if (should_fill_key(&flow->id, ufid_flags)) {
806 err = ovs_nla_put_masked_key(flow, skb);
811 if (should_fill_mask(ufid_flags)) {
812 err = ovs_nla_put_mask(flow, skb);
817 err = ovs_flow_cmd_fill_stats(flow, skb);
821 if (should_fill_actions(ufid_flags)) {
822 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
827 genlmsg_end(skb, ovs_header);
831 genlmsg_cancel(skb, ovs_header);
835 /* May not be called with RCU read lock. */
836 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
837 const struct sw_flow_id *sfid,
838 struct genl_info *info,
845 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
846 GROUP_ID(&ovs_dp_flow_multicast_group)))
849 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
850 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
852 return ERR_PTR(-ENOMEM);
857 /* Called with ovs_mutex. */
858 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
860 struct genl_info *info, u8 cmd,
861 bool always, u32 ufid_flags)
866 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
867 &flow->id, info, always, ufid_flags);
868 if (IS_ERR_OR_NULL(skb))
871 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
872 info->snd_portid, info->snd_seq, 0,
878 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
880 struct nlattr **a = info->attrs;
881 struct ovs_header *ovs_header = info->userhdr;
882 struct sw_flow *flow = NULL, *new_flow;
883 struct sw_flow_mask mask;
884 struct sk_buff *reply;
886 struct sw_flow_key key;
887 struct sw_flow_actions *acts;
888 struct sw_flow_match match;
889 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
891 bool log = !a[OVS_FLOW_ATTR_PROBE];
893 /* Must have key and actions. */
895 if (!a[OVS_FLOW_ATTR_KEY]) {
896 OVS_NLERR(log, "Flow key attr not present in new flow.");
899 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
900 OVS_NLERR(log, "Flow actions attr not present in new flow.");
904 /* Most of the time we need to allocate a new flow, do it before
907 new_flow = ovs_flow_alloc();
908 if (IS_ERR(new_flow)) {
909 error = PTR_ERR(new_flow);
914 ovs_match_init(&match, &key, &mask);
915 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
916 a[OVS_FLOW_ATTR_MASK], log);
920 ovs_flow_mask_key(&new_flow->key, &key, &mask);
922 /* Extract flow identifier. */
923 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
928 /* Validate actions. */
929 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
932 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
936 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
939 error = PTR_ERR(reply);
944 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
950 /* Check if this is a duplicate flow */
951 if (ovs_identifier_is_ufid(&new_flow->id))
952 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
954 flow = ovs_flow_tbl_lookup(&dp->table, &key);
956 rcu_assign_pointer(new_flow->sf_acts, acts);
958 /* Put flow in bucket. */
959 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
960 if (unlikely(error)) {
965 if (unlikely(reply)) {
966 error = ovs_flow_cmd_fill_info(new_flow,
967 ovs_header->dp_ifindex,
968 reply, info->snd_portid,
976 struct sw_flow_actions *old_acts;
978 /* Bail out if we're not allowed to modify an existing flow.
979 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
980 * because Generic Netlink treats the latter as a dump
981 * request. We also accept NLM_F_EXCL in case that bug ever
984 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
989 /* The flow identifier has to be the same for flow updates.
990 * Look for any overlapping flow.
992 if (unlikely(!ovs_flow_cmp(flow, &match))) {
993 if (ovs_identifier_is_key(&flow->id))
994 flow = ovs_flow_tbl_lookup_exact(&dp->table,
996 else /* UFID matches but key is different */
1000 goto err_unlock_ovs;
1003 /* Update actions. */
1004 old_acts = ovsl_dereference(flow->sf_acts);
1005 rcu_assign_pointer(flow->sf_acts, acts);
1007 if (unlikely(reply)) {
1008 error = ovs_flow_cmd_fill_info(flow,
1009 ovs_header->dp_ifindex,
1010 reply, info->snd_portid,
1018 ovs_nla_free_flow_actions(old_acts);
1019 ovs_flow_free(new_flow, false);
1023 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1032 ovs_flow_free(new_flow, false);
1037 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1038 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1039 const struct sw_flow_key *key,
1040 const struct sw_flow_mask *mask,
1043 struct sw_flow_actions *acts;
1044 struct sw_flow_key masked_key;
1047 ovs_flow_mask_key(&masked_key, key, mask);
1048 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
1051 "Actions may not be safe on all matching packets");
1052 return ERR_PTR(error);
1058 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1060 struct nlattr **a = info->attrs;
1061 struct ovs_header *ovs_header = info->userhdr;
1062 struct sw_flow_key key;
1063 struct sw_flow *flow;
1064 struct sw_flow_mask mask;
1065 struct sk_buff *reply = NULL;
1066 struct datapath *dp;
1067 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1068 struct sw_flow_match match;
1069 struct sw_flow_id sfid;
1070 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1072 bool log = !a[OVS_FLOW_ATTR_PROBE];
1077 if (!a[OVS_FLOW_ATTR_KEY]) {
1078 OVS_NLERR(log, "Flow key attribute not present in set flow.");
1082 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1083 ovs_match_init(&match, &key, &mask);
1084 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1085 a[OVS_FLOW_ATTR_MASK], log);
1089 /* Validate actions. */
1090 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1091 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1094 error = PTR_ERR(acts);
1098 /* Can allocate before locking if have acts. */
1099 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1101 if (IS_ERR(reply)) {
1102 error = PTR_ERR(reply);
1103 goto err_kfree_acts;
1108 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1109 if (unlikely(!dp)) {
1111 goto err_unlock_ovs;
1113 /* Check that the flow exists. */
1115 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1117 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1118 if (unlikely(!flow)) {
1120 goto err_unlock_ovs;
1123 /* Update actions, if present. */
1125 old_acts = ovsl_dereference(flow->sf_acts);
1126 rcu_assign_pointer(flow->sf_acts, acts);
1128 if (unlikely(reply)) {
1129 error = ovs_flow_cmd_fill_info(flow,
1130 ovs_header->dp_ifindex,
1131 reply, info->snd_portid,
1138 /* Could not alloc without acts before locking. */
1139 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1140 info, OVS_FLOW_CMD_NEW, false,
1143 if (unlikely(IS_ERR(reply))) {
1144 error = PTR_ERR(reply);
1145 goto err_unlock_ovs;
1150 if (a[OVS_FLOW_ATTR_CLEAR])
1151 ovs_flow_stats_clear(flow);
1155 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1157 ovs_nla_free_flow_actions(old_acts);
1170 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1172 struct nlattr **a = info->attrs;
1173 struct ovs_header *ovs_header = info->userhdr;
1174 struct sw_flow_key key;
1175 struct sk_buff *reply;
1176 struct sw_flow *flow;
1177 struct datapath *dp;
1178 struct sw_flow_match match;
1179 struct sw_flow_id ufid;
1180 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1182 bool log = !a[OVS_FLOW_ATTR_PROBE];
1185 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1186 if (a[OVS_FLOW_ATTR_KEY]) {
1187 ovs_match_init(&match, &key, NULL);
1188 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1190 } else if (!ufid_present) {
1192 "Flow get message rejected, Key attribute missing.");
1199 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1206 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1208 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1214 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1215 OVS_FLOW_CMD_NEW, true, ufid_flags);
1216 if (IS_ERR(reply)) {
1217 err = PTR_ERR(reply);
1222 return genlmsg_reply(reply, info);
1228 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1230 struct nlattr **a = info->attrs;
1231 struct ovs_header *ovs_header = info->userhdr;
1232 struct sw_flow_key key;
1233 struct sk_buff *reply;
1234 struct sw_flow *flow = NULL;
1235 struct datapath *dp;
1236 struct sw_flow_match match;
1237 struct sw_flow_id ufid;
1238 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1240 bool log = !a[OVS_FLOW_ATTR_PROBE];
1243 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1244 if (a[OVS_FLOW_ATTR_KEY]) {
1245 ovs_match_init(&match, &key, NULL);
1246 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1253 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1254 if (unlikely(!dp)) {
1259 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1260 err = ovs_flow_tbl_flush(&dp->table);
1265 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1267 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1268 if (unlikely(!flow)) {
1273 ovs_flow_tbl_remove(&dp->table, flow);
1276 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1277 &flow->id, info, false, ufid_flags);
1279 if (likely(reply)) {
1280 if (likely(!IS_ERR(reply))) {
1281 rcu_read_lock(); /*To keep RCU checker happy. */
1282 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1283 reply, info->snd_portid,
1289 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1291 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1292 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1297 ovs_flow_free(flow, true);
1304 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1306 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1307 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1308 struct table_instance *ti;
1309 struct datapath *dp;
1313 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1314 OVS_FLOW_ATTR_MAX, flow_policy);
1317 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1320 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1326 ti = rcu_dereference(dp->table.ti);
1328 struct sw_flow *flow;
1331 bucket = cb->args[0];
1333 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1337 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1338 NETLINK_CB(cb->skb).portid,
1339 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1340 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
1343 cb->args[0] = bucket;
1350 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1351 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1352 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1353 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1354 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1355 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1356 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1357 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1360 static struct genl_ops dp_flow_genl_ops[] = {
1361 { .cmd = OVS_FLOW_CMD_NEW,
1362 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1363 .policy = flow_policy,
1364 .doit = ovs_flow_cmd_new
1366 { .cmd = OVS_FLOW_CMD_DEL,
1367 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1368 .policy = flow_policy,
1369 .doit = ovs_flow_cmd_del
1371 { .cmd = OVS_FLOW_CMD_GET,
1372 .flags = 0, /* OK for unprivileged users. */
1373 .policy = flow_policy,
1374 .doit = ovs_flow_cmd_get,
1375 .dumpit = ovs_flow_cmd_dump
1377 { .cmd = OVS_FLOW_CMD_SET,
1378 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1379 .policy = flow_policy,
1380 .doit = ovs_flow_cmd_set,
1384 static struct genl_family dp_flow_genl_family = {
1385 .id = GENL_ID_GENERATE,
1386 .hdrsize = sizeof(struct ovs_header),
1387 .name = OVS_FLOW_FAMILY,
1388 .version = OVS_FLOW_VERSION,
1389 .maxattr = OVS_FLOW_ATTR_MAX,
1391 .parallel_ops = true,
1392 .ops = dp_flow_genl_ops,
1393 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1394 .mcgrps = &ovs_dp_flow_multicast_group,
1398 static size_t ovs_dp_cmd_msg_size(void)
1400 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1402 msgsize += nla_total_size(IFNAMSIZ);
1403 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1404 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1405 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1410 /* Called with ovs_mutex. */
1411 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1412 u32 portid, u32 seq, u32 flags, u8 cmd)
1414 struct ovs_header *ovs_header;
1415 struct ovs_dp_stats dp_stats;
1416 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1419 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1424 ovs_header->dp_ifindex = get_dpifindex(dp);
1426 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1428 goto nla_put_failure;
1430 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1431 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1433 goto nla_put_failure;
1435 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1436 sizeof(struct ovs_dp_megaflow_stats),
1437 &dp_megaflow_stats))
1438 goto nla_put_failure;
1440 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1441 goto nla_put_failure;
1443 genlmsg_end(skb, ovs_header);
1447 genlmsg_cancel(skb, ovs_header);
1452 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1454 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1457 /* Called with rcu_read_lock or ovs_mutex. */
1458 static struct datapath *lookup_datapath(struct net *net,
1459 const struct ovs_header *ovs_header,
1460 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1462 struct datapath *dp;
1464 if (!a[OVS_DP_ATTR_NAME])
1465 dp = get_dp(net, ovs_header->dp_ifindex);
1467 struct vport *vport;
1469 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1470 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1472 return dp ? dp : ERR_PTR(-ENODEV);
1475 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1477 struct datapath *dp;
1479 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1483 WARN(dp->user_features, "Dropping previously announced user features\n");
1484 dp->user_features = 0;
1487 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1489 if (a[OVS_DP_ATTR_USER_FEATURES])
1490 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1493 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1495 struct nlattr **a = info->attrs;
1496 struct vport_parms parms;
1497 struct sk_buff *reply;
1498 struct datapath *dp;
1499 struct vport *vport;
1500 struct ovs_net *ovs_net;
1504 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1507 reply = ovs_dp_cmd_alloc_info(info);
1512 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1514 goto err_free_reply;
1516 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1518 /* Allocate table. */
1519 err = ovs_flow_tbl_init(&dp->table);
1523 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1524 if (!dp->stats_percpu) {
1526 goto err_destroy_table;
1529 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1533 goto err_destroy_percpu;
1536 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1537 INIT_HLIST_HEAD(&dp->ports[i]);
1539 /* Set up our datapath device. */
1540 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1541 parms.type = OVS_VPORT_TYPE_INTERNAL;
1542 parms.options = NULL;
1544 parms.port_no = OVSP_LOCAL;
1545 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1547 ovs_dp_change(dp, a);
1549 /* So far only local changes have been made, now need the lock. */
1552 vport = new_vport(&parms);
1553 if (IS_ERR(vport)) {
1554 err = PTR_ERR(vport);
1558 if (err == -EEXIST) {
1559 /* An outdated user space instance that does not understand
1560 * the concept of user_features has attempted to create a new
1561 * datapath and is likely to reuse it. Drop all user features.
1563 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1564 ovs_dp_reset_user_features(skb, info);
1567 goto err_destroy_ports_array;
1570 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1571 info->snd_seq, 0, OVS_DP_CMD_NEW);
1574 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1575 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1578 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1581 err_destroy_ports_array:
1585 free_percpu(dp->stats_percpu);
1587 ovs_flow_tbl_destroy(&dp->table);
1589 release_net(ovs_dp_get_net(dp));
1597 /* Called with ovs_mutex. */
1598 static void __dp_destroy(struct datapath *dp)
1602 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1603 struct vport *vport;
1604 struct hlist_node *n;
1606 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1607 if (vport->port_no != OVSP_LOCAL)
1608 ovs_dp_detach_port(vport);
1611 list_del_rcu(&dp->list_node);
1613 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1614 * all ports in datapath are destroyed first before freeing datapath.
1616 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1618 /* RCU destroy the flow table */
1619 call_rcu(&dp->rcu, destroy_dp_rcu);
1622 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1624 struct sk_buff *reply;
1625 struct datapath *dp;
1628 reply = ovs_dp_cmd_alloc_info(info);
1633 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1636 goto err_unlock_free;
1638 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1639 info->snd_seq, 0, OVS_DP_CMD_DEL);
1645 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1654 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1656 struct sk_buff *reply;
1657 struct datapath *dp;
1660 reply = ovs_dp_cmd_alloc_info(info);
1665 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1668 goto err_unlock_free;
1670 ovs_dp_change(dp, info->attrs);
1672 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1673 info->snd_seq, 0, OVS_DP_CMD_NEW);
1677 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1686 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1688 struct sk_buff *reply;
1689 struct datapath *dp;
1692 reply = ovs_dp_cmd_alloc_info(info);
1697 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1700 goto err_unlock_free;
1702 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1703 info->snd_seq, 0, OVS_DP_CMD_NEW);
1707 return genlmsg_reply(reply, info);
1715 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1717 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1718 struct datapath *dp;
1719 int skip = cb->args[0];
1723 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1725 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1726 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1727 OVS_DP_CMD_NEW) < 0)
1738 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1739 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1740 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1741 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1744 static struct genl_ops dp_datapath_genl_ops[] = {
1745 { .cmd = OVS_DP_CMD_NEW,
1746 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1747 .policy = datapath_policy,
1748 .doit = ovs_dp_cmd_new
1750 { .cmd = OVS_DP_CMD_DEL,
1751 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1752 .policy = datapath_policy,
1753 .doit = ovs_dp_cmd_del
1755 { .cmd = OVS_DP_CMD_GET,
1756 .flags = 0, /* OK for unprivileged users. */
1757 .policy = datapath_policy,
1758 .doit = ovs_dp_cmd_get,
1759 .dumpit = ovs_dp_cmd_dump
1761 { .cmd = OVS_DP_CMD_SET,
1762 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1763 .policy = datapath_policy,
1764 .doit = ovs_dp_cmd_set,
1768 static struct genl_family dp_datapath_genl_family = {
1769 .id = GENL_ID_GENERATE,
1770 .hdrsize = sizeof(struct ovs_header),
1771 .name = OVS_DATAPATH_FAMILY,
1772 .version = OVS_DATAPATH_VERSION,
1773 .maxattr = OVS_DP_ATTR_MAX,
1775 .parallel_ops = true,
1776 .ops = dp_datapath_genl_ops,
1777 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1778 .mcgrps = &ovs_dp_datapath_multicast_group,
1782 /* Called with ovs_mutex or RCU read lock. */
1783 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1784 u32 portid, u32 seq, u32 flags, u8 cmd)
1786 struct ovs_header *ovs_header;
1787 struct ovs_vport_stats vport_stats;
1790 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1795 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1797 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1798 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1799 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1800 goto nla_put_failure;
1802 ovs_vport_get_stats(vport, &vport_stats);
1803 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1805 goto nla_put_failure;
1807 if (ovs_vport_get_upcall_portids(vport, skb))
1808 goto nla_put_failure;
1810 err = ovs_vport_get_options(vport, skb);
1811 if (err == -EMSGSIZE)
1814 genlmsg_end(skb, ovs_header);
1820 genlmsg_cancel(skb, ovs_header);
1824 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1826 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1829 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1830 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1833 struct sk_buff *skb;
1836 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1838 return ERR_PTR(-ENOMEM);
1840 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1846 /* Called with ovs_mutex or RCU read lock. */
1847 static struct vport *lookup_vport(struct net *net,
1848 const struct ovs_header *ovs_header,
1849 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1851 struct datapath *dp;
1852 struct vport *vport;
1854 if (a[OVS_VPORT_ATTR_NAME]) {
1855 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1857 return ERR_PTR(-ENODEV);
1858 if (ovs_header->dp_ifindex &&
1859 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1860 return ERR_PTR(-ENODEV);
1862 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1863 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1865 if (port_no >= DP_MAX_PORTS)
1866 return ERR_PTR(-EFBIG);
1868 dp = get_dp(net, ovs_header->dp_ifindex);
1870 return ERR_PTR(-ENODEV);
1872 vport = ovs_vport_ovsl_rcu(dp, port_no);
1874 return ERR_PTR(-ENODEV);
1877 return ERR_PTR(-EINVAL);
1880 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1882 struct nlattr **a = info->attrs;
1883 struct ovs_header *ovs_header = info->userhdr;
1884 struct vport_parms parms;
1885 struct sk_buff *reply;
1886 struct vport *vport;
1887 struct datapath *dp;
1891 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1892 !a[OVS_VPORT_ATTR_UPCALL_PID])
1895 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1896 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1897 if (port_no >= DP_MAX_PORTS)
1900 reply = ovs_vport_cmd_alloc_info();
1906 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1909 goto exit_unlock_free;
1912 vport = ovs_vport_ovsl(dp, port_no);
1915 goto exit_unlock_free;
1917 for (port_no = 1; ; port_no++) {
1918 if (port_no >= DP_MAX_PORTS) {
1920 goto exit_unlock_free;
1922 vport = ovs_vport_ovsl(dp, port_no);
1928 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1929 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1930 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1932 parms.port_no = port_no;
1933 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1935 vport = new_vport(&parms);
1936 err = PTR_ERR(vport);
1937 if (IS_ERR(vport)) {
1940 goto exit_unlock_free;
1943 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1944 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1948 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1957 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1959 struct nlattr **a = info->attrs;
1960 struct sk_buff *reply;
1961 struct vport *vport;
1964 reply = ovs_vport_cmd_alloc_info();
1969 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1970 err = PTR_ERR(vport);
1972 goto exit_unlock_free;
1974 if (a[OVS_VPORT_ATTR_TYPE] &&
1975 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1977 goto exit_unlock_free;
1980 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1981 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1983 goto exit_unlock_free;
1986 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1987 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1989 err = ovs_vport_set_upcall_portids(vport, ids);
1991 goto exit_unlock_free;
1994 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1995 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1999 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2008 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2010 struct nlattr **a = info->attrs;
2011 struct sk_buff *reply;
2012 struct vport *vport;
2015 reply = ovs_vport_cmd_alloc_info();
2020 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2021 err = PTR_ERR(vport);
2023 goto exit_unlock_free;
2025 if (vport->port_no == OVSP_LOCAL) {
2027 goto exit_unlock_free;
2030 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2031 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2033 ovs_dp_detach_port(vport);
2036 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2045 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2047 struct nlattr **a = info->attrs;
2048 struct ovs_header *ovs_header = info->userhdr;
2049 struct sk_buff *reply;
2050 struct vport *vport;
2053 reply = ovs_vport_cmd_alloc_info();
2058 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2059 err = PTR_ERR(vport);
2061 goto exit_unlock_free;
2062 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2063 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2067 return genlmsg_reply(reply, info);
2075 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2077 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2078 struct datapath *dp;
2079 int bucket = cb->args[0], skip = cb->args[1];
2083 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2088 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2089 struct vport *vport;
2092 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2094 ovs_vport_cmd_fill_info(vport, skb,
2095 NETLINK_CB(cb->skb).portid,
2098 OVS_VPORT_CMD_NEW) < 0)
2114 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2115 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2116 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2117 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2118 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2119 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2120 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2123 static struct genl_ops dp_vport_genl_ops[] = {
2124 { .cmd = OVS_VPORT_CMD_NEW,
2125 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2126 .policy = vport_policy,
2127 .doit = ovs_vport_cmd_new
2129 { .cmd = OVS_VPORT_CMD_DEL,
2130 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2131 .policy = vport_policy,
2132 .doit = ovs_vport_cmd_del
2134 { .cmd = OVS_VPORT_CMD_GET,
2135 .flags = 0, /* OK for unprivileged users. */
2136 .policy = vport_policy,
2137 .doit = ovs_vport_cmd_get,
2138 .dumpit = ovs_vport_cmd_dump
2140 { .cmd = OVS_VPORT_CMD_SET,
2141 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2142 .policy = vport_policy,
2143 .doit = ovs_vport_cmd_set,
2147 struct genl_family dp_vport_genl_family = {
2148 .id = GENL_ID_GENERATE,
2149 .hdrsize = sizeof(struct ovs_header),
2150 .name = OVS_VPORT_FAMILY,
2151 .version = OVS_VPORT_VERSION,
2152 .maxattr = OVS_VPORT_ATTR_MAX,
2154 .parallel_ops = true,
2155 .ops = dp_vport_genl_ops,
2156 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2157 .mcgrps = &ovs_dp_vport_multicast_group,
2161 static struct genl_family *dp_genl_families[] = {
2162 &dp_datapath_genl_family,
2163 &dp_vport_genl_family,
2164 &dp_flow_genl_family,
2165 &dp_packet_genl_family,
2168 static void dp_unregister_genl(int n_families)
2172 for (i = 0; i < n_families; i++)
2173 genl_unregister_family(dp_genl_families[i]);
2176 static int dp_register_genl(void)
2181 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2183 err = genl_register_family(dp_genl_families[i]);
2191 dp_unregister_genl(i);
2195 static int __net_init ovs_init_net(struct net *net)
2197 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2199 INIT_LIST_HEAD(&ovs_net->dps);
2200 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2204 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2205 struct list_head *head)
2207 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2208 struct datapath *dp;
2210 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2213 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2214 struct vport *vport;
2216 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2217 struct netdev_vport *netdev_vport;
2219 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2222 netdev_vport = netdev_vport_priv(vport);
2223 if (dev_net(netdev_vport->dev) == dnet)
2224 list_add(&vport->detach_list, head);
2230 static void __net_exit ovs_exit_net(struct net *dnet)
2232 struct datapath *dp, *dp_next;
2233 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2234 struct vport *vport, *vport_next;
2239 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2244 list_vports_from_net(net, dnet, &head);
2247 /* Detach all vports from given namespace. */
2248 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2249 list_del(&vport->detach_list);
2250 ovs_dp_detach_port(vport);
2255 cancel_work_sync(&ovs_net->dp_notify_work);
2258 static struct pernet_operations ovs_net_ops = {
2259 .init = ovs_init_net,
2260 .exit = ovs_exit_net,
2262 .size = sizeof(struct ovs_net),
2265 DEFINE_COMPAT_PNET_REG_FUNC(device);
2267 static int __init dp_init(void)
2271 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2273 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2276 err = action_fifos_init();
2280 err = ovs_internal_dev_rtnl_link_register();
2282 goto error_action_fifos_exit;
2284 err = ovs_flow_init();
2286 goto error_unreg_rtnl_link;
2288 err = ovs_vport_init();
2290 goto error_flow_exit;
2292 err = register_pernet_device(&ovs_net_ops);
2294 goto error_vport_exit;
2296 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2298 goto error_netns_exit;
2300 err = ovs_netdev_init();
2302 goto error_unreg_notifier;
2304 err = dp_register_genl();
2306 goto error_unreg_netdev;
2312 error_unreg_notifier:
2313 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2315 unregister_pernet_device(&ovs_net_ops);
2320 error_unreg_rtnl_link:
2321 ovs_internal_dev_rtnl_link_unregister();
2322 error_action_fifos_exit:
2323 action_fifos_exit();
2328 static void dp_cleanup(void)
2330 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2332 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2333 unregister_pernet_device(&ovs_net_ops);
2337 ovs_internal_dev_rtnl_link_unregister();
2338 action_fifos_exit();
2341 module_init(dp_init);
2342 module_exit(dp_cleanup);
2344 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2345 MODULE_LICENSE("GPL");
2346 MODULE_VERSION(VERSION);