2 * Copyright (c) 2007-2013 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
62 #include "vport-internal_dev.h"
63 #include "vport-netdev.h"
65 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
66 LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
67 #error Kernels before 2.6.18 or after 3.8 are not supported by this version of Open vSwitch.
70 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
71 static void rehash_flow_table(struct work_struct *work);
72 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
74 int ovs_net_id __read_mostly;
76 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
77 struct genl_multicast_group *grp)
79 genl_notify(skb, genl_info_net(info), info->snd_portid,
80 grp->id, info->nlhdr, GFP_KERNEL);
86 * All writes e.g. Writes to device state (add/remove datapath, port, set
87 * operations on vports, etc.), Writes to other state (flow table
88 * modifications, set miscellaneous datapath parameters, etc.) are protected
91 * Reads are protected by RCU.
93 * There are a few special cases (mostly stats) that have their own
94 * synchronization but they nest under all of above and don't interact with
97 * The RTNL lock nests inside ovs_mutex.
100 static DEFINE_MUTEX(ovs_mutex);
104 mutex_lock(&ovs_mutex);
107 void ovs_unlock(void)
109 mutex_unlock(&ovs_mutex);
112 #ifdef CONFIG_LOCKDEP
113 int lockdep_ovsl_is_held(void)
116 return lockdep_is_held(&ovs_mutex);
122 static struct vport *new_vport(const struct vport_parms *);
123 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
124 const struct dp_upcall_info *);
125 static int queue_userspace_packet(struct net *, int dp_ifindex,
127 const struct dp_upcall_info *);
129 /* Must be called with rcu_read_lock or ovs_mutex. */
130 static struct datapath *get_dp(struct net *net, int dp_ifindex)
132 struct datapath *dp = NULL;
133 struct net_device *dev;
136 dev = dev_get_by_index_rcu(net, dp_ifindex);
138 struct vport *vport = ovs_internal_dev_get_vport(dev);
147 /* Must be called with rcu_read_lock or ovs_mutex. */
148 const char *ovs_dp_name(const struct datapath *dp)
150 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
151 return vport->ops->get_name(vport);
154 static int get_dpifindex(struct datapath *dp)
161 local = ovs_vport_rcu(dp, OVSP_LOCAL);
163 ifindex = netdev_vport_priv(local)->dev->ifindex;
172 static void destroy_dp_rcu(struct rcu_head *rcu)
174 struct datapath *dp = container_of(rcu, struct datapath, rcu);
176 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
177 free_percpu(dp->stats_percpu);
178 release_net(ovs_dp_get_net(dp));
183 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
186 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
189 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
192 struct hlist_head *head;
194 head = vport_hash_bucket(dp, port_no);
195 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
196 if (vport->port_no == port_no)
202 /* Called with ovs_mutex. */
203 static struct vport *new_vport(const struct vport_parms *parms)
207 vport = ovs_vport_add(parms);
208 if (!IS_ERR(vport)) {
209 struct datapath *dp = parms->dp;
210 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
212 hlist_add_head_rcu(&vport->dp_hash_node, head);
217 void ovs_dp_detach_port(struct vport *p)
221 /* First drop references to device. */
222 hlist_del_rcu(&p->dp_hash_node);
224 /* Then destroy it. */
228 /* Must be called with rcu_read_lock. */
229 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
231 struct datapath *dp = p->dp;
232 struct sw_flow *flow;
233 struct dp_stats_percpu *stats;
234 struct sw_flow_key key;
238 stats = this_cpu_ptr(dp->stats_percpu);
240 /* Extract flow from 'skb' into 'key'. */
241 error = ovs_flow_extract(skb, p->port_no, &key);
242 if (unlikely(error)) {
248 flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
249 if (unlikely(!flow)) {
250 struct dp_upcall_info upcall;
252 upcall.cmd = OVS_PACKET_CMD_MISS;
254 upcall.userdata = NULL;
255 upcall.portid = p->upcall_portid;
256 ovs_dp_upcall(dp, skb, &upcall);
258 stats_counter = &stats->n_missed;
262 OVS_CB(skb)->flow = flow;
263 OVS_CB(skb)->pkt_key = &key;
265 stats_counter = &stats->n_hit;
266 ovs_flow_used(OVS_CB(skb)->flow, skb);
267 ovs_execute_actions(dp, skb);
270 /* Update datapath statistics. */
271 u64_stats_update_begin(&stats->sync);
273 u64_stats_update_end(&stats->sync);
276 static struct genl_family dp_packet_genl_family = {
277 .id = GENL_ID_GENERATE,
278 .hdrsize = sizeof(struct ovs_header),
279 .name = OVS_PACKET_FAMILY,
280 .version = OVS_PACKET_VERSION,
281 .maxattr = OVS_PACKET_ATTR_MAX,
285 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
286 const struct dp_upcall_info *upcall_info)
288 struct dp_stats_percpu *stats;
292 if (upcall_info->portid == 0) {
297 dp_ifindex = get_dpifindex(dp);
303 forward_ip_summed(skb, true);
305 if (!skb_is_gso(skb))
306 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
308 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
315 stats = this_cpu_ptr(dp->stats_percpu);
317 u64_stats_update_begin(&stats->sync);
319 u64_stats_update_end(&stats->sync);
324 static int queue_gso_packets(struct net *net, int dp_ifindex,
326 const struct dp_upcall_info *upcall_info)
328 unsigned short gso_type = skb_shinfo(skb)->gso_type;
329 struct dp_upcall_info later_info;
330 struct sw_flow_key later_key;
331 struct sk_buff *segs, *nskb;
334 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
336 return PTR_ERR(segs);
338 /* Queue all of the segments. */
341 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
345 if (skb == segs && gso_type & SKB_GSO_UDP) {
346 /* The initial flow key extracted by ovs_flow_extract()
347 * in this case is for a first fragment, so we need to
348 * properly mark later fragments.
350 later_key = *upcall_info->key;
351 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
353 later_info = *upcall_info;
354 later_info.key = &later_key;
355 upcall_info = &later_info;
357 } while ((skb = skb->next));
359 /* Free all of the segments. */
367 } while ((skb = nskb));
371 static size_t key_attr_size(void)
373 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
374 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
375 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
376 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
377 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
378 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
379 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
380 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
381 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
382 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
383 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
384 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
385 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
386 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
387 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
388 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
389 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
390 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
391 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
394 static size_t upcall_msg_size(const struct sk_buff *skb,
395 const struct nlattr *userdata)
397 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
398 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
399 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
401 /* OVS_PACKET_ATTR_USERDATA */
403 size += NLA_ALIGN(userdata->nla_len);
408 static int queue_userspace_packet(struct net *net, int dp_ifindex,
410 const struct dp_upcall_info *upcall_info)
412 struct ovs_header *upcall;
413 struct sk_buff *nskb = NULL;
414 struct sk_buff *user_skb; /* to be queued to userspace */
418 if (vlan_tx_tag_present(skb)) {
419 nskb = skb_clone(skb, GFP_ATOMIC);
423 err = vlan_deaccel_tag(nskb);
430 if (nla_attr_size(skb->len) > USHRT_MAX) {
435 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
441 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
442 0, upcall_info->cmd);
443 upcall->dp_ifindex = dp_ifindex;
445 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
446 ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb);
447 nla_nest_end(user_skb, nla);
449 if (upcall_info->userdata)
450 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
451 nla_len(upcall_info->userdata),
452 nla_data(upcall_info->userdata));
454 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
456 skb_copy_and_csum_dev(skb, nla_data(nla));
458 genlmsg_end(user_skb, upcall);
459 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
466 /* Called with ovs_mutex. */
467 static int flush_flows(struct datapath *dp)
469 struct flow_table *old_table;
470 struct flow_table *new_table;
472 old_table = ovsl_dereference(dp->table);
473 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
477 rcu_assign_pointer(dp->table, new_table);
479 ovs_flow_tbl_destroy(old_table, true);
483 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
486 struct sw_flow_actions *acts;
488 int req_size = NLA_ALIGN(attr_len);
489 int next_offset = offsetof(struct sw_flow_actions, actions) +
492 if (req_size <= (ksize(*sfa) - next_offset))
495 new_acts_size = ksize(*sfa) * 2;
497 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
498 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
499 return ERR_PTR(-EMSGSIZE);
500 new_acts_size = MAX_ACTIONS_BUFSIZE;
503 acts = ovs_flow_actions_alloc(new_acts_size);
507 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
508 acts->actions_len = (*sfa)->actions_len;
513 (*sfa)->actions_len += req_size;
514 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
517 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
521 a = reserve_sfa_size(sfa, nla_attr_size(len));
525 a->nla_type = attrtype;
526 a->nla_len = nla_attr_size(len);
529 memcpy(nla_data(a), data, len);
530 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
535 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
537 int used = (*sfa)->actions_len;
540 err = add_action(sfa, attrtype, NULL, 0);
547 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
549 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
551 a->nla_len = sfa->actions_len - st_offset;
554 static int validate_and_copy_actions(const struct nlattr *attr,
555 const struct sw_flow_key *key, int depth,
556 struct sw_flow_actions **sfa);
558 static int validate_and_copy_sample(const struct nlattr *attr,
559 const struct sw_flow_key *key, int depth,
560 struct sw_flow_actions **sfa)
562 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
563 const struct nlattr *probability, *actions;
564 const struct nlattr *a;
565 int rem, start, err, st_acts;
567 memset(attrs, 0, sizeof(attrs));
568 nla_for_each_nested(a, attr, rem) {
569 int type = nla_type(a);
570 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
577 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
578 if (!probability || nla_len(probability) != sizeof(u32))
581 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
582 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
585 /* validation done, copy sample action. */
586 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
589 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
592 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
596 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
600 add_nested_action_end(*sfa, st_acts);
601 add_nested_action_end(*sfa, start);
606 static int validate_tp_port(const struct sw_flow_key *flow_key)
608 if (flow_key->eth.type == htons(ETH_P_IP)) {
609 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
611 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
612 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
619 static int validate_and_copy_set_tun(const struct nlattr *attr,
620 struct sw_flow_actions **sfa)
622 struct sw_flow_match match;
623 struct sw_flow_key key;
626 ovs_match_init(&match, &key, NULL);
627 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
631 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
635 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
636 sizeof(match.key->tun_key));
637 add_nested_action_end(*sfa, start);
642 static int validate_set(const struct nlattr *a,
643 const struct sw_flow_key *flow_key,
644 struct sw_flow_actions **sfa,
647 const struct nlattr *ovs_key = nla_data(a);
648 int key_type = nla_type(ovs_key);
650 /* There can be only one key in a action */
651 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
654 if (key_type > OVS_KEY_ATTR_MAX ||
655 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
656 ovs_key_lens[key_type] != -1))
660 const struct ovs_key_ipv4 *ipv4_key;
661 const struct ovs_key_ipv6 *ipv6_key;
664 case OVS_KEY_ATTR_PRIORITY:
665 case OVS_KEY_ATTR_ETHERNET:
668 case OVS_KEY_ATTR_SKB_MARK:
669 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
670 if (nla_get_u32(ovs_key) != 0)
675 case OVS_KEY_ATTR_TUNNEL:
677 err = validate_and_copy_set_tun(a, sfa);
682 case OVS_KEY_ATTR_IPV4:
683 if (flow_key->eth.type != htons(ETH_P_IP))
686 if (!flow_key->ip.proto)
689 ipv4_key = nla_data(ovs_key);
690 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
693 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
698 case OVS_KEY_ATTR_IPV6:
699 if (flow_key->eth.type != htons(ETH_P_IPV6))
702 if (!flow_key->ip.proto)
705 ipv6_key = nla_data(ovs_key);
706 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
709 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
712 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
717 case OVS_KEY_ATTR_TCP:
718 if (flow_key->ip.proto != IPPROTO_TCP)
721 return validate_tp_port(flow_key);
723 case OVS_KEY_ATTR_UDP:
724 if (flow_key->ip.proto != IPPROTO_UDP)
727 return validate_tp_port(flow_key);
736 static int validate_userspace(const struct nlattr *attr)
738 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
739 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
740 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
742 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
745 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
746 attr, userspace_policy);
750 if (!a[OVS_USERSPACE_ATTR_PID] ||
751 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
757 static int copy_action(const struct nlattr *from,
758 struct sw_flow_actions **sfa)
760 int totlen = NLA_ALIGN(from->nla_len);
763 to = reserve_sfa_size(sfa, from->nla_len);
767 memcpy(to, from, totlen);
771 static int validate_and_copy_actions(const struct nlattr *attr,
772 const struct sw_flow_key *key,
774 struct sw_flow_actions **sfa)
776 const struct nlattr *a;
779 if (depth >= SAMPLE_ACTION_DEPTH)
782 nla_for_each_nested(a, attr, rem) {
783 /* Expected argument lengths, (u32)-1 for variable length. */
784 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
785 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
786 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
787 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
788 [OVS_ACTION_ATTR_POP_VLAN] = 0,
789 [OVS_ACTION_ATTR_SET] = (u32)-1,
790 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
792 const struct ovs_action_push_vlan *vlan;
793 int type = nla_type(a);
796 if (type > OVS_ACTION_ATTR_MAX ||
797 (action_lens[type] != nla_len(a) &&
798 action_lens[type] != (u32)-1))
803 case OVS_ACTION_ATTR_UNSPEC:
806 case OVS_ACTION_ATTR_USERSPACE:
807 err = validate_userspace(a);
812 case OVS_ACTION_ATTR_OUTPUT:
813 if (nla_get_u32(a) >= DP_MAX_PORTS)
818 case OVS_ACTION_ATTR_POP_VLAN:
821 case OVS_ACTION_ATTR_PUSH_VLAN:
823 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
825 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
829 case OVS_ACTION_ATTR_SET:
830 err = validate_set(a, key, sfa, &skip_copy);
835 case OVS_ACTION_ATTR_SAMPLE:
836 err = validate_and_copy_sample(a, key, depth, sfa);
846 err = copy_action(a, sfa);
858 static void clear_stats(struct sw_flow *flow)
862 flow->packet_count = 0;
863 flow->byte_count = 0;
866 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
868 struct ovs_header *ovs_header = info->userhdr;
869 struct nlattr **a = info->attrs;
870 struct sw_flow_actions *acts;
871 struct sk_buff *packet;
872 struct sw_flow *flow;
879 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
880 !a[OVS_PACKET_ATTR_ACTIONS])
883 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
884 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
888 skb_reserve(packet, NET_IP_ALIGN);
890 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
892 skb_reset_mac_header(packet);
893 eth = eth_hdr(packet);
895 /* Normally, setting the skb 'protocol' field would be handled by a
896 * call to eth_type_trans(), but it assumes there's a sending
897 * device, which we may not have. */
898 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
899 packet->protocol = eth->h_proto;
901 packet->protocol = htons(ETH_P_802_2);
903 /* Build an sw_flow for sending this packet. */
904 flow = ovs_flow_alloc();
909 err = ovs_flow_extract(packet, -1, &flow->key);
913 err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
916 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
921 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
922 rcu_assign_pointer(flow->sf_acts, acts);
926 OVS_CB(packet)->flow = flow;
927 OVS_CB(packet)->pkt_key = &flow->key;
928 packet->priority = flow->key.phy.priority;
929 skb_set_mark(packet, flow->key.phy.skb_mark);
932 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
938 err = ovs_execute_actions(dp, packet);
942 ovs_flow_free(flow, false);
948 ovs_flow_free(flow, false);
955 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
956 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
957 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
959 [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
961 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
962 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
965 static struct genl_ops dp_packet_genl_ops[] = {
966 { .cmd = OVS_PACKET_CMD_EXECUTE,
967 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
968 .policy = packet_policy,
969 .doit = ovs_packet_cmd_execute
973 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
976 struct flow_table *table = ovsl_dereference(dp->table);
978 stats->n_flows = ovs_flow_tbl_count(table);
980 stats->n_hit = stats->n_missed = stats->n_lost = 0;
981 for_each_possible_cpu(i) {
982 const struct dp_stats_percpu *percpu_stats;
983 struct dp_stats_percpu local_stats;
986 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
989 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
990 local_stats = *percpu_stats;
991 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
993 stats->n_hit += local_stats.n_hit;
994 stats->n_missed += local_stats.n_missed;
995 stats->n_lost += local_stats.n_lost;
999 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1000 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1001 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1002 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1005 static struct genl_family dp_flow_genl_family = {
1006 .id = GENL_ID_GENERATE,
1007 .hdrsize = sizeof(struct ovs_header),
1008 .name = OVS_FLOW_FAMILY,
1009 .version = OVS_FLOW_VERSION,
1010 .maxattr = OVS_FLOW_ATTR_MAX,
1014 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1015 .name = OVS_FLOW_MCGROUP
1018 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1019 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1021 const struct nlattr *a;
1022 struct nlattr *start;
1025 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1029 nla_for_each_nested(a, attr, rem) {
1030 int type = nla_type(a);
1031 struct nlattr *st_sample;
1034 case OVS_SAMPLE_ATTR_PROBABILITY:
1035 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1038 case OVS_SAMPLE_ATTR_ACTIONS:
1039 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1042 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1045 nla_nest_end(skb, st_sample);
1050 nla_nest_end(skb, start);
1054 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1056 const struct nlattr *ovs_key = nla_data(a);
1057 int key_type = nla_type(ovs_key);
1058 struct nlattr *start;
1062 case OVS_KEY_ATTR_IPV4_TUNNEL:
1063 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1067 err = ipv4_tun_to_nlattr(skb,
1068 nla_data(ovs_key), nla_data(ovs_key));
1071 nla_nest_end(skb, start);
1074 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1082 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1084 const struct nlattr *a;
1087 nla_for_each_attr(a, attr, len, rem) {
1088 int type = nla_type(a);
1091 case OVS_ACTION_ATTR_SET:
1092 err = set_action_to_attr(a, skb);
1097 case OVS_ACTION_ATTR_SAMPLE:
1098 err = sample_action_to_attr(a, skb);
1103 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1112 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1114 return NLMSG_ALIGN(sizeof(struct ovs_header))
1115 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
1116 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
1117 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1118 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1119 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1120 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1123 /* Called with ovs_mutex. */
1124 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1125 struct sk_buff *skb, u32 portid,
1126 u32 seq, u32 flags, u8 cmd)
1128 const int skb_orig_len = skb->len;
1129 struct sw_flow_mask *mask;
1130 struct nlattr *start;
1131 struct ovs_flow_stats stats;
1132 struct ovs_header *ovs_header;
1138 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1142 ovs_header->dp_ifindex = get_dpifindex(dp);
1144 /* Fill flow key. */
1145 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1147 goto nla_put_failure;
1149 err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1150 &flow->unmasked_key, skb);
1153 nla_nest_end(skb, nla);
1155 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1157 goto nla_put_failure;
1159 mask = rcu_dereference_check(flow->mask, lockdep_ovsl_is_held());
1160 err = ovs_flow_to_nlattrs(&flow->key, &mask->key, skb);
1164 nla_nest_end(skb, nla);
1166 spin_lock_bh(&flow->lock);
1168 stats.n_packets = flow->packet_count;
1169 stats.n_bytes = flow->byte_count;
1170 tcp_flags = flow->tcp_flags;
1171 spin_unlock_bh(&flow->lock);
1174 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1175 goto nla_put_failure;
1177 if (stats.n_packets &&
1178 nla_put(skb, OVS_FLOW_ATTR_STATS,
1179 sizeof(struct ovs_flow_stats), &stats))
1180 goto nla_put_failure;
1183 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1184 goto nla_put_failure;
1186 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1187 * this is the first flow to be dumped into 'skb'. This is unusual for
1188 * Netlink but individual action lists can be longer than
1189 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1190 * The userspace caller can always fetch the actions separately if it
1191 * really wants them. (Most userspace callers in fact don't care.)
1193 * This can only fail for dump operations because the skb is always
1194 * properly sized for single flows.
1196 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1198 const struct sw_flow_actions *sf_acts;
1200 sf_acts = rcu_dereference_check(flow->sf_acts,
1201 lockdep_ovsl_is_held());
1203 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1205 nla_nest_end(skb, start);
1210 nla_nest_cancel(skb, start);
1212 } else if (skb_orig_len)
1213 goto nla_put_failure;
1215 return genlmsg_end(skb, ovs_header);
1220 genlmsg_cancel(skb, ovs_header);
1224 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1226 const struct sw_flow_actions *sf_acts;
1228 sf_acts = ovsl_dereference(flow->sf_acts);
1230 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
1233 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1234 struct datapath *dp,
1235 u32 portid, u32 seq, u8 cmd)
1237 struct sk_buff *skb;
1240 skb = ovs_flow_cmd_alloc_info(flow);
1242 return ERR_PTR(-ENOMEM);
1244 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1249 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1251 struct nlattr **a = info->attrs;
1252 struct ovs_header *ovs_header = info->userhdr;
1253 struct sw_flow_key key;
1254 struct sw_flow *flow = NULL;
1255 struct sw_flow_mask mask;
1256 struct sk_buff *reply;
1257 struct datapath *dp;
1258 struct flow_table *table;
1259 struct sw_flow_actions *acts = NULL;
1260 struct sw_flow_match match;
1265 if (!a[OVS_FLOW_ATTR_KEY])
1268 ovs_match_init(&match, &key, &mask);
1269 error = ovs_match_from_nlattrs(&match,
1270 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1274 /* Validate actions. */
1275 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1276 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1277 error = PTR_ERR(acts);
1281 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0, &acts);
1284 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1290 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1293 goto err_unlock_ovs;
1295 table = ovsl_dereference(dp->table);
1297 /* Check if this is a duplicate flow */
1298 flow = ovs_flow_lookup(table, &key);
1300 struct sw_flow_mask *mask_p;
1301 /* Bail out if we're not allowed to create a new flow. */
1303 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1304 goto err_unlock_ovs;
1306 /* Expand table, if necessary, to make room. */
1307 if (ovs_flow_tbl_need_to_expand(table)) {
1308 struct flow_table *new_table;
1310 new_table = ovs_flow_tbl_expand(table);
1311 if (!IS_ERR(new_table)) {
1312 rcu_assign_pointer(dp->table, new_table);
1313 ovs_flow_tbl_destroy(table, true);
1314 table = ovsl_dereference(dp->table);
1318 /* Allocate flow. */
1319 flow = ovs_flow_alloc();
1321 error = PTR_ERR(flow);
1322 goto err_unlock_ovs;
1326 /* Make sure mask is unique in the system */
1327 mask_p = ovs_sw_flow_mask_find(table, &mask);
1329 /* Allocate a new mask if none exsits. */
1330 mask_p = ovs_sw_flow_mask_alloc();
1333 mask_p->key = mask.key;
1334 mask_p->range = mask.range;
1335 ovs_sw_flow_mask_insert(table, mask_p);
1338 ovs_sw_flow_mask_add_ref(mask_p);
1339 rcu_assign_pointer(flow->mask, mask_p);
1340 rcu_assign_pointer(flow->sf_acts, acts);
1342 /* Put flow in bucket. */
1343 ovs_flow_insert(table, flow, &key, match.range.end);
1345 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1346 info->snd_seq, OVS_FLOW_CMD_NEW);
1348 /* We found a matching flow. */
1349 struct sw_flow_actions *old_acts;
1351 /* Bail out if we're not allowed to modify an existing flow.
1352 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1353 * because Generic Netlink treats the latter as a dump
1354 * request. We also accept NLM_F_EXCL in case that bug ever
1358 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1359 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1360 goto err_unlock_ovs;
1362 /* The unmasked key has to be the same for flow updates. */
1364 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1365 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1366 goto err_unlock_ovs;
1369 /* Update actions. */
1370 old_acts = ovsl_dereference(flow->sf_acts);
1371 rcu_assign_pointer(flow->sf_acts, acts);
1372 ovs_flow_deferred_free_acts(old_acts);
1374 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1375 info->snd_seq, OVS_FLOW_CMD_NEW);
1378 if (a[OVS_FLOW_ATTR_CLEAR]) {
1379 spin_lock_bh(&flow->lock);
1381 spin_unlock_bh(&flow->lock);
1387 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1389 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1390 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1394 ovs_flow_free(flow, false);
1403 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1405 struct nlattr **a = info->attrs;
1406 struct ovs_header *ovs_header = info->userhdr;
1407 struct sw_flow_key key;
1408 struct sk_buff *reply;
1409 struct sw_flow *flow;
1410 struct datapath *dp;
1411 struct flow_table *table;
1412 struct sw_flow_match match;
1415 if (!a[OVS_FLOW_ATTR_KEY]) {
1416 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1420 ovs_match_init(&match, &key, NULL);
1421 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1426 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1432 table = ovsl_dereference(dp->table);
1433 flow = ovs_flow_lookup_unmasked_key(table, &match);
1439 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1440 info->snd_seq, OVS_FLOW_CMD_NEW);
1441 if (IS_ERR(reply)) {
1442 err = PTR_ERR(reply);
1447 return genlmsg_reply(reply, info);
1453 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1455 struct nlattr **a = info->attrs;
1456 struct ovs_header *ovs_header = info->userhdr;
1457 struct sw_flow_key key;
1458 struct sk_buff *reply;
1459 struct sw_flow *flow;
1460 struct datapath *dp;
1461 struct flow_table *table;
1462 struct sw_flow_match match;
1466 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1472 if (!a[OVS_FLOW_ATTR_KEY]) {
1473 err = flush_flows(dp);
1477 ovs_match_init(&match, &key, NULL);
1478 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1482 table = ovsl_dereference(dp->table);
1483 flow = ovs_flow_lookup_unmasked_key(table, &match);
1489 reply = ovs_flow_cmd_alloc_info(flow);
1495 ovs_flow_remove(table, flow);
1497 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1498 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1501 ovs_flow_free(flow, true);
1504 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1511 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1513 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1514 struct datapath *dp;
1515 struct flow_table *table;
1518 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1524 table = rcu_dereference(dp->table);
1526 struct sw_flow *flow;
1529 bucket = cb->args[0];
1531 flow = ovs_flow_dump_next(table, &bucket, &obj);
1535 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1536 NETLINK_CB(cb->skb).portid,
1537 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1538 OVS_FLOW_CMD_NEW) < 0)
1541 cb->args[0] = bucket;
1548 static struct genl_ops dp_flow_genl_ops[] = {
1549 { .cmd = OVS_FLOW_CMD_NEW,
1550 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1551 .policy = flow_policy,
1552 .doit = ovs_flow_cmd_new_or_set
1554 { .cmd = OVS_FLOW_CMD_DEL,
1555 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1556 .policy = flow_policy,
1557 .doit = ovs_flow_cmd_del
1559 { .cmd = OVS_FLOW_CMD_GET,
1560 .flags = 0, /* OK for unprivileged users. */
1561 .policy = flow_policy,
1562 .doit = ovs_flow_cmd_get,
1563 .dumpit = ovs_flow_cmd_dump
1565 { .cmd = OVS_FLOW_CMD_SET,
1566 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1567 .policy = flow_policy,
1568 .doit = ovs_flow_cmd_new_or_set,
1572 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1573 #ifdef HAVE_NLA_NUL_STRING
1574 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1576 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1579 static struct genl_family dp_datapath_genl_family = {
1580 .id = GENL_ID_GENERATE,
1581 .hdrsize = sizeof(struct ovs_header),
1582 .name = OVS_DATAPATH_FAMILY,
1583 .version = OVS_DATAPATH_VERSION,
1584 .maxattr = OVS_DP_ATTR_MAX,
1588 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1589 .name = OVS_DATAPATH_MCGROUP
1592 static size_t ovs_dp_cmd_msg_size(void)
1594 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1596 msgsize += nla_total_size(IFNAMSIZ);
1597 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1602 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1603 u32 portid, u32 seq, u32 flags, u8 cmd)
1605 struct ovs_header *ovs_header;
1606 struct ovs_dp_stats dp_stats;
1609 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1614 ovs_header->dp_ifindex = get_dpifindex(dp);
1617 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1620 goto nla_put_failure;
1622 get_dp_stats(dp, &dp_stats);
1623 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1624 goto nla_put_failure;
1626 return genlmsg_end(skb, ovs_header);
1629 genlmsg_cancel(skb, ovs_header);
1634 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1637 struct sk_buff *skb;
1640 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1642 return ERR_PTR(-ENOMEM);
1644 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1647 return ERR_PTR(retval);
1652 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1654 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1657 /* Called with ovs_mutex. */
1658 static struct datapath *lookup_datapath(struct net *net,
1659 struct ovs_header *ovs_header,
1660 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1662 struct datapath *dp;
1664 if (!a[OVS_DP_ATTR_NAME])
1665 dp = get_dp(net, ovs_header->dp_ifindex);
1667 struct vport *vport;
1670 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1671 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1674 return dp ? dp : ERR_PTR(-ENODEV);
1677 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1679 struct nlattr **a = info->attrs;
1680 struct vport_parms parms;
1681 struct sk_buff *reply;
1682 struct datapath *dp;
1683 struct vport *vport;
1684 struct ovs_net *ovs_net;
1688 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1691 err = ovs_dp_cmd_validate(a);
1698 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1700 goto err_unlock_ovs;
1702 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1704 /* Allocate table. */
1706 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1710 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1711 if (!dp->stats_percpu) {
1713 goto err_destroy_table;
1716 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1720 goto err_destroy_percpu;
1723 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1724 INIT_HLIST_HEAD(&dp->ports[i]);
1726 /* Set up our datapath device. */
1727 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1728 parms.type = OVS_VPORT_TYPE_INTERNAL;
1729 parms.options = NULL;
1731 parms.port_no = OVSP_LOCAL;
1732 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1734 vport = new_vport(&parms);
1735 if (IS_ERR(vport)) {
1736 err = PTR_ERR(vport);
1740 goto err_destroy_ports_array;
1743 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1744 info->snd_seq, OVS_DP_CMD_NEW);
1745 err = PTR_ERR(reply);
1747 goto err_destroy_local_port;
1749 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1750 list_add_tail(&dp->list_node, &ovs_net->dps);
1754 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1757 err_destroy_local_port:
1758 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1759 err_destroy_ports_array:
1762 free_percpu(dp->stats_percpu);
1764 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
1766 release_net(ovs_dp_get_net(dp));
1774 /* Called with ovs_mutex. */
1775 static void __dp_destroy(struct datapath *dp)
1779 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1780 struct vport *vport;
1781 struct hlist_node *n;
1783 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1784 if (vport->port_no != OVSP_LOCAL)
1785 ovs_dp_detach_port(vport);
1788 list_del(&dp->list_node);
1790 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1791 * all port in datapath are destroyed first before freeing datapath.
1793 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1795 call_rcu(&dp->rcu, destroy_dp_rcu);
1798 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1800 struct sk_buff *reply;
1801 struct datapath *dp;
1804 err = ovs_dp_cmd_validate(info->attrs);
1809 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1814 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1815 info->snd_seq, OVS_DP_CMD_DEL);
1816 err = PTR_ERR(reply);
1823 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1831 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1833 struct sk_buff *reply;
1834 struct datapath *dp;
1837 err = ovs_dp_cmd_validate(info->attrs);
1842 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1847 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1848 info->snd_seq, OVS_DP_CMD_NEW);
1849 if (IS_ERR(reply)) {
1850 err = PTR_ERR(reply);
1851 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1852 ovs_dp_datapath_multicast_group.id, err);
1858 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1866 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1868 struct sk_buff *reply;
1869 struct datapath *dp;
1872 err = ovs_dp_cmd_validate(info->attrs);
1877 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1883 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1884 info->snd_seq, OVS_DP_CMD_NEW);
1885 if (IS_ERR(reply)) {
1886 err = PTR_ERR(reply);
1891 return genlmsg_reply(reply, info);
1898 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1900 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1901 struct datapath *dp;
1902 int skip = cb->args[0];
1906 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1908 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1909 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1910 OVS_DP_CMD_NEW) < 0)
1921 static struct genl_ops dp_datapath_genl_ops[] = {
1922 { .cmd = OVS_DP_CMD_NEW,
1923 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1924 .policy = datapath_policy,
1925 .doit = ovs_dp_cmd_new
1927 { .cmd = OVS_DP_CMD_DEL,
1928 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1929 .policy = datapath_policy,
1930 .doit = ovs_dp_cmd_del
1932 { .cmd = OVS_DP_CMD_GET,
1933 .flags = 0, /* OK for unprivileged users. */
1934 .policy = datapath_policy,
1935 .doit = ovs_dp_cmd_get,
1936 .dumpit = ovs_dp_cmd_dump
1938 { .cmd = OVS_DP_CMD_SET,
1939 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1940 .policy = datapath_policy,
1941 .doit = ovs_dp_cmd_set,
1945 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1946 #ifdef HAVE_NLA_NUL_STRING
1947 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1948 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1950 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1952 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1953 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1954 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1955 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1958 static struct genl_family dp_vport_genl_family = {
1959 .id = GENL_ID_GENERATE,
1960 .hdrsize = sizeof(struct ovs_header),
1961 .name = OVS_VPORT_FAMILY,
1962 .version = OVS_VPORT_VERSION,
1963 .maxattr = OVS_VPORT_ATTR_MAX,
1967 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1968 .name = OVS_VPORT_MCGROUP
1971 /* Called with ovs_mutex or RCU read lock. */
1972 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1973 u32 portid, u32 seq, u32 flags, u8 cmd)
1975 struct ovs_header *ovs_header;
1976 struct ovs_vport_stats vport_stats;
1979 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1984 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1986 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1987 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1988 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1989 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1990 goto nla_put_failure;
1992 ovs_vport_get_stats(vport, &vport_stats);
1993 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1995 goto nla_put_failure;
1997 err = ovs_vport_get_options(vport, skb);
1998 if (err == -EMSGSIZE)
2001 return genlmsg_end(skb, ovs_header);
2006 genlmsg_cancel(skb, ovs_header);
2010 /* Called with ovs_mutex or RCU read lock. */
2011 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
2014 struct sk_buff *skb;
2017 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2019 return ERR_PTR(-ENOMEM);
2021 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
2027 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2029 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
2032 /* Called with ovs_mutex or RCU read lock. */
2033 static struct vport *lookup_vport(struct net *net,
2034 struct ovs_header *ovs_header,
2035 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2037 struct datapath *dp;
2038 struct vport *vport;
2040 if (a[OVS_VPORT_ATTR_NAME]) {
2041 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2043 return ERR_PTR(-ENODEV);
2044 if (ovs_header->dp_ifindex &&
2045 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2046 return ERR_PTR(-ENODEV);
2048 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2049 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2051 if (port_no >= DP_MAX_PORTS)
2052 return ERR_PTR(-EFBIG);
2054 dp = get_dp(net, ovs_header->dp_ifindex);
2056 return ERR_PTR(-ENODEV);
2058 vport = ovs_vport_ovsl_rcu(dp, port_no);
2060 return ERR_PTR(-ENODEV);
2063 return ERR_PTR(-EINVAL);
2066 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2068 struct nlattr **a = info->attrs;
2069 struct ovs_header *ovs_header = info->userhdr;
2070 struct vport_parms parms;
2071 struct sk_buff *reply;
2072 struct vport *vport;
2073 struct datapath *dp;
2078 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2079 !a[OVS_VPORT_ATTR_UPCALL_PID])
2082 err = ovs_vport_cmd_validate(a);
2087 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2092 if (a[OVS_VPORT_ATTR_PORT_NO]) {
2093 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2096 if (port_no >= DP_MAX_PORTS)
2099 vport = ovs_vport_ovsl(dp, port_no);
2104 for (port_no = 1; ; port_no++) {
2105 if (port_no >= DP_MAX_PORTS) {
2109 vport = ovs_vport_ovsl(dp, port_no);
2115 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2116 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2117 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2119 parms.port_no = port_no;
2120 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2122 vport = new_vport(&parms);
2123 err = PTR_ERR(vport);
2128 if (a[OVS_VPORT_ATTR_STATS])
2129 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2131 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2133 if (IS_ERR(reply)) {
2134 err = PTR_ERR(reply);
2135 ovs_dp_detach_port(vport);
2139 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2147 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2149 struct nlattr **a = info->attrs;
2150 struct sk_buff *reply;
2151 struct vport *vport;
2154 err = ovs_vport_cmd_validate(a);
2159 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2160 err = PTR_ERR(vport);
2164 if (a[OVS_VPORT_ATTR_TYPE] &&
2165 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2170 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2176 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2177 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2182 if (a[OVS_VPORT_ATTR_STATS])
2183 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2185 if (a[OVS_VPORT_ATTR_UPCALL_PID])
2186 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2188 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2189 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2193 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2204 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2206 struct nlattr **a = info->attrs;
2207 struct sk_buff *reply;
2208 struct vport *vport;
2211 err = ovs_vport_cmd_validate(a);
2216 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2217 err = PTR_ERR(vport);
2221 if (vport->port_no == OVSP_LOCAL) {
2226 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2227 info->snd_seq, OVS_VPORT_CMD_DEL);
2228 err = PTR_ERR(reply);
2233 ovs_dp_detach_port(vport);
2235 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
2243 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2245 struct nlattr **a = info->attrs;
2246 struct ovs_header *ovs_header = info->userhdr;
2247 struct sk_buff *reply;
2248 struct vport *vport;
2251 err = ovs_vport_cmd_validate(a);
2256 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2257 err = PTR_ERR(vport);
2261 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2262 info->snd_seq, OVS_VPORT_CMD_NEW);
2263 err = PTR_ERR(reply);
2269 return genlmsg_reply(reply, info);
2277 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2279 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2280 struct datapath *dp;
2281 int bucket = cb->args[0], skip = cb->args[1];
2284 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2289 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2290 struct vport *vport;
2293 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2295 ovs_vport_cmd_fill_info(vport, skb,
2296 NETLINK_CB(cb->skb).portid,
2299 OVS_VPORT_CMD_NEW) < 0)
2315 static struct genl_ops dp_vport_genl_ops[] = {
2316 { .cmd = OVS_VPORT_CMD_NEW,
2317 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2318 .policy = vport_policy,
2319 .doit = ovs_vport_cmd_new
2321 { .cmd = OVS_VPORT_CMD_DEL,
2322 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2323 .policy = vport_policy,
2324 .doit = ovs_vport_cmd_del
2326 { .cmd = OVS_VPORT_CMD_GET,
2327 .flags = 0, /* OK for unprivileged users. */
2328 .policy = vport_policy,
2329 .doit = ovs_vport_cmd_get,
2330 .dumpit = ovs_vport_cmd_dump
2332 { .cmd = OVS_VPORT_CMD_SET,
2333 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2334 .policy = vport_policy,
2335 .doit = ovs_vport_cmd_set,
2339 struct genl_family_and_ops {
2340 struct genl_family *family;
2341 struct genl_ops *ops;
2343 struct genl_multicast_group *group;
2346 static const struct genl_family_and_ops dp_genl_families[] = {
2347 { &dp_datapath_genl_family,
2348 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2349 &ovs_dp_datapath_multicast_group },
2350 { &dp_vport_genl_family,
2351 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2352 &ovs_dp_vport_multicast_group },
2353 { &dp_flow_genl_family,
2354 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2355 &ovs_dp_flow_multicast_group },
2356 { &dp_packet_genl_family,
2357 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2361 static void dp_unregister_genl(int n_families)
2365 for (i = 0; i < n_families; i++)
2366 genl_unregister_family(dp_genl_families[i].family);
2369 static int dp_register_genl(void)
2376 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2377 const struct genl_family_and_ops *f = &dp_genl_families[i];
2379 err = genl_register_family_with_ops(f->family, f->ops,
2386 err = genl_register_mc_group(f->family, f->group);
2395 dp_unregister_genl(n_registered);
2399 static void rehash_flow_table(struct work_struct *work)
2401 struct datapath *dp;
2407 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2409 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2410 struct flow_table *old_table = ovsl_dereference(dp->table);
2411 struct flow_table *new_table;
2413 new_table = ovs_flow_tbl_rehash(old_table);
2414 if (!IS_ERR(new_table)) {
2415 rcu_assign_pointer(dp->table, new_table);
2416 ovs_flow_tbl_destroy(old_table, true);
2422 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2425 static int __net_init ovs_init_net(struct net *net)
2427 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2429 INIT_LIST_HEAD(&ovs_net->dps);
2430 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2434 static void __net_exit ovs_exit_net(struct net *net)
2436 struct datapath *dp, *dp_next;
2437 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2440 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2444 cancel_work_sync(&ovs_net->dp_notify_work);
2447 static struct pernet_operations ovs_net_ops = {
2448 .init = ovs_init_net,
2449 .exit = ovs_exit_net,
2451 .size = sizeof(struct ovs_net),
2454 static int __init dp_init(void)
2458 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2460 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2463 err = ovs_workqueues_init();
2467 err = ovs_flow_init();
2471 err = ovs_vport_init();
2473 goto error_flow_exit;
2475 err = register_pernet_device(&ovs_net_ops);
2477 goto error_vport_exit;
2479 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2481 goto error_netns_exit;
2483 err = dp_register_genl();
2485 goto error_unreg_notifier;
2487 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2491 error_unreg_notifier:
2492 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2494 unregister_pernet_device(&ovs_net_ops);
2500 ovs_workqueues_exit();
2505 static void dp_cleanup(void)
2507 cancel_delayed_work_sync(&rehash_flow_wq);
2508 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2509 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2510 unregister_pernet_device(&ovs_net_ops);
2514 ovs_workqueues_exit();
2517 module_init(dp_init);
2518 module_exit(dp_cleanup);
2520 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2521 MODULE_LICENSE("GPL");
2522 MODULE_VERSION(VERSION);