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/ethtool.h>
40 #include <linux/wait.h>
41 #include <asm/div64.h>
42 #include <linux/highmem.h>
43 #include <linux/netfilter_bridge.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/inetdevice.h>
46 #include <linux/list.h>
47 #include <linux/lockdep.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/workqueue.h>
52 #include <net/genetlink.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
58 #include "vport-internal_dev.h"
59 #include "vport-netdev.h"
62 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
63 static void rehash_flow_table(struct work_struct *work);
64 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
66 int ovs_net_id __read_mostly;
68 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
69 struct genl_multicast_group *grp)
71 genl_notify(skb, genl_info_net(info), info->snd_portid,
72 grp->id, info->nlhdr, GFP_KERNEL);
78 * All writes e.g. Writes to device state (add/remove datapath, port, set
79 * operations on vports, etc.), Writes to other state (flow table
80 * modifications, set miscellaneous datapath parameters, etc.) are protected
83 * Reads are protected by RCU.
85 * There are a few special cases (mostly stats) that have their own
86 * synchronization but they nest under all of above and don't interact with
89 * The RTNL lock nests inside ovs_mutex.
92 static DEFINE_MUTEX(ovs_mutex);
96 mutex_lock(&ovs_mutex);
101 mutex_unlock(&ovs_mutex);
104 #ifdef CONFIG_LOCKDEP
105 int lockdep_ovsl_is_held(void)
108 return lockdep_is_held(&ovs_mutex);
114 static struct vport *new_vport(const struct vport_parms *);
115 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
116 const struct dp_upcall_info *);
117 static int queue_userspace_packet(struct net *, int dp_ifindex,
119 const struct dp_upcall_info *);
121 /* Must be called with rcu_read_lock or ovs_mutex. */
122 static struct datapath *get_dp(struct net *net, int dp_ifindex)
124 struct datapath *dp = NULL;
125 struct net_device *dev;
128 dev = dev_get_by_index_rcu(net, dp_ifindex);
130 struct vport *vport = ovs_internal_dev_get_vport(dev);
139 /* Must be called with rcu_read_lock or ovs_mutex. */
140 const char *ovs_dp_name(const struct datapath *dp)
142 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
143 return vport->ops->get_name(vport);
146 static int get_dpifindex(struct datapath *dp)
153 local = ovs_vport_rcu(dp, OVSP_LOCAL);
155 ifindex = netdev_vport_priv(local)->dev->ifindex;
164 static void destroy_dp_rcu(struct rcu_head *rcu)
166 struct datapath *dp = container_of(rcu, struct datapath, rcu);
168 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
169 free_percpu(dp->stats_percpu);
170 release_net(ovs_dp_get_net(dp));
175 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
178 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
181 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
184 struct hlist_head *head;
186 head = vport_hash_bucket(dp, port_no);
187 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
188 if (vport->port_no == port_no)
194 /* Called with ovs_mutex. */
195 static struct vport *new_vport(const struct vport_parms *parms)
199 vport = ovs_vport_add(parms);
200 if (!IS_ERR(vport)) {
201 struct datapath *dp = parms->dp;
202 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
204 hlist_add_head_rcu(&vport->dp_hash_node, head);
209 void ovs_dp_detach_port(struct vport *p)
213 /* First drop references to device. */
214 hlist_del_rcu(&p->dp_hash_node);
216 /* Then destroy it. */
220 /* Must be called with rcu_read_lock. */
221 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
223 struct datapath *dp = p->dp;
224 struct sw_flow *flow;
225 struct dp_stats_percpu *stats;
226 struct sw_flow_key key;
231 stats = this_cpu_ptr(dp->stats_percpu);
233 /* Extract flow from 'skb' into 'key'. */
234 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
235 if (unlikely(error)) {
241 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
242 if (unlikely(!flow)) {
243 struct dp_upcall_info upcall;
245 upcall.cmd = OVS_PACKET_CMD_MISS;
247 upcall.userdata = NULL;
248 upcall.portid = p->upcall_portid;
249 ovs_dp_upcall(dp, skb, &upcall);
251 stats_counter = &stats->n_missed;
255 OVS_CB(skb)->flow = flow;
257 stats_counter = &stats->n_hit;
258 ovs_flow_used(OVS_CB(skb)->flow, skb);
259 ovs_execute_actions(dp, skb);
262 /* Update datapath statistics. */
263 u64_stats_update_begin(&stats->sync);
265 u64_stats_update_end(&stats->sync);
268 static struct genl_family dp_packet_genl_family = {
269 .id = GENL_ID_GENERATE,
270 .hdrsize = sizeof(struct ovs_header),
271 .name = OVS_PACKET_FAMILY,
272 .version = OVS_PACKET_VERSION,
273 .maxattr = OVS_PACKET_ATTR_MAX,
275 .parallel_ops = true,
278 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
279 const struct dp_upcall_info *upcall_info)
281 struct dp_stats_percpu *stats;
285 if (upcall_info->portid == 0) {
290 dp_ifindex = get_dpifindex(dp);
296 if (!skb_is_gso(skb))
297 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
299 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
306 stats = this_cpu_ptr(dp->stats_percpu);
308 u64_stats_update_begin(&stats->sync);
310 u64_stats_update_end(&stats->sync);
315 static int queue_gso_packets(struct net *net, int dp_ifindex,
317 const struct dp_upcall_info *upcall_info)
319 unsigned short gso_type = skb_shinfo(skb)->gso_type;
320 struct dp_upcall_info later_info;
321 struct sw_flow_key later_key;
322 struct sk_buff *segs, *nskb;
325 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
327 return PTR_ERR(segs);
329 /* Queue all of the segments. */
332 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
336 if (skb == segs && gso_type & SKB_GSO_UDP) {
337 /* The initial flow key extracted by ovs_flow_extract()
338 * in this case is for a first fragment, so we need to
339 * properly mark later fragments.
341 later_key = *upcall_info->key;
342 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
344 later_info = *upcall_info;
345 later_info.key = &later_key;
346 upcall_info = &later_info;
348 } while ((skb = skb->next));
350 /* Free all of the segments. */
358 } while ((skb = nskb));
362 static size_t key_attr_size(void)
364 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
365 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
366 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
367 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
368 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
369 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
370 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
371 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
372 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
373 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
374 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
377 static size_t upcall_msg_size(const struct sk_buff *skb,
378 const struct nlattr *userdata)
380 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
381 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
382 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
384 /* OVS_PACKET_ATTR_USERDATA */
386 size += NLA_ALIGN(userdata->nla_len);
391 static int queue_userspace_packet(struct net *net, int dp_ifindex,
393 const struct dp_upcall_info *upcall_info)
395 struct ovs_header *upcall;
396 struct sk_buff *nskb = NULL;
397 struct sk_buff *user_skb; /* to be queued to userspace */
401 if (vlan_tx_tag_present(skb)) {
402 nskb = skb_clone(skb, GFP_ATOMIC);
406 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
414 if (nla_attr_size(skb->len) > USHRT_MAX) {
419 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
425 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
426 0, upcall_info->cmd);
427 upcall->dp_ifindex = dp_ifindex;
429 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
430 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
431 nla_nest_end(user_skb, nla);
433 if (upcall_info->userdata)
434 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
435 nla_len(upcall_info->userdata),
436 nla_data(upcall_info->userdata));
438 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
440 skb_copy_and_csum_dev(skb, nla_data(nla));
442 genlmsg_end(user_skb, upcall);
443 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
450 /* Called with ovs_mutex. */
451 static int flush_flows(struct datapath *dp)
453 struct flow_table *old_table;
454 struct flow_table *new_table;
456 old_table = ovsl_dereference(dp->table);
457 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
461 rcu_assign_pointer(dp->table, new_table);
463 ovs_flow_tbl_deferred_destroy(old_table);
467 static int validate_actions(const struct nlattr *attr,
468 const struct sw_flow_key *key, int depth);
470 static int validate_sample(const struct nlattr *attr,
471 const struct sw_flow_key *key, int depth)
473 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
474 const struct nlattr *probability, *actions;
475 const struct nlattr *a;
478 memset(attrs, 0, sizeof(attrs));
479 nla_for_each_nested(a, attr, rem) {
480 int type = nla_type(a);
481 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
488 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
489 if (!probability || nla_len(probability) != sizeof(u32))
492 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
493 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
495 return validate_actions(actions, key, depth + 1);
498 static int validate_tp_port(const struct sw_flow_key *flow_key)
500 if (flow_key->eth.type == htons(ETH_P_IP)) {
501 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
503 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
504 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
511 static int validate_set(const struct nlattr *a,
512 const struct sw_flow_key *flow_key)
514 const struct nlattr *ovs_key = nla_data(a);
515 int key_type = nla_type(ovs_key);
517 /* There can be only one key in a action */
518 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
521 if (key_type > OVS_KEY_ATTR_MAX ||
522 nla_len(ovs_key) != ovs_key_lens[key_type])
526 const struct ovs_key_ipv4 *ipv4_key;
527 const struct ovs_key_ipv6 *ipv6_key;
529 case OVS_KEY_ATTR_PRIORITY:
530 case OVS_KEY_ATTR_SKB_MARK:
531 case OVS_KEY_ATTR_ETHERNET:
534 case OVS_KEY_ATTR_IPV4:
535 if (flow_key->eth.type != htons(ETH_P_IP))
538 if (!flow_key->ip.proto)
541 ipv4_key = nla_data(ovs_key);
542 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
545 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
550 case OVS_KEY_ATTR_IPV6:
551 if (flow_key->eth.type != htons(ETH_P_IPV6))
554 if (!flow_key->ip.proto)
557 ipv6_key = nla_data(ovs_key);
558 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
561 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
564 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
569 case OVS_KEY_ATTR_TCP:
570 if (flow_key->ip.proto != IPPROTO_TCP)
573 return validate_tp_port(flow_key);
575 case OVS_KEY_ATTR_UDP:
576 if (flow_key->ip.proto != IPPROTO_UDP)
579 return validate_tp_port(flow_key);
588 static int validate_userspace(const struct nlattr *attr)
590 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
591 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
592 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
594 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
597 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
598 attr, userspace_policy);
602 if (!a[OVS_USERSPACE_ATTR_PID] ||
603 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
609 static int validate_actions(const struct nlattr *attr,
610 const struct sw_flow_key *key, int depth)
612 const struct nlattr *a;
615 if (depth >= SAMPLE_ACTION_DEPTH)
618 nla_for_each_nested(a, attr, rem) {
619 /* Expected argument lengths, (u32)-1 for variable length. */
620 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
621 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
622 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
623 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
624 [OVS_ACTION_ATTR_POP_VLAN] = 0,
625 [OVS_ACTION_ATTR_SET] = (u32)-1,
626 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
628 const struct ovs_action_push_vlan *vlan;
629 int type = nla_type(a);
631 if (type > OVS_ACTION_ATTR_MAX ||
632 (action_lens[type] != nla_len(a) &&
633 action_lens[type] != (u32)-1))
637 case OVS_ACTION_ATTR_UNSPEC:
640 case OVS_ACTION_ATTR_USERSPACE:
641 err = validate_userspace(a);
646 case OVS_ACTION_ATTR_OUTPUT:
647 if (nla_get_u32(a) >= DP_MAX_PORTS)
652 case OVS_ACTION_ATTR_POP_VLAN:
655 case OVS_ACTION_ATTR_PUSH_VLAN:
657 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
659 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
663 case OVS_ACTION_ATTR_SET:
664 err = validate_set(a, key);
669 case OVS_ACTION_ATTR_SAMPLE:
670 err = validate_sample(a, key, depth);
686 static void clear_stats(struct sw_flow *flow)
690 flow->packet_count = 0;
691 flow->byte_count = 0;
694 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
696 struct ovs_header *ovs_header = info->userhdr;
697 struct nlattr **a = info->attrs;
698 struct sw_flow_actions *acts;
699 struct sk_buff *packet;
700 struct sw_flow *flow;
708 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
709 !a[OVS_PACKET_ATTR_ACTIONS])
712 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
713 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
717 skb_reserve(packet, NET_IP_ALIGN);
719 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
721 skb_reset_mac_header(packet);
722 eth = eth_hdr(packet);
724 /* Normally, setting the skb 'protocol' field would be handled by a
725 * call to eth_type_trans(), but it assumes there's a sending
726 * device, which we may not have. */
727 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
728 packet->protocol = eth->h_proto;
730 packet->protocol = htons(ETH_P_802_2);
732 /* Build an sw_flow for sending this packet. */
733 flow = ovs_flow_alloc();
738 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
742 err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
746 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
750 flow->hash = ovs_flow_hash(&flow->key, key_len);
752 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
756 rcu_assign_pointer(flow->sf_acts, acts);
758 OVS_CB(packet)->flow = flow;
759 packet->priority = flow->key.phy.priority;
760 packet->mark = flow->key.phy.skb_mark;
763 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
769 err = ovs_execute_actions(dp, packet);
786 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
787 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
788 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
789 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
792 static struct genl_ops dp_packet_genl_ops[] = {
793 { .cmd = OVS_PACKET_CMD_EXECUTE,
794 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
795 .policy = packet_policy,
796 .doit = ovs_packet_cmd_execute
800 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
803 struct flow_table *table = ovsl_dereference(dp->table);
805 stats->n_flows = ovs_flow_tbl_count(table);
807 stats->n_hit = stats->n_missed = stats->n_lost = 0;
808 for_each_possible_cpu(i) {
809 const struct dp_stats_percpu *percpu_stats;
810 struct dp_stats_percpu local_stats;
813 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
816 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
817 local_stats = *percpu_stats;
818 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
820 stats->n_hit += local_stats.n_hit;
821 stats->n_missed += local_stats.n_missed;
822 stats->n_lost += local_stats.n_lost;
826 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
827 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
828 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
829 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
832 static struct genl_family dp_flow_genl_family = {
833 .id = GENL_ID_GENERATE,
834 .hdrsize = sizeof(struct ovs_header),
835 .name = OVS_FLOW_FAMILY,
836 .version = OVS_FLOW_VERSION,
837 .maxattr = OVS_FLOW_ATTR_MAX,
839 .parallel_ops = true,
842 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
843 .name = OVS_FLOW_MCGROUP
846 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
848 return NLMSG_ALIGN(sizeof(struct ovs_header))
849 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
850 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
851 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
852 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
853 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
856 /* Called with ovs_mutex. */
857 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
858 struct sk_buff *skb, u32 portid,
859 u32 seq, u32 flags, u8 cmd)
861 const int skb_orig_len = skb->len;
862 const struct sw_flow_actions *sf_acts;
863 struct ovs_flow_stats stats;
864 struct ovs_header *ovs_header;
870 sf_acts = ovsl_dereference(flow->sf_acts);
872 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
876 ovs_header->dp_ifindex = get_dpifindex(dp);
878 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
880 goto nla_put_failure;
881 err = ovs_flow_to_nlattrs(&flow->key, skb);
884 nla_nest_end(skb, nla);
886 spin_lock_bh(&flow->lock);
888 stats.n_packets = flow->packet_count;
889 stats.n_bytes = flow->byte_count;
890 tcp_flags = flow->tcp_flags;
891 spin_unlock_bh(&flow->lock);
894 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
895 goto nla_put_failure;
897 if (stats.n_packets &&
898 nla_put(skb, OVS_FLOW_ATTR_STATS,
899 sizeof(struct ovs_flow_stats), &stats))
900 goto nla_put_failure;
903 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
904 goto nla_put_failure;
906 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
907 * this is the first flow to be dumped into 'skb'. This is unusual for
908 * Netlink but individual action lists can be longer than
909 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
910 * The userspace caller can always fetch the actions separately if it
911 * really wants them. (Most userspace callers in fact don't care.)
913 * This can only fail for dump operations because the skb is always
914 * properly sized for single flows.
916 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
918 if (err < 0 && skb_orig_len)
921 return genlmsg_end(skb, ovs_header);
926 genlmsg_cancel(skb, ovs_header);
930 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
932 const struct sw_flow_actions *sf_acts;
934 sf_acts = ovsl_dereference(flow->sf_acts);
936 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
939 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
941 u32 portid, u32 seq, u8 cmd)
946 skb = ovs_flow_cmd_alloc_info(flow);
948 return ERR_PTR(-ENOMEM);
950 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
955 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
957 struct nlattr **a = info->attrs;
958 struct ovs_header *ovs_header = info->userhdr;
959 struct sw_flow_key key;
960 struct sw_flow *flow;
961 struct sk_buff *reply;
963 struct flow_table *table;
969 if (!a[OVS_FLOW_ATTR_KEY])
971 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
975 /* Validate actions. */
976 if (a[OVS_FLOW_ATTR_ACTIONS]) {
977 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
980 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
986 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
991 table = ovsl_dereference(dp->table);
992 flow = ovs_flow_tbl_lookup(table, &key, key_len);
994 struct sw_flow_actions *acts;
996 /* Bail out if we're not allowed to create a new flow. */
998 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1001 /* Expand table, if necessary, to make room. */
1002 if (ovs_flow_tbl_need_to_expand(table)) {
1003 struct flow_table *new_table;
1005 new_table = ovs_flow_tbl_expand(table);
1006 if (!IS_ERR(new_table)) {
1007 rcu_assign_pointer(dp->table, new_table);
1008 ovs_flow_tbl_deferred_destroy(table);
1009 table = ovsl_dereference(dp->table);
1013 /* Allocate flow. */
1014 flow = ovs_flow_alloc();
1016 error = PTR_ERR(flow);
1017 goto err_unlock_ovs;
1022 /* Obtain actions. */
1023 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1024 error = PTR_ERR(acts);
1026 goto error_free_flow;
1027 rcu_assign_pointer(flow->sf_acts, acts);
1029 /* Put flow in bucket. */
1030 flow->hash = ovs_flow_hash(&key, key_len);
1031 ovs_flow_tbl_insert(table, flow);
1033 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1037 /* We found a matching flow. */
1038 struct sw_flow_actions *old_acts;
1039 struct nlattr *acts_attrs;
1041 /* Bail out if we're not allowed to modify an existing flow.
1042 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1043 * because Generic Netlink treats the latter as a dump
1044 * request. We also accept NLM_F_EXCL in case that bug ever
1048 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1049 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1050 goto err_unlock_ovs;
1052 /* Update actions. */
1053 old_acts = ovsl_dereference(flow->sf_acts);
1054 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1056 (old_acts->actions_len != nla_len(acts_attrs) ||
1057 memcmp(old_acts->actions, nla_data(acts_attrs),
1058 old_acts->actions_len))) {
1059 struct sw_flow_actions *new_acts;
1061 new_acts = ovs_flow_actions_alloc(acts_attrs);
1062 error = PTR_ERR(new_acts);
1063 if (IS_ERR(new_acts))
1064 goto err_unlock_ovs;
1066 rcu_assign_pointer(flow->sf_acts, new_acts);
1067 ovs_flow_deferred_free_acts(old_acts);
1070 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1071 info->snd_seq, OVS_FLOW_CMD_NEW);
1074 if (a[OVS_FLOW_ATTR_CLEAR]) {
1075 spin_lock_bh(&flow->lock);
1077 spin_unlock_bh(&flow->lock);
1083 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1085 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1086 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1090 ovs_flow_free(flow);
1097 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1099 struct nlattr **a = info->attrs;
1100 struct ovs_header *ovs_header = info->userhdr;
1101 struct sw_flow_key key;
1102 struct sk_buff *reply;
1103 struct sw_flow *flow;
1104 struct datapath *dp;
1105 struct flow_table *table;
1109 if (!a[OVS_FLOW_ATTR_KEY])
1111 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1116 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1122 table = ovsl_dereference(dp->table);
1123 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1129 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1130 info->snd_seq, OVS_FLOW_CMD_NEW);
1131 if (IS_ERR(reply)) {
1132 err = PTR_ERR(reply);
1137 return genlmsg_reply(reply, info);
1143 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1145 struct nlattr **a = info->attrs;
1146 struct ovs_header *ovs_header = info->userhdr;
1147 struct sw_flow_key key;
1148 struct sk_buff *reply;
1149 struct sw_flow *flow;
1150 struct datapath *dp;
1151 struct flow_table *table;
1156 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1162 if (!a[OVS_FLOW_ATTR_KEY]) {
1163 err = flush_flows(dp);
1166 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1170 table = ovsl_dereference(dp->table);
1171 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1177 reply = ovs_flow_cmd_alloc_info(flow);
1183 ovs_flow_tbl_remove(table, flow);
1185 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1186 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1189 ovs_flow_deferred_free(flow);
1192 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1199 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1201 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1202 struct datapath *dp;
1203 struct flow_table *table;
1206 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1212 table = ovsl_dereference(dp->table);
1215 struct sw_flow *flow;
1218 bucket = cb->args[0];
1220 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1224 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1225 NETLINK_CB(cb->skb).portid,
1226 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1227 OVS_FLOW_CMD_NEW) < 0)
1230 cb->args[0] = bucket;
1237 static struct genl_ops dp_flow_genl_ops[] = {
1238 { .cmd = OVS_FLOW_CMD_NEW,
1239 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1240 .policy = flow_policy,
1241 .doit = ovs_flow_cmd_new_or_set
1243 { .cmd = OVS_FLOW_CMD_DEL,
1244 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1245 .policy = flow_policy,
1246 .doit = ovs_flow_cmd_del
1248 { .cmd = OVS_FLOW_CMD_GET,
1249 .flags = 0, /* OK for unprivileged users. */
1250 .policy = flow_policy,
1251 .doit = ovs_flow_cmd_get,
1252 .dumpit = ovs_flow_cmd_dump
1254 { .cmd = OVS_FLOW_CMD_SET,
1255 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1256 .policy = flow_policy,
1257 .doit = ovs_flow_cmd_new_or_set,
1261 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1262 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1263 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1266 static struct genl_family dp_datapath_genl_family = {
1267 .id = GENL_ID_GENERATE,
1268 .hdrsize = sizeof(struct ovs_header),
1269 .name = OVS_DATAPATH_FAMILY,
1270 .version = OVS_DATAPATH_VERSION,
1271 .maxattr = OVS_DP_ATTR_MAX,
1273 .parallel_ops = true,
1276 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1277 .name = OVS_DATAPATH_MCGROUP
1280 static size_t ovs_dp_cmd_msg_size(void)
1282 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1284 msgsize += nla_total_size(IFNAMSIZ);
1285 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1290 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1291 u32 portid, u32 seq, u32 flags, u8 cmd)
1293 struct ovs_header *ovs_header;
1294 struct ovs_dp_stats dp_stats;
1297 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1302 ovs_header->dp_ifindex = get_dpifindex(dp);
1305 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1308 goto nla_put_failure;
1310 get_dp_stats(dp, &dp_stats);
1311 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1312 goto nla_put_failure;
1314 return genlmsg_end(skb, ovs_header);
1317 genlmsg_cancel(skb, ovs_header);
1322 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1325 struct sk_buff *skb;
1328 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1330 return ERR_PTR(-ENOMEM);
1332 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1335 return ERR_PTR(retval);
1340 /* Called with ovs_mutex. */
1341 static struct datapath *lookup_datapath(struct net *net,
1342 struct ovs_header *ovs_header,
1343 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1345 struct datapath *dp;
1347 if (!a[OVS_DP_ATTR_NAME])
1348 dp = get_dp(net, ovs_header->dp_ifindex);
1350 struct vport *vport;
1353 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1354 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1357 return dp ? dp : ERR_PTR(-ENODEV);
1360 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1362 struct nlattr **a = info->attrs;
1363 struct vport_parms parms;
1364 struct sk_buff *reply;
1365 struct datapath *dp;
1366 struct vport *vport;
1367 struct ovs_net *ovs_net;
1371 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1377 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1379 goto err_unlock_ovs;
1381 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1383 /* Allocate table. */
1385 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1389 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1390 if (!dp->stats_percpu) {
1392 goto err_destroy_table;
1395 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1399 goto err_destroy_percpu;
1402 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1403 INIT_HLIST_HEAD(&dp->ports[i]);
1405 /* Set up our datapath device. */
1406 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1407 parms.type = OVS_VPORT_TYPE_INTERNAL;
1408 parms.options = NULL;
1410 parms.port_no = OVSP_LOCAL;
1411 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1413 vport = new_vport(&parms);
1414 if (IS_ERR(vport)) {
1415 err = PTR_ERR(vport);
1419 goto err_destroy_ports_array;
1422 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1423 info->snd_seq, OVS_DP_CMD_NEW);
1424 err = PTR_ERR(reply);
1426 goto err_destroy_local_port;
1428 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1429 list_add_tail(&dp->list_node, &ovs_net->dps);
1433 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1436 err_destroy_local_port:
1437 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1438 err_destroy_ports_array:
1441 free_percpu(dp->stats_percpu);
1443 ovs_flow_tbl_destroy(ovsl_dereference(dp->table));
1445 release_net(ovs_dp_get_net(dp));
1453 /* Called with ovs_mutex. */
1454 static void __dp_destroy(struct datapath *dp)
1458 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1459 struct vport *vport;
1460 struct hlist_node *n;
1462 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1463 if (vport->port_no != OVSP_LOCAL)
1464 ovs_dp_detach_port(vport);
1467 list_del(&dp->list_node);
1469 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1470 * all port in datapath are destroyed first before freeing datapath.
1472 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1474 call_rcu(&dp->rcu, destroy_dp_rcu);
1477 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1479 struct sk_buff *reply;
1480 struct datapath *dp;
1484 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1489 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1490 info->snd_seq, OVS_DP_CMD_DEL);
1491 err = PTR_ERR(reply);
1498 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1506 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1508 struct sk_buff *reply;
1509 struct datapath *dp;
1513 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1518 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1519 info->snd_seq, OVS_DP_CMD_NEW);
1520 if (IS_ERR(reply)) {
1521 err = PTR_ERR(reply);
1522 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1523 ovs_dp_datapath_multicast_group.id, err);
1529 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1537 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1539 struct sk_buff *reply;
1540 struct datapath *dp;
1544 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1550 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1551 info->snd_seq, OVS_DP_CMD_NEW);
1552 if (IS_ERR(reply)) {
1553 err = PTR_ERR(reply);
1558 return genlmsg_reply(reply, info);
1565 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1567 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1568 struct datapath *dp;
1569 int skip = cb->args[0];
1573 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1575 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1576 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1577 OVS_DP_CMD_NEW) < 0)
1588 static struct genl_ops dp_datapath_genl_ops[] = {
1589 { .cmd = OVS_DP_CMD_NEW,
1590 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1591 .policy = datapath_policy,
1592 .doit = ovs_dp_cmd_new
1594 { .cmd = OVS_DP_CMD_DEL,
1595 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1596 .policy = datapath_policy,
1597 .doit = ovs_dp_cmd_del
1599 { .cmd = OVS_DP_CMD_GET,
1600 .flags = 0, /* OK for unprivileged users. */
1601 .policy = datapath_policy,
1602 .doit = ovs_dp_cmd_get,
1603 .dumpit = ovs_dp_cmd_dump
1605 { .cmd = OVS_DP_CMD_SET,
1606 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1607 .policy = datapath_policy,
1608 .doit = ovs_dp_cmd_set,
1612 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1613 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1614 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1615 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1616 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1617 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1618 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1621 static struct genl_family dp_vport_genl_family = {
1622 .id = GENL_ID_GENERATE,
1623 .hdrsize = sizeof(struct ovs_header),
1624 .name = OVS_VPORT_FAMILY,
1625 .version = OVS_VPORT_VERSION,
1626 .maxattr = OVS_VPORT_ATTR_MAX,
1628 .parallel_ops = true,
1631 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1632 .name = OVS_VPORT_MCGROUP
1635 /* Called with ovs_mutex or RCU read lock. */
1636 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1637 u32 portid, u32 seq, u32 flags, u8 cmd)
1639 struct ovs_header *ovs_header;
1640 struct ovs_vport_stats vport_stats;
1643 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1648 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1650 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1651 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1652 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1653 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1654 goto nla_put_failure;
1656 ovs_vport_get_stats(vport, &vport_stats);
1657 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1659 goto nla_put_failure;
1661 err = ovs_vport_get_options(vport, skb);
1662 if (err == -EMSGSIZE)
1665 return genlmsg_end(skb, ovs_header);
1670 genlmsg_cancel(skb, ovs_header);
1674 /* Called with ovs_mutex or RCU read lock. */
1675 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1678 struct sk_buff *skb;
1681 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1683 return ERR_PTR(-ENOMEM);
1685 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1691 /* Called with ovs_mutex or RCU read lock. */
1692 static struct vport *lookup_vport(struct net *net,
1693 struct ovs_header *ovs_header,
1694 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1696 struct datapath *dp;
1697 struct vport *vport;
1699 if (a[OVS_VPORT_ATTR_NAME]) {
1700 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1702 return ERR_PTR(-ENODEV);
1703 if (ovs_header->dp_ifindex &&
1704 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1705 return ERR_PTR(-ENODEV);
1707 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1708 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1710 if (port_no >= DP_MAX_PORTS)
1711 return ERR_PTR(-EFBIG);
1713 dp = get_dp(net, ovs_header->dp_ifindex);
1715 return ERR_PTR(-ENODEV);
1717 vport = ovs_vport_ovsl_rcu(dp, port_no);
1719 return ERR_PTR(-ENODEV);
1722 return ERR_PTR(-EINVAL);
1725 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1727 struct nlattr **a = info->attrs;
1728 struct ovs_header *ovs_header = info->userhdr;
1729 struct vport_parms parms;
1730 struct sk_buff *reply;
1731 struct vport *vport;
1732 struct datapath *dp;
1737 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1738 !a[OVS_VPORT_ATTR_UPCALL_PID])
1742 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1747 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1748 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1751 if (port_no >= DP_MAX_PORTS)
1754 vport = ovs_vport_ovsl(dp, port_no);
1759 for (port_no = 1; ; port_no++) {
1760 if (port_no >= DP_MAX_PORTS) {
1764 vport = ovs_vport_ovsl(dp, port_no);
1770 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1771 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1772 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1774 parms.port_no = port_no;
1775 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1777 vport = new_vport(&parms);
1778 err = PTR_ERR(vport);
1783 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1785 if (IS_ERR(reply)) {
1786 err = PTR_ERR(reply);
1787 ovs_dp_detach_port(vport);
1791 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1799 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1801 struct nlattr **a = info->attrs;
1802 struct sk_buff *reply;
1803 struct vport *vport;
1807 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1808 err = PTR_ERR(vport);
1812 if (a[OVS_VPORT_ATTR_TYPE] &&
1813 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1818 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1824 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1825 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1830 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1831 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1833 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1834 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1838 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1851 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1853 struct nlattr **a = info->attrs;
1854 struct sk_buff *reply;
1855 struct vport *vport;
1859 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1860 err = PTR_ERR(vport);
1864 if (vport->port_no == OVSP_LOCAL) {
1869 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1871 err = PTR_ERR(reply);
1876 ovs_dp_detach_port(vport);
1878 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1885 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1887 struct nlattr **a = info->attrs;
1888 struct ovs_header *ovs_header = info->userhdr;
1889 struct sk_buff *reply;
1890 struct vport *vport;
1894 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1895 err = PTR_ERR(vport);
1899 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1901 err = PTR_ERR(reply);
1907 return genlmsg_reply(reply, info);
1914 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1916 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1917 struct datapath *dp;
1918 int bucket = cb->args[0], skip = cb->args[1];
1921 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1926 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1927 struct vport *vport;
1930 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1932 ovs_vport_cmd_fill_info(vport, skb,
1933 NETLINK_CB(cb->skb).portid,
1936 OVS_VPORT_CMD_NEW) < 0)
1952 static struct genl_ops dp_vport_genl_ops[] = {
1953 { .cmd = OVS_VPORT_CMD_NEW,
1954 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1955 .policy = vport_policy,
1956 .doit = ovs_vport_cmd_new
1958 { .cmd = OVS_VPORT_CMD_DEL,
1959 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1960 .policy = vport_policy,
1961 .doit = ovs_vport_cmd_del
1963 { .cmd = OVS_VPORT_CMD_GET,
1964 .flags = 0, /* OK for unprivileged users. */
1965 .policy = vport_policy,
1966 .doit = ovs_vport_cmd_get,
1967 .dumpit = ovs_vport_cmd_dump
1969 { .cmd = OVS_VPORT_CMD_SET,
1970 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1971 .policy = vport_policy,
1972 .doit = ovs_vport_cmd_set,
1976 struct genl_family_and_ops {
1977 struct genl_family *family;
1978 struct genl_ops *ops;
1980 struct genl_multicast_group *group;
1983 static const struct genl_family_and_ops dp_genl_families[] = {
1984 { &dp_datapath_genl_family,
1985 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1986 &ovs_dp_datapath_multicast_group },
1987 { &dp_vport_genl_family,
1988 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1989 &ovs_dp_vport_multicast_group },
1990 { &dp_flow_genl_family,
1991 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1992 &ovs_dp_flow_multicast_group },
1993 { &dp_packet_genl_family,
1994 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1998 static void dp_unregister_genl(int n_families)
2002 for (i = 0; i < n_families; i++)
2003 genl_unregister_family(dp_genl_families[i].family);
2006 static int dp_register_genl(void)
2013 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2014 const struct genl_family_and_ops *f = &dp_genl_families[i];
2016 err = genl_register_family_with_ops(f->family, f->ops,
2023 err = genl_register_mc_group(f->family, f->group);
2032 dp_unregister_genl(n_registered);
2036 static void rehash_flow_table(struct work_struct *work)
2038 struct datapath *dp;
2044 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2046 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2047 struct flow_table *old_table = ovsl_dereference(dp->table);
2048 struct flow_table *new_table;
2050 new_table = ovs_flow_tbl_rehash(old_table);
2051 if (!IS_ERR(new_table)) {
2052 rcu_assign_pointer(dp->table, new_table);
2053 ovs_flow_tbl_deferred_destroy(old_table);
2059 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2062 static int __net_init ovs_init_net(struct net *net)
2064 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2066 INIT_LIST_HEAD(&ovs_net->dps);
2067 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2071 static void __net_exit ovs_exit_net(struct net *net)
2073 struct datapath *dp, *dp_next;
2074 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2077 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2081 cancel_work_sync(&ovs_net->dp_notify_work);
2084 static struct pernet_operations ovs_net_ops = {
2085 .init = ovs_init_net,
2086 .exit = ovs_exit_net,
2088 .size = sizeof(struct ovs_net),
2091 static int __init dp_init(void)
2095 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2097 pr_info("Open vSwitch switching datapath\n");
2099 err = ovs_flow_init();
2103 err = ovs_vport_init();
2105 goto error_flow_exit;
2107 err = register_pernet_device(&ovs_net_ops);
2109 goto error_vport_exit;
2111 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2113 goto error_netns_exit;
2115 err = dp_register_genl();
2117 goto error_unreg_notifier;
2119 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2123 error_unreg_notifier:
2124 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2126 unregister_pernet_device(&ovs_net_ops);
2135 static void dp_cleanup(void)
2137 cancel_delayed_work_sync(&rehash_flow_wq);
2138 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2139 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2140 unregister_pernet_device(&ovs_net_ops);
2146 module_init(dp_init);
2147 module_exit(dp_cleanup);
2149 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2150 MODULE_LICENSE("GPL");