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 <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>
62 #include "vport-internal_dev.h"
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
65 LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
66 #error Kernels before 2.6.18 or after 3.8 are not supported by this version of Open vSwitch.
69 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
70 static void rehash_flow_table(struct work_struct *work);
71 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
73 int ovs_net_id __read_mostly;
75 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
76 struct genl_multicast_group *grp)
78 genl_notify(skb, genl_info_net(info), info->snd_portid,
79 grp->id, info->nlhdr, GFP_KERNEL);
85 * All writes e.g. Writes to device state (add/remove datapath, port, set
86 * operations on vports, etc.), Writes to other state (flow table
87 * modifications, set miscellaneous datapath parameters, etc.) are protected
90 * Reads are protected by RCU.
92 * There are a few special cases (mostly stats) that have their own
93 * synchronization but they nest under all of above and don't interact with
96 * The RTNL lock nests inside ovs_mutex.
99 static DEFINE_MUTEX(ovs_mutex);
103 mutex_lock(&ovs_mutex);
106 void ovs_unlock(void)
108 mutex_unlock(&ovs_mutex);
111 #ifdef CONFIG_LOCKDEP
112 int lockdep_ovsl_is_held(void)
115 return lockdep_is_held(&ovs_mutex);
121 static struct vport *new_vport(const struct vport_parms *);
122 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
123 const struct dp_upcall_info *);
124 static int queue_userspace_packet(struct net *, int dp_ifindex,
126 const struct dp_upcall_info *);
128 /* Must be called with rcu_read_lock or ovs_mutex. */
129 static struct datapath *get_dp(struct net *net, int dp_ifindex)
131 struct datapath *dp = NULL;
132 struct net_device *dev;
135 dev = dev_get_by_index_rcu(net, dp_ifindex);
137 struct vport *vport = ovs_internal_dev_get_vport(dev);
146 /* Must be called with rcu_read_lock or ovs_mutex. */
147 const char *ovs_dp_name(const struct datapath *dp)
149 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
150 return vport->ops->get_name(vport);
153 static int get_dpifindex(struct datapath *dp)
160 local = ovs_vport_rcu(dp, OVSP_LOCAL);
162 ifindex = local->ops->get_ifindex(local);
171 static void destroy_dp_rcu(struct rcu_head *rcu)
173 struct datapath *dp = container_of(rcu, struct datapath, rcu);
175 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
176 free_percpu(dp->stats_percpu);
177 release_net(ovs_dp_get_net(dp));
182 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
185 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
188 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
191 struct hlist_head *head;
193 head = vport_hash_bucket(dp, port_no);
194 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
195 if (vport->port_no == port_no)
201 /* Called with ovs_mutex. */
202 static struct vport *new_vport(const struct vport_parms *parms)
206 vport = ovs_vport_add(parms);
207 if (!IS_ERR(vport)) {
208 struct datapath *dp = parms->dp;
209 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
211 hlist_add_head_rcu(&vport->dp_hash_node, head);
216 void ovs_dp_detach_port(struct vport *p)
220 /* First drop references to device. */
221 hlist_del_rcu(&p->dp_hash_node);
223 /* Then destroy it. */
227 /* Must be called with rcu_read_lock. */
228 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
230 struct datapath *dp = p->dp;
231 struct sw_flow *flow;
232 struct dp_stats_percpu *stats;
233 struct sw_flow_key key;
238 stats = this_cpu_ptr(dp->stats_percpu);
240 /* Extract flow from 'skb' into 'key'. */
241 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
242 if (unlikely(error)) {
248 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
249 if (unlikely(!flow)) {
250 struct dp_upcall_info upcall;
252 upcall.cmd = OVS_PACKET_CMD_MISS;
254 upcall.userdata = NULL;
255 upcall.portid = p->upcall_portid;
256 ovs_dp_upcall(dp, skb, &upcall);
258 stats_counter = &stats->n_missed;
262 OVS_CB(skb)->flow = flow;
264 stats_counter = &stats->n_hit;
265 ovs_flow_used(OVS_CB(skb)->flow, skb);
266 ovs_execute_actions(dp, skb);
269 /* Update datapath statistics. */
270 u64_stats_update_begin(&stats->sync);
272 u64_stats_update_end(&stats->sync);
275 static struct genl_family dp_packet_genl_family = {
276 .id = GENL_ID_GENERATE,
277 .hdrsize = sizeof(struct ovs_header),
278 .name = OVS_PACKET_FAMILY,
279 .version = OVS_PACKET_VERSION,
280 .maxattr = OVS_PACKET_ATTR_MAX,
284 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
285 const struct dp_upcall_info *upcall_info)
287 struct dp_stats_percpu *stats;
291 if (upcall_info->portid == 0) {
296 dp_ifindex = get_dpifindex(dp);
302 forward_ip_summed(skb, true);
304 if (!skb_is_gso(skb))
305 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
307 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
314 stats = this_cpu_ptr(dp->stats_percpu);
316 u64_stats_update_begin(&stats->sync);
318 u64_stats_update_end(&stats->sync);
323 static int queue_gso_packets(struct net *net, int dp_ifindex,
325 const struct dp_upcall_info *upcall_info)
327 unsigned short gso_type = skb_shinfo(skb)->gso_type;
328 struct dp_upcall_info later_info;
329 struct sw_flow_key later_key;
330 struct sk_buff *segs, *nskb;
333 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
335 return PTR_ERR(segs);
337 /* Queue all of the segments. */
340 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
344 if (skb == segs && gso_type & SKB_GSO_UDP) {
345 /* The initial flow key extracted by ovs_flow_extract()
346 * in this case is for a first fragment, so we need to
347 * properly mark later fragments.
349 later_key = *upcall_info->key;
350 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
352 later_info = *upcall_info;
353 later_info.key = &later_key;
354 upcall_info = &later_info;
356 } while ((skb = skb->next));
358 /* Free all of the segments. */
366 } while ((skb = nskb));
370 static size_t key_attr_size(void)
372 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
373 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
374 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
375 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
376 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
377 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
378 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
379 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
380 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
381 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
382 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
383 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
385 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
386 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
387 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
388 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
389 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
390 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
393 static size_t upcall_msg_size(const struct sk_buff *skb,
394 const struct nlattr *userdata)
396 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
397 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
398 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
400 /* OVS_PACKET_ATTR_USERDATA */
402 size += NLA_ALIGN(userdata->nla_len);
407 static int queue_userspace_packet(struct net *net, int dp_ifindex,
409 const struct dp_upcall_info *upcall_info)
411 struct ovs_header *upcall;
412 struct sk_buff *nskb = NULL;
413 struct sk_buff *user_skb; /* to be queued to userspace */
417 if (vlan_tx_tag_present(skb)) {
418 nskb = skb_clone(skb, GFP_ATOMIC);
422 err = vlan_deaccel_tag(nskb);
429 if (nla_attr_size(skb->len) > USHRT_MAX) {
434 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
440 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
441 0, upcall_info->cmd);
442 upcall->dp_ifindex = dp_ifindex;
444 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
445 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
446 nla_nest_end(user_skb, nla);
448 if (upcall_info->userdata)
449 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
450 nla_len(upcall_info->userdata),
451 nla_data(upcall_info->userdata));
453 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
455 skb_copy_and_csum_dev(skb, nla_data(nla));
457 genlmsg_end(user_skb, upcall);
458 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
465 /* Called with ovs_mutex. */
466 static int flush_flows(struct datapath *dp)
468 struct flow_table *old_table;
469 struct flow_table *new_table;
471 old_table = ovsl_dereference(dp->table);
472 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
476 rcu_assign_pointer(dp->table, new_table);
478 ovs_flow_tbl_deferred_destroy(old_table);
482 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
485 struct sw_flow_actions *acts;
487 int req_size = NLA_ALIGN(attr_len);
488 int next_offset = offsetof(struct sw_flow_actions, actions) +
491 if (req_size <= (ksize(*sfa) - next_offset))
494 new_acts_size = ksize(*sfa) * 2;
496 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
497 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
498 return ERR_PTR(-EMSGSIZE);
499 new_acts_size = MAX_ACTIONS_BUFSIZE;
502 acts = ovs_flow_actions_alloc(new_acts_size);
506 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
507 acts->actions_len = (*sfa)->actions_len;
512 (*sfa)->actions_len += req_size;
513 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
516 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
520 a = reserve_sfa_size(sfa, nla_attr_size(len));
524 a->nla_type = attrtype;
525 a->nla_len = nla_attr_size(len);
528 memcpy(nla_data(a), data, len);
529 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
534 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
536 int used = (*sfa)->actions_len;
539 err = add_action(sfa, attrtype, NULL, 0);
546 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
548 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
550 a->nla_len = sfa->actions_len - st_offset;
553 static int validate_and_copy_actions(const struct nlattr *attr,
554 const struct sw_flow_key *key, int depth,
555 struct sw_flow_actions **sfa);
557 static int validate_and_copy_sample(const struct nlattr *attr,
558 const struct sw_flow_key *key, int depth,
559 struct sw_flow_actions **sfa)
561 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
562 const struct nlattr *probability, *actions;
563 const struct nlattr *a;
564 int rem, start, err, st_acts;
566 memset(attrs, 0, sizeof(attrs));
567 nla_for_each_nested(a, attr, rem) {
568 int type = nla_type(a);
569 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
576 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
577 if (!probability || nla_len(probability) != sizeof(u32))
580 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
581 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
584 /* validation done, copy sample action. */
585 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
588 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
591 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
595 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
599 add_nested_action_end(*sfa, st_acts);
600 add_nested_action_end(*sfa, start);
605 static int validate_tp_port(const struct sw_flow_key *flow_key)
607 if (flow_key->eth.type == htons(ETH_P_IP)) {
608 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
610 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
611 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
618 static int validate_and_copy_set_tun(const struct nlattr *attr,
619 struct sw_flow_actions **sfa)
621 struct ovs_key_ipv4_tunnel tun_key;
624 err = ipv4_tun_from_nlattr(nla_data(attr), &tun_key);
628 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
632 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key, sizeof(tun_key));
633 add_nested_action_end(*sfa, start);
638 static int validate_set(const struct nlattr *a,
639 const struct sw_flow_key *flow_key,
640 struct sw_flow_actions **sfa,
643 const struct nlattr *ovs_key = nla_data(a);
644 int key_type = nla_type(ovs_key);
646 /* There can be only one key in a action */
647 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
650 if (key_type > OVS_KEY_ATTR_MAX ||
651 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
652 ovs_key_lens[key_type] != -1))
656 const struct ovs_key_ipv4 *ipv4_key;
657 const struct ovs_key_ipv6 *ipv6_key;
660 case OVS_KEY_ATTR_PRIORITY:
661 case OVS_KEY_ATTR_ETHERNET:
664 case OVS_KEY_ATTR_SKB_MARK:
665 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
666 if (nla_get_u32(ovs_key) != 0)
671 case OVS_KEY_ATTR_TUNNEL:
673 err = validate_and_copy_set_tun(a, sfa);
678 case OVS_KEY_ATTR_IPV4:
679 if (flow_key->eth.type != htons(ETH_P_IP))
682 if (!flow_key->ip.proto)
685 ipv4_key = nla_data(ovs_key);
686 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
689 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
694 case OVS_KEY_ATTR_IPV6:
695 if (flow_key->eth.type != htons(ETH_P_IPV6))
698 if (!flow_key->ip.proto)
701 ipv6_key = nla_data(ovs_key);
702 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
705 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
708 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
713 case OVS_KEY_ATTR_TCP:
714 if (flow_key->ip.proto != IPPROTO_TCP)
717 return validate_tp_port(flow_key);
719 case OVS_KEY_ATTR_UDP:
720 if (flow_key->ip.proto != IPPROTO_UDP)
723 return validate_tp_port(flow_key);
732 static int validate_userspace(const struct nlattr *attr)
734 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
735 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
736 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
738 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
741 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
742 attr, userspace_policy);
746 if (!a[OVS_USERSPACE_ATTR_PID] ||
747 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
753 static int copy_action(const struct nlattr *from,
754 struct sw_flow_actions **sfa)
756 int totlen = NLA_ALIGN(from->nla_len);
759 to = reserve_sfa_size(sfa, from->nla_len);
763 memcpy(to, from, totlen);
767 static int validate_and_copy_actions(const struct nlattr *attr,
768 const struct sw_flow_key *key,
770 struct sw_flow_actions **sfa)
772 const struct nlattr *a;
775 if (depth >= SAMPLE_ACTION_DEPTH)
778 nla_for_each_nested(a, attr, rem) {
779 /* Expected argument lengths, (u32)-1 for variable length. */
780 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
781 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
782 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
783 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
784 [OVS_ACTION_ATTR_POP_VLAN] = 0,
785 [OVS_ACTION_ATTR_SET] = (u32)-1,
786 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
788 const struct ovs_action_push_vlan *vlan;
789 int type = nla_type(a);
792 if (type > OVS_ACTION_ATTR_MAX ||
793 (action_lens[type] != nla_len(a) &&
794 action_lens[type] != (u32)-1))
799 case OVS_ACTION_ATTR_UNSPEC:
802 case OVS_ACTION_ATTR_USERSPACE:
803 err = validate_userspace(a);
808 case OVS_ACTION_ATTR_OUTPUT:
809 if (nla_get_u32(a) >= DP_MAX_PORTS)
814 case OVS_ACTION_ATTR_POP_VLAN:
817 case OVS_ACTION_ATTR_PUSH_VLAN:
819 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
821 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
825 case OVS_ACTION_ATTR_SET:
826 err = validate_set(a, key, sfa, &skip_copy);
831 case OVS_ACTION_ATTR_SAMPLE:
832 err = validate_and_copy_sample(a, key, depth, sfa);
842 err = copy_action(a, sfa);
854 static void clear_stats(struct sw_flow *flow)
858 flow->packet_count = 0;
859 flow->byte_count = 0;
862 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
864 struct ovs_header *ovs_header = info->userhdr;
865 struct nlattr **a = info->attrs;
866 struct sw_flow_actions *acts;
867 struct sk_buff *packet;
868 struct sw_flow *flow;
876 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
877 !a[OVS_PACKET_ATTR_ACTIONS])
880 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
881 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
885 skb_reserve(packet, NET_IP_ALIGN);
887 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
889 skb_reset_mac_header(packet);
890 eth = eth_hdr(packet);
892 /* Normally, setting the skb 'protocol' field would be handled by a
893 * call to eth_type_trans(), but it assumes there's a sending
894 * device, which we may not have. */
895 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
896 packet->protocol = eth->h_proto;
898 packet->protocol = htons(ETH_P_802_2);
900 /* Build an sw_flow for sending this packet. */
901 flow = ovs_flow_alloc();
906 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
910 err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]);
913 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
918 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
919 rcu_assign_pointer(flow->sf_acts, acts);
923 OVS_CB(packet)->flow = flow;
924 packet->priority = flow->key.phy.priority;
925 skb_set_mark(packet, flow->key.phy.skb_mark);
928 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
934 err = ovs_execute_actions(dp, packet);
951 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
952 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
953 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
955 [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
957 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
958 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
961 static struct genl_ops dp_packet_genl_ops[] = {
962 { .cmd = OVS_PACKET_CMD_EXECUTE,
963 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
964 .policy = packet_policy,
965 .doit = ovs_packet_cmd_execute
969 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
972 struct flow_table *table = ovsl_dereference(dp->table);
974 stats->n_flows = ovs_flow_tbl_count(table);
976 stats->n_hit = stats->n_missed = stats->n_lost = 0;
977 for_each_possible_cpu(i) {
978 const struct dp_stats_percpu *percpu_stats;
979 struct dp_stats_percpu local_stats;
982 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
985 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
986 local_stats = *percpu_stats;
987 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
989 stats->n_hit += local_stats.n_hit;
990 stats->n_missed += local_stats.n_missed;
991 stats->n_lost += local_stats.n_lost;
995 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
996 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
997 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
998 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1001 static struct genl_family dp_flow_genl_family = {
1002 .id = GENL_ID_GENERATE,
1003 .hdrsize = sizeof(struct ovs_header),
1004 .name = OVS_FLOW_FAMILY,
1005 .version = OVS_FLOW_VERSION,
1006 .maxattr = OVS_FLOW_ATTR_MAX,
1010 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1011 .name = OVS_FLOW_MCGROUP
1014 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1015 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1017 const struct nlattr *a;
1018 struct nlattr *start;
1021 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1025 nla_for_each_nested(a, attr, rem) {
1026 int type = nla_type(a);
1027 struct nlattr *st_sample;
1030 case OVS_SAMPLE_ATTR_PROBABILITY:
1031 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1034 case OVS_SAMPLE_ATTR_ACTIONS:
1035 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1038 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1041 nla_nest_end(skb, st_sample);
1046 nla_nest_end(skb, start);
1050 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1052 const struct nlattr *ovs_key = nla_data(a);
1053 int key_type = nla_type(ovs_key);
1054 struct nlattr *start;
1058 case OVS_KEY_ATTR_IPV4_TUNNEL:
1059 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1063 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key));
1066 nla_nest_end(skb, start);
1069 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1077 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1079 const struct nlattr *a;
1082 nla_for_each_attr(a, attr, len, rem) {
1083 int type = nla_type(a);
1086 case OVS_ACTION_ATTR_SET:
1087 err = set_action_to_attr(a, skb);
1092 case OVS_ACTION_ATTR_SAMPLE:
1093 err = sample_action_to_attr(a, skb);
1098 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1107 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1109 return NLMSG_ALIGN(sizeof(struct ovs_header))
1110 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1111 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1112 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1113 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1114 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1117 /* Called with ovs_mutex. */
1118 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1119 struct sk_buff *skb, u32 portid,
1120 u32 seq, u32 flags, u8 cmd)
1122 const int skb_orig_len = skb->len;
1123 const struct sw_flow_actions *sf_acts;
1124 struct nlattr *start;
1125 struct ovs_flow_stats stats;
1126 struct ovs_header *ovs_header;
1132 sf_acts = ovsl_dereference(flow->sf_acts);
1134 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1138 ovs_header->dp_ifindex = get_dpifindex(dp);
1140 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1142 goto nla_put_failure;
1143 err = ovs_flow_to_nlattrs(&flow->key, skb);
1146 nla_nest_end(skb, nla);
1148 spin_lock_bh(&flow->lock);
1150 stats.n_packets = flow->packet_count;
1151 stats.n_bytes = flow->byte_count;
1152 tcp_flags = flow->tcp_flags;
1153 spin_unlock_bh(&flow->lock);
1156 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1157 goto nla_put_failure;
1159 if (stats.n_packets &&
1160 nla_put(skb, OVS_FLOW_ATTR_STATS,
1161 sizeof(struct ovs_flow_stats), &stats))
1162 goto nla_put_failure;
1165 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1166 goto nla_put_failure;
1168 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1169 * this is the first flow to be dumped into 'skb'. This is unusual for
1170 * Netlink but individual action lists can be longer than
1171 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1172 * The userspace caller can always fetch the actions separately if it
1173 * really wants them. (Most userspace callers in fact don't care.)
1175 * This can only fail for dump operations because the skb is always
1176 * properly sized for single flows.
1178 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1180 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1182 nla_nest_end(skb, start);
1187 nla_nest_cancel(skb, start);
1189 } else if (skb_orig_len)
1190 goto nla_put_failure;
1192 return genlmsg_end(skb, ovs_header);
1197 genlmsg_cancel(skb, ovs_header);
1201 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1203 const struct sw_flow_actions *sf_acts;
1205 sf_acts = ovsl_dereference(flow->sf_acts);
1207 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
1210 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1211 struct datapath *dp,
1212 u32 portid, u32 seq, u8 cmd)
1214 struct sk_buff *skb;
1217 skb = ovs_flow_cmd_alloc_info(flow);
1219 return ERR_PTR(-ENOMEM);
1221 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1226 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1228 struct nlattr **a = info->attrs;
1229 struct ovs_header *ovs_header = info->userhdr;
1230 struct sw_flow_key key;
1231 struct sw_flow *flow;
1232 struct sk_buff *reply;
1233 struct datapath *dp;
1234 struct flow_table *table;
1235 struct sw_flow_actions *acts = NULL;
1241 if (!a[OVS_FLOW_ATTR_KEY])
1243 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1247 /* Validate actions. */
1248 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1249 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1250 error = PTR_ERR(acts);
1254 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0, &acts);
1257 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1263 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1266 goto err_unlock_ovs;
1268 table = ovsl_dereference(dp->table);
1269 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1271 /* Bail out if we're not allowed to create a new flow. */
1273 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1274 goto err_unlock_ovs;
1276 /* Expand table, if necessary, to make room. */
1277 if (ovs_flow_tbl_need_to_expand(table)) {
1278 struct flow_table *new_table;
1280 new_table = ovs_flow_tbl_expand(table);
1281 if (!IS_ERR(new_table)) {
1282 rcu_assign_pointer(dp->table, new_table);
1283 ovs_flow_tbl_deferred_destroy(table);
1284 table = ovsl_dereference(dp->table);
1288 /* Allocate flow. */
1289 flow = ovs_flow_alloc();
1291 error = PTR_ERR(flow);
1292 goto err_unlock_ovs;
1296 rcu_assign_pointer(flow->sf_acts, acts);
1298 /* Put flow in bucket. */
1299 ovs_flow_tbl_insert(table, flow, &key, key_len);
1301 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1305 /* We found a matching flow. */
1306 struct sw_flow_actions *old_acts;
1308 /* Bail out if we're not allowed to modify an existing flow.
1309 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1310 * because Generic Netlink treats the latter as a dump
1311 * request. We also accept NLM_F_EXCL in case that bug ever
1315 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1316 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1317 goto err_unlock_ovs;
1319 /* Update actions. */
1320 old_acts = ovsl_dereference(flow->sf_acts);
1321 rcu_assign_pointer(flow->sf_acts, acts);
1322 ovs_flow_deferred_free_acts(old_acts);
1324 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1325 info->snd_seq, OVS_FLOW_CMD_NEW);
1328 if (a[OVS_FLOW_ATTR_CLEAR]) {
1329 spin_lock_bh(&flow->lock);
1331 spin_unlock_bh(&flow->lock);
1337 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1339 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1340 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1351 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1353 struct nlattr **a = info->attrs;
1354 struct ovs_header *ovs_header = info->userhdr;
1355 struct sw_flow_key key;
1356 struct sk_buff *reply;
1357 struct sw_flow *flow;
1358 struct datapath *dp;
1359 struct flow_table *table;
1363 if (!a[OVS_FLOW_ATTR_KEY])
1365 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1370 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1376 table = ovsl_dereference(dp->table);
1377 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1383 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1384 info->snd_seq, OVS_FLOW_CMD_NEW);
1385 if (IS_ERR(reply)) {
1386 err = PTR_ERR(reply);
1391 return genlmsg_reply(reply, info);
1397 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1399 struct nlattr **a = info->attrs;
1400 struct ovs_header *ovs_header = info->userhdr;
1401 struct sw_flow_key key;
1402 struct sk_buff *reply;
1403 struct sw_flow *flow;
1404 struct datapath *dp;
1405 struct flow_table *table;
1410 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1416 if (!a[OVS_FLOW_ATTR_KEY]) {
1417 err = flush_flows(dp);
1420 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1424 table = ovsl_dereference(dp->table);
1425 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1431 reply = ovs_flow_cmd_alloc_info(flow);
1437 ovs_flow_tbl_remove(table, flow);
1439 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1440 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1443 ovs_flow_deferred_free(flow);
1446 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1453 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1455 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1456 struct datapath *dp;
1457 struct flow_table *table;
1460 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1466 table = ovsl_dereference(dp->table);
1469 struct sw_flow *flow;
1472 bucket = cb->args[0];
1474 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1478 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1479 NETLINK_CB(cb->skb).portid,
1480 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1481 OVS_FLOW_CMD_NEW) < 0)
1484 cb->args[0] = bucket;
1491 static struct genl_ops dp_flow_genl_ops[] = {
1492 { .cmd = OVS_FLOW_CMD_NEW,
1493 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1494 .policy = flow_policy,
1495 .doit = ovs_flow_cmd_new_or_set
1497 { .cmd = OVS_FLOW_CMD_DEL,
1498 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1499 .policy = flow_policy,
1500 .doit = ovs_flow_cmd_del
1502 { .cmd = OVS_FLOW_CMD_GET,
1503 .flags = 0, /* OK for unprivileged users. */
1504 .policy = flow_policy,
1505 .doit = ovs_flow_cmd_get,
1506 .dumpit = ovs_flow_cmd_dump
1508 { .cmd = OVS_FLOW_CMD_SET,
1509 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1510 .policy = flow_policy,
1511 .doit = ovs_flow_cmd_new_or_set,
1515 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1516 #ifdef HAVE_NLA_NUL_STRING
1517 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1519 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1522 static struct genl_family dp_datapath_genl_family = {
1523 .id = GENL_ID_GENERATE,
1524 .hdrsize = sizeof(struct ovs_header),
1525 .name = OVS_DATAPATH_FAMILY,
1526 .version = OVS_DATAPATH_VERSION,
1527 .maxattr = OVS_DP_ATTR_MAX,
1531 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1532 .name = OVS_DATAPATH_MCGROUP
1535 static size_t ovs_dp_cmd_msg_size(void)
1537 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1539 msgsize += nla_total_size(IFNAMSIZ);
1540 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1545 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1546 u32 portid, u32 seq, u32 flags, u8 cmd)
1548 struct ovs_header *ovs_header;
1549 struct ovs_dp_stats dp_stats;
1552 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1557 ovs_header->dp_ifindex = get_dpifindex(dp);
1560 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1563 goto nla_put_failure;
1565 get_dp_stats(dp, &dp_stats);
1566 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1567 goto nla_put_failure;
1569 return genlmsg_end(skb, ovs_header);
1572 genlmsg_cancel(skb, ovs_header);
1577 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1580 struct sk_buff *skb;
1583 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1585 return ERR_PTR(-ENOMEM);
1587 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1590 return ERR_PTR(retval);
1595 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1597 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1600 /* Called with ovs_mutex. */
1601 static struct datapath *lookup_datapath(struct net *net,
1602 struct ovs_header *ovs_header,
1603 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1605 struct datapath *dp;
1607 if (!a[OVS_DP_ATTR_NAME])
1608 dp = get_dp(net, ovs_header->dp_ifindex);
1610 struct vport *vport;
1613 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1614 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1617 return dp ? dp : ERR_PTR(-ENODEV);
1620 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1622 struct nlattr **a = info->attrs;
1623 struct vport_parms parms;
1624 struct sk_buff *reply;
1625 struct datapath *dp;
1626 struct vport *vport;
1627 struct ovs_net *ovs_net;
1631 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1634 err = ovs_dp_cmd_validate(a);
1641 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1643 goto err_unlock_ovs;
1645 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1647 /* Allocate table. */
1649 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1653 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1654 if (!dp->stats_percpu) {
1656 goto err_destroy_table;
1659 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1663 goto err_destroy_percpu;
1666 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1667 INIT_HLIST_HEAD(&dp->ports[i]);
1669 /* Set up our datapath device. */
1670 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1671 parms.type = OVS_VPORT_TYPE_INTERNAL;
1672 parms.options = NULL;
1674 parms.port_no = OVSP_LOCAL;
1675 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1677 vport = new_vport(&parms);
1678 if (IS_ERR(vport)) {
1679 err = PTR_ERR(vport);
1683 goto err_destroy_ports_array;
1686 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1687 info->snd_seq, OVS_DP_CMD_NEW);
1688 err = PTR_ERR(reply);
1690 goto err_destroy_local_port;
1692 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1693 list_add_tail(&dp->list_node, &ovs_net->dps);
1697 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1700 err_destroy_local_port:
1701 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1702 err_destroy_ports_array:
1705 free_percpu(dp->stats_percpu);
1707 ovs_flow_tbl_destroy(ovsl_dereference(dp->table));
1709 release_net(ovs_dp_get_net(dp));
1717 /* Called with ovs_mutex. */
1718 static void __dp_destroy(struct datapath *dp)
1722 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1723 struct vport *vport;
1724 struct hlist_node *n;
1726 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1727 if (vport->port_no != OVSP_LOCAL)
1728 ovs_dp_detach_port(vport);
1731 list_del(&dp->list_node);
1733 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1734 * all port in datapath are destroyed first before freeing datapath.
1736 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1738 call_rcu(&dp->rcu, destroy_dp_rcu);
1741 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1743 struct sk_buff *reply;
1744 struct datapath *dp;
1747 err = ovs_dp_cmd_validate(info->attrs);
1752 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1757 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1758 info->snd_seq, OVS_DP_CMD_DEL);
1759 err = PTR_ERR(reply);
1766 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1774 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1776 struct sk_buff *reply;
1777 struct datapath *dp;
1780 err = ovs_dp_cmd_validate(info->attrs);
1785 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1790 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1791 info->snd_seq, OVS_DP_CMD_NEW);
1792 if (IS_ERR(reply)) {
1793 err = PTR_ERR(reply);
1794 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1795 ovs_dp_datapath_multicast_group.id, err);
1801 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1809 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1811 struct sk_buff *reply;
1812 struct datapath *dp;
1815 err = ovs_dp_cmd_validate(info->attrs);
1820 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1826 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1827 info->snd_seq, OVS_DP_CMD_NEW);
1828 if (IS_ERR(reply)) {
1829 err = PTR_ERR(reply);
1834 return genlmsg_reply(reply, info);
1841 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1843 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1844 struct datapath *dp;
1845 int skip = cb->args[0];
1849 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1851 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1852 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1853 OVS_DP_CMD_NEW) < 0)
1864 static struct genl_ops dp_datapath_genl_ops[] = {
1865 { .cmd = OVS_DP_CMD_NEW,
1866 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1867 .policy = datapath_policy,
1868 .doit = ovs_dp_cmd_new
1870 { .cmd = OVS_DP_CMD_DEL,
1871 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1872 .policy = datapath_policy,
1873 .doit = ovs_dp_cmd_del
1875 { .cmd = OVS_DP_CMD_GET,
1876 .flags = 0, /* OK for unprivileged users. */
1877 .policy = datapath_policy,
1878 .doit = ovs_dp_cmd_get,
1879 .dumpit = ovs_dp_cmd_dump
1881 { .cmd = OVS_DP_CMD_SET,
1882 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1883 .policy = datapath_policy,
1884 .doit = ovs_dp_cmd_set,
1888 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1889 #ifdef HAVE_NLA_NUL_STRING
1890 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1891 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1893 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1895 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1896 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1897 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1898 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1901 static struct genl_family dp_vport_genl_family = {
1902 .id = GENL_ID_GENERATE,
1903 .hdrsize = sizeof(struct ovs_header),
1904 .name = OVS_VPORT_FAMILY,
1905 .version = OVS_VPORT_VERSION,
1906 .maxattr = OVS_VPORT_ATTR_MAX,
1910 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1911 .name = OVS_VPORT_MCGROUP
1914 /* Called with ovs_mutex or RCU read lock. */
1915 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1916 u32 portid, u32 seq, u32 flags, u8 cmd)
1918 struct ovs_header *ovs_header;
1919 struct ovs_vport_stats vport_stats;
1922 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1927 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1929 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1930 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1931 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1932 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1933 goto nla_put_failure;
1935 ovs_vport_get_stats(vport, &vport_stats);
1936 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1938 goto nla_put_failure;
1940 err = ovs_vport_get_options(vport, skb);
1941 if (err == -EMSGSIZE)
1944 return genlmsg_end(skb, ovs_header);
1949 genlmsg_cancel(skb, ovs_header);
1953 /* Called with ovs_mutex or RCU read lock. */
1954 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1957 struct sk_buff *skb;
1960 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1962 return ERR_PTR(-ENOMEM);
1964 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1970 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1972 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1975 /* Called with ovs_mutex or RCU read lock. */
1976 static struct vport *lookup_vport(struct net *net,
1977 struct ovs_header *ovs_header,
1978 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1980 struct datapath *dp;
1981 struct vport *vport;
1983 if (a[OVS_VPORT_ATTR_NAME]) {
1984 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1986 return ERR_PTR(-ENODEV);
1987 if (ovs_header->dp_ifindex &&
1988 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1989 return ERR_PTR(-ENODEV);
1991 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1992 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1994 if (port_no >= DP_MAX_PORTS)
1995 return ERR_PTR(-EFBIG);
1997 dp = get_dp(net, ovs_header->dp_ifindex);
1999 return ERR_PTR(-ENODEV);
2001 vport = ovs_vport_ovsl_rcu(dp, port_no);
2003 return ERR_PTR(-ENODEV);
2006 return ERR_PTR(-EINVAL);
2009 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2011 struct nlattr **a = info->attrs;
2012 struct ovs_header *ovs_header = info->userhdr;
2013 struct vport_parms parms;
2014 struct sk_buff *reply;
2015 struct vport *vport;
2016 struct datapath *dp;
2021 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2022 !a[OVS_VPORT_ATTR_UPCALL_PID])
2025 err = ovs_vport_cmd_validate(a);
2030 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2035 if (a[OVS_VPORT_ATTR_PORT_NO]) {
2036 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2039 if (port_no >= DP_MAX_PORTS)
2042 vport = ovs_vport_ovsl(dp, port_no);
2047 for (port_no = 1; ; port_no++) {
2048 if (port_no >= DP_MAX_PORTS) {
2052 vport = ovs_vport_ovsl(dp, port_no);
2058 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2059 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2060 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2062 parms.port_no = port_no;
2063 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2065 vport = new_vport(&parms);
2066 err = PTR_ERR(vport);
2071 if (a[OVS_VPORT_ATTR_STATS])
2072 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2074 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2076 if (IS_ERR(reply)) {
2077 err = PTR_ERR(reply);
2078 ovs_dp_detach_port(vport);
2082 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2090 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2092 struct nlattr **a = info->attrs;
2093 struct sk_buff *reply;
2094 struct vport *vport;
2097 err = ovs_vport_cmd_validate(a);
2102 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2103 err = PTR_ERR(vport);
2108 if (a[OVS_VPORT_ATTR_TYPE] &&
2109 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
2112 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2118 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
2119 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2123 if (a[OVS_VPORT_ATTR_STATS])
2124 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2126 if (a[OVS_VPORT_ATTR_UPCALL_PID])
2127 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2129 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2130 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2134 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2145 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2147 struct nlattr **a = info->attrs;
2148 struct sk_buff *reply;
2149 struct vport *vport;
2152 err = ovs_vport_cmd_validate(a);
2157 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2158 err = PTR_ERR(vport);
2162 if (vport->port_no == OVSP_LOCAL) {
2167 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2168 info->snd_seq, OVS_VPORT_CMD_DEL);
2169 err = PTR_ERR(reply);
2174 ovs_dp_detach_port(vport);
2176 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2184 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2186 struct nlattr **a = info->attrs;
2187 struct ovs_header *ovs_header = info->userhdr;
2188 struct sk_buff *reply;
2189 struct vport *vport;
2192 err = ovs_vport_cmd_validate(a);
2197 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2198 err = PTR_ERR(vport);
2202 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2203 info->snd_seq, OVS_VPORT_CMD_NEW);
2204 err = PTR_ERR(reply);
2210 return genlmsg_reply(reply, info);
2218 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2220 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2221 struct datapath *dp;
2222 int bucket = cb->args[0], skip = cb->args[1];
2225 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2230 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2231 struct vport *vport;
2234 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2236 ovs_vport_cmd_fill_info(vport, skb,
2237 NETLINK_CB(cb->skb).portid,
2240 OVS_VPORT_CMD_NEW) < 0)
2256 static struct genl_ops dp_vport_genl_ops[] = {
2257 { .cmd = OVS_VPORT_CMD_NEW,
2258 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2259 .policy = vport_policy,
2260 .doit = ovs_vport_cmd_new
2262 { .cmd = OVS_VPORT_CMD_DEL,
2263 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2264 .policy = vport_policy,
2265 .doit = ovs_vport_cmd_del
2267 { .cmd = OVS_VPORT_CMD_GET,
2268 .flags = 0, /* OK for unprivileged users. */
2269 .policy = vport_policy,
2270 .doit = ovs_vport_cmd_get,
2271 .dumpit = ovs_vport_cmd_dump
2273 { .cmd = OVS_VPORT_CMD_SET,
2274 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2275 .policy = vport_policy,
2276 .doit = ovs_vport_cmd_set,
2280 struct genl_family_and_ops {
2281 struct genl_family *family;
2282 struct genl_ops *ops;
2284 struct genl_multicast_group *group;
2287 static const struct genl_family_and_ops dp_genl_families[] = {
2288 { &dp_datapath_genl_family,
2289 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2290 &ovs_dp_datapath_multicast_group },
2291 { &dp_vport_genl_family,
2292 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2293 &ovs_dp_vport_multicast_group },
2294 { &dp_flow_genl_family,
2295 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2296 &ovs_dp_flow_multicast_group },
2297 { &dp_packet_genl_family,
2298 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2302 static void dp_unregister_genl(int n_families)
2306 for (i = 0; i < n_families; i++)
2307 genl_unregister_family(dp_genl_families[i].family);
2310 static int dp_register_genl(void)
2317 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2318 const struct genl_family_and_ops *f = &dp_genl_families[i];
2320 err = genl_register_family_with_ops(f->family, f->ops,
2327 err = genl_register_mc_group(f->family, f->group);
2336 dp_unregister_genl(n_registered);
2340 static void rehash_flow_table(struct work_struct *work)
2342 struct datapath *dp;
2348 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2350 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2351 struct flow_table *old_table = ovsl_dereference(dp->table);
2352 struct flow_table *new_table;
2354 new_table = ovs_flow_tbl_rehash(old_table);
2355 if (!IS_ERR(new_table)) {
2356 rcu_assign_pointer(dp->table, new_table);
2357 ovs_flow_tbl_deferred_destroy(old_table);
2363 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2366 static int __net_init ovs_init_net(struct net *net)
2368 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2370 INIT_LIST_HEAD(&ovs_net->dps);
2371 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2375 static void __net_exit ovs_exit_net(struct net *net)
2377 struct datapath *dp, *dp_next;
2378 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2381 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2385 cancel_work_sync(&ovs_net->dp_notify_work);
2388 static struct pernet_operations ovs_net_ops = {
2389 .init = ovs_init_net,
2390 .exit = ovs_exit_net,
2392 .size = sizeof(struct ovs_net),
2395 static int __init dp_init(void)
2399 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2401 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2404 err = ovs_workqueues_init();
2408 err = ovs_flow_init();
2412 err = ovs_vport_init();
2414 goto error_flow_exit;
2416 err = register_pernet_device(&ovs_net_ops);
2418 goto error_vport_exit;
2420 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2422 goto error_netns_exit;
2424 err = dp_register_genl();
2426 goto error_unreg_notifier;
2428 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2432 error_unreg_notifier:
2433 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2435 unregister_pernet_device(&ovs_net_ops);
2441 ovs_workqueues_exit();
2446 static void dp_cleanup(void)
2448 cancel_delayed_work_sync(&rehash_flow_wq);
2449 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2450 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2451 unregister_pernet_device(&ovs_net_ops);
2455 ovs_workqueues_exit();
2458 module_init(dp_init);
2459 module_exit(dp_cleanup);
2461 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2462 MODULE_LICENSE("GPL");
2463 MODULE_VERSION(VERSION);