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_head *head;
163 head = vport_hash_bucket(dp, port_no);
164 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
165 if (vport->port_no == port_no)
171 /* Called with RTNL lock and genl_lock. */
172 static struct vport *new_vport(const struct vport_parms *parms)
176 vport = ovs_vport_add(parms);
177 if (!IS_ERR(vport)) {
178 struct datapath *dp = parms->dp;
179 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
181 hlist_add_head_rcu(&vport->dp_hash_node, head);
186 /* Called with RTNL lock. */
187 void ovs_dp_detach_port(struct vport *p)
191 /* First drop references to device. */
192 hlist_del_rcu(&p->dp_hash_node);
194 /* Then destroy it. */
198 /* Must be called with rcu_read_lock. */
199 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
201 struct datapath *dp = p->dp;
202 struct sw_flow *flow;
203 struct dp_stats_percpu *stats;
204 struct sw_flow_key key;
209 stats = this_cpu_ptr(dp->stats_percpu);
211 /* Extract flow from 'skb' into 'key'. */
212 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
213 if (unlikely(error)) {
219 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
220 if (unlikely(!flow)) {
221 struct dp_upcall_info upcall;
223 upcall.cmd = OVS_PACKET_CMD_MISS;
225 upcall.userdata = NULL;
226 upcall.portid = p->upcall_portid;
227 ovs_dp_upcall(dp, skb, &upcall);
229 stats_counter = &stats->n_missed;
233 OVS_CB(skb)->flow = flow;
235 stats_counter = &stats->n_hit;
236 ovs_flow_used(OVS_CB(skb)->flow, skb);
237 ovs_execute_actions(dp, skb);
240 /* Update datapath statistics. */
241 u64_stats_update_begin(&stats->sync);
243 u64_stats_update_end(&stats->sync);
246 static struct genl_family dp_packet_genl_family = {
247 .id = GENL_ID_GENERATE,
248 .hdrsize = sizeof(struct ovs_header),
249 .name = OVS_PACKET_FAMILY,
250 .version = OVS_PACKET_VERSION,
251 .maxattr = OVS_PACKET_ATTR_MAX,
255 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
256 const struct dp_upcall_info *upcall_info)
258 struct dp_stats_percpu *stats;
262 if (upcall_info->portid == 0) {
267 dp_ifindex = get_dpifindex(dp);
273 forward_ip_summed(skb, true);
275 if (!skb_is_gso(skb))
276 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
278 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
285 stats = this_cpu_ptr(dp->stats_percpu);
287 u64_stats_update_begin(&stats->sync);
289 u64_stats_update_end(&stats->sync);
294 static int queue_gso_packets(struct net *net, int dp_ifindex,
296 const struct dp_upcall_info *upcall_info)
298 unsigned short gso_type = skb_shinfo(skb)->gso_type;
299 struct dp_upcall_info later_info;
300 struct sw_flow_key later_key;
301 struct sk_buff *segs, *nskb;
304 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
306 return PTR_ERR(segs);
308 /* Queue all of the segments. */
311 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
315 if (skb == segs && gso_type & SKB_GSO_UDP) {
316 /* The initial flow key extracted by ovs_flow_extract()
317 * in this case is for a first fragment, so we need to
318 * properly mark later fragments.
320 later_key = *upcall_info->key;
321 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
323 later_info = *upcall_info;
324 later_info.key = &later_key;
325 upcall_info = &later_info;
327 } while ((skb = skb->next));
329 /* Free all of the segments. */
337 } while ((skb = nskb));
341 static size_t key_attr_size(void)
343 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
344 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
345 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
346 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
347 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
348 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
349 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
350 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
351 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
352 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
353 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
354 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
355 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
356 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
357 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
358 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
359 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
360 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
361 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
364 static size_t upcall_msg_size(const struct sk_buff *skb,
365 const struct nlattr *userdata)
367 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
368 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
369 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
371 /* OVS_PACKET_ATTR_USERDATA */
373 size += NLA_ALIGN(userdata->nla_len);
378 static int queue_userspace_packet(struct net *net, int dp_ifindex,
380 const struct dp_upcall_info *upcall_info)
382 struct ovs_header *upcall;
383 struct sk_buff *nskb = NULL;
384 struct sk_buff *user_skb; /* to be queued to userspace */
388 if (vlan_tx_tag_present(skb)) {
389 nskb = skb_clone(skb, GFP_ATOMIC);
393 err = vlan_deaccel_tag(nskb);
400 if (nla_attr_size(skb->len) > USHRT_MAX) {
405 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
411 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
412 0, upcall_info->cmd);
413 upcall->dp_ifindex = dp_ifindex;
415 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
416 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
417 nla_nest_end(user_skb, nla);
419 if (upcall_info->userdata)
420 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
421 nla_len(upcall_info->userdata),
422 nla_data(upcall_info->userdata));
424 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
426 skb_copy_and_csum_dev(skb, nla_data(nla));
428 genlmsg_end(user_skb, upcall);
429 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
436 /* Called with genl_mutex. */
437 static int flush_flows(struct datapath *dp)
439 struct flow_table *old_table;
440 struct flow_table *new_table;
442 old_table = genl_dereference(dp->table);
443 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
447 rcu_assign_pointer(dp->table, new_table);
449 ovs_flow_tbl_deferred_destroy(old_table);
453 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
456 struct sw_flow_actions *acts;
458 int req_size = NLA_ALIGN(attr_len);
459 int next_offset = offsetof(struct sw_flow_actions, actions) +
462 if (req_size <= (ksize(*sfa) - next_offset))
465 new_acts_size = ksize(*sfa) * 2;
467 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
468 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
469 return ERR_PTR(-EMSGSIZE);
470 new_acts_size = MAX_ACTIONS_BUFSIZE;
473 acts = ovs_flow_actions_alloc(new_acts_size);
477 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
478 acts->actions_len = (*sfa)->actions_len;
483 (*sfa)->actions_len += req_size;
484 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
487 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
491 a = reserve_sfa_size(sfa, nla_attr_size(len));
495 a->nla_type = attrtype;
496 a->nla_len = nla_attr_size(len);
499 memcpy(nla_data(a), data, len);
500 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
505 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
507 int used = (*sfa)->actions_len;
510 err = add_action(sfa, attrtype, NULL, 0);
517 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
519 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
521 a->nla_len = sfa->actions_len - st_offset;
524 static int validate_and_copy_actions(const struct nlattr *attr,
525 const struct sw_flow_key *key, int depth,
526 struct sw_flow_actions **sfa);
528 static int validate_and_copy_sample(const struct nlattr *attr,
529 const struct sw_flow_key *key, int depth,
530 struct sw_flow_actions **sfa)
532 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
533 const struct nlattr *probability, *actions;
534 const struct nlattr *a;
535 int rem, start, err, st_acts;
537 memset(attrs, 0, sizeof(attrs));
538 nla_for_each_nested(a, attr, rem) {
539 int type = nla_type(a);
540 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
547 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
548 if (!probability || nla_len(probability) != sizeof(u32))
551 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
552 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
555 /* validation done, copy sample action. */
556 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
559 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
562 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
566 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
570 add_nested_action_end(*sfa, st_acts);
571 add_nested_action_end(*sfa, start);
576 static int validate_tp_port(const struct sw_flow_key *flow_key)
578 if (flow_key->eth.type == htons(ETH_P_IP)) {
579 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
581 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
582 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
589 static int validate_and_copy_set_tun(const struct nlattr *attr,
590 struct sw_flow_actions **sfa)
592 struct ovs_key_ipv4_tunnel tun_key;
595 err = ipv4_tun_from_nlattr(nla_data(attr), &tun_key);
599 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
603 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key, sizeof(tun_key));
604 add_nested_action_end(*sfa, start);
609 static int validate_set(const struct nlattr *a,
610 const struct sw_flow_key *flow_key,
611 struct sw_flow_actions **sfa,
614 const struct nlattr *ovs_key = nla_data(a);
615 int key_type = nla_type(ovs_key);
617 /* There can be only one key in a action */
618 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
621 if (key_type > OVS_KEY_ATTR_MAX ||
622 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
623 ovs_key_lens[key_type] != -1))
627 const struct ovs_key_ipv4 *ipv4_key;
628 const struct ovs_key_ipv6 *ipv6_key;
631 case OVS_KEY_ATTR_PRIORITY:
632 case OVS_KEY_ATTR_ETHERNET:
635 case OVS_KEY_ATTR_SKB_MARK:
636 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
637 if (nla_get_u32(ovs_key) != 0)
642 case OVS_KEY_ATTR_TUNNEL:
644 err = validate_and_copy_set_tun(a, sfa);
649 case OVS_KEY_ATTR_IPV4:
650 if (flow_key->eth.type != htons(ETH_P_IP))
653 if (!flow_key->ip.proto)
656 ipv4_key = nla_data(ovs_key);
657 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
660 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
665 case OVS_KEY_ATTR_IPV6:
666 if (flow_key->eth.type != htons(ETH_P_IPV6))
669 if (!flow_key->ip.proto)
672 ipv6_key = nla_data(ovs_key);
673 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
676 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
679 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
684 case OVS_KEY_ATTR_TCP:
685 if (flow_key->ip.proto != IPPROTO_TCP)
688 return validate_tp_port(flow_key);
690 case OVS_KEY_ATTR_UDP:
691 if (flow_key->ip.proto != IPPROTO_UDP)
694 return validate_tp_port(flow_key);
703 static int validate_userspace(const struct nlattr *attr)
705 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
706 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
707 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
709 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
712 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
713 attr, userspace_policy);
717 if (!a[OVS_USERSPACE_ATTR_PID] ||
718 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
724 static int copy_action(const struct nlattr *from,
725 struct sw_flow_actions **sfa)
727 int totlen = NLA_ALIGN(from->nla_len);
730 to = reserve_sfa_size(sfa, from->nla_len);
734 memcpy(to, from, totlen);
738 static int validate_and_copy_actions(const struct nlattr *attr,
739 const struct sw_flow_key *key,
741 struct sw_flow_actions **sfa)
743 const struct nlattr *a;
746 if (depth >= SAMPLE_ACTION_DEPTH)
749 nla_for_each_nested(a, attr, rem) {
750 /* Expected argument lengths, (u32)-1 for variable length. */
751 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
752 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
753 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
754 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
755 [OVS_ACTION_ATTR_POP_VLAN] = 0,
756 [OVS_ACTION_ATTR_SET] = (u32)-1,
757 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
759 const struct ovs_action_push_vlan *vlan;
760 int type = nla_type(a);
763 if (type > OVS_ACTION_ATTR_MAX ||
764 (action_lens[type] != nla_len(a) &&
765 action_lens[type] != (u32)-1))
770 case OVS_ACTION_ATTR_UNSPEC:
773 case OVS_ACTION_ATTR_USERSPACE:
774 err = validate_userspace(a);
779 case OVS_ACTION_ATTR_OUTPUT:
780 if (nla_get_u32(a) >= DP_MAX_PORTS)
785 case OVS_ACTION_ATTR_POP_VLAN:
788 case OVS_ACTION_ATTR_PUSH_VLAN:
790 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
792 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
796 case OVS_ACTION_ATTR_SET:
797 err = validate_set(a, key, sfa, &skip_copy);
802 case OVS_ACTION_ATTR_SAMPLE:
803 err = validate_and_copy_sample(a, key, depth, sfa);
813 err = copy_action(a, sfa);
825 static void clear_stats(struct sw_flow *flow)
829 flow->packet_count = 0;
830 flow->byte_count = 0;
833 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
835 struct ovs_header *ovs_header = info->userhdr;
836 struct nlattr **a = info->attrs;
837 struct sw_flow_actions *acts;
838 struct sk_buff *packet;
839 struct sw_flow *flow;
847 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
848 !a[OVS_PACKET_ATTR_ACTIONS])
851 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
852 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
856 skb_reserve(packet, NET_IP_ALIGN);
858 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
860 skb_reset_mac_header(packet);
861 eth = eth_hdr(packet);
863 /* Normally, setting the skb 'protocol' field would be handled by a
864 * call to eth_type_trans(), but it assumes there's a sending
865 * device, which we may not have. */
866 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
867 packet->protocol = eth->h_proto;
869 packet->protocol = htons(ETH_P_802_2);
871 /* Build an sw_flow for sending this packet. */
872 flow = ovs_flow_alloc();
877 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
881 err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]);
884 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
889 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
890 rcu_assign_pointer(flow->sf_acts, acts);
894 OVS_CB(packet)->flow = flow;
895 packet->priority = flow->key.phy.priority;
896 skb_set_mark(packet, flow->key.phy.skb_mark);
899 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
905 err = ovs_execute_actions(dp, packet);
922 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
923 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
924 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
926 [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
928 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
929 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
932 static struct genl_ops dp_packet_genl_ops[] = {
933 { .cmd = OVS_PACKET_CMD_EXECUTE,
934 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
935 .policy = packet_policy,
936 .doit = ovs_packet_cmd_execute
940 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
943 struct flow_table *table = genl_dereference(dp->table);
945 stats->n_flows = ovs_flow_tbl_count(table);
947 stats->n_hit = stats->n_missed = stats->n_lost = 0;
948 for_each_possible_cpu(i) {
949 const struct dp_stats_percpu *percpu_stats;
950 struct dp_stats_percpu local_stats;
953 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
956 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
957 local_stats = *percpu_stats;
958 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
960 stats->n_hit += local_stats.n_hit;
961 stats->n_missed += local_stats.n_missed;
962 stats->n_lost += local_stats.n_lost;
966 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
967 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
968 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
969 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
972 static struct genl_family dp_flow_genl_family = {
973 .id = GENL_ID_GENERATE,
974 .hdrsize = sizeof(struct ovs_header),
975 .name = OVS_FLOW_FAMILY,
976 .version = OVS_FLOW_VERSION,
977 .maxattr = OVS_FLOW_ATTR_MAX,
981 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
982 .name = OVS_FLOW_MCGROUP
985 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
986 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
988 const struct nlattr *a;
989 struct nlattr *start;
992 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
996 nla_for_each_nested(a, attr, rem) {
997 int type = nla_type(a);
998 struct nlattr *st_sample;
1001 case OVS_SAMPLE_ATTR_PROBABILITY:
1002 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1005 case OVS_SAMPLE_ATTR_ACTIONS:
1006 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1009 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1012 nla_nest_end(skb, st_sample);
1017 nla_nest_end(skb, start);
1021 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1023 const struct nlattr *ovs_key = nla_data(a);
1024 int key_type = nla_type(ovs_key);
1025 struct nlattr *start;
1029 case OVS_KEY_ATTR_IPV4_TUNNEL:
1030 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1034 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key));
1037 nla_nest_end(skb, start);
1040 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1048 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1050 const struct nlattr *a;
1053 nla_for_each_attr(a, attr, len, rem) {
1054 int type = nla_type(a);
1057 case OVS_ACTION_ATTR_SET:
1058 err = set_action_to_attr(a, skb);
1063 case OVS_ACTION_ATTR_SAMPLE:
1064 err = sample_action_to_attr(a, skb);
1069 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1078 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1080 return NLMSG_ALIGN(sizeof(struct ovs_header))
1081 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1082 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1083 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1084 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1085 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1088 /* Called with genl_lock. */
1089 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1090 struct sk_buff *skb, u32 portid,
1091 u32 seq, u32 flags, u8 cmd)
1093 const int skb_orig_len = skb->len;
1094 const struct sw_flow_actions *sf_acts;
1095 struct nlattr *start;
1096 struct ovs_flow_stats stats;
1097 struct ovs_header *ovs_header;
1103 sf_acts = rcu_dereference_protected(flow->sf_acts,
1104 lockdep_genl_is_held());
1106 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1110 ovs_header->dp_ifindex = get_dpifindex(dp);
1112 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1114 goto nla_put_failure;
1115 err = ovs_flow_to_nlattrs(&flow->key, skb);
1118 nla_nest_end(skb, nla);
1120 spin_lock_bh(&flow->lock);
1122 stats.n_packets = flow->packet_count;
1123 stats.n_bytes = flow->byte_count;
1124 tcp_flags = flow->tcp_flags;
1125 spin_unlock_bh(&flow->lock);
1128 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1129 goto nla_put_failure;
1131 if (stats.n_packets &&
1132 nla_put(skb, OVS_FLOW_ATTR_STATS,
1133 sizeof(struct ovs_flow_stats), &stats))
1134 goto nla_put_failure;
1137 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1138 goto nla_put_failure;
1140 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1141 * this is the first flow to be dumped into 'skb'. This is unusual for
1142 * Netlink but individual action lists can be longer than
1143 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1144 * The userspace caller can always fetch the actions separately if it
1145 * really wants them. (Most userspace callers in fact don't care.)
1147 * This can only fail for dump operations because the skb is always
1148 * properly sized for single flows.
1150 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1152 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1154 nla_nest_end(skb, start);
1159 nla_nest_cancel(skb, start);
1161 } else if (skb_orig_len)
1162 goto nla_put_failure;
1164 return genlmsg_end(skb, ovs_header);
1169 genlmsg_cancel(skb, ovs_header);
1173 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1175 const struct sw_flow_actions *sf_acts;
1177 sf_acts = rcu_dereference_protected(flow->sf_acts,
1178 lockdep_genl_is_held());
1180 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
1183 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1184 struct datapath *dp,
1185 u32 portid, u32 seq, u8 cmd)
1187 struct sk_buff *skb;
1190 skb = ovs_flow_cmd_alloc_info(flow);
1192 return ERR_PTR(-ENOMEM);
1194 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1199 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1201 struct nlattr **a = info->attrs;
1202 struct ovs_header *ovs_header = info->userhdr;
1203 struct sw_flow_key key;
1204 struct sw_flow *flow;
1205 struct sk_buff *reply;
1206 struct datapath *dp;
1207 struct flow_table *table;
1208 struct sw_flow_actions *acts = NULL;
1214 if (!a[OVS_FLOW_ATTR_KEY])
1216 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1220 /* Validate actions. */
1221 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1222 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1223 error = PTR_ERR(acts);
1227 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0, &acts);
1230 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1235 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1240 table = genl_dereference(dp->table);
1241 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1243 /* Bail out if we're not allowed to create a new flow. */
1245 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1248 /* Expand table, if necessary, to make room. */
1249 if (ovs_flow_tbl_need_to_expand(table)) {
1250 struct flow_table *new_table;
1252 new_table = ovs_flow_tbl_expand(table);
1253 if (!IS_ERR(new_table)) {
1254 rcu_assign_pointer(dp->table, new_table);
1255 ovs_flow_tbl_deferred_destroy(table);
1256 table = genl_dereference(dp->table);
1260 /* Allocate flow. */
1261 flow = ovs_flow_alloc();
1263 error = PTR_ERR(flow);
1268 rcu_assign_pointer(flow->sf_acts, acts);
1270 /* Put flow in bucket. */
1271 ovs_flow_tbl_insert(table, flow, &key, key_len);
1273 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1277 /* We found a matching flow. */
1278 struct sw_flow_actions *old_acts;
1280 /* Bail out if we're not allowed to modify an existing flow.
1281 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1282 * because Generic Netlink treats the latter as a dump
1283 * request. We also accept NLM_F_EXCL in case that bug ever
1287 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1288 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1291 /* Update actions. */
1292 old_acts = rcu_dereference_protected(flow->sf_acts,
1293 lockdep_genl_is_held());
1294 rcu_assign_pointer(flow->sf_acts, acts);
1295 ovs_flow_deferred_free_acts(old_acts);
1297 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1298 info->snd_seq, OVS_FLOW_CMD_NEW);
1301 if (a[OVS_FLOW_ATTR_CLEAR]) {
1302 spin_lock_bh(&flow->lock);
1304 spin_unlock_bh(&flow->lock);
1309 genl_notify(reply, genl_info_net(info), info->snd_portid,
1310 ovs_dp_flow_multicast_group.id, info->nlhdr,
1313 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1314 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1323 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1325 struct nlattr **a = info->attrs;
1326 struct ovs_header *ovs_header = info->userhdr;
1327 struct sw_flow_key key;
1328 struct sk_buff *reply;
1329 struct sw_flow *flow;
1330 struct datapath *dp;
1331 struct flow_table *table;
1335 if (!a[OVS_FLOW_ATTR_KEY])
1337 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1341 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1345 table = genl_dereference(dp->table);
1346 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1350 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1351 info->snd_seq, OVS_FLOW_CMD_NEW);
1353 return PTR_ERR(reply);
1355 return genlmsg_reply(reply, info);
1358 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1360 struct nlattr **a = info->attrs;
1361 struct ovs_header *ovs_header = info->userhdr;
1362 struct sw_flow_key key;
1363 struct sk_buff *reply;
1364 struct sw_flow *flow;
1365 struct datapath *dp;
1366 struct flow_table *table;
1370 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1374 if (!a[OVS_FLOW_ATTR_KEY])
1375 return flush_flows(dp);
1377 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1381 table = genl_dereference(dp->table);
1382 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1386 reply = ovs_flow_cmd_alloc_info(flow);
1390 ovs_flow_tbl_remove(table, flow);
1392 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1393 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1396 ovs_flow_deferred_free(flow);
1398 genl_notify(reply, genl_info_net(info), info->snd_portid,
1399 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1403 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1405 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1406 struct datapath *dp;
1407 struct flow_table *table;
1409 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1413 table = genl_dereference(dp->table);
1416 struct sw_flow *flow;
1419 bucket = cb->args[0];
1421 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1425 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1426 NETLINK_CB(cb->skb).portid,
1427 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1428 OVS_FLOW_CMD_NEW) < 0)
1431 cb->args[0] = bucket;
1437 static struct genl_ops dp_flow_genl_ops[] = {
1438 { .cmd = OVS_FLOW_CMD_NEW,
1439 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1440 .policy = flow_policy,
1441 .doit = ovs_flow_cmd_new_or_set
1443 { .cmd = OVS_FLOW_CMD_DEL,
1444 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1445 .policy = flow_policy,
1446 .doit = ovs_flow_cmd_del
1448 { .cmd = OVS_FLOW_CMD_GET,
1449 .flags = 0, /* OK for unprivileged users. */
1450 .policy = flow_policy,
1451 .doit = ovs_flow_cmd_get,
1452 .dumpit = ovs_flow_cmd_dump
1454 { .cmd = OVS_FLOW_CMD_SET,
1455 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1456 .policy = flow_policy,
1457 .doit = ovs_flow_cmd_new_or_set,
1461 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1462 #ifdef HAVE_NLA_NUL_STRING
1463 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1465 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1468 static struct genl_family dp_datapath_genl_family = {
1469 .id = GENL_ID_GENERATE,
1470 .hdrsize = sizeof(struct ovs_header),
1471 .name = OVS_DATAPATH_FAMILY,
1472 .version = OVS_DATAPATH_VERSION,
1473 .maxattr = OVS_DP_ATTR_MAX,
1477 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1478 .name = OVS_DATAPATH_MCGROUP
1481 static size_t ovs_dp_cmd_msg_size(void)
1483 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1485 msgsize += nla_total_size(IFNAMSIZ);
1486 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1491 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1492 u32 portid, u32 seq, u32 flags, u8 cmd)
1494 struct ovs_header *ovs_header;
1495 struct ovs_dp_stats dp_stats;
1498 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1503 ovs_header->dp_ifindex = get_dpifindex(dp);
1506 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1509 goto nla_put_failure;
1511 get_dp_stats(dp, &dp_stats);
1512 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1513 goto nla_put_failure;
1515 return genlmsg_end(skb, ovs_header);
1518 genlmsg_cancel(skb, ovs_header);
1523 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1526 struct sk_buff *skb;
1529 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1531 return ERR_PTR(-ENOMEM);
1533 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1536 return ERR_PTR(retval);
1541 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1543 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1546 /* Called with genl_mutex and optionally with RTNL lock also. */
1547 static struct datapath *lookup_datapath(struct net *net,
1548 struct ovs_header *ovs_header,
1549 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1551 struct datapath *dp;
1553 if (!a[OVS_DP_ATTR_NAME])
1554 dp = get_dp(net, ovs_header->dp_ifindex);
1556 struct vport *vport;
1559 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1560 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1563 return dp ? dp : ERR_PTR(-ENODEV);
1566 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1568 struct nlattr **a = info->attrs;
1569 struct vport_parms parms;
1570 struct sk_buff *reply;
1571 struct datapath *dp;
1572 struct vport *vport;
1573 struct ovs_net *ovs_net;
1577 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1580 err = ovs_dp_cmd_validate(a);
1587 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1589 goto err_unlock_rtnl;
1591 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1593 /* Allocate table. */
1595 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1599 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1600 if (!dp->stats_percpu) {
1602 goto err_destroy_table;
1605 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1609 goto err_destroy_percpu;
1612 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1613 INIT_HLIST_HEAD(&dp->ports[i]);
1615 /* Set up our datapath device. */
1616 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1617 parms.type = OVS_VPORT_TYPE_INTERNAL;
1618 parms.options = NULL;
1620 parms.port_no = OVSP_LOCAL;
1621 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1623 vport = new_vport(&parms);
1624 if (IS_ERR(vport)) {
1625 err = PTR_ERR(vport);
1629 goto err_destroy_ports_array;
1632 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1633 info->snd_seq, OVS_DP_CMD_NEW);
1634 err = PTR_ERR(reply);
1636 goto err_destroy_local_port;
1638 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1639 list_add_tail(&dp->list_node, &ovs_net->dps);
1643 genl_notify(reply, genl_info_net(info), info->snd_portid,
1644 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1648 err_destroy_local_port:
1649 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1650 err_destroy_ports_array:
1653 free_percpu(dp->stats_percpu);
1655 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1657 release_net(ovs_dp_get_net(dp));
1665 /* Called with genl_mutex. */
1666 static void __dp_destroy(struct datapath *dp)
1672 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1673 struct vport *vport;
1674 struct hlist_node *n;
1676 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1677 if (vport->port_no != OVSP_LOCAL)
1678 ovs_dp_detach_port(vport);
1681 list_del(&dp->list_node);
1682 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1684 /* rtnl_unlock() will wait until all the references to devices that
1685 * are pending unregistration have been dropped. We do it here to
1686 * ensure that any internal devices (which contain DP pointers) are
1687 * fully destroyed before freeing the datapath.
1691 call_rcu(&dp->rcu, destroy_dp_rcu);
1694 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1696 struct sk_buff *reply;
1697 struct datapath *dp;
1700 err = ovs_dp_cmd_validate(info->attrs);
1704 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1709 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1710 info->snd_seq, OVS_DP_CMD_DEL);
1711 err = PTR_ERR(reply);
1717 genl_notify(reply, genl_info_net(info), info->snd_portid,
1718 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1724 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1726 struct sk_buff *reply;
1727 struct datapath *dp;
1730 err = ovs_dp_cmd_validate(info->attrs);
1734 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1738 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1739 info->snd_seq, OVS_DP_CMD_NEW);
1740 if (IS_ERR(reply)) {
1741 err = PTR_ERR(reply);
1742 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1743 ovs_dp_datapath_multicast_group.id, err);
1747 genl_notify(reply, genl_info_net(info), info->snd_portid,
1748 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1754 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1756 struct sk_buff *reply;
1757 struct datapath *dp;
1760 err = ovs_dp_cmd_validate(info->attrs);
1764 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1768 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1769 info->snd_seq, OVS_DP_CMD_NEW);
1771 return PTR_ERR(reply);
1773 return genlmsg_reply(reply, info);
1776 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1778 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1779 struct datapath *dp;
1780 int skip = cb->args[0];
1783 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1785 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1786 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1787 OVS_DP_CMD_NEW) < 0)
1797 static struct genl_ops dp_datapath_genl_ops[] = {
1798 { .cmd = OVS_DP_CMD_NEW,
1799 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1800 .policy = datapath_policy,
1801 .doit = ovs_dp_cmd_new
1803 { .cmd = OVS_DP_CMD_DEL,
1804 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1805 .policy = datapath_policy,
1806 .doit = ovs_dp_cmd_del
1808 { .cmd = OVS_DP_CMD_GET,
1809 .flags = 0, /* OK for unprivileged users. */
1810 .policy = datapath_policy,
1811 .doit = ovs_dp_cmd_get,
1812 .dumpit = ovs_dp_cmd_dump
1814 { .cmd = OVS_DP_CMD_SET,
1815 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1816 .policy = datapath_policy,
1817 .doit = ovs_dp_cmd_set,
1821 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1822 #ifdef HAVE_NLA_NUL_STRING
1823 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1824 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1826 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1828 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1829 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1830 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1831 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1834 static struct genl_family dp_vport_genl_family = {
1835 .id = GENL_ID_GENERATE,
1836 .hdrsize = sizeof(struct ovs_header),
1837 .name = OVS_VPORT_FAMILY,
1838 .version = OVS_VPORT_VERSION,
1839 .maxattr = OVS_VPORT_ATTR_MAX,
1843 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1844 .name = OVS_VPORT_MCGROUP
1847 /* Called with RTNL lock or RCU read lock. */
1848 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1849 u32 portid, u32 seq, u32 flags, u8 cmd)
1851 struct ovs_header *ovs_header;
1852 struct ovs_vport_stats vport_stats;
1855 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1860 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1862 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1863 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1864 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1865 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1866 goto nla_put_failure;
1868 ovs_vport_get_stats(vport, &vport_stats);
1869 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1871 goto nla_put_failure;
1873 err = ovs_vport_get_options(vport, skb);
1874 if (err == -EMSGSIZE)
1877 return genlmsg_end(skb, ovs_header);
1882 genlmsg_cancel(skb, ovs_header);
1886 /* Called with RTNL lock or RCU read lock. */
1887 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1890 struct sk_buff *skb;
1893 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1895 return ERR_PTR(-ENOMEM);
1897 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1903 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1905 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1908 /* Called with RTNL lock or RCU read lock. */
1909 static struct vport *lookup_vport(struct net *net,
1910 struct ovs_header *ovs_header,
1911 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1913 struct datapath *dp;
1914 struct vport *vport;
1916 if (a[OVS_VPORT_ATTR_NAME]) {
1917 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1919 return ERR_PTR(-ENODEV);
1920 if (ovs_header->dp_ifindex &&
1921 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1922 return ERR_PTR(-ENODEV);
1924 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1925 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1927 if (port_no >= DP_MAX_PORTS)
1928 return ERR_PTR(-EFBIG);
1930 dp = get_dp(net, ovs_header->dp_ifindex);
1932 return ERR_PTR(-ENODEV);
1934 vport = ovs_vport_rtnl_rcu(dp, port_no);
1936 return ERR_PTR(-ENODEV);
1939 return ERR_PTR(-EINVAL);
1942 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1944 struct nlattr **a = info->attrs;
1945 struct ovs_header *ovs_header = info->userhdr;
1946 struct vport_parms parms;
1947 struct sk_buff *reply;
1948 struct vport *vport;
1949 struct datapath *dp;
1954 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1955 !a[OVS_VPORT_ATTR_UPCALL_PID])
1958 err = ovs_vport_cmd_validate(a);
1963 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1968 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1969 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1972 if (port_no >= DP_MAX_PORTS)
1975 vport = ovs_vport_rtnl(dp, port_no);
1980 for (port_no = 1; ; port_no++) {
1981 if (port_no >= DP_MAX_PORTS) {
1985 vport = ovs_vport_rtnl(dp, port_no);
1991 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1992 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1993 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1995 parms.port_no = port_no;
1996 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1998 vport = new_vport(&parms);
1999 err = PTR_ERR(vport);
2004 if (a[OVS_VPORT_ATTR_STATS])
2005 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2007 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2009 if (IS_ERR(reply)) {
2010 err = PTR_ERR(reply);
2011 ovs_dp_detach_port(vport);
2014 genl_notify(reply, genl_info_net(info), info->snd_portid,
2015 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2023 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2025 struct nlattr **a = info->attrs;
2026 struct sk_buff *reply;
2027 struct vport *vport;
2030 err = ovs_vport_cmd_validate(a);
2035 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2036 err = PTR_ERR(vport);
2041 if (a[OVS_VPORT_ATTR_TYPE] &&
2042 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
2045 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2051 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
2052 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2056 if (a[OVS_VPORT_ATTR_STATS])
2057 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2059 if (a[OVS_VPORT_ATTR_UPCALL_PID])
2060 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2062 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2063 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2066 genl_notify(reply, genl_info_net(info), info->snd_portid,
2067 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2080 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2082 struct nlattr **a = info->attrs;
2083 struct sk_buff *reply;
2084 struct vport *vport;
2087 err = ovs_vport_cmd_validate(a);
2092 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2093 err = PTR_ERR(vport);
2097 if (vport->port_no == OVSP_LOCAL) {
2102 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2103 info->snd_seq, OVS_VPORT_CMD_DEL);
2104 err = PTR_ERR(reply);
2109 ovs_dp_detach_port(vport);
2111 genl_notify(reply, genl_info_net(info), info->snd_portid,
2112 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2120 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2122 struct nlattr **a = info->attrs;
2123 struct ovs_header *ovs_header = info->userhdr;
2124 struct sk_buff *reply;
2125 struct vport *vport;
2128 err = ovs_vport_cmd_validate(a);
2133 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2134 err = PTR_ERR(vport);
2138 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2139 info->snd_seq, OVS_VPORT_CMD_NEW);
2140 err = PTR_ERR(reply);
2146 return genlmsg_reply(reply, info);
2154 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2156 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2157 struct datapath *dp;
2158 int bucket = cb->args[0], skip = cb->args[1];
2161 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2166 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2167 struct vport *vport;
2170 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2172 ovs_vport_cmd_fill_info(vport, skb,
2173 NETLINK_CB(cb->skb).portid,
2176 OVS_VPORT_CMD_NEW) < 0)
2192 static struct genl_ops dp_vport_genl_ops[] = {
2193 { .cmd = OVS_VPORT_CMD_NEW,
2194 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2195 .policy = vport_policy,
2196 .doit = ovs_vport_cmd_new
2198 { .cmd = OVS_VPORT_CMD_DEL,
2199 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2200 .policy = vport_policy,
2201 .doit = ovs_vport_cmd_del
2203 { .cmd = OVS_VPORT_CMD_GET,
2204 .flags = 0, /* OK for unprivileged users. */
2205 .policy = vport_policy,
2206 .doit = ovs_vport_cmd_get,
2207 .dumpit = ovs_vport_cmd_dump
2209 { .cmd = OVS_VPORT_CMD_SET,
2210 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2211 .policy = vport_policy,
2212 .doit = ovs_vport_cmd_set,
2216 struct genl_family_and_ops {
2217 struct genl_family *family;
2218 struct genl_ops *ops;
2220 struct genl_multicast_group *group;
2223 static const struct genl_family_and_ops dp_genl_families[] = {
2224 { &dp_datapath_genl_family,
2225 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2226 &ovs_dp_datapath_multicast_group },
2227 { &dp_vport_genl_family,
2228 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2229 &ovs_dp_vport_multicast_group },
2230 { &dp_flow_genl_family,
2231 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2232 &ovs_dp_flow_multicast_group },
2233 { &dp_packet_genl_family,
2234 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2238 static void dp_unregister_genl(int n_families)
2242 for (i = 0; i < n_families; i++)
2243 genl_unregister_family(dp_genl_families[i].family);
2246 static int dp_register_genl(void)
2253 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2254 const struct genl_family_and_ops *f = &dp_genl_families[i];
2256 err = genl_register_family_with_ops(f->family, f->ops,
2263 err = genl_register_mc_group(f->family, f->group);
2272 dp_unregister_genl(n_registered);
2276 static int __rehash_flow_table(void *dummy)
2278 struct datapath *dp;
2283 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2285 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2286 struct flow_table *old_table = genl_dereference(dp->table);
2287 struct flow_table *new_table;
2289 new_table = ovs_flow_tbl_rehash(old_table);
2290 if (!IS_ERR(new_table)) {
2291 rcu_assign_pointer(dp->table, new_table);
2292 ovs_flow_tbl_deferred_destroy(old_table);
2300 static void rehash_flow_table(struct work_struct *work)
2302 genl_exec(__rehash_flow_table, NULL);
2303 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2306 static int dp_destroy_all(void *data)
2308 struct datapath *dp, *dp_next;
2309 struct ovs_net *ovs_net = data;
2311 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2317 static int __net_init ovs_init_net(struct net *net)
2319 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2321 INIT_LIST_HEAD(&ovs_net->dps);
2325 static void __net_exit ovs_exit_net(struct net *net)
2327 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2329 genl_exec(dp_destroy_all, ovs_net);
2332 static struct pernet_operations ovs_net_ops = {
2333 .init = ovs_init_net,
2334 .exit = ovs_exit_net,
2336 .size = sizeof(struct ovs_net),
2339 static int __init dp_init(void)
2343 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2345 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2348 err = genl_exec_init();
2352 err = ovs_workqueues_init();
2354 goto error_genl_exec;
2356 err = ovs_flow_init();
2360 err = ovs_vport_init();
2362 goto error_flow_exit;
2364 err = register_pernet_device(&ovs_net_ops);
2366 goto error_vport_exit;
2368 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2370 goto error_netns_exit;
2372 err = dp_register_genl();
2374 goto error_unreg_notifier;
2376 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2380 error_unreg_notifier:
2381 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2383 unregister_pernet_device(&ovs_net_ops);
2389 ovs_workqueues_exit();
2396 static void dp_cleanup(void)
2398 cancel_delayed_work_sync(&rehash_flow_wq);
2399 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2400 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2401 unregister_pernet_device(&ovs_net_ops);
2405 ovs_workqueues_exit();
2409 module_init(dp_init);
2410 module_exit(dp_cleanup);
2412 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2413 MODULE_LICENSE("GPL");
2414 MODULE_VERSION(VERSION);