2 * Copyright (c) 2007-2014 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
59 #include "flow_table.h"
60 #include "flow_netlink.h"
62 #include "vport-internal_dev.h"
63 #include "vport-netdev.h"
65 int ovs_net_id __read_mostly;
67 static struct genl_family dp_packet_genl_family;
68 static struct genl_family dp_flow_genl_family;
69 static struct genl_family dp_datapath_genl_family;
71 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
72 .name = OVS_FLOW_MCGROUP
75 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
76 .name = OVS_DATAPATH_MCGROUP
79 struct genl_multicast_group ovs_dp_vport_multicast_group = {
80 .name = OVS_VPORT_MCGROUP
83 /* Check if need to build a reply message.
84 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
85 static bool ovs_must_notify(struct genl_info *info,
86 const struct genl_multicast_group *grp)
88 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
89 netlink_has_listeners(genl_info_net(info)->genl_sock, GROUP_ID(grp));
92 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
93 struct sk_buff *skb, struct genl_info *info)
95 genl_notify(family, skb, genl_info_net(info),
96 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
102 * All writes e.g. Writes to device state (add/remove datapath, port, set
103 * operations on vports, etc.), Writes to other state (flow table
104 * modifications, set miscellaneous datapath parameters, etc.) are protected
107 * Reads are protected by RCU.
109 * There are a few special cases (mostly stats) that have their own
110 * synchronization but they nest under all of above and don't interact with
113 * The RTNL lock nests inside ovs_mutex.
116 static DEFINE_MUTEX(ovs_mutex);
120 mutex_lock(&ovs_mutex);
123 void ovs_unlock(void)
125 mutex_unlock(&ovs_mutex);
128 #ifdef CONFIG_LOCKDEP
129 int lockdep_ovsl_is_held(void)
132 return lockdep_is_held(&ovs_mutex);
138 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
139 const struct dp_upcall_info *);
140 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
141 const struct dp_upcall_info *);
143 /* Must be called with rcu_read_lock. */
144 static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
146 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
157 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
158 * returned dp pointer valid. */
159 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
163 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
165 dp = get_dp_rcu(net, dp_ifindex);
171 /* Must be called with rcu_read_lock or ovs_mutex. */
172 const char *ovs_dp_name(const struct datapath *dp)
174 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
175 return vport->ops->get_name(vport);
178 static int get_dpifindex(struct datapath *dp)
185 local = ovs_vport_rcu(dp, OVSP_LOCAL);
187 ifindex = netdev_vport_priv(local)->dev->ifindex;
196 static void destroy_dp_rcu(struct rcu_head *rcu)
198 struct datapath *dp = container_of(rcu, struct datapath, rcu);
200 ovs_flow_tbl_destroy(&dp->table);
201 free_percpu(dp->stats_percpu);
202 release_net(ovs_dp_get_net(dp));
207 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
210 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
213 /* Called with ovs_mutex or RCU read lock. */
214 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
217 struct hlist_head *head;
219 head = vport_hash_bucket(dp, port_no);
220 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
221 if (vport->port_no == port_no)
227 /* Called with ovs_mutex. */
228 static struct vport *new_vport(const struct vport_parms *parms)
232 vport = ovs_vport_add(parms);
233 if (!IS_ERR(vport)) {
234 struct datapath *dp = parms->dp;
235 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
237 hlist_add_head_rcu(&vport->dp_hash_node, head);
242 void ovs_dp_detach_port(struct vport *p)
246 /* First drop references to device. */
247 hlist_del_rcu(&p->dp_hash_node);
249 /* Then destroy it. */
253 void ovs_dp_process_packet_with_key(struct sk_buff *skb,
254 struct sw_flow_key *pkt_key,
257 const struct vport *p = OVS_CB(skb)->input_vport;
258 struct datapath *dp = p->dp;
259 struct sw_flow *flow;
260 struct dp_stats_percpu *stats;
264 stats = this_cpu_ptr(dp->stats_percpu);
265 OVS_CB(skb)->pkt_key = pkt_key;
268 flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, skb_get_hash(skb),
270 if (unlikely(!flow)) {
271 struct dp_upcall_info upcall;
273 upcall.cmd = OVS_PACKET_CMD_MISS;
274 upcall.userdata = NULL;
275 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
276 ovs_dp_upcall(dp, skb, &upcall);
278 stats_counter = &stats->n_missed;
282 OVS_CB(skb)->flow = flow;
284 ovs_flow_stats_update(OVS_CB(skb)->flow, pkt_key->tp.flags, skb);
285 ovs_execute_actions(dp, skb, recirc);
286 stats_counter = &stats->n_hit;
289 /* Update datapath statistics. */
290 u64_stats_update_begin(&stats->sync);
292 stats->n_mask_hit += n_mask_hit;
293 u64_stats_update_end(&stats->sync);
296 /* Must be called with rcu_read_lock. */
297 void ovs_dp_process_received_packet(struct sk_buff *skb)
300 struct sw_flow_key key;
302 /* Extract flow from 'skb' into 'key'. */
303 error = ovs_flow_extract(skb, OVS_CB(skb)->input_vport->port_no, &key);
304 if (unlikely(error)) {
309 ovs_dp_process_packet_with_key(skb, &key, false);
312 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
313 const struct dp_upcall_info *upcall_info)
315 struct dp_stats_percpu *stats;
318 BUG_ON(!OVS_CB(skb)->pkt_key);
320 if (upcall_info->portid == 0) {
325 if (!skb_is_gso(skb))
326 err = queue_userspace_packet(dp, skb, upcall_info);
328 err = queue_gso_packets(dp, skb, upcall_info);
335 stats = this_cpu_ptr(dp->stats_percpu);
337 u64_stats_update_begin(&stats->sync);
339 u64_stats_update_end(&stats->sync);
344 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
345 const struct dp_upcall_info *upcall_info)
347 unsigned short gso_type = skb_shinfo(skb)->gso_type;
348 struct sw_flow_key later_key;
349 struct sk_buff *segs, *nskb;
352 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
354 return PTR_ERR(segs);
356 if (gso_type & SKB_GSO_UDP) {
357 /* The initial flow key extracted by ovs_flow_extract()
358 * in this case is for a first fragment, so we need to
359 * properly mark later fragments.
361 later_key = *OVS_CB(skb)->pkt_key;
362 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
365 /* Queue all of the segments. */
368 if (gso_type & SKB_GSO_UDP && skb != segs)
369 OVS_CB(skb)->pkt_key = &later_key;
371 err = queue_userspace_packet(dp, skb, upcall_info);
375 } while ((skb = skb->next));
377 /* Free all of the segments. */
385 } while ((skb = nskb));
389 static size_t key_attr_size(void)
391 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
392 * updating this function. */
393 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
395 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
396 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
397 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
398 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
399 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
400 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
401 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
402 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
403 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
404 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
405 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
406 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
407 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
408 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
409 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
410 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
411 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
412 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
413 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
414 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
415 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
416 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
417 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
420 static size_t upcall_msg_size(const struct nlattr *userdata,
423 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
424 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
425 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
427 /* OVS_PACKET_ATTR_USERDATA */
429 size += NLA_ALIGN(userdata->nla_len);
434 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
435 const struct dp_upcall_info *upcall_info)
437 struct ovs_header *upcall;
438 struct sk_buff *nskb = NULL;
439 struct sk_buff *user_skb; /* to be queued to userspace */
440 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
442 struct genl_info info = {
443 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
444 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
446 .snd_portid = upcall_info->portid,
452 dp_ifindex = get_dpifindex(dp);
456 if (vlan_tx_tag_present(skb)) {
457 nskb = skb_clone(skb, GFP_ATOMIC);
461 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
465 vlan_set_tci(nskb, 0);
470 if (nla_attr_size(skb->len) > USHRT_MAX) {
475 /* Complete checksum if needed */
476 if (skb->ip_summed == CHECKSUM_PARTIAL &&
477 (err = skb_checksum_help(skb)))
480 /* Older versions of OVS user space enforce alignment of the last
481 * Netlink attribute to NLA_ALIGNTO which would require extensive
482 * padding logic. Only perform zerocopy if padding is not required.
484 if (dp->user_features & OVS_DP_F_UNALIGNED)
485 hlen = skb_zerocopy_headlen(skb);
489 len = upcall_msg_size(upcall_info->userdata, hlen);
490 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
496 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
497 0, upcall_info->cmd);
498 upcall->dp_ifindex = dp_ifindex;
500 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
501 err = ovs_nla_put_flow(dp, pkt_key, pkt_key, user_skb);
503 nla_nest_end(user_skb, nla);
505 if (upcall_info->userdata)
506 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
507 nla_len(upcall_info->userdata),
508 nla_data(upcall_info->userdata));
510 /* Only reserve room for attribute header, packet data is added
511 * in skb_zerocopy() */
512 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
516 nla->nla_len = nla_attr_size(skb->len);
518 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
522 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
523 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
524 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
527 memset(skb_put(user_skb, plen), 0, plen);
530 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
532 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
540 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
542 struct ovs_header *ovs_header = info->userhdr;
543 struct nlattr **a = info->attrs;
544 struct sw_flow_actions *acts;
545 struct sk_buff *packet;
546 struct sw_flow *flow;
549 struct vport *input_vport;
554 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
555 !a[OVS_PACKET_ATTR_ACTIONS])
558 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
559 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
563 skb_reserve(packet, NET_IP_ALIGN);
565 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
567 skb_reset_mac_header(packet);
568 eth = eth_hdr(packet);
570 /* Normally, setting the skb 'protocol' field would be handled by a
571 * call to eth_type_trans(), but it assumes there's a sending
572 * device, which we may not have. */
573 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
574 packet->protocol = eth->h_proto;
576 packet->protocol = htons(ETH_P_802_2);
578 /* Build an sw_flow for sending this packet. */
579 flow = ovs_flow_alloc();
584 err = ovs_flow_extract(packet, -1, &flow->key);
588 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
591 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
596 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
598 rcu_assign_pointer(flow->sf_acts, acts);
602 OVS_CB(packet)->flow = flow;
603 OVS_CB(packet)->pkt_key = &flow->key;
604 packet->priority = flow->key.phy.priority;
605 packet->mark = flow->key.phy.skb_mark;
608 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
613 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
615 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
620 OVS_CB(packet)->input_vport = input_vport;
623 err = ovs_execute_actions(dp, packet, false);
627 ovs_flow_free(flow, false);
633 ovs_flow_free(flow, false);
640 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
641 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
642 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
643 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
646 static struct genl_ops dp_packet_genl_ops[] = {
647 { .cmd = OVS_PACKET_CMD_EXECUTE,
648 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
649 .policy = packet_policy,
650 .doit = ovs_packet_cmd_execute
654 static struct genl_family dp_packet_genl_family = {
655 .id = GENL_ID_GENERATE,
656 .hdrsize = sizeof(struct ovs_header),
657 .name = OVS_PACKET_FAMILY,
658 .version = OVS_PACKET_VERSION,
659 .maxattr = OVS_PACKET_ATTR_MAX,
661 .parallel_ops = true,
662 .ops = dp_packet_genl_ops,
663 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
666 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
667 struct ovs_dp_megaflow_stats *mega_stats)
671 memset(mega_stats, 0, sizeof(*mega_stats));
673 stats->n_flows = ovs_flow_tbl_count(&dp->table);
674 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
676 stats->n_hit = stats->n_missed = stats->n_lost = 0;
678 for_each_possible_cpu(i) {
679 const struct dp_stats_percpu *percpu_stats;
680 struct dp_stats_percpu local_stats;
683 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
686 start = u64_stats_fetch_begin_irq(&percpu_stats->sync);
687 local_stats = *percpu_stats;
688 } while (u64_stats_fetch_retry_irq(&percpu_stats->sync, start));
690 stats->n_hit += local_stats.n_hit;
691 stats->n_missed += local_stats.n_missed;
692 stats->n_lost += local_stats.n_lost;
693 mega_stats->n_mask_hit += local_stats.n_mask_hit;
697 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
699 return NLMSG_ALIGN(sizeof(struct ovs_header))
700 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
701 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
702 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
703 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
704 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
705 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
708 /* Called with ovs_mutex or RCU read lock. */
709 static int ovs_flow_cmd_fill_match(struct datapath *dp,
710 const struct sw_flow *flow,
717 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
721 err = ovs_nla_put_flow(dp, &flow->unmasked_key,
722 &flow->unmasked_key, skb);
725 nla_nest_end(skb, nla);
727 /* Fill flow mask. */
728 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
732 err = ovs_nla_put_flow(dp, &flow->key, &flow->mask->key, skb);
735 nla_nest_end(skb, nla);
740 /* Called with ovs_mutex or RCU read lock. */
741 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
744 struct ovs_flow_stats stats;
748 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
751 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
754 if (stats.n_packets &&
755 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
758 if ((u8)ntohs(tcp_flags) &&
759 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
765 /* Called with ovs_mutex or RCU read lock. */
766 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
767 struct sk_buff *skb, int skb_orig_len)
769 struct nlattr *start;
772 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
773 * this is the first flow to be dumped into 'skb'. This is unusual for
774 * Netlink but individual action lists can be longer than
775 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
776 * The userspace caller can always fetch the actions separately if it
777 * really wants them. (Most userspace callers in fact don't care.)
779 * This can only fail for dump operations because the skb is always
780 * properly sized for single flows.
782 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
784 const struct sw_flow_actions *sf_acts;
786 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
787 err = ovs_nla_put_actions(sf_acts->actions,
788 sf_acts->actions_len, skb);
791 nla_nest_end(skb, start);
796 nla_nest_cancel(skb, start);
798 } else if (skb_orig_len) {
805 /* Called with ovs_mutex or RCU read lock. */
806 static int ovs_flow_cmd_fill_info(struct datapath *dp,
807 const struct sw_flow *flow, int dp_ifindex,
808 struct sk_buff *skb, u32 portid,
809 u32 seq, u32 flags, u8 cmd)
811 const int skb_orig_len = skb->len;
812 struct ovs_header *ovs_header;
815 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
818 ovs_header->dp_ifindex = dp_ifindex;
820 err = ovs_flow_cmd_fill_match(dp, flow, skb);
824 err = ovs_flow_cmd_fill_stats(flow, skb);
828 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
832 return genlmsg_end(skb, ovs_header);
835 genlmsg_cancel(skb, ovs_header);
839 /* May not be called with RCU read lock. */
840 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
841 struct genl_info *info,
846 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
849 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
852 return ERR_PTR(-ENOMEM);
857 /* Called with ovs_mutex. */
858 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
859 const struct sw_flow *flow,
861 struct genl_info *info, u8 cmd,
867 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
869 if (!skb || IS_ERR(skb))
872 retval = ovs_flow_cmd_fill_info(dp, flow, dp_ifindex, skb,
873 info->snd_portid, info->snd_seq, 0,
879 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
881 struct nlattr **a = info->attrs;
882 struct ovs_header *ovs_header = info->userhdr;
883 struct sw_flow *flow, *new_flow;
884 struct sw_flow_mask mask;
885 struct sk_buff *reply;
887 struct sw_flow_actions *acts;
888 struct sw_flow_match match;
891 /* Must have key and actions. */
893 if (!a[OVS_FLOW_ATTR_KEY]) {
894 OVS_NLERR("Flow key attribute not present in new flow.\n");
897 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
898 OVS_NLERR("Flow actions attribute not present in new flow.\n");
902 /* Most of the time we need to allocate a new flow, do it before
904 new_flow = ovs_flow_alloc();
905 if (IS_ERR(new_flow)) {
906 error = PTR_ERR(new_flow);
911 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
912 error = ovs_nla_get_match(&match,
913 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
917 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
919 /* Validate actions. */
920 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
921 error = PTR_ERR(acts);
925 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
928 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
932 reply = ovs_flow_cmd_alloc_info(acts, info, false);
934 error = PTR_ERR(reply);
939 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
944 /* Check if this is a duplicate flow */
945 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
947 rcu_assign_pointer(new_flow->sf_acts, acts);
949 /* Put flow in bucket. */
950 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
951 if (unlikely(error)) {
956 if (unlikely(reply)) {
957 error = ovs_flow_cmd_fill_info(dp, new_flow,
958 ovs_header->dp_ifindex,
959 reply, info->snd_portid,
966 struct sw_flow_actions *old_acts;
968 /* Bail out if we're not allowed to modify an existing flow.
969 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
970 * because Generic Netlink treats the latter as a dump
971 * request. We also accept NLM_F_EXCL in case that bug ever
974 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
979 /* The unmasked key has to be the same for flow updates. */
980 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
981 /* Look for any overlapping flow. */
982 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
988 /* Update actions. */
989 old_acts = ovsl_dereference(flow->sf_acts);
990 rcu_assign_pointer(flow->sf_acts, acts);
992 if (unlikely(reply)) {
993 error = ovs_flow_cmd_fill_info(dp, flow,
994 ovs_header->dp_ifindex,
995 reply, info->snd_portid,
1002 ovs_nla_free_flow_actions(old_acts);
1003 ovs_flow_free(new_flow, false);
1007 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1016 ovs_flow_free(new_flow, false);
1021 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1022 const struct sw_flow_key *key,
1023 const struct sw_flow_mask *mask)
1025 struct sw_flow_actions *acts;
1026 struct sw_flow_key masked_key;
1029 acts = ovs_nla_alloc_flow_actions(nla_len(a));
1033 ovs_flow_mask_key(&masked_key, key, mask);
1034 error = ovs_nla_copy_actions(a, &masked_key, &acts);
1036 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1038 return ERR_PTR(error);
1044 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1046 struct nlattr **a = info->attrs;
1047 struct ovs_header *ovs_header = info->userhdr;
1048 struct sw_flow_key key;
1049 struct sw_flow *flow;
1050 struct sw_flow_mask mask;
1051 struct sk_buff *reply = NULL;
1052 struct datapath *dp;
1053 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1054 struct sw_flow_match match;
1059 if (!a[OVS_FLOW_ATTR_KEY]) {
1060 OVS_NLERR("Flow key attribute not present in set flow.\n");
1064 ovs_match_init(&match, &key, &mask);
1065 error = ovs_nla_get_match(&match,
1066 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1070 /* Validate actions. */
1071 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1072 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1074 error = PTR_ERR(acts);
1079 /* Can allocate before locking if have acts. */
1081 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1082 if (IS_ERR(reply)) {
1083 error = PTR_ERR(reply);
1084 goto err_kfree_acts;
1089 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1090 if (unlikely(!dp)) {
1092 goto err_unlock_ovs;
1094 /* Check that the flow exists. */
1095 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1096 if (unlikely(!flow)) {
1098 goto err_unlock_ovs;
1101 /* Update actions, if present. */
1103 old_acts = ovsl_dereference(flow->sf_acts);
1104 rcu_assign_pointer(flow->sf_acts, acts);
1106 if (unlikely(reply)) {
1107 error = ovs_flow_cmd_fill_info(dp, flow,
1108 ovs_header->dp_ifindex,
1109 reply, info->snd_portid,
1115 /* Could not alloc without acts before locking. */
1116 reply = ovs_flow_cmd_build_info(dp, flow,
1117 ovs_header->dp_ifindex,
1118 info, OVS_FLOW_CMD_NEW, false);
1119 if (unlikely(IS_ERR(reply))) {
1120 error = PTR_ERR(reply);
1121 goto err_unlock_ovs;
1126 if (a[OVS_FLOW_ATTR_CLEAR])
1127 ovs_flow_stats_clear(flow);
1131 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1133 ovs_nla_free_flow_actions(old_acts);
1145 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1147 struct nlattr **a = info->attrs;
1148 struct ovs_header *ovs_header = info->userhdr;
1149 struct sw_flow_key key;
1150 struct sk_buff *reply;
1151 struct sw_flow *flow;
1152 struct datapath *dp;
1153 struct sw_flow_match match;
1156 if (!a[OVS_FLOW_ATTR_KEY]) {
1157 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1161 ovs_match_init(&match, &key, NULL);
1162 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1167 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1173 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1179 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1180 OVS_FLOW_CMD_NEW, true);
1181 if (IS_ERR(reply)) {
1182 err = PTR_ERR(reply);
1187 return genlmsg_reply(reply, info);
1193 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1195 struct nlattr **a = info->attrs;
1196 struct ovs_header *ovs_header = info->userhdr;
1197 struct sw_flow_key key;
1198 struct sk_buff *reply;
1199 struct sw_flow *flow;
1200 struct datapath *dp;
1201 struct sw_flow_match match;
1204 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1205 ovs_match_init(&match, &key, NULL);
1206 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1212 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1213 if (unlikely(!dp)) {
1217 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1218 err = ovs_flow_tbl_flush(&dp->table);
1221 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1222 if (unlikely(!flow)) {
1227 ovs_flow_tbl_remove(&dp->table, flow);
1230 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1233 if (likely(reply)) {
1234 if (likely(!IS_ERR(reply))) {
1235 rcu_read_lock(); /* Keep RCU checker happy. */
1236 err = ovs_flow_cmd_fill_info(dp, flow,
1237 ovs_header->dp_ifindex,
1238 reply, info->snd_portid,
1243 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1245 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1246 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1251 ovs_flow_free(flow, true);
1258 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1260 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1261 struct table_instance *ti;
1262 struct datapath *dp;
1265 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1271 ti = rcu_dereference(dp->table.ti);
1273 struct sw_flow *flow;
1276 bucket = cb->args[0];
1278 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1282 if (ovs_flow_cmd_fill_info(dp, flow, ovs_header->dp_ifindex, skb,
1283 NETLINK_CB(cb->skb).portid,
1284 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1285 OVS_FLOW_CMD_NEW) < 0)
1288 cb->args[0] = bucket;
1295 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1296 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1297 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1298 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1301 static struct genl_ops dp_flow_genl_ops[] = {
1302 { .cmd = OVS_FLOW_CMD_NEW,
1303 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1304 .policy = flow_policy,
1305 .doit = ovs_flow_cmd_new
1307 { .cmd = OVS_FLOW_CMD_DEL,
1308 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1309 .policy = flow_policy,
1310 .doit = ovs_flow_cmd_del
1312 { .cmd = OVS_FLOW_CMD_GET,
1313 .flags = 0, /* OK for unprivileged users. */
1314 .policy = flow_policy,
1315 .doit = ovs_flow_cmd_get,
1316 .dumpit = ovs_flow_cmd_dump
1318 { .cmd = OVS_FLOW_CMD_SET,
1319 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1320 .policy = flow_policy,
1321 .doit = ovs_flow_cmd_set,
1325 static struct genl_family dp_flow_genl_family = {
1326 .id = GENL_ID_GENERATE,
1327 .hdrsize = sizeof(struct ovs_header),
1328 .name = OVS_FLOW_FAMILY,
1329 .version = OVS_FLOW_VERSION,
1330 .maxattr = OVS_FLOW_ATTR_MAX,
1332 .parallel_ops = true,
1333 .ops = dp_flow_genl_ops,
1334 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1335 .mcgrps = &ovs_dp_flow_multicast_group,
1339 static size_t ovs_dp_cmd_msg_size(void)
1341 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1343 msgsize += nla_total_size(IFNAMSIZ);
1344 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1345 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1346 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1351 /* Called with ovs_mutex or RCU read lock. */
1352 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1353 u32 portid, u32 seq, u32 flags, u8 cmd)
1355 struct ovs_header *ovs_header;
1356 struct ovs_dp_stats dp_stats;
1357 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1360 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1365 ovs_header->dp_ifindex = get_dpifindex(dp);
1367 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1369 goto nla_put_failure;
1371 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1372 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1374 goto nla_put_failure;
1376 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1377 sizeof(struct ovs_dp_megaflow_stats),
1378 &dp_megaflow_stats))
1379 goto nla_put_failure;
1381 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1382 goto nla_put_failure;
1384 return genlmsg_end(skb, ovs_header);
1387 genlmsg_cancel(skb, ovs_header);
1392 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1394 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1397 /* Called with rcu_read_lock or ovs_mutex. */
1398 static struct datapath *lookup_datapath(struct net *net,
1399 struct ovs_header *ovs_header,
1400 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1402 struct datapath *dp;
1404 if (!a[OVS_DP_ATTR_NAME])
1405 dp = get_dp(net, ovs_header->dp_ifindex);
1407 struct vport *vport;
1409 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1410 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1412 return dp ? dp : ERR_PTR(-ENODEV);
1415 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1417 struct datapath *dp;
1419 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1423 WARN(dp->user_features, "Dropping previously announced user features\n");
1424 dp->user_features = 0;
1427 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1429 if (a[OVS_DP_ATTR_USER_FEATURES])
1430 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1433 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1435 struct nlattr **a = info->attrs;
1436 struct vport_parms parms;
1437 struct sk_buff *reply;
1438 struct datapath *dp;
1439 struct vport *vport;
1440 struct ovs_net *ovs_net;
1444 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1447 reply = ovs_dp_cmd_alloc_info(info);
1452 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1454 goto err_free_reply;
1456 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1458 /* Allocate table. */
1459 err = ovs_flow_tbl_init(&dp->table);
1463 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1464 if (!dp->stats_percpu) {
1466 goto err_destroy_table;
1469 for_each_possible_cpu(i) {
1470 struct dp_stats_percpu *dpath_stats;
1471 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1472 u64_stats_init(&dpath_stats->sync);
1475 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1479 goto err_destroy_percpu;
1482 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1483 INIT_HLIST_HEAD(&dp->ports[i]);
1485 /* Set up our datapath device. */
1486 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1487 parms.type = OVS_VPORT_TYPE_INTERNAL;
1488 parms.options = NULL;
1490 parms.port_no = OVSP_LOCAL;
1491 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1493 ovs_dp_change(dp, a);
1495 /* So far only local changes have been made, now need the lock. */
1498 vport = new_vport(&parms);
1499 if (IS_ERR(vport)) {
1500 err = PTR_ERR(vport);
1504 if (err == -EEXIST) {
1505 /* An outdated user space instance that does not understand
1506 * the concept of user_features has attempted to create a new
1507 * datapath and is likely to reuse it. Drop all user features.
1509 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1510 ovs_dp_reset_user_features(skb, info);
1513 goto err_destroy_ports_array;
1516 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1517 info->snd_seq, 0, OVS_DP_CMD_NEW);
1520 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1521 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1525 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1528 err_destroy_ports_array:
1532 free_percpu(dp->stats_percpu);
1534 ovs_flow_tbl_destroy(&dp->table);
1536 release_net(ovs_dp_get_net(dp));
1544 /* Called with ovs_mutex. */
1545 static void __dp_destroy(struct datapath *dp)
1549 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1550 struct vport *vport;
1551 struct hlist_node *n;
1553 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1554 if (vport->port_no != OVSP_LOCAL)
1555 ovs_dp_detach_port(vport);
1558 list_del_rcu(&dp->list_node);
1560 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1561 * all ports in datapath are destroyed first before freeing datapath.
1563 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1565 /* RCU destroy the flow table */
1566 call_rcu(&dp->rcu, destroy_dp_rcu);
1569 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1571 struct sk_buff *reply;
1572 struct datapath *dp;
1575 reply = ovs_dp_cmd_alloc_info(info);
1580 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1583 goto err_unlock_free;
1585 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1586 info->snd_seq, 0, OVS_DP_CMD_DEL);
1592 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1601 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1603 struct sk_buff *reply;
1604 struct datapath *dp;
1607 reply = ovs_dp_cmd_alloc_info(info);
1612 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1615 goto err_unlock_free;
1617 ovs_dp_change(dp, info->attrs);
1619 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1620 info->snd_seq, 0, OVS_DP_CMD_NEW);
1624 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1633 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1635 struct sk_buff *reply;
1636 struct datapath *dp;
1639 reply = ovs_dp_cmd_alloc_info(info);
1644 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1647 goto err_unlock_free;
1649 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1650 info->snd_seq, 0, OVS_DP_CMD_NEW);
1654 return genlmsg_reply(reply, info);
1662 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1664 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1665 struct datapath *dp;
1666 int skip = cb->args[0];
1670 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1672 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1673 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1674 OVS_DP_CMD_NEW) < 0)
1685 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1686 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1687 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1688 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1691 static struct genl_ops dp_datapath_genl_ops[] = {
1692 { .cmd = OVS_DP_CMD_NEW,
1693 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1694 .policy = datapath_policy,
1695 .doit = ovs_dp_cmd_new
1697 { .cmd = OVS_DP_CMD_DEL,
1698 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1699 .policy = datapath_policy,
1700 .doit = ovs_dp_cmd_del
1702 { .cmd = OVS_DP_CMD_GET,
1703 .flags = 0, /* OK for unprivileged users. */
1704 .policy = datapath_policy,
1705 .doit = ovs_dp_cmd_get,
1706 .dumpit = ovs_dp_cmd_dump
1708 { .cmd = OVS_DP_CMD_SET,
1709 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1710 .policy = datapath_policy,
1711 .doit = ovs_dp_cmd_set,
1715 static struct genl_family dp_datapath_genl_family = {
1716 .id = GENL_ID_GENERATE,
1717 .hdrsize = sizeof(struct ovs_header),
1718 .name = OVS_DATAPATH_FAMILY,
1719 .version = OVS_DATAPATH_VERSION,
1720 .maxattr = OVS_DP_ATTR_MAX,
1722 .parallel_ops = true,
1723 .ops = dp_datapath_genl_ops,
1724 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1725 .mcgrps = &ovs_dp_datapath_multicast_group,
1729 /* Called with ovs_mutex or RCU read lock. */
1730 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1731 u32 portid, u32 seq, u32 flags, u8 cmd)
1733 struct ovs_header *ovs_header;
1734 struct ovs_vport_stats vport_stats;
1737 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1742 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1744 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1745 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1746 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1747 goto nla_put_failure;
1749 ovs_vport_get_stats(vport, &vport_stats);
1750 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1752 goto nla_put_failure;
1754 if (ovs_vport_get_upcall_portids(vport, skb))
1755 goto nla_put_failure;
1757 err = ovs_vport_get_options(vport, skb);
1758 if (err == -EMSGSIZE)
1761 return genlmsg_end(skb, ovs_header);
1766 genlmsg_cancel(skb, ovs_header);
1770 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1772 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1775 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1776 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1779 struct sk_buff *skb;
1782 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1784 return ERR_PTR(-ENOMEM);
1786 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1792 /* Called with ovs_mutex or RCU read lock. */
1793 static struct vport *lookup_vport(struct net *net,
1794 struct ovs_header *ovs_header,
1795 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1797 struct datapath *dp;
1798 struct vport *vport;
1800 if (a[OVS_VPORT_ATTR_NAME]) {
1801 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1803 return ERR_PTR(-ENODEV);
1804 if (ovs_header->dp_ifindex &&
1805 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1806 return ERR_PTR(-ENODEV);
1808 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1809 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1811 if (port_no >= DP_MAX_PORTS)
1812 return ERR_PTR(-EFBIG);
1814 dp = get_dp(net, ovs_header->dp_ifindex);
1816 return ERR_PTR(-ENODEV);
1818 vport = ovs_vport_ovsl_rcu(dp, port_no);
1820 return ERR_PTR(-ENODEV);
1823 return ERR_PTR(-EINVAL);
1826 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1828 struct nlattr **a = info->attrs;
1829 struct ovs_header *ovs_header = info->userhdr;
1830 struct vport_parms parms;
1831 struct sk_buff *reply;
1832 struct vport *vport;
1833 struct datapath *dp;
1837 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1838 !a[OVS_VPORT_ATTR_UPCALL_PID])
1841 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1842 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1843 if (port_no >= DP_MAX_PORTS)
1846 reply = ovs_vport_cmd_alloc_info();
1851 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1854 goto exit_unlock_free;
1857 vport = ovs_vport_ovsl(dp, port_no);
1860 goto exit_unlock_free;
1862 for (port_no = 1; ; port_no++) {
1863 if (port_no >= DP_MAX_PORTS) {
1865 goto exit_unlock_free;
1867 vport = ovs_vport_ovsl(dp, port_no);
1873 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1874 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1875 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1877 parms.port_no = port_no;
1878 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1880 vport = new_vport(&parms);
1881 err = PTR_ERR(vport);
1883 goto exit_unlock_free;
1886 if (a[OVS_VPORT_ATTR_STATS])
1887 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1889 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1890 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1894 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1903 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1905 struct nlattr **a = info->attrs;
1906 struct sk_buff *reply;
1907 struct vport *vport;
1910 reply = ovs_vport_cmd_alloc_info();
1915 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1916 err = PTR_ERR(vport);
1918 goto exit_unlock_free;
1920 if (a[OVS_VPORT_ATTR_TYPE] &&
1921 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1923 goto exit_unlock_free;
1926 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1927 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1929 goto exit_unlock_free;
1932 if (a[OVS_VPORT_ATTR_STATS])
1933 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1936 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1937 err = ovs_vport_set_upcall_portids(vport,
1938 a[OVS_VPORT_ATTR_UPCALL_PID]);
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_del(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 (vport->port_no == OVSP_LOCAL) {
1976 goto exit_unlock_free;
1979 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1980 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1982 ovs_dp_detach_port(vport);
1985 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1994 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1996 struct nlattr **a = info->attrs;
1997 struct ovs_header *ovs_header = info->userhdr;
1998 struct sk_buff *reply;
1999 struct vport *vport;
2002 reply = ovs_vport_cmd_alloc_info();
2007 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2008 err = PTR_ERR(vport);
2010 goto exit_unlock_free;
2011 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2012 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2016 return genlmsg_reply(reply, info);
2024 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2026 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2027 struct datapath *dp;
2028 int bucket = cb->args[0], skip = cb->args[1];
2032 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2037 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2038 struct vport *vport;
2041 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2043 ovs_vport_cmd_fill_info(vport, skb,
2044 NETLINK_CB(cb->skb).portid,
2047 OVS_VPORT_CMD_NEW) < 0)
2063 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2064 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2065 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2066 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2067 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2068 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2069 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2072 static struct genl_ops dp_vport_genl_ops[] = {
2073 { .cmd = OVS_VPORT_CMD_NEW,
2074 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2075 .policy = vport_policy,
2076 .doit = ovs_vport_cmd_new
2078 { .cmd = OVS_VPORT_CMD_DEL,
2079 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2080 .policy = vport_policy,
2081 .doit = ovs_vport_cmd_del
2083 { .cmd = OVS_VPORT_CMD_GET,
2084 .flags = 0, /* OK for unprivileged users. */
2085 .policy = vport_policy,
2086 .doit = ovs_vport_cmd_get,
2087 .dumpit = ovs_vport_cmd_dump
2089 { .cmd = OVS_VPORT_CMD_SET,
2090 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2091 .policy = vport_policy,
2092 .doit = ovs_vport_cmd_set,
2096 struct genl_family dp_vport_genl_family = {
2097 .id = GENL_ID_GENERATE,
2098 .hdrsize = sizeof(struct ovs_header),
2099 .name = OVS_VPORT_FAMILY,
2100 .version = OVS_VPORT_VERSION,
2101 .maxattr = OVS_VPORT_ATTR_MAX,
2103 .parallel_ops = true,
2104 .ops = dp_vport_genl_ops,
2105 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2106 .mcgrps = &ovs_dp_vport_multicast_group,
2110 static struct genl_family *dp_genl_families[] = {
2111 &dp_datapath_genl_family,
2112 &dp_vport_genl_family,
2113 &dp_flow_genl_family,
2114 &dp_packet_genl_family,
2117 static void dp_unregister_genl(int n_families)
2121 for (i = 0; i < n_families; i++)
2122 genl_unregister_family(dp_genl_families[i]);
2125 static int dp_register_genl(void)
2130 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2132 err = genl_register_family(dp_genl_families[i]);
2140 dp_unregister_genl(i);
2144 static int __net_init ovs_init_net(struct net *net)
2146 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2148 INIT_LIST_HEAD(&ovs_net->dps);
2149 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2153 static void __net_exit ovs_exit_net(struct net *net)
2155 struct datapath *dp, *dp_next;
2156 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2159 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2163 cancel_work_sync(&ovs_net->dp_notify_work);
2166 static struct pernet_operations ovs_net_ops = {
2167 .init = ovs_init_net,
2168 .exit = ovs_exit_net,
2170 .size = sizeof(struct ovs_net),
2173 DEFINE_COMPAT_PNET_REG_FUNC(device);
2175 static int __init dp_init(void)
2179 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2181 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2184 err = ovs_flow_init();
2188 err = ovs_vport_init();
2190 goto error_flow_exit;
2192 err = register_pernet_device(&ovs_net_ops);
2194 goto error_vport_exit;
2196 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2198 goto error_netns_exit;
2200 err = dp_register_genl();
2202 goto error_unreg_notifier;
2206 error_unreg_notifier:
2207 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2209 unregister_pernet_device(&ovs_net_ops);
2218 static void dp_cleanup(void)
2220 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2221 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2222 unregister_pernet_device(&ovs_net_ops);
2228 module_init(dp_init);
2229 module_exit(dp_cleanup);
2231 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2232 MODULE_LICENSE("GPL");
2233 MODULE_VERSION(VERSION);