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;
74 int (*ovs_dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
75 EXPORT_SYMBOL(ovs_dp_ioctl_hook);
80 * Writes to device state (add/remove datapath, port, set operations on vports,
81 * etc.) are protected by RTNL.
83 * Writes to other state (flow table modifications, set miscellaneous datapath
84 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
87 * Reads are protected by RCU.
89 * There are a few special cases (mostly stats) that have their own
90 * synchronization but they nest under all of above and don't interact with
94 static struct vport *new_vport(const struct vport_parms *);
95 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
96 const struct dp_upcall_info *);
97 static int queue_userspace_packet(struct net *, int dp_ifindex,
99 const struct dp_upcall_info *);
101 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
102 static struct datapath *get_dp(struct net *net, int dp_ifindex)
104 struct datapath *dp = NULL;
105 struct net_device *dev;
108 dev = dev_get_by_index_rcu(net, dp_ifindex);
110 struct vport *vport = ovs_internal_dev_get_vport(dev);
119 /* Must be called with rcu_read_lock or RTNL lock. */
120 const char *ovs_dp_name(const struct datapath *dp)
122 struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
123 return vport->ops->get_name(vport);
126 static int get_dpifindex(struct datapath *dp)
133 local = ovs_vport_rcu(dp, OVSP_LOCAL);
135 ifindex = local->ops->get_ifindex(local);
144 static size_t br_nlmsg_size(void)
146 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
147 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
148 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
149 + nla_total_size(4) /* IFLA_MASTER */
150 + nla_total_size(4) /* IFLA_MTU */
151 + nla_total_size(1); /* IFLA_OPERSTATE */
154 /* Caller must hold RTNL lock. */
155 static int dp_fill_ifinfo(struct sk_buff *skb,
156 const struct vport *port,
157 int event, unsigned int flags)
159 struct datapath *dp = port->dp;
160 struct ifinfomsg *hdr;
161 struct nlmsghdr *nlh;
163 if (!port->ops->get_ifindex)
166 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
170 hdr = nlmsg_data(nlh);
171 hdr->ifi_family = AF_BRIDGE;
173 hdr->ifi_type = ARPHRD_ETHER;
174 hdr->ifi_index = port->ops->get_ifindex(port);
175 hdr->ifi_flags = port->ops->get_dev_flags(port);
178 if (nla_put_string(skb, IFLA_IFNAME, port->ops->get_name(port)) ||
179 nla_put_u32(skb, IFLA_MASTER, get_dpifindex(dp)) ||
180 nla_put_u32(skb, IFLA_MTU, port->ops->get_mtu(port)) ||
181 #ifdef IFLA_OPERSTATE
182 nla_put_u8(skb, IFLA_OPERSTATE,
183 port->ops->is_running(port) ?
184 port->ops->get_operstate(port) :
187 nla_put(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port)))
188 goto nla_put_failure;
190 return nlmsg_end(skb, nlh);
193 nlmsg_cancel(skb, nlh);
197 /* Caller must hold RTNL lock. */
198 static void dp_ifinfo_notify(int event, struct vport *port)
203 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
209 err = dp_fill_ifinfo(skb, port, event, 0);
211 if (err == -ENODEV) {
214 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
215 WARN_ON(err == -EMSGSIZE);
220 rtnl_notify(skb, ovs_dp_get_net(port->dp), 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
224 rtnl_set_sk_err(ovs_dp_get_net(port->dp), RTNLGRP_LINK, err);
229 static void release_dp(struct kobject *kobj)
231 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
235 static struct kobj_type dp_ktype = {
236 .release = release_dp
239 static void destroy_dp_rcu(struct rcu_head *rcu)
241 struct datapath *dp = container_of(rcu, struct datapath, rcu);
243 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
244 free_percpu(dp->stats_percpu);
245 release_net(ovs_dp_get_net(dp));
247 kobject_put(&dp->ifobj);
250 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
253 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
256 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
259 struct hlist_node *n;
260 struct hlist_head *head;
262 head = vport_hash_bucket(dp, port_no);
263 hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
264 if (vport->port_no == port_no)
270 /* Called with RTNL lock and genl_lock. */
271 static struct vport *new_vport(const struct vport_parms *parms)
275 vport = ovs_vport_add(parms);
276 if (!IS_ERR(vport)) {
277 struct datapath *dp = parms->dp;
278 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
280 hlist_add_head_rcu(&vport->dp_hash_node, head);
281 dp_ifinfo_notify(RTM_NEWLINK, vport);
286 /* Called with RTNL lock. */
287 void ovs_dp_detach_port(struct vport *p)
291 if (p->port_no != OVSP_LOCAL)
292 ovs_dp_sysfs_del_if(p);
294 dp_ifinfo_notify(RTM_DELLINK, p);
296 /* First drop references to device. */
297 hlist_del_rcu(&p->dp_hash_node);
299 /* Then destroy it. */
303 /* Must be called with rcu_read_lock. */
304 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
306 struct datapath *dp = p->dp;
307 struct sw_flow *flow;
308 struct dp_stats_percpu *stats;
312 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
314 if (!OVS_CB(skb)->flow) {
315 struct sw_flow_key key;
318 /* Extract flow from 'skb' into 'key'. */
319 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
320 if (unlikely(error)) {
326 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
328 if (unlikely(!flow)) {
329 struct dp_upcall_info upcall;
331 upcall.cmd = OVS_PACKET_CMD_MISS;
333 upcall.userdata = NULL;
334 upcall.portid = p->upcall_portid;
335 ovs_dp_upcall(dp, skb, &upcall);
337 stats_counter = &stats->n_missed;
341 OVS_CB(skb)->flow = flow;
344 stats_counter = &stats->n_hit;
345 ovs_flow_used(OVS_CB(skb)->flow, skb);
346 ovs_execute_actions(dp, skb);
349 /* Update datapath statistics. */
350 u64_stats_update_begin(&stats->sync);
352 u64_stats_update_end(&stats->sync);
355 static struct genl_family dp_packet_genl_family = {
356 .id = GENL_ID_GENERATE,
357 .hdrsize = sizeof(struct ovs_header),
358 .name = OVS_PACKET_FAMILY,
359 .version = OVS_PACKET_VERSION,
360 .maxattr = OVS_PACKET_ATTR_MAX,
364 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
365 const struct dp_upcall_info *upcall_info)
367 struct dp_stats_percpu *stats;
371 if (upcall_info->portid == 0) {
376 dp_ifindex = get_dpifindex(dp);
382 forward_ip_summed(skb, true);
384 if (!skb_is_gso(skb))
385 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
387 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
394 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
396 u64_stats_update_begin(&stats->sync);
398 u64_stats_update_end(&stats->sync);
403 static int queue_gso_packets(struct net *net, int dp_ifindex,
405 const struct dp_upcall_info *upcall_info)
407 unsigned short gso_type = skb_shinfo(skb)->gso_type;
408 struct dp_upcall_info later_info;
409 struct sw_flow_key later_key;
410 struct sk_buff *segs, *nskb;
413 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
415 return PTR_ERR(segs);
417 /* Queue all of the segments. */
420 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
424 if (skb == segs && gso_type & SKB_GSO_UDP) {
425 /* The initial flow key extracted by ovs_flow_extract()
426 * in this case is for a first fragment, so we need to
427 * properly mark later fragments.
429 later_key = *upcall_info->key;
430 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
432 later_info = *upcall_info;
433 later_info.key = &later_key;
434 upcall_info = &later_info;
436 } while ((skb = skb->next));
438 /* Free all of the segments. */
446 } while ((skb = nskb));
450 static int queue_userspace_packet(struct net *net, int dp_ifindex,
452 const struct dp_upcall_info *upcall_info)
454 struct ovs_header *upcall;
455 struct sk_buff *nskb = NULL;
456 struct sk_buff *user_skb; /* to be queued to userspace */
461 if (vlan_tx_tag_present(skb)) {
462 nskb = skb_clone(skb, GFP_ATOMIC);
466 err = vlan_deaccel_tag(nskb);
473 if (nla_attr_size(skb->len) > USHRT_MAX) {
478 len = sizeof(struct ovs_header);
479 len += nla_total_size(skb->len);
480 len += nla_total_size(FLOW_BUFSIZE);
481 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
482 len += nla_total_size(8);
484 user_skb = genlmsg_new(len, GFP_ATOMIC);
490 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
491 0, upcall_info->cmd);
492 upcall->dp_ifindex = dp_ifindex;
494 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
495 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
496 nla_nest_end(user_skb, nla);
498 if (upcall_info->userdata)
499 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
500 nla_get_u64(upcall_info->userdata));
502 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
504 skb_copy_and_csum_dev(skb, nla_data(nla));
506 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
513 /* Called with genl_mutex. */
514 static int flush_flows(struct datapath *dp)
516 struct flow_table *old_table;
517 struct flow_table *new_table;
519 old_table = genl_dereference(dp->table);
520 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
524 rcu_assign_pointer(dp->table, new_table);
526 ovs_flow_tbl_deferred_destroy(old_table);
530 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
533 struct sw_flow_actions *acts;
535 int req_size = NLA_ALIGN(attr_len);
536 int next_offset = offsetof(struct sw_flow_actions, actions) +
539 if (req_size <= (ksize(*sfa) - next_offset))
542 new_acts_size = ksize(*sfa) * 2;
544 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
545 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
546 return ERR_PTR(-EMSGSIZE);
547 new_acts_size = MAX_ACTIONS_BUFSIZE;
550 acts = ovs_flow_actions_alloc(new_acts_size);
554 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
555 acts->actions_len = (*sfa)->actions_len;
560 (*sfa)->actions_len += req_size;
561 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
564 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
568 a = reserve_sfa_size(sfa, nla_attr_size(len));
572 a->nla_type = attrtype;
573 a->nla_len = nla_attr_size(len);
576 memcpy(nla_data(a), data, len);
577 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
582 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
584 int used = (*sfa)->actions_len;
587 err = add_action(sfa, attrtype, NULL, 0);
594 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
596 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
598 a->nla_len = sfa->actions_len - st_offset;
601 static int validate_and_copy_actions(const struct nlattr *attr,
602 const struct sw_flow_key *key, int depth,
603 struct sw_flow_actions **sfa);
605 static int validate_and_copy_sample(const struct nlattr *attr,
606 const struct sw_flow_key *key, int depth,
607 struct sw_flow_actions **sfa)
609 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
610 const struct nlattr *probability, *actions;
611 const struct nlattr *a;
612 int rem, start, err, st_acts;
614 memset(attrs, 0, sizeof(attrs));
615 nla_for_each_nested(a, attr, rem) {
616 int type = nla_type(a);
617 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
624 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
625 if (!probability || nla_len(probability) != sizeof(u32))
628 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
629 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
632 /* validation done, copy sample action. */
633 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
636 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
639 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
643 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
647 add_nested_action_end(*sfa, st_acts);
648 add_nested_action_end(*sfa, start);
653 static int validate_tp_port(const struct sw_flow_key *flow_key)
655 if (flow_key->eth.type == htons(ETH_P_IP)) {
656 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
658 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
659 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
666 static int validate_and_copy_set_tun(const struct nlattr *attr,
667 struct sw_flow_actions **sfa)
669 struct ovs_key_ipv4_tunnel tun_key;
672 err = ipv4_tun_from_nlattr(nla_data(attr), &tun_key);
676 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
680 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key, sizeof(tun_key));
681 add_nested_action_end(*sfa, start);
686 static int validate_set(const struct nlattr *a,
687 const struct sw_flow_key *flow_key,
688 struct sw_flow_actions **sfa,
691 const struct nlattr *ovs_key = nla_data(a);
692 int key_type = nla_type(ovs_key);
694 /* There can be only one key in a action */
695 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
698 if (key_type > OVS_KEY_ATTR_MAX ||
699 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
700 ovs_key_lens[key_type] != -1))
704 const struct ovs_key_ipv4 *ipv4_key;
705 const struct ovs_key_ipv6 *ipv6_key;
708 case OVS_KEY_ATTR_PRIORITY:
709 case OVS_KEY_ATTR_TUN_ID:
710 case OVS_KEY_ATTR_ETHERNET:
713 case OVS_KEY_ATTR_SKB_MARK:
714 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
715 if (nla_get_u32(ovs_key) != 0)
720 case OVS_KEY_ATTR_TUNNEL:
722 err = validate_and_copy_set_tun(a, sfa);
727 case OVS_KEY_ATTR_IPV4:
728 if (flow_key->eth.type != htons(ETH_P_IP))
731 if (!flow_key->ip.proto)
734 ipv4_key = nla_data(ovs_key);
735 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
738 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
743 case OVS_KEY_ATTR_IPV6:
744 if (flow_key->eth.type != htons(ETH_P_IPV6))
747 if (!flow_key->ip.proto)
750 ipv6_key = nla_data(ovs_key);
751 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
754 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
757 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
762 case OVS_KEY_ATTR_TCP:
763 if (flow_key->ip.proto != IPPROTO_TCP)
766 return validate_tp_port(flow_key);
768 case OVS_KEY_ATTR_UDP:
769 if (flow_key->ip.proto != IPPROTO_UDP)
772 return validate_tp_port(flow_key);
781 static int validate_userspace(const struct nlattr *attr)
783 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
784 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
785 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
787 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
790 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
791 attr, userspace_policy);
795 if (!a[OVS_USERSPACE_ATTR_PID] ||
796 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
802 static int copy_action(const struct nlattr *from,
803 struct sw_flow_actions **sfa)
805 int totlen = NLA_ALIGN(from->nla_len);
808 to = reserve_sfa_size(sfa, from->nla_len);
812 memcpy(to, from, totlen);
816 static int validate_and_copy_actions(const struct nlattr *attr,
817 const struct sw_flow_key *key,
819 struct sw_flow_actions **sfa)
821 const struct nlattr *a;
824 if (depth >= SAMPLE_ACTION_DEPTH)
827 nla_for_each_nested(a, attr, rem) {
828 /* Expected argument lengths, (u32)-1 for variable length. */
829 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
830 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
831 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
832 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
833 [OVS_ACTION_ATTR_POP_VLAN] = 0,
834 [OVS_ACTION_ATTR_SET] = (u32)-1,
835 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
837 const struct ovs_action_push_vlan *vlan;
838 int type = nla_type(a);
841 if (type > OVS_ACTION_ATTR_MAX ||
842 (action_lens[type] != nla_len(a) &&
843 action_lens[type] != (u32)-1))
848 case OVS_ACTION_ATTR_UNSPEC:
851 case OVS_ACTION_ATTR_USERSPACE:
852 err = validate_userspace(a);
857 case OVS_ACTION_ATTR_OUTPUT:
858 if (nla_get_u32(a) >= DP_MAX_PORTS)
863 case OVS_ACTION_ATTR_POP_VLAN:
866 case OVS_ACTION_ATTR_PUSH_VLAN:
868 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
870 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
874 case OVS_ACTION_ATTR_SET:
875 err = validate_set(a, key, sfa, &skip_copy);
880 case OVS_ACTION_ATTR_SAMPLE:
881 err = validate_and_copy_sample(a, key, depth, sfa);
891 err = copy_action(a, sfa);
903 static void clear_stats(struct sw_flow *flow)
907 flow->packet_count = 0;
908 flow->byte_count = 0;
911 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
913 struct ovs_header *ovs_header = info->userhdr;
914 struct nlattr **a = info->attrs;
915 struct sw_flow_actions *acts;
916 struct sk_buff *packet;
917 struct sw_flow *flow;
925 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
926 !a[OVS_PACKET_ATTR_ACTIONS] ||
927 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
930 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
931 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
935 skb_reserve(packet, NET_IP_ALIGN);
937 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
939 skb_reset_mac_header(packet);
940 eth = eth_hdr(packet);
942 /* Normally, setting the skb 'protocol' field would be handled by a
943 * call to eth_type_trans(), but it assumes there's a sending
944 * device, which we may not have. */
945 if (ntohs(eth->h_proto) >= 1536)
946 packet->protocol = eth->h_proto;
948 packet->protocol = htons(ETH_P_802_2);
950 /* Build an sw_flow for sending this packet. */
951 flow = ovs_flow_alloc();
956 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
960 err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]);
963 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
968 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
969 rcu_assign_pointer(flow->sf_acts, acts);
973 OVS_CB(packet)->flow = flow;
974 packet->priority = flow->key.phy.priority;
975 skb_set_mark(packet, flow->key.phy.skb_mark);
978 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
984 err = ovs_execute_actions(dp, packet);
1001 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
1002 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
1003 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
1004 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
1007 static struct genl_ops dp_packet_genl_ops[] = {
1008 { .cmd = OVS_PACKET_CMD_EXECUTE,
1009 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1010 .policy = packet_policy,
1011 .doit = ovs_packet_cmd_execute
1015 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
1018 struct flow_table *table = genl_dereference(dp->table);
1020 stats->n_flows = ovs_flow_tbl_count(table);
1022 stats->n_hit = stats->n_missed = stats->n_lost = 0;
1023 for_each_possible_cpu(i) {
1024 const struct dp_stats_percpu *percpu_stats;
1025 struct dp_stats_percpu local_stats;
1028 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
1031 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
1032 local_stats = *percpu_stats;
1033 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
1035 stats->n_hit += local_stats.n_hit;
1036 stats->n_missed += local_stats.n_missed;
1037 stats->n_lost += local_stats.n_lost;
1041 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1042 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1043 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1044 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1047 static struct genl_family dp_flow_genl_family = {
1048 .id = GENL_ID_GENERATE,
1049 .hdrsize = sizeof(struct ovs_header),
1050 .name = OVS_FLOW_FAMILY,
1051 .version = OVS_FLOW_VERSION,
1052 .maxattr = OVS_FLOW_ATTR_MAX,
1056 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1057 .name = OVS_FLOW_MCGROUP
1060 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1061 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1063 const struct nlattr *a;
1064 struct nlattr *start;
1067 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1071 nla_for_each_nested(a, attr, rem) {
1072 int type = nla_type(a);
1073 struct nlattr *st_sample;
1076 case OVS_SAMPLE_ATTR_PROBABILITY:
1077 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1080 case OVS_SAMPLE_ATTR_ACTIONS:
1081 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1084 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1087 nla_nest_end(skb, st_sample);
1092 nla_nest_end(skb, start);
1096 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1098 const struct nlattr *ovs_key = nla_data(a);
1099 int key_type = nla_type(ovs_key);
1100 struct nlattr *start;
1104 case OVS_KEY_ATTR_IPV4_TUNNEL:
1105 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1109 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key));
1112 nla_nest_end(skb, start);
1115 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1123 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1125 const struct nlattr *a;
1128 nla_for_each_attr(a, attr, len, rem) {
1129 int type = nla_type(a);
1132 case OVS_ACTION_ATTR_SET:
1133 err = set_action_to_attr(a, skb);
1138 case OVS_ACTION_ATTR_SAMPLE:
1139 err = sample_action_to_attr(a, skb);
1144 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1153 /* Called with genl_lock. */
1154 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1155 struct sk_buff *skb, u32 portid,
1156 u32 seq, u32 flags, u8 cmd)
1158 const int skb_orig_len = skb->len;
1159 const struct sw_flow_actions *sf_acts;
1160 struct nlattr *start;
1161 struct ovs_flow_stats stats;
1162 struct ovs_header *ovs_header;
1168 sf_acts = rcu_dereference_protected(flow->sf_acts,
1169 lockdep_genl_is_held());
1171 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1175 ovs_header->dp_ifindex = get_dpifindex(dp);
1177 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1179 goto nla_put_failure;
1180 err = ovs_flow_to_nlattrs(&flow->key, skb);
1183 nla_nest_end(skb, nla);
1185 spin_lock_bh(&flow->lock);
1187 stats.n_packets = flow->packet_count;
1188 stats.n_bytes = flow->byte_count;
1189 tcp_flags = flow->tcp_flags;
1190 spin_unlock_bh(&flow->lock);
1193 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1194 goto nla_put_failure;
1196 if (stats.n_packets &&
1197 nla_put(skb, OVS_FLOW_ATTR_STATS,
1198 sizeof(struct ovs_flow_stats), &stats))
1199 goto nla_put_failure;
1202 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1203 goto nla_put_failure;
1205 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1206 * this is the first flow to be dumped into 'skb'. This is unusual for
1207 * Netlink but individual action lists can be longer than
1208 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1209 * The userspace caller can always fetch the actions separately if it
1210 * really wants them. (Most userspace callers in fact don't care.)
1212 * This can only fail for dump operations because the skb is always
1213 * properly sized for single flows.
1215 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1217 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1218 if (err < 0 && skb_orig_len)
1220 nla_nest_end(skb, start);
1221 } else if (skb_orig_len) {
1226 return genlmsg_end(skb, ovs_header);
1231 genlmsg_cancel(skb, ovs_header);
1235 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1237 const struct sw_flow_actions *sf_acts;
1240 sf_acts = rcu_dereference_protected(flow->sf_acts,
1241 lockdep_genl_is_held());
1243 /* OVS_FLOW_ATTR_KEY */
1244 len = nla_total_size(FLOW_BUFSIZE);
1245 /* OVS_FLOW_ATTR_ACTIONS */
1246 len += nla_total_size(sf_acts->actions_len);
1247 /* OVS_FLOW_ATTR_STATS */
1248 len += nla_total_size(sizeof(struct ovs_flow_stats));
1249 /* OVS_FLOW_ATTR_TCP_FLAGS */
1250 len += nla_total_size(1);
1251 /* OVS_FLOW_ATTR_USED */
1252 len += nla_total_size(8);
1254 len += NLMSG_ALIGN(sizeof(struct ovs_header));
1256 return genlmsg_new(len, GFP_KERNEL);
1259 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1260 struct datapath *dp,
1261 u32 portid, u32 seq, u8 cmd)
1263 struct sk_buff *skb;
1266 skb = ovs_flow_cmd_alloc_info(flow);
1268 return ERR_PTR(-ENOMEM);
1270 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1275 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1277 struct nlattr **a = info->attrs;
1278 struct ovs_header *ovs_header = info->userhdr;
1279 struct sw_flow_key key;
1280 struct sw_flow *flow;
1281 struct sk_buff *reply;
1282 struct datapath *dp;
1283 struct flow_table *table;
1284 struct sw_flow_actions *acts = NULL;
1290 if (!a[OVS_FLOW_ATTR_KEY])
1292 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1296 /* Validate actions. */
1297 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1298 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1299 error = PTR_ERR(acts);
1303 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0, &acts);
1306 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1311 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1316 table = genl_dereference(dp->table);
1317 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1319 /* Bail out if we're not allowed to create a new flow. */
1321 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1324 /* Expand table, if necessary, to make room. */
1325 if (ovs_flow_tbl_need_to_expand(table)) {
1326 struct flow_table *new_table;
1328 new_table = ovs_flow_tbl_expand(table);
1329 if (!IS_ERR(new_table)) {
1330 rcu_assign_pointer(dp->table, new_table);
1331 ovs_flow_tbl_deferred_destroy(table);
1332 table = genl_dereference(dp->table);
1336 /* Allocate flow. */
1337 flow = ovs_flow_alloc();
1339 error = PTR_ERR(flow);
1344 rcu_assign_pointer(flow->sf_acts, acts);
1346 /* Put flow in bucket. */
1347 ovs_flow_tbl_insert(table, flow, &key, key_len);
1349 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1353 /* We found a matching flow. */
1354 struct sw_flow_actions *old_acts;
1356 /* Bail out if we're not allowed to modify an existing flow.
1357 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1358 * because Generic Netlink treats the latter as a dump
1359 * request. We also accept NLM_F_EXCL in case that bug ever
1363 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1364 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1367 /* Update actions. */
1368 old_acts = rcu_dereference_protected(flow->sf_acts,
1369 lockdep_genl_is_held());
1370 rcu_assign_pointer(flow->sf_acts, acts);
1371 ovs_flow_deferred_free_acts(old_acts);
1373 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1374 info->snd_seq, OVS_FLOW_CMD_NEW);
1377 if (a[OVS_FLOW_ATTR_CLEAR]) {
1378 spin_lock_bh(&flow->lock);
1380 spin_unlock_bh(&flow->lock);
1385 genl_notify(reply, genl_info_net(info), info->snd_portid,
1386 ovs_dp_flow_multicast_group.id, info->nlhdr,
1389 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1390 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1399 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1401 struct nlattr **a = info->attrs;
1402 struct ovs_header *ovs_header = info->userhdr;
1403 struct sw_flow_key key;
1404 struct sk_buff *reply;
1405 struct sw_flow *flow;
1406 struct datapath *dp;
1407 struct flow_table *table;
1411 if (!a[OVS_FLOW_ATTR_KEY])
1413 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1417 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1421 table = genl_dereference(dp->table);
1422 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1426 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1427 info->snd_seq, OVS_FLOW_CMD_NEW);
1429 return PTR_ERR(reply);
1431 return genlmsg_reply(reply, info);
1434 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1436 struct nlattr **a = info->attrs;
1437 struct ovs_header *ovs_header = info->userhdr;
1438 struct sw_flow_key key;
1439 struct sk_buff *reply;
1440 struct sw_flow *flow;
1441 struct datapath *dp;
1442 struct flow_table *table;
1446 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1450 if (!a[OVS_FLOW_ATTR_KEY])
1451 return flush_flows(dp);
1453 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1457 table = genl_dereference(dp->table);
1458 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1462 reply = ovs_flow_cmd_alloc_info(flow);
1466 ovs_flow_tbl_remove(table, flow);
1468 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1469 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1472 ovs_flow_deferred_free(flow);
1474 genl_notify(reply, genl_info_net(info), info->snd_portid,
1475 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1479 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1481 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1482 struct datapath *dp;
1483 struct flow_table *table;
1485 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1489 table = genl_dereference(dp->table);
1492 struct sw_flow *flow;
1495 bucket = cb->args[0];
1497 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1501 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1502 NETLINK_CB(cb->skb).portid,
1503 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1504 OVS_FLOW_CMD_NEW) < 0)
1507 cb->args[0] = bucket;
1513 static struct genl_ops dp_flow_genl_ops[] = {
1514 { .cmd = OVS_FLOW_CMD_NEW,
1515 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1516 .policy = flow_policy,
1517 .doit = ovs_flow_cmd_new_or_set
1519 { .cmd = OVS_FLOW_CMD_DEL,
1520 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1521 .policy = flow_policy,
1522 .doit = ovs_flow_cmd_del
1524 { .cmd = OVS_FLOW_CMD_GET,
1525 .flags = 0, /* OK for unprivileged users. */
1526 .policy = flow_policy,
1527 .doit = ovs_flow_cmd_get,
1528 .dumpit = ovs_flow_cmd_dump
1530 { .cmd = OVS_FLOW_CMD_SET,
1531 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1532 .policy = flow_policy,
1533 .doit = ovs_flow_cmd_new_or_set,
1537 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1538 #ifdef HAVE_NLA_NUL_STRING
1539 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1541 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1544 static struct genl_family dp_datapath_genl_family = {
1545 .id = GENL_ID_GENERATE,
1546 .hdrsize = sizeof(struct ovs_header),
1547 .name = OVS_DATAPATH_FAMILY,
1548 .version = OVS_DATAPATH_VERSION,
1549 .maxattr = OVS_DP_ATTR_MAX,
1553 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1554 .name = OVS_DATAPATH_MCGROUP
1557 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1558 u32 portid, u32 seq, u32 flags, u8 cmd)
1560 struct ovs_header *ovs_header;
1561 struct ovs_dp_stats dp_stats;
1564 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1569 ovs_header->dp_ifindex = get_dpifindex(dp);
1572 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1575 goto nla_put_failure;
1577 get_dp_stats(dp, &dp_stats);
1578 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1579 goto nla_put_failure;
1581 return genlmsg_end(skb, ovs_header);
1584 genlmsg_cancel(skb, ovs_header);
1589 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1592 struct sk_buff *skb;
1595 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1597 return ERR_PTR(-ENOMEM);
1599 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1602 return ERR_PTR(retval);
1607 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1609 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1612 /* Called with genl_mutex and optionally with RTNL lock also. */
1613 static struct datapath *lookup_datapath(struct net *net,
1614 struct ovs_header *ovs_header,
1615 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1617 struct datapath *dp;
1619 if (!a[OVS_DP_ATTR_NAME])
1620 dp = get_dp(net, ovs_header->dp_ifindex);
1622 struct vport *vport;
1625 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1626 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1629 return dp ? dp : ERR_PTR(-ENODEV);
1632 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1634 struct nlattr **a = info->attrs;
1635 struct vport_parms parms;
1636 struct sk_buff *reply;
1637 struct datapath *dp;
1638 struct vport *vport;
1639 struct ovs_net *ovs_net;
1643 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1646 err = ovs_dp_cmd_validate(a);
1653 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1655 goto err_unlock_rtnl;
1657 /* Initialize kobject for bridge. This will be added as
1658 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1659 dp->ifobj.kset = NULL;
1660 kobject_init(&dp->ifobj, &dp_ktype);
1662 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1664 /* Allocate table. */
1666 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1670 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1671 if (!dp->stats_percpu) {
1673 goto err_destroy_table;
1676 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1680 goto err_destroy_percpu;
1683 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1684 INIT_HLIST_HEAD(&dp->ports[i]);
1686 /* Set up our datapath device. */
1687 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1688 parms.type = OVS_VPORT_TYPE_INTERNAL;
1689 parms.options = NULL;
1691 parms.port_no = OVSP_LOCAL;
1692 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1694 vport = new_vport(&parms);
1695 if (IS_ERR(vport)) {
1696 err = PTR_ERR(vport);
1700 goto err_destroy_ports_array;
1703 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1704 info->snd_seq, OVS_DP_CMD_NEW);
1705 err = PTR_ERR(reply);
1707 goto err_destroy_local_port;
1709 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1710 list_add_tail(&dp->list_node, &ovs_net->dps);
1711 ovs_dp_sysfs_add_dp(dp);
1715 genl_notify(reply, genl_info_net(info), info->snd_portid,
1716 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1720 err_destroy_local_port:
1721 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1722 err_destroy_ports_array:
1725 free_percpu(dp->stats_percpu);
1727 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1729 release_net(ovs_dp_get_net(dp));
1737 /* Called with genl_mutex. */
1738 static void __dp_destroy(struct datapath *dp)
1744 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1745 struct vport *vport;
1746 struct hlist_node *node, *n;
1748 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1749 if (vport->port_no != OVSP_LOCAL)
1750 ovs_dp_detach_port(vport);
1753 ovs_dp_sysfs_del_dp(dp);
1754 list_del(&dp->list_node);
1755 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1757 /* rtnl_unlock() will wait until all the references to devices that
1758 * are pending unregistration have been dropped. We do it here to
1759 * ensure that any internal devices (which contain DP pointers) are
1760 * fully destroyed before freeing the datapath.
1764 call_rcu(&dp->rcu, destroy_dp_rcu);
1767 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1769 struct sk_buff *reply;
1770 struct datapath *dp;
1773 err = ovs_dp_cmd_validate(info->attrs);
1777 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1782 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1783 info->snd_seq, OVS_DP_CMD_DEL);
1784 err = PTR_ERR(reply);
1790 genl_notify(reply, genl_info_net(info), info->snd_portid,
1791 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1797 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1799 struct sk_buff *reply;
1800 struct datapath *dp;
1803 err = ovs_dp_cmd_validate(info->attrs);
1807 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1811 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1812 info->snd_seq, OVS_DP_CMD_NEW);
1813 if (IS_ERR(reply)) {
1814 err = PTR_ERR(reply);
1815 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1816 ovs_dp_datapath_multicast_group.id, err);
1820 genl_notify(reply, genl_info_net(info), info->snd_portid,
1821 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1827 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1829 struct sk_buff *reply;
1830 struct datapath *dp;
1833 err = ovs_dp_cmd_validate(info->attrs);
1837 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1841 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1842 info->snd_seq, OVS_DP_CMD_NEW);
1844 return PTR_ERR(reply);
1846 return genlmsg_reply(reply, info);
1849 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1851 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1852 struct datapath *dp;
1853 int skip = cb->args[0];
1856 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1858 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1859 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1860 OVS_DP_CMD_NEW) < 0)
1870 static struct genl_ops dp_datapath_genl_ops[] = {
1871 { .cmd = OVS_DP_CMD_NEW,
1872 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1873 .policy = datapath_policy,
1874 .doit = ovs_dp_cmd_new
1876 { .cmd = OVS_DP_CMD_DEL,
1877 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1878 .policy = datapath_policy,
1879 .doit = ovs_dp_cmd_del
1881 { .cmd = OVS_DP_CMD_GET,
1882 .flags = 0, /* OK for unprivileged users. */
1883 .policy = datapath_policy,
1884 .doit = ovs_dp_cmd_get,
1885 .dumpit = ovs_dp_cmd_dump
1887 { .cmd = OVS_DP_CMD_SET,
1888 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1889 .policy = datapath_policy,
1890 .doit = ovs_dp_cmd_set,
1894 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1895 #ifdef HAVE_NLA_NUL_STRING
1896 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1897 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1898 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1900 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1901 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1903 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1904 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1905 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1906 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1909 static struct genl_family dp_vport_genl_family = {
1910 .id = GENL_ID_GENERATE,
1911 .hdrsize = sizeof(struct ovs_header),
1912 .name = OVS_VPORT_FAMILY,
1913 .version = OVS_VPORT_VERSION,
1914 .maxattr = OVS_VPORT_ATTR_MAX,
1918 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1919 .name = OVS_VPORT_MCGROUP
1922 /* Called with RTNL lock or RCU read lock. */
1923 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1924 u32 portid, u32 seq, u32 flags, u8 cmd)
1926 struct ovs_header *ovs_header;
1927 struct ovs_vport_stats vport_stats;
1930 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1935 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1937 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1938 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1939 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1940 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1941 goto nla_put_failure;
1943 ovs_vport_get_stats(vport, &vport_stats);
1944 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1946 goto nla_put_failure;
1948 if (nla_put(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1949 vport->ops->get_addr(vport)))
1950 goto nla_put_failure;
1952 err = ovs_vport_get_options(vport, skb);
1953 if (err == -EMSGSIZE)
1956 return genlmsg_end(skb, ovs_header);
1961 genlmsg_cancel(skb, ovs_header);
1965 /* Called with RTNL lock or RCU read lock. */
1966 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1969 struct sk_buff *skb;
1972 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1974 return ERR_PTR(-ENOMEM);
1976 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1979 return ERR_PTR(retval);
1984 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1986 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1989 /* Called with RTNL lock or RCU read lock. */
1990 static struct vport *lookup_vport(struct net *net,
1991 struct ovs_header *ovs_header,
1992 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1994 struct datapath *dp;
1995 struct vport *vport;
1997 if (a[OVS_VPORT_ATTR_NAME]) {
1998 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2000 return ERR_PTR(-ENODEV);
2001 if (ovs_header->dp_ifindex &&
2002 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2003 return ERR_PTR(-ENODEV);
2005 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2006 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2008 if (port_no >= DP_MAX_PORTS)
2009 return ERR_PTR(-EFBIG);
2011 dp = get_dp(net, ovs_header->dp_ifindex);
2013 return ERR_PTR(-ENODEV);
2015 vport = ovs_vport_rtnl_rcu(dp, port_no);
2017 return ERR_PTR(-ENOENT);
2020 return ERR_PTR(-EINVAL);
2023 /* Called with RTNL lock. */
2024 static int change_vport(struct vport *vport,
2025 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2029 if (a[OVS_VPORT_ATTR_STATS])
2030 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2032 if (a[OVS_VPORT_ATTR_ADDRESS])
2033 err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
2038 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2040 struct nlattr **a = info->attrs;
2041 struct ovs_header *ovs_header = info->userhdr;
2042 struct vport_parms parms;
2043 struct sk_buff *reply;
2044 struct vport *vport;
2045 struct datapath *dp;
2050 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2051 !a[OVS_VPORT_ATTR_UPCALL_PID])
2054 err = ovs_vport_cmd_validate(a);
2059 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2064 if (a[OVS_VPORT_ATTR_PORT_NO]) {
2065 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2068 if (port_no >= DP_MAX_PORTS)
2071 vport = ovs_vport_rtnl(dp, port_no);
2076 for (port_no = 1; ; port_no++) {
2077 if (port_no >= DP_MAX_PORTS) {
2081 vport = ovs_vport_rtnl(dp, port_no);
2087 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2088 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2089 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2091 parms.port_no = port_no;
2092 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2094 vport = new_vport(&parms);
2095 err = PTR_ERR(vport);
2099 ovs_dp_sysfs_add_if(vport);
2101 err = change_vport(vport, a);
2103 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2107 err = PTR_ERR(reply);
2110 ovs_dp_detach_port(vport);
2113 genl_notify(reply, genl_info_net(info), info->snd_portid,
2114 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2122 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2124 struct nlattr **a = info->attrs;
2125 struct sk_buff *reply;
2126 struct vport *vport;
2129 err = ovs_vport_cmd_validate(a);
2134 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2135 err = PTR_ERR(vport);
2140 if (a[OVS_VPORT_ATTR_TYPE] &&
2141 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
2144 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
2145 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2147 err = change_vport(vport, a);
2150 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
2151 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2153 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2154 info->snd_seq, OVS_VPORT_CMD_NEW);
2155 if (IS_ERR(reply)) {
2156 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
2157 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
2161 genl_notify(reply, genl_info_net(info), info->snd_portid,
2162 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2170 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2172 struct nlattr **a = info->attrs;
2173 struct sk_buff *reply;
2174 struct vport *vport;
2177 err = ovs_vport_cmd_validate(a);
2182 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2183 err = PTR_ERR(vport);
2187 if (vport->port_no == OVSP_LOCAL) {
2192 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2193 info->snd_seq, OVS_VPORT_CMD_DEL);
2194 err = PTR_ERR(reply);
2198 ovs_dp_detach_port(vport);
2200 genl_notify(reply, genl_info_net(info), info->snd_portid,
2201 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2209 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2211 struct nlattr **a = info->attrs;
2212 struct ovs_header *ovs_header = info->userhdr;
2213 struct sk_buff *reply;
2214 struct vport *vport;
2217 err = ovs_vport_cmd_validate(a);
2222 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2223 err = PTR_ERR(vport);
2227 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2228 info->snd_seq, OVS_VPORT_CMD_NEW);
2229 err = PTR_ERR(reply);
2235 return genlmsg_reply(reply, info);
2243 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2245 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2246 struct datapath *dp;
2247 int bucket = cb->args[0], skip = cb->args[1];
2250 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2255 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2256 struct vport *vport;
2257 struct hlist_node *n;
2260 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
2262 ovs_vport_cmd_fill_info(vport, skb,
2263 NETLINK_CB(cb->skb).portid,
2266 OVS_VPORT_CMD_NEW) < 0)
2282 static struct genl_ops dp_vport_genl_ops[] = {
2283 { .cmd = OVS_VPORT_CMD_NEW,
2284 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2285 .policy = vport_policy,
2286 .doit = ovs_vport_cmd_new
2288 { .cmd = OVS_VPORT_CMD_DEL,
2289 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2290 .policy = vport_policy,
2291 .doit = ovs_vport_cmd_del
2293 { .cmd = OVS_VPORT_CMD_GET,
2294 .flags = 0, /* OK for unprivileged users. */
2295 .policy = vport_policy,
2296 .doit = ovs_vport_cmd_get,
2297 .dumpit = ovs_vport_cmd_dump
2299 { .cmd = OVS_VPORT_CMD_SET,
2300 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2301 .policy = vport_policy,
2302 .doit = ovs_vport_cmd_set,
2306 struct genl_family_and_ops {
2307 struct genl_family *family;
2308 struct genl_ops *ops;
2310 struct genl_multicast_group *group;
2313 static const struct genl_family_and_ops dp_genl_families[] = {
2314 { &dp_datapath_genl_family,
2315 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2316 &ovs_dp_datapath_multicast_group },
2317 { &dp_vport_genl_family,
2318 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2319 &ovs_dp_vport_multicast_group },
2320 { &dp_flow_genl_family,
2321 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2322 &ovs_dp_flow_multicast_group },
2323 { &dp_packet_genl_family,
2324 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2328 static void dp_unregister_genl(int n_families)
2332 for (i = 0; i < n_families; i++)
2333 genl_unregister_family(dp_genl_families[i].family);
2336 static int dp_register_genl(void)
2343 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2344 const struct genl_family_and_ops *f = &dp_genl_families[i];
2346 err = genl_register_family_with_ops(f->family, f->ops,
2353 err = genl_register_mc_group(f->family, f->group);
2362 dp_unregister_genl(n_registered);
2366 static int __rehash_flow_table(void *dummy)
2368 struct datapath *dp;
2373 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2375 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2376 struct flow_table *old_table = genl_dereference(dp->table);
2377 struct flow_table *new_table;
2379 new_table = ovs_flow_tbl_rehash(old_table);
2380 if (!IS_ERR(new_table)) {
2381 rcu_assign_pointer(dp->table, new_table);
2382 ovs_flow_tbl_deferred_destroy(old_table);
2390 static void rehash_flow_table(struct work_struct *work)
2392 genl_exec(__rehash_flow_table, NULL);
2393 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2396 static int dp_destroy_all(void *data)
2398 struct datapath *dp, *dp_next;
2399 struct ovs_net *ovs_net = data;
2401 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2407 static int __net_init ovs_init_net(struct net *net)
2409 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2411 INIT_LIST_HEAD(&ovs_net->dps);
2415 static void __net_exit ovs_exit_net(struct net *net)
2417 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2419 genl_exec(dp_destroy_all, ovs_net);
2422 static struct pernet_operations ovs_net_ops = {
2423 .init = ovs_init_net,
2424 .exit = ovs_exit_net,
2426 .size = sizeof(struct ovs_net),
2429 static int __init dp_init(void)
2431 struct sk_buff *dummy_skb;
2434 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2436 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2439 err = genl_exec_init();
2443 err = ovs_workqueues_init();
2445 goto error_genl_exec;
2447 err = ovs_tnl_init();
2451 err = ovs_flow_init();
2453 goto error_tnl_exit;
2455 err = ovs_vport_init();
2457 goto error_flow_exit;
2459 err = register_pernet_device(&ovs_net_ops);
2461 goto error_vport_exit;
2463 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2465 goto error_netns_exit;
2467 err = dp_register_genl();
2469 goto error_unreg_notifier;
2471 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2475 error_unreg_notifier:
2476 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2478 unregister_pernet_device(&ovs_net_ops);
2486 ovs_workqueues_exit();
2493 static void dp_cleanup(void)
2495 cancel_delayed_work_sync(&rehash_flow_wq);
2496 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2497 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2498 unregister_pernet_device(&ovs_net_ops);
2503 ovs_workqueues_exit();
2507 module_init(dp_init);
2508 module_exit(dp_cleanup);
2510 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2511 MODULE_LICENSE("GPL");
2512 MODULE_VERSION(VERSION);