2 * Copyright (c) 2007-2012 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>
58 #include "genl_exec.h"
61 #include "vport-internal_dev.h"
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
64 LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
65 #error Kernels before 2.6.18 or after 3.8 are not supported by this version of Open vSwitch.
68 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
69 static void rehash_flow_table(struct work_struct *work);
70 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
72 int ovs_net_id __read_mostly;
77 * Writes to device state (add/remove datapath, port, set operations on vports,
78 * etc.) are protected by RTNL.
80 * Writes to other state (flow table modifications, set miscellaneous datapath
81 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
84 * Reads are protected by RCU.
86 * There are a few special cases (mostly stats) that have their own
87 * synchronization but they nest under all of above and don't interact with
91 static struct vport *new_vport(const struct vport_parms *);
92 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
93 const struct dp_upcall_info *);
94 static int queue_userspace_packet(struct net *, int dp_ifindex,
96 const struct dp_upcall_info *);
98 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
99 static struct datapath *get_dp(struct net *net, int dp_ifindex)
101 struct datapath *dp = NULL;
102 struct net_device *dev;
105 dev = dev_get_by_index_rcu(net, dp_ifindex);
107 struct vport *vport = ovs_internal_dev_get_vport(dev);
116 /* Must be called with rcu_read_lock or RTNL lock. */
117 const char *ovs_dp_name(const struct datapath *dp)
119 struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
120 return vport->ops->get_name(vport);
123 static int get_dpifindex(struct datapath *dp)
130 local = ovs_vport_rcu(dp, OVSP_LOCAL);
132 ifindex = local->ops->get_ifindex(local);
141 static void destroy_dp_rcu(struct rcu_head *rcu)
143 struct datapath *dp = container_of(rcu, struct datapath, rcu);
145 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
146 free_percpu(dp->stats_percpu);
147 release_net(ovs_dp_get_net(dp));
152 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
155 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
158 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
161 struct hlist_node *n;
162 struct hlist_head *head;
164 head = vport_hash_bucket(dp, port_no);
165 hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
166 if (vport->port_no == port_no)
172 /* Called with RTNL lock and genl_lock. */
173 static struct vport *new_vport(const struct vport_parms *parms)
177 vport = ovs_vport_add(parms);
178 if (!IS_ERR(vport)) {
179 struct datapath *dp = parms->dp;
180 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
182 hlist_add_head_rcu(&vport->dp_hash_node, head);
187 /* Called with RTNL lock. */
188 void ovs_dp_detach_port(struct vport *p)
192 /* First drop references to device. */
193 hlist_del_rcu(&p->dp_hash_node);
195 /* Then destroy it. */
199 /* Must be called with rcu_read_lock. */
200 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
202 struct datapath *dp = p->dp;
203 struct sw_flow *flow;
204 struct dp_stats_percpu *stats;
208 stats = this_cpu_ptr(dp->stats_percpu);
210 if (!OVS_CB(skb)->flow) {
211 struct sw_flow_key key;
214 /* Extract flow from 'skb' into 'key'. */
215 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
216 if (unlikely(error)) {
222 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
224 if (unlikely(!flow)) {
225 struct dp_upcall_info upcall;
227 upcall.cmd = OVS_PACKET_CMD_MISS;
229 upcall.userdata = NULL;
230 upcall.portid = p->upcall_portid;
231 ovs_dp_upcall(dp, skb, &upcall);
233 stats_counter = &stats->n_missed;
237 OVS_CB(skb)->flow = flow;
240 stats_counter = &stats->n_hit;
241 ovs_flow_used(OVS_CB(skb)->flow, skb);
242 ovs_execute_actions(dp, skb);
245 /* Update datapath statistics. */
246 u64_stats_update_begin(&stats->sync);
248 u64_stats_update_end(&stats->sync);
251 static struct genl_family dp_packet_genl_family = {
252 .id = GENL_ID_GENERATE,
253 .hdrsize = sizeof(struct ovs_header),
254 .name = OVS_PACKET_FAMILY,
255 .version = OVS_PACKET_VERSION,
256 .maxattr = OVS_PACKET_ATTR_MAX,
260 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
261 const struct dp_upcall_info *upcall_info)
263 struct dp_stats_percpu *stats;
267 if (upcall_info->portid == 0) {
272 dp_ifindex = get_dpifindex(dp);
278 forward_ip_summed(skb, true);
280 if (!skb_is_gso(skb))
281 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
283 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
290 stats = this_cpu_ptr(dp->stats_percpu);
292 u64_stats_update_begin(&stats->sync);
294 u64_stats_update_end(&stats->sync);
299 static int queue_gso_packets(struct net *net, int dp_ifindex,
301 const struct dp_upcall_info *upcall_info)
303 unsigned short gso_type = skb_shinfo(skb)->gso_type;
304 struct dp_upcall_info later_info;
305 struct sw_flow_key later_key;
306 struct sk_buff *segs, *nskb;
309 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
311 return PTR_ERR(segs);
313 /* Queue all of the segments. */
316 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
320 if (skb == segs && gso_type & SKB_GSO_UDP) {
321 /* The initial flow key extracted by ovs_flow_extract()
322 * in this case is for a first fragment, so we need to
323 * properly mark later fragments.
325 later_key = *upcall_info->key;
326 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
328 later_info = *upcall_info;
329 later_info.key = &later_key;
330 upcall_info = &later_info;
332 } while ((skb = skb->next));
334 /* Free all of the segments. */
342 } while ((skb = nskb));
346 static int queue_userspace_packet(struct net *net, int dp_ifindex,
348 const struct dp_upcall_info *upcall_info)
350 struct ovs_header *upcall;
351 struct sk_buff *nskb = NULL;
352 struct sk_buff *user_skb; /* to be queued to userspace */
357 if (vlan_tx_tag_present(skb)) {
358 nskb = skb_clone(skb, GFP_ATOMIC);
362 err = vlan_deaccel_tag(nskb);
369 if (nla_attr_size(skb->len) > USHRT_MAX) {
374 len = sizeof(struct ovs_header);
375 len += nla_total_size(skb->len);
376 len += nla_total_size(FLOW_BUFSIZE);
377 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
378 len += nla_total_size(8);
380 user_skb = genlmsg_new(len, GFP_ATOMIC);
386 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
387 0, upcall_info->cmd);
388 upcall->dp_ifindex = dp_ifindex;
390 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
391 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
392 nla_nest_end(user_skb, nla);
394 if (upcall_info->userdata)
395 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
396 nla_get_u64(upcall_info->userdata));
398 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
400 skb_copy_and_csum_dev(skb, nla_data(nla));
402 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
409 /* Called with genl_mutex. */
410 static int flush_flows(struct datapath *dp)
412 struct flow_table *old_table;
413 struct flow_table *new_table;
415 old_table = genl_dereference(dp->table);
416 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
420 rcu_assign_pointer(dp->table, new_table);
422 ovs_flow_tbl_deferred_destroy(old_table);
426 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
429 struct sw_flow_actions *acts;
431 int req_size = NLA_ALIGN(attr_len);
432 int next_offset = offsetof(struct sw_flow_actions, actions) +
435 if (req_size <= (ksize(*sfa) - next_offset))
438 new_acts_size = ksize(*sfa) * 2;
440 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
441 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
442 return ERR_PTR(-EMSGSIZE);
443 new_acts_size = MAX_ACTIONS_BUFSIZE;
446 acts = ovs_flow_actions_alloc(new_acts_size);
450 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
451 acts->actions_len = (*sfa)->actions_len;
456 (*sfa)->actions_len += req_size;
457 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
460 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
464 a = reserve_sfa_size(sfa, nla_attr_size(len));
468 a->nla_type = attrtype;
469 a->nla_len = nla_attr_size(len);
472 memcpy(nla_data(a), data, len);
473 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
478 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
480 int used = (*sfa)->actions_len;
483 err = add_action(sfa, attrtype, NULL, 0);
490 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
492 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
494 a->nla_len = sfa->actions_len - st_offset;
497 static int validate_and_copy_actions(const struct nlattr *attr,
498 const struct sw_flow_key *key, int depth,
499 struct sw_flow_actions **sfa);
501 static int validate_and_copy_sample(const struct nlattr *attr,
502 const struct sw_flow_key *key, int depth,
503 struct sw_flow_actions **sfa)
505 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
506 const struct nlattr *probability, *actions;
507 const struct nlattr *a;
508 int rem, start, err, st_acts;
510 memset(attrs, 0, sizeof(attrs));
511 nla_for_each_nested(a, attr, rem) {
512 int type = nla_type(a);
513 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
520 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
521 if (!probability || nla_len(probability) != sizeof(u32))
524 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
525 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
528 /* validation done, copy sample action. */
529 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
532 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
535 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
539 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
543 add_nested_action_end(*sfa, st_acts);
544 add_nested_action_end(*sfa, start);
549 static int validate_tp_port(const struct sw_flow_key *flow_key)
551 if (flow_key->eth.type == htons(ETH_P_IP)) {
552 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
554 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
555 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
562 static int validate_and_copy_set_tun(const struct nlattr *attr,
563 struct sw_flow_actions **sfa)
565 struct ovs_key_ipv4_tunnel tun_key;
568 err = ipv4_tun_from_nlattr(nla_data(attr), &tun_key);
572 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
576 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key, sizeof(tun_key));
577 add_nested_action_end(*sfa, start);
582 static int validate_set(const struct nlattr *a,
583 const struct sw_flow_key *flow_key,
584 struct sw_flow_actions **sfa,
587 const struct nlattr *ovs_key = nla_data(a);
588 int key_type = nla_type(ovs_key);
590 /* There can be only one key in a action */
591 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
594 if (key_type > OVS_KEY_ATTR_MAX ||
595 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
596 ovs_key_lens[key_type] != -1))
600 const struct ovs_key_ipv4 *ipv4_key;
601 const struct ovs_key_ipv6 *ipv6_key;
604 case OVS_KEY_ATTR_PRIORITY:
605 case OVS_KEY_ATTR_TUN_ID:
606 case OVS_KEY_ATTR_ETHERNET:
609 case OVS_KEY_ATTR_SKB_MARK:
610 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
611 if (nla_get_u32(ovs_key) != 0)
616 case OVS_KEY_ATTR_TUNNEL:
618 err = validate_and_copy_set_tun(a, sfa);
623 case OVS_KEY_ATTR_IPV4:
624 if (flow_key->eth.type != htons(ETH_P_IP))
627 if (!flow_key->ip.proto)
630 ipv4_key = nla_data(ovs_key);
631 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
634 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
639 case OVS_KEY_ATTR_IPV6:
640 if (flow_key->eth.type != htons(ETH_P_IPV6))
643 if (!flow_key->ip.proto)
646 ipv6_key = nla_data(ovs_key);
647 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
650 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
653 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
658 case OVS_KEY_ATTR_TCP:
659 if (flow_key->ip.proto != IPPROTO_TCP)
662 return validate_tp_port(flow_key);
664 case OVS_KEY_ATTR_UDP:
665 if (flow_key->ip.proto != IPPROTO_UDP)
668 return validate_tp_port(flow_key);
677 static int validate_userspace(const struct nlattr *attr)
679 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
680 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
681 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
683 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
686 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
687 attr, userspace_policy);
691 if (!a[OVS_USERSPACE_ATTR_PID] ||
692 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
698 static int copy_action(const struct nlattr *from,
699 struct sw_flow_actions **sfa)
701 int totlen = NLA_ALIGN(from->nla_len);
704 to = reserve_sfa_size(sfa, from->nla_len);
708 memcpy(to, from, totlen);
712 static int validate_and_copy_actions(const struct nlattr *attr,
713 const struct sw_flow_key *key,
715 struct sw_flow_actions **sfa)
717 const struct nlattr *a;
720 if (depth >= SAMPLE_ACTION_DEPTH)
723 nla_for_each_nested(a, attr, rem) {
724 /* Expected argument lengths, (u32)-1 for variable length. */
725 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
726 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
727 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
728 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
729 [OVS_ACTION_ATTR_POP_VLAN] = 0,
730 [OVS_ACTION_ATTR_SET] = (u32)-1,
731 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
733 const struct ovs_action_push_vlan *vlan;
734 int type = nla_type(a);
737 if (type > OVS_ACTION_ATTR_MAX ||
738 (action_lens[type] != nla_len(a) &&
739 action_lens[type] != (u32)-1))
744 case OVS_ACTION_ATTR_UNSPEC:
747 case OVS_ACTION_ATTR_USERSPACE:
748 err = validate_userspace(a);
753 case OVS_ACTION_ATTR_OUTPUT:
754 if (nla_get_u32(a) >= DP_MAX_PORTS)
759 case OVS_ACTION_ATTR_POP_VLAN:
762 case OVS_ACTION_ATTR_PUSH_VLAN:
764 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
766 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
770 case OVS_ACTION_ATTR_SET:
771 err = validate_set(a, key, sfa, &skip_copy);
776 case OVS_ACTION_ATTR_SAMPLE:
777 err = validate_and_copy_sample(a, key, depth, sfa);
787 err = copy_action(a, sfa);
799 static void clear_stats(struct sw_flow *flow)
803 flow->packet_count = 0;
804 flow->byte_count = 0;
807 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
809 struct ovs_header *ovs_header = info->userhdr;
810 struct nlattr **a = info->attrs;
811 struct sw_flow_actions *acts;
812 struct sk_buff *packet;
813 struct sw_flow *flow;
821 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
822 !a[OVS_PACKET_ATTR_ACTIONS] ||
823 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
826 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
827 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
831 skb_reserve(packet, NET_IP_ALIGN);
833 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
835 skb_reset_mac_header(packet);
836 eth = eth_hdr(packet);
838 /* Normally, setting the skb 'protocol' field would be handled by a
839 * call to eth_type_trans(), but it assumes there's a sending
840 * device, which we may not have. */
841 if (ntohs(eth->h_proto) >= 1536)
842 packet->protocol = eth->h_proto;
844 packet->protocol = htons(ETH_P_802_2);
846 /* Build an sw_flow for sending this packet. */
847 flow = ovs_flow_alloc();
852 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
856 err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]);
859 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
864 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
865 rcu_assign_pointer(flow->sf_acts, acts);
869 OVS_CB(packet)->flow = flow;
870 packet->priority = flow->key.phy.priority;
871 skb_set_mark(packet, flow->key.phy.skb_mark);
874 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
880 err = ovs_execute_actions(dp, packet);
897 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
898 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
899 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
900 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
903 static struct genl_ops dp_packet_genl_ops[] = {
904 { .cmd = OVS_PACKET_CMD_EXECUTE,
905 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
906 .policy = packet_policy,
907 .doit = ovs_packet_cmd_execute
911 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
914 struct flow_table *table = genl_dereference(dp->table);
916 stats->n_flows = ovs_flow_tbl_count(table);
918 stats->n_hit = stats->n_missed = stats->n_lost = 0;
919 for_each_possible_cpu(i) {
920 const struct dp_stats_percpu *percpu_stats;
921 struct dp_stats_percpu local_stats;
924 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
927 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
928 local_stats = *percpu_stats;
929 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
931 stats->n_hit += local_stats.n_hit;
932 stats->n_missed += local_stats.n_missed;
933 stats->n_lost += local_stats.n_lost;
937 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
938 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
939 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
940 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
943 static struct genl_family dp_flow_genl_family = {
944 .id = GENL_ID_GENERATE,
945 .hdrsize = sizeof(struct ovs_header),
946 .name = OVS_FLOW_FAMILY,
947 .version = OVS_FLOW_VERSION,
948 .maxattr = OVS_FLOW_ATTR_MAX,
952 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
953 .name = OVS_FLOW_MCGROUP
956 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
957 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
959 const struct nlattr *a;
960 struct nlattr *start;
963 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
967 nla_for_each_nested(a, attr, rem) {
968 int type = nla_type(a);
969 struct nlattr *st_sample;
972 case OVS_SAMPLE_ATTR_PROBABILITY:
973 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
976 case OVS_SAMPLE_ATTR_ACTIONS:
977 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
980 err = actions_to_attr(nla_data(a), nla_len(a), skb);
983 nla_nest_end(skb, st_sample);
988 nla_nest_end(skb, start);
992 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
994 const struct nlattr *ovs_key = nla_data(a);
995 int key_type = nla_type(ovs_key);
996 struct nlattr *start;
1000 case OVS_KEY_ATTR_IPV4_TUNNEL:
1001 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1005 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key));
1008 nla_nest_end(skb, start);
1011 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1019 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1021 const struct nlattr *a;
1024 nla_for_each_attr(a, attr, len, rem) {
1025 int type = nla_type(a);
1028 case OVS_ACTION_ATTR_SET:
1029 err = set_action_to_attr(a, skb);
1034 case OVS_ACTION_ATTR_SAMPLE:
1035 err = sample_action_to_attr(a, skb);
1040 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1049 /* Called with genl_lock. */
1050 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1051 struct sk_buff *skb, u32 portid,
1052 u32 seq, u32 flags, u8 cmd)
1054 const int skb_orig_len = skb->len;
1055 const struct sw_flow_actions *sf_acts;
1056 struct nlattr *start;
1057 struct ovs_flow_stats stats;
1058 struct ovs_header *ovs_header;
1064 sf_acts = rcu_dereference_protected(flow->sf_acts,
1065 lockdep_genl_is_held());
1067 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1071 ovs_header->dp_ifindex = get_dpifindex(dp);
1073 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1075 goto nla_put_failure;
1076 err = ovs_flow_to_nlattrs(&flow->key, skb);
1079 nla_nest_end(skb, nla);
1081 spin_lock_bh(&flow->lock);
1083 stats.n_packets = flow->packet_count;
1084 stats.n_bytes = flow->byte_count;
1085 tcp_flags = flow->tcp_flags;
1086 spin_unlock_bh(&flow->lock);
1089 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1090 goto nla_put_failure;
1092 if (stats.n_packets &&
1093 nla_put(skb, OVS_FLOW_ATTR_STATS,
1094 sizeof(struct ovs_flow_stats), &stats))
1095 goto nla_put_failure;
1098 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1099 goto nla_put_failure;
1101 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1102 * this is the first flow to be dumped into 'skb'. This is unusual for
1103 * Netlink but individual action lists can be longer than
1104 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1105 * The userspace caller can always fetch the actions separately if it
1106 * really wants them. (Most userspace callers in fact don't care.)
1108 * This can only fail for dump operations because the skb is always
1109 * properly sized for single flows.
1111 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1113 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1115 nla_nest_end(skb, start);
1120 nla_nest_cancel(skb, start);
1122 } else if (skb_orig_len)
1123 goto nla_put_failure;
1125 return genlmsg_end(skb, ovs_header);
1130 genlmsg_cancel(skb, ovs_header);
1134 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1136 const struct sw_flow_actions *sf_acts;
1139 sf_acts = rcu_dereference_protected(flow->sf_acts,
1140 lockdep_genl_is_held());
1142 /* OVS_FLOW_ATTR_KEY */
1143 len = nla_total_size(FLOW_BUFSIZE);
1144 /* OVS_FLOW_ATTR_ACTIONS */
1145 len += nla_total_size(sf_acts->actions_len);
1146 /* OVS_FLOW_ATTR_STATS */
1147 len += nla_total_size(sizeof(struct ovs_flow_stats));
1148 /* OVS_FLOW_ATTR_TCP_FLAGS */
1149 len += nla_total_size(1);
1150 /* OVS_FLOW_ATTR_USED */
1151 len += nla_total_size(8);
1153 len += NLMSG_ALIGN(sizeof(struct ovs_header));
1155 return genlmsg_new(len, GFP_KERNEL);
1158 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1159 struct datapath *dp,
1160 u32 portid, u32 seq, u8 cmd)
1162 struct sk_buff *skb;
1165 skb = ovs_flow_cmd_alloc_info(flow);
1167 return ERR_PTR(-ENOMEM);
1169 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1174 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1176 struct nlattr **a = info->attrs;
1177 struct ovs_header *ovs_header = info->userhdr;
1178 struct sw_flow_key key;
1179 struct sw_flow *flow;
1180 struct sk_buff *reply;
1181 struct datapath *dp;
1182 struct flow_table *table;
1183 struct sw_flow_actions *acts = NULL;
1189 if (!a[OVS_FLOW_ATTR_KEY])
1191 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1195 /* Validate actions. */
1196 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1197 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1198 error = PTR_ERR(acts);
1202 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0, &acts);
1205 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1210 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1215 table = genl_dereference(dp->table);
1216 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1218 /* Bail out if we're not allowed to create a new flow. */
1220 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1223 /* Expand table, if necessary, to make room. */
1224 if (ovs_flow_tbl_need_to_expand(table)) {
1225 struct flow_table *new_table;
1227 new_table = ovs_flow_tbl_expand(table);
1228 if (!IS_ERR(new_table)) {
1229 rcu_assign_pointer(dp->table, new_table);
1230 ovs_flow_tbl_deferred_destroy(table);
1231 table = genl_dereference(dp->table);
1235 /* Allocate flow. */
1236 flow = ovs_flow_alloc();
1238 error = PTR_ERR(flow);
1243 rcu_assign_pointer(flow->sf_acts, acts);
1245 /* Put flow in bucket. */
1246 ovs_flow_tbl_insert(table, flow, &key, key_len);
1248 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1252 /* We found a matching flow. */
1253 struct sw_flow_actions *old_acts;
1255 /* Bail out if we're not allowed to modify an existing flow.
1256 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1257 * because Generic Netlink treats the latter as a dump
1258 * request. We also accept NLM_F_EXCL in case that bug ever
1262 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1263 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1266 /* Update actions. */
1267 old_acts = rcu_dereference_protected(flow->sf_acts,
1268 lockdep_genl_is_held());
1269 rcu_assign_pointer(flow->sf_acts, acts);
1270 ovs_flow_deferred_free_acts(old_acts);
1272 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1273 info->snd_seq, OVS_FLOW_CMD_NEW);
1276 if (a[OVS_FLOW_ATTR_CLEAR]) {
1277 spin_lock_bh(&flow->lock);
1279 spin_unlock_bh(&flow->lock);
1284 genl_notify(reply, genl_info_net(info), info->snd_portid,
1285 ovs_dp_flow_multicast_group.id, info->nlhdr,
1288 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1289 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1298 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1300 struct nlattr **a = info->attrs;
1301 struct ovs_header *ovs_header = info->userhdr;
1302 struct sw_flow_key key;
1303 struct sk_buff *reply;
1304 struct sw_flow *flow;
1305 struct datapath *dp;
1306 struct flow_table *table;
1310 if (!a[OVS_FLOW_ATTR_KEY])
1312 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1316 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1320 table = genl_dereference(dp->table);
1321 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1325 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1326 info->snd_seq, OVS_FLOW_CMD_NEW);
1328 return PTR_ERR(reply);
1330 return genlmsg_reply(reply, info);
1333 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1335 struct nlattr **a = info->attrs;
1336 struct ovs_header *ovs_header = info->userhdr;
1337 struct sw_flow_key key;
1338 struct sk_buff *reply;
1339 struct sw_flow *flow;
1340 struct datapath *dp;
1341 struct flow_table *table;
1345 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1349 if (!a[OVS_FLOW_ATTR_KEY])
1350 return flush_flows(dp);
1352 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1356 table = genl_dereference(dp->table);
1357 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1361 reply = ovs_flow_cmd_alloc_info(flow);
1365 ovs_flow_tbl_remove(table, flow);
1367 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1368 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1371 ovs_flow_deferred_free(flow);
1373 genl_notify(reply, genl_info_net(info), info->snd_portid,
1374 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1378 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1380 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1381 struct datapath *dp;
1382 struct flow_table *table;
1384 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1388 table = genl_dereference(dp->table);
1391 struct sw_flow *flow;
1394 bucket = cb->args[0];
1396 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1400 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1401 NETLINK_CB(cb->skb).portid,
1402 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1403 OVS_FLOW_CMD_NEW) < 0)
1406 cb->args[0] = bucket;
1412 static struct genl_ops dp_flow_genl_ops[] = {
1413 { .cmd = OVS_FLOW_CMD_NEW,
1414 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1415 .policy = flow_policy,
1416 .doit = ovs_flow_cmd_new_or_set
1418 { .cmd = OVS_FLOW_CMD_DEL,
1419 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1420 .policy = flow_policy,
1421 .doit = ovs_flow_cmd_del
1423 { .cmd = OVS_FLOW_CMD_GET,
1424 .flags = 0, /* OK for unprivileged users. */
1425 .policy = flow_policy,
1426 .doit = ovs_flow_cmd_get,
1427 .dumpit = ovs_flow_cmd_dump
1429 { .cmd = OVS_FLOW_CMD_SET,
1430 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1431 .policy = flow_policy,
1432 .doit = ovs_flow_cmd_new_or_set,
1436 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1437 #ifdef HAVE_NLA_NUL_STRING
1438 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1440 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1443 static struct genl_family dp_datapath_genl_family = {
1444 .id = GENL_ID_GENERATE,
1445 .hdrsize = sizeof(struct ovs_header),
1446 .name = OVS_DATAPATH_FAMILY,
1447 .version = OVS_DATAPATH_VERSION,
1448 .maxattr = OVS_DP_ATTR_MAX,
1452 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1453 .name = OVS_DATAPATH_MCGROUP
1456 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1457 u32 portid, u32 seq, u32 flags, u8 cmd)
1459 struct ovs_header *ovs_header;
1460 struct ovs_dp_stats dp_stats;
1463 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1468 ovs_header->dp_ifindex = get_dpifindex(dp);
1471 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1474 goto nla_put_failure;
1476 get_dp_stats(dp, &dp_stats);
1477 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1478 goto nla_put_failure;
1480 return genlmsg_end(skb, ovs_header);
1483 genlmsg_cancel(skb, ovs_header);
1488 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1491 struct sk_buff *skb;
1494 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1496 return ERR_PTR(-ENOMEM);
1498 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1501 return ERR_PTR(retval);
1506 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1508 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1511 /* Called with genl_mutex and optionally with RTNL lock also. */
1512 static struct datapath *lookup_datapath(struct net *net,
1513 struct ovs_header *ovs_header,
1514 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1516 struct datapath *dp;
1518 if (!a[OVS_DP_ATTR_NAME])
1519 dp = get_dp(net, ovs_header->dp_ifindex);
1521 struct vport *vport;
1524 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1525 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1528 return dp ? dp : ERR_PTR(-ENODEV);
1531 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1533 struct nlattr **a = info->attrs;
1534 struct vport_parms parms;
1535 struct sk_buff *reply;
1536 struct datapath *dp;
1537 struct vport *vport;
1538 struct ovs_net *ovs_net;
1542 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1545 err = ovs_dp_cmd_validate(a);
1552 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1554 goto err_unlock_rtnl;
1556 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1558 /* Allocate table. */
1560 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1564 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1565 if (!dp->stats_percpu) {
1567 goto err_destroy_table;
1570 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1574 goto err_destroy_percpu;
1577 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1578 INIT_HLIST_HEAD(&dp->ports[i]);
1580 /* Set up our datapath device. */
1581 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1582 parms.type = OVS_VPORT_TYPE_INTERNAL;
1583 parms.options = NULL;
1585 parms.port_no = OVSP_LOCAL;
1586 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1588 vport = new_vport(&parms);
1589 if (IS_ERR(vport)) {
1590 err = PTR_ERR(vport);
1594 goto err_destroy_ports_array;
1597 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1598 info->snd_seq, OVS_DP_CMD_NEW);
1599 err = PTR_ERR(reply);
1601 goto err_destroy_local_port;
1603 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1604 list_add_tail(&dp->list_node, &ovs_net->dps);
1608 genl_notify(reply, genl_info_net(info), info->snd_portid,
1609 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1613 err_destroy_local_port:
1614 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1615 err_destroy_ports_array:
1618 free_percpu(dp->stats_percpu);
1620 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1622 release_net(ovs_dp_get_net(dp));
1630 /* Called with genl_mutex. */
1631 static void __dp_destroy(struct datapath *dp)
1637 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1638 struct vport *vport;
1639 struct hlist_node *node, *n;
1641 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1642 if (vport->port_no != OVSP_LOCAL)
1643 ovs_dp_detach_port(vport);
1646 list_del(&dp->list_node);
1647 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1649 /* rtnl_unlock() will wait until all the references to devices that
1650 * are pending unregistration have been dropped. We do it here to
1651 * ensure that any internal devices (which contain DP pointers) are
1652 * fully destroyed before freeing the datapath.
1656 call_rcu(&dp->rcu, destroy_dp_rcu);
1659 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1661 struct sk_buff *reply;
1662 struct datapath *dp;
1665 err = ovs_dp_cmd_validate(info->attrs);
1669 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1674 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1675 info->snd_seq, OVS_DP_CMD_DEL);
1676 err = PTR_ERR(reply);
1682 genl_notify(reply, genl_info_net(info), info->snd_portid,
1683 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1689 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1691 struct sk_buff *reply;
1692 struct datapath *dp;
1695 err = ovs_dp_cmd_validate(info->attrs);
1699 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1703 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1704 info->snd_seq, OVS_DP_CMD_NEW);
1705 if (IS_ERR(reply)) {
1706 err = PTR_ERR(reply);
1707 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1708 ovs_dp_datapath_multicast_group.id, err);
1712 genl_notify(reply, genl_info_net(info), info->snd_portid,
1713 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1719 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1721 struct sk_buff *reply;
1722 struct datapath *dp;
1725 err = ovs_dp_cmd_validate(info->attrs);
1729 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1733 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1734 info->snd_seq, OVS_DP_CMD_NEW);
1736 return PTR_ERR(reply);
1738 return genlmsg_reply(reply, info);
1741 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1743 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1744 struct datapath *dp;
1745 int skip = cb->args[0];
1748 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1750 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1751 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1752 OVS_DP_CMD_NEW) < 0)
1762 static struct genl_ops dp_datapath_genl_ops[] = {
1763 { .cmd = OVS_DP_CMD_NEW,
1764 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1765 .policy = datapath_policy,
1766 .doit = ovs_dp_cmd_new
1768 { .cmd = OVS_DP_CMD_DEL,
1769 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1770 .policy = datapath_policy,
1771 .doit = ovs_dp_cmd_del
1773 { .cmd = OVS_DP_CMD_GET,
1774 .flags = 0, /* OK for unprivileged users. */
1775 .policy = datapath_policy,
1776 .doit = ovs_dp_cmd_get,
1777 .dumpit = ovs_dp_cmd_dump
1779 { .cmd = OVS_DP_CMD_SET,
1780 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1781 .policy = datapath_policy,
1782 .doit = ovs_dp_cmd_set,
1786 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1787 #ifdef HAVE_NLA_NUL_STRING
1788 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1789 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1791 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1793 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1794 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1795 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1796 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1799 static struct genl_family dp_vport_genl_family = {
1800 .id = GENL_ID_GENERATE,
1801 .hdrsize = sizeof(struct ovs_header),
1802 .name = OVS_VPORT_FAMILY,
1803 .version = OVS_VPORT_VERSION,
1804 .maxattr = OVS_VPORT_ATTR_MAX,
1808 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1809 .name = OVS_VPORT_MCGROUP
1812 /* Called with RTNL lock or RCU read lock. */
1813 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1814 u32 portid, u32 seq, u32 flags, u8 cmd)
1816 struct ovs_header *ovs_header;
1817 struct ovs_vport_stats vport_stats;
1820 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1825 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1827 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1828 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1829 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1830 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1831 goto nla_put_failure;
1833 ovs_vport_get_stats(vport, &vport_stats);
1834 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1836 goto nla_put_failure;
1838 err = ovs_vport_get_options(vport, skb);
1839 if (err == -EMSGSIZE)
1842 return genlmsg_end(skb, ovs_header);
1847 genlmsg_cancel(skb, ovs_header);
1851 /* Called with RTNL lock or RCU read lock. */
1852 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1855 struct sk_buff *skb;
1858 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1860 return ERR_PTR(-ENOMEM);
1862 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1865 return ERR_PTR(retval);
1870 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1872 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1875 /* Called with RTNL lock or RCU read lock. */
1876 static struct vport *lookup_vport(struct net *net,
1877 struct ovs_header *ovs_header,
1878 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1880 struct datapath *dp;
1881 struct vport *vport;
1883 if (a[OVS_VPORT_ATTR_NAME]) {
1884 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1886 return ERR_PTR(-ENODEV);
1887 if (ovs_header->dp_ifindex &&
1888 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1889 return ERR_PTR(-ENODEV);
1891 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1892 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1894 if (port_no >= DP_MAX_PORTS)
1895 return ERR_PTR(-EFBIG);
1897 dp = get_dp(net, ovs_header->dp_ifindex);
1899 return ERR_PTR(-ENODEV);
1901 vport = ovs_vport_rtnl_rcu(dp, port_no);
1903 return ERR_PTR(-ENODEV);
1906 return ERR_PTR(-EINVAL);
1909 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1911 struct nlattr **a = info->attrs;
1912 struct ovs_header *ovs_header = info->userhdr;
1913 struct vport_parms parms;
1914 struct sk_buff *reply;
1915 struct vport *vport;
1916 struct datapath *dp;
1921 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1922 !a[OVS_VPORT_ATTR_UPCALL_PID])
1925 err = ovs_vport_cmd_validate(a);
1930 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1935 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1936 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1939 if (port_no >= DP_MAX_PORTS)
1942 vport = ovs_vport_rtnl(dp, port_no);
1947 for (port_no = 1; ; port_no++) {
1948 if (port_no >= DP_MAX_PORTS) {
1952 vport = ovs_vport_rtnl(dp, port_no);
1958 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1959 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1960 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1962 parms.port_no = port_no;
1963 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1965 vport = new_vport(&parms);
1966 err = PTR_ERR(vport);
1970 if (a[OVS_VPORT_ATTR_STATS])
1971 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1973 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1975 if (IS_ERR(reply)) {
1976 err = PTR_ERR(reply);
1977 ovs_dp_detach_port(vport);
1980 genl_notify(reply, genl_info_net(info), info->snd_portid,
1981 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1989 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1991 struct nlattr **a = info->attrs;
1992 struct sk_buff *reply;
1993 struct vport *vport;
1996 err = ovs_vport_cmd_validate(a);
2001 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2002 err = PTR_ERR(vport);
2007 if (a[OVS_VPORT_ATTR_TYPE] &&
2008 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
2011 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
2012 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2016 if (a[OVS_VPORT_ATTR_STATS])
2017 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2019 if (a[OVS_VPORT_ATTR_UPCALL_PID])
2020 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2022 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2023 info->snd_seq, OVS_VPORT_CMD_NEW);
2024 if (IS_ERR(reply)) {
2025 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
2026 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
2030 genl_notify(reply, genl_info_net(info), info->snd_portid,
2031 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2039 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2041 struct nlattr **a = info->attrs;
2042 struct sk_buff *reply;
2043 struct vport *vport;
2046 err = ovs_vport_cmd_validate(a);
2051 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2052 err = PTR_ERR(vport);
2056 if (vport->port_no == OVSP_LOCAL) {
2061 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2062 info->snd_seq, OVS_VPORT_CMD_DEL);
2063 err = PTR_ERR(reply);
2067 ovs_dp_detach_port(vport);
2069 genl_notify(reply, genl_info_net(info), info->snd_portid,
2070 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2078 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2080 struct nlattr **a = info->attrs;
2081 struct ovs_header *ovs_header = info->userhdr;
2082 struct sk_buff *reply;
2083 struct vport *vport;
2086 err = ovs_vport_cmd_validate(a);
2091 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2092 err = PTR_ERR(vport);
2096 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2097 info->snd_seq, OVS_VPORT_CMD_NEW);
2098 err = PTR_ERR(reply);
2104 return genlmsg_reply(reply, info);
2112 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2114 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2115 struct datapath *dp;
2116 int bucket = cb->args[0], skip = cb->args[1];
2119 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2124 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2125 struct vport *vport;
2126 struct hlist_node *n;
2129 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
2131 ovs_vport_cmd_fill_info(vport, skb,
2132 NETLINK_CB(cb->skb).portid,
2135 OVS_VPORT_CMD_NEW) < 0)
2151 static struct genl_ops dp_vport_genl_ops[] = {
2152 { .cmd = OVS_VPORT_CMD_NEW,
2153 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2154 .policy = vport_policy,
2155 .doit = ovs_vport_cmd_new
2157 { .cmd = OVS_VPORT_CMD_DEL,
2158 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2159 .policy = vport_policy,
2160 .doit = ovs_vport_cmd_del
2162 { .cmd = OVS_VPORT_CMD_GET,
2163 .flags = 0, /* OK for unprivileged users. */
2164 .policy = vport_policy,
2165 .doit = ovs_vport_cmd_get,
2166 .dumpit = ovs_vport_cmd_dump
2168 { .cmd = OVS_VPORT_CMD_SET,
2169 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2170 .policy = vport_policy,
2171 .doit = ovs_vport_cmd_set,
2175 struct genl_family_and_ops {
2176 struct genl_family *family;
2177 struct genl_ops *ops;
2179 struct genl_multicast_group *group;
2182 static const struct genl_family_and_ops dp_genl_families[] = {
2183 { &dp_datapath_genl_family,
2184 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2185 &ovs_dp_datapath_multicast_group },
2186 { &dp_vport_genl_family,
2187 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2188 &ovs_dp_vport_multicast_group },
2189 { &dp_flow_genl_family,
2190 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2191 &ovs_dp_flow_multicast_group },
2192 { &dp_packet_genl_family,
2193 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2197 static void dp_unregister_genl(int n_families)
2201 for (i = 0; i < n_families; i++)
2202 genl_unregister_family(dp_genl_families[i].family);
2205 static int dp_register_genl(void)
2212 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2213 const struct genl_family_and_ops *f = &dp_genl_families[i];
2215 err = genl_register_family_with_ops(f->family, f->ops,
2222 err = genl_register_mc_group(f->family, f->group);
2231 dp_unregister_genl(n_registered);
2235 static int __rehash_flow_table(void *dummy)
2237 struct datapath *dp;
2242 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2244 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2245 struct flow_table *old_table = genl_dereference(dp->table);
2246 struct flow_table *new_table;
2248 new_table = ovs_flow_tbl_rehash(old_table);
2249 if (!IS_ERR(new_table)) {
2250 rcu_assign_pointer(dp->table, new_table);
2251 ovs_flow_tbl_deferred_destroy(old_table);
2259 static void rehash_flow_table(struct work_struct *work)
2261 genl_exec(__rehash_flow_table, NULL);
2262 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2265 static int dp_destroy_all(void *data)
2267 struct datapath *dp, *dp_next;
2268 struct ovs_net *ovs_net = data;
2270 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2276 static int __net_init ovs_init_net(struct net *net)
2278 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2280 INIT_LIST_HEAD(&ovs_net->dps);
2284 static void __net_exit ovs_exit_net(struct net *net)
2286 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2288 genl_exec(dp_destroy_all, ovs_net);
2291 static struct pernet_operations ovs_net_ops = {
2292 .init = ovs_init_net,
2293 .exit = ovs_exit_net,
2295 .size = sizeof(struct ovs_net),
2298 static int __init dp_init(void)
2302 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2304 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2307 err = genl_exec_init();
2311 err = ovs_workqueues_init();
2313 goto error_genl_exec;
2315 err = ovs_tnl_init();
2319 err = ovs_flow_init();
2321 goto error_tnl_exit;
2323 err = ovs_vport_init();
2325 goto error_flow_exit;
2327 err = register_pernet_device(&ovs_net_ops);
2329 goto error_vport_exit;
2331 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2333 goto error_netns_exit;
2335 err = dp_register_genl();
2337 goto error_unreg_notifier;
2339 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2343 error_unreg_notifier:
2344 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2346 unregister_pernet_device(&ovs_net_ops);
2354 ovs_workqueues_exit();
2361 static void dp_cleanup(void)
2363 cancel_delayed_work_sync(&rehash_flow_wq);
2364 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2365 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2366 unregister_pernet_device(&ovs_net_ops);
2371 ovs_workqueues_exit();
2375 module_init(dp_init);
2376 module_exit(dp_cleanup);
2378 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2379 MODULE_LICENSE("GPL");
2380 MODULE_VERSION(VERSION);