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/openvswitch.h>
48 #include <linux/rculist.h>
49 #include <linux/dmi.h>
50 #include <linux/workqueue.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
57 #include "vport-internal_dev.h"
60 * struct ovs_net - Per net-namespace data for ovs.
61 * @dps: List of datapaths to enable dumping them all out.
62 * Protected by genl_mutex.
68 static int ovs_net_id __read_mostly;
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 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
75 struct genl_multicast_group *grp)
77 genl_notify(skb, genl_info_net(info), info->snd_portid,
78 grp->id, info->nlhdr, GFP_KERNEL);
84 * Writes to device state (add/remove datapath, port, set operations on vports,
85 * etc.) are protected by RTNL.
87 * Writes to other state (flow table modifications, set miscellaneous datapath
88 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
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
98 static struct vport *new_vport(const struct vport_parms *);
99 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
100 const struct dp_upcall_info *);
101 static int queue_userspace_packet(struct net *, int dp_ifindex,
103 const struct dp_upcall_info *);
105 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
106 static struct datapath *get_dp(struct net *net, int dp_ifindex)
108 struct datapath *dp = NULL;
109 struct net_device *dev;
112 dev = dev_get_by_index_rcu(net, dp_ifindex);
114 struct vport *vport = ovs_internal_dev_get_vport(dev);
123 /* Must be called with rcu_read_lock or RTNL lock. */
124 const char *ovs_dp_name(const struct datapath *dp)
126 struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
127 return vport->ops->get_name(vport);
130 static int get_dpifindex(struct datapath *dp)
137 local = ovs_vport_rcu(dp, OVSP_LOCAL);
139 ifindex = local->ops->get_ifindex(local);
148 static void destroy_dp_rcu(struct rcu_head *rcu)
150 struct datapath *dp = container_of(rcu, struct datapath, rcu);
152 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
153 free_percpu(dp->stats_percpu);
154 release_net(ovs_dp_get_net(dp));
159 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
162 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
165 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
168 struct hlist_head *head;
170 head = vport_hash_bucket(dp, port_no);
171 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
172 if (vport->port_no == port_no)
178 /* Called with RTNL lock and genl_lock. */
179 static struct vport *new_vport(const struct vport_parms *parms)
183 vport = ovs_vport_add(parms);
184 if (!IS_ERR(vport)) {
185 struct datapath *dp = parms->dp;
186 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
188 hlist_add_head_rcu(&vport->dp_hash_node, head);
194 /* Called with RTNL lock. */
195 void ovs_dp_detach_port(struct vport *p)
199 /* First drop references to device. */
200 hlist_del_rcu(&p->dp_hash_node);
202 /* Then destroy it. */
206 /* Must be called with rcu_read_lock. */
207 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
209 struct datapath *dp = p->dp;
210 struct sw_flow *flow;
211 struct dp_stats_percpu *stats;
212 struct sw_flow_key key;
217 stats = this_cpu_ptr(dp->stats_percpu);
219 /* Extract flow from 'skb' into 'key'. */
220 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
221 if (unlikely(error)) {
227 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
228 if (unlikely(!flow)) {
229 struct dp_upcall_info upcall;
231 upcall.cmd = OVS_PACKET_CMD_MISS;
233 upcall.userdata = NULL;
234 upcall.portid = p->upcall_portid;
235 ovs_dp_upcall(dp, skb, &upcall);
237 stats_counter = &stats->n_missed;
241 OVS_CB(skb)->flow = flow;
243 stats_counter = &stats->n_hit;
244 ovs_flow_used(OVS_CB(skb)->flow, skb);
245 ovs_execute_actions(dp, skb);
248 /* Update datapath statistics. */
249 u64_stats_update_begin(&stats->sync);
251 u64_stats_update_end(&stats->sync);
254 static struct genl_family dp_packet_genl_family = {
255 .id = GENL_ID_GENERATE,
256 .hdrsize = sizeof(struct ovs_header),
257 .name = OVS_PACKET_FAMILY,
258 .version = OVS_PACKET_VERSION,
259 .maxattr = OVS_PACKET_ATTR_MAX,
263 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
264 const struct dp_upcall_info *upcall_info)
266 struct dp_stats_percpu *stats;
270 if (upcall_info->portid == 0) {
275 dp_ifindex = get_dpifindex(dp);
281 if (!skb_is_gso(skb))
282 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
284 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
291 stats = this_cpu_ptr(dp->stats_percpu);
293 u64_stats_update_begin(&stats->sync);
295 u64_stats_update_end(&stats->sync);
300 static int queue_gso_packets(struct net *net, int dp_ifindex,
302 const struct dp_upcall_info *upcall_info)
304 unsigned short gso_type = skb_shinfo(skb)->gso_type;
305 struct dp_upcall_info later_info;
306 struct sw_flow_key later_key;
307 struct sk_buff *segs, *nskb;
310 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
312 return PTR_ERR(segs);
314 /* Queue all of the segments. */
317 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
321 if (skb == segs && gso_type & SKB_GSO_UDP) {
322 /* The initial flow key extracted by ovs_flow_extract()
323 * in this case is for a first fragment, so we need to
324 * properly mark later fragments.
326 later_key = *upcall_info->key;
327 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
329 later_info = *upcall_info;
330 later_info.key = &later_key;
331 upcall_info = &later_info;
333 } while ((skb = skb->next));
335 /* Free all of the segments. */
343 } while ((skb = nskb));
347 static size_t key_attr_size(void)
349 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
350 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
351 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
352 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
353 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
354 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
355 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
356 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
357 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
358 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
359 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
362 static size_t upcall_msg_size(const struct sk_buff *skb,
363 const struct nlattr *userdata)
365 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
366 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
367 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
369 /* OVS_PACKET_ATTR_USERDATA */
371 size += NLA_ALIGN(userdata->nla_len);
376 static int queue_userspace_packet(struct net *net, int dp_ifindex,
378 const struct dp_upcall_info *upcall_info)
380 struct ovs_header *upcall;
381 struct sk_buff *nskb = NULL;
382 struct sk_buff *user_skb; /* to be queued to userspace */
386 if (vlan_tx_tag_present(skb)) {
387 nskb = skb_clone(skb, GFP_ATOMIC);
391 nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
399 if (nla_attr_size(skb->len) > USHRT_MAX) {
404 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
410 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
411 0, upcall_info->cmd);
412 upcall->dp_ifindex = dp_ifindex;
414 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
415 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
416 nla_nest_end(user_skb, nla);
418 if (upcall_info->userdata)
419 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
420 nla_len(upcall_info->userdata),
421 nla_data(upcall_info->userdata));
423 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
425 skb_copy_and_csum_dev(skb, nla_data(nla));
427 genlmsg_end(user_skb, upcall);
428 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
435 /* Called with genl_mutex. */
436 static int flush_flows(struct datapath *dp)
438 struct flow_table *old_table;
439 struct flow_table *new_table;
441 old_table = genl_dereference(dp->table);
442 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
446 rcu_assign_pointer(dp->table, new_table);
448 ovs_flow_tbl_deferred_destroy(old_table);
452 static int validate_actions(const struct nlattr *attr,
453 const struct sw_flow_key *key, int depth);
455 static int validate_sample(const struct nlattr *attr,
456 const struct sw_flow_key *key, int depth)
458 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
459 const struct nlattr *probability, *actions;
460 const struct nlattr *a;
463 memset(attrs, 0, sizeof(attrs));
464 nla_for_each_nested(a, attr, rem) {
465 int type = nla_type(a);
466 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
473 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
474 if (!probability || nla_len(probability) != sizeof(u32))
477 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
478 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
480 return validate_actions(actions, key, depth + 1);
483 static int validate_tp_port(const struct sw_flow_key *flow_key)
485 if (flow_key->eth.type == htons(ETH_P_IP)) {
486 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
488 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
489 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
496 static int validate_set(const struct nlattr *a,
497 const struct sw_flow_key *flow_key)
499 const struct nlattr *ovs_key = nla_data(a);
500 int key_type = nla_type(ovs_key);
502 /* There can be only one key in a action */
503 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
506 if (key_type > OVS_KEY_ATTR_MAX ||
507 nla_len(ovs_key) != ovs_key_lens[key_type])
511 const struct ovs_key_ipv4 *ipv4_key;
512 const struct ovs_key_ipv6 *ipv6_key;
514 case OVS_KEY_ATTR_PRIORITY:
515 case OVS_KEY_ATTR_SKB_MARK:
516 case OVS_KEY_ATTR_ETHERNET:
519 case OVS_KEY_ATTR_IPV4:
520 if (flow_key->eth.type != htons(ETH_P_IP))
523 if (!flow_key->ip.proto)
526 ipv4_key = nla_data(ovs_key);
527 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
530 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
535 case OVS_KEY_ATTR_IPV6:
536 if (flow_key->eth.type != htons(ETH_P_IPV6))
539 if (!flow_key->ip.proto)
542 ipv6_key = nla_data(ovs_key);
543 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
546 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
549 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
554 case OVS_KEY_ATTR_TCP:
555 if (flow_key->ip.proto != IPPROTO_TCP)
558 return validate_tp_port(flow_key);
560 case OVS_KEY_ATTR_UDP:
561 if (flow_key->ip.proto != IPPROTO_UDP)
564 return validate_tp_port(flow_key);
573 static int validate_userspace(const struct nlattr *attr)
575 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
576 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
577 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
579 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
582 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
583 attr, userspace_policy);
587 if (!a[OVS_USERSPACE_ATTR_PID] ||
588 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
594 static int validate_actions(const struct nlattr *attr,
595 const struct sw_flow_key *key, int depth)
597 const struct nlattr *a;
600 if (depth >= SAMPLE_ACTION_DEPTH)
603 nla_for_each_nested(a, attr, rem) {
604 /* Expected argument lengths, (u32)-1 for variable length. */
605 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
606 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
607 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
608 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
609 [OVS_ACTION_ATTR_POP_VLAN] = 0,
610 [OVS_ACTION_ATTR_SET] = (u32)-1,
611 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
613 const struct ovs_action_push_vlan *vlan;
614 int type = nla_type(a);
616 if (type > OVS_ACTION_ATTR_MAX ||
617 (action_lens[type] != nla_len(a) &&
618 action_lens[type] != (u32)-1))
622 case OVS_ACTION_ATTR_UNSPEC:
625 case OVS_ACTION_ATTR_USERSPACE:
626 err = validate_userspace(a);
631 case OVS_ACTION_ATTR_OUTPUT:
632 if (nla_get_u32(a) >= DP_MAX_PORTS)
637 case OVS_ACTION_ATTR_POP_VLAN:
640 case OVS_ACTION_ATTR_PUSH_VLAN:
642 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
644 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
648 case OVS_ACTION_ATTR_SET:
649 err = validate_set(a, key);
654 case OVS_ACTION_ATTR_SAMPLE:
655 err = validate_sample(a, key, depth);
671 static void clear_stats(struct sw_flow *flow)
675 flow->packet_count = 0;
676 flow->byte_count = 0;
679 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
681 struct ovs_header *ovs_header = info->userhdr;
682 struct nlattr **a = info->attrs;
683 struct sw_flow_actions *acts;
684 struct sk_buff *packet;
685 struct sw_flow *flow;
693 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
694 !a[OVS_PACKET_ATTR_ACTIONS])
697 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
698 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
702 skb_reserve(packet, NET_IP_ALIGN);
704 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
706 skb_reset_mac_header(packet);
707 eth = eth_hdr(packet);
709 /* Normally, setting the skb 'protocol' field would be handled by a
710 * call to eth_type_trans(), but it assumes there's a sending
711 * device, which we may not have. */
712 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
713 packet->protocol = eth->h_proto;
715 packet->protocol = htons(ETH_P_802_2);
717 /* Build an sw_flow for sending this packet. */
718 flow = ovs_flow_alloc();
723 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
727 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
728 &flow->key.phy.skb_mark,
729 &flow->key.phy.in_port,
730 a[OVS_PACKET_ATTR_KEY]);
734 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
738 flow->hash = ovs_flow_hash(&flow->key, key_len);
740 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
744 rcu_assign_pointer(flow->sf_acts, acts);
746 OVS_CB(packet)->flow = flow;
747 packet->priority = flow->key.phy.priority;
748 packet->mark = flow->key.phy.skb_mark;
751 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
757 err = ovs_execute_actions(dp, packet);
774 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
775 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
776 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
777 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
780 static struct genl_ops dp_packet_genl_ops[] = {
781 { .cmd = OVS_PACKET_CMD_EXECUTE,
782 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
783 .policy = packet_policy,
784 .doit = ovs_packet_cmd_execute
788 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
791 struct flow_table *table = genl_dereference(dp->table);
793 stats->n_flows = ovs_flow_tbl_count(table);
795 stats->n_hit = stats->n_missed = stats->n_lost = 0;
796 for_each_possible_cpu(i) {
797 const struct dp_stats_percpu *percpu_stats;
798 struct dp_stats_percpu local_stats;
801 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
804 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
805 local_stats = *percpu_stats;
806 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
808 stats->n_hit += local_stats.n_hit;
809 stats->n_missed += local_stats.n_missed;
810 stats->n_lost += local_stats.n_lost;
814 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
815 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
816 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
817 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
820 static struct genl_family dp_flow_genl_family = {
821 .id = GENL_ID_GENERATE,
822 .hdrsize = sizeof(struct ovs_header),
823 .name = OVS_FLOW_FAMILY,
824 .version = OVS_FLOW_VERSION,
825 .maxattr = OVS_FLOW_ATTR_MAX,
829 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
830 .name = OVS_FLOW_MCGROUP
833 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
835 return NLMSG_ALIGN(sizeof(struct ovs_header))
836 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
837 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
838 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
839 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
840 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
843 /* Called with genl_lock. */
844 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
845 struct sk_buff *skb, u32 portid,
846 u32 seq, u32 flags, u8 cmd)
848 const int skb_orig_len = skb->len;
849 const struct sw_flow_actions *sf_acts;
850 struct ovs_flow_stats stats;
851 struct ovs_header *ovs_header;
857 sf_acts = rcu_dereference_protected(flow->sf_acts,
858 lockdep_genl_is_held());
860 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
864 ovs_header->dp_ifindex = get_dpifindex(dp);
866 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
868 goto nla_put_failure;
869 err = ovs_flow_to_nlattrs(&flow->key, skb);
872 nla_nest_end(skb, nla);
874 spin_lock_bh(&flow->lock);
876 stats.n_packets = flow->packet_count;
877 stats.n_bytes = flow->byte_count;
878 tcp_flags = flow->tcp_flags;
879 spin_unlock_bh(&flow->lock);
882 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
883 goto nla_put_failure;
885 if (stats.n_packets &&
886 nla_put(skb, OVS_FLOW_ATTR_STATS,
887 sizeof(struct ovs_flow_stats), &stats))
888 goto nla_put_failure;
891 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
892 goto nla_put_failure;
894 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
895 * this is the first flow to be dumped into 'skb'. This is unusual for
896 * Netlink but individual action lists can be longer than
897 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
898 * The userspace caller can always fetch the actions separately if it
899 * really wants them. (Most userspace callers in fact don't care.)
901 * This can only fail for dump operations because the skb is always
902 * properly sized for single flows.
904 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
906 if (err < 0 && skb_orig_len)
909 return genlmsg_end(skb, ovs_header);
914 genlmsg_cancel(skb, ovs_header);
918 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
920 const struct sw_flow_actions *sf_acts;
922 sf_acts = rcu_dereference_protected(flow->sf_acts,
923 lockdep_genl_is_held());
925 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
928 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
930 u32 portid, u32 seq, u8 cmd)
935 skb = ovs_flow_cmd_alloc_info(flow);
937 return ERR_PTR(-ENOMEM);
939 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
944 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
946 struct nlattr **a = info->attrs;
947 struct ovs_header *ovs_header = info->userhdr;
948 struct sw_flow_key key;
949 struct sw_flow *flow;
950 struct sk_buff *reply;
952 struct flow_table *table;
958 if (!a[OVS_FLOW_ATTR_KEY])
960 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
964 /* Validate actions. */
965 if (a[OVS_FLOW_ATTR_ACTIONS]) {
966 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
969 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
974 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
979 table = genl_dereference(dp->table);
980 flow = ovs_flow_tbl_lookup(table, &key, key_len);
982 struct sw_flow_actions *acts;
984 /* Bail out if we're not allowed to create a new flow. */
986 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
989 /* Expand table, if necessary, to make room. */
990 if (ovs_flow_tbl_need_to_expand(table)) {
991 struct flow_table *new_table;
993 new_table = ovs_flow_tbl_expand(table);
994 if (!IS_ERR(new_table)) {
995 rcu_assign_pointer(dp->table, new_table);
996 ovs_flow_tbl_deferred_destroy(table);
997 table = genl_dereference(dp->table);
1001 /* Allocate flow. */
1002 flow = ovs_flow_alloc();
1004 error = PTR_ERR(flow);
1010 /* Obtain actions. */
1011 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1012 error = PTR_ERR(acts);
1014 goto error_free_flow;
1015 rcu_assign_pointer(flow->sf_acts, acts);
1017 /* Put flow in bucket. */
1018 flow->hash = ovs_flow_hash(&key, key_len);
1019 ovs_flow_tbl_insert(table, flow);
1021 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1025 /* We found a matching flow. */
1026 struct sw_flow_actions *old_acts;
1027 struct nlattr *acts_attrs;
1029 /* Bail out if we're not allowed to modify an existing flow.
1030 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1031 * because Generic Netlink treats the latter as a dump
1032 * request. We also accept NLM_F_EXCL in case that bug ever
1036 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1037 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1040 /* Update actions. */
1041 old_acts = rcu_dereference_protected(flow->sf_acts,
1042 lockdep_genl_is_held());
1043 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1045 (old_acts->actions_len != nla_len(acts_attrs) ||
1046 memcmp(old_acts->actions, nla_data(acts_attrs),
1047 old_acts->actions_len))) {
1048 struct sw_flow_actions *new_acts;
1050 new_acts = ovs_flow_actions_alloc(acts_attrs);
1051 error = PTR_ERR(new_acts);
1052 if (IS_ERR(new_acts))
1055 rcu_assign_pointer(flow->sf_acts, new_acts);
1056 ovs_flow_deferred_free_acts(old_acts);
1059 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1060 info->snd_seq, OVS_FLOW_CMD_NEW);
1063 if (a[OVS_FLOW_ATTR_CLEAR]) {
1064 spin_lock_bh(&flow->lock);
1066 spin_unlock_bh(&flow->lock);
1071 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1073 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1074 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1078 ovs_flow_free(flow);
1083 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1085 struct nlattr **a = info->attrs;
1086 struct ovs_header *ovs_header = info->userhdr;
1087 struct sw_flow_key key;
1088 struct sk_buff *reply;
1089 struct sw_flow *flow;
1090 struct datapath *dp;
1091 struct flow_table *table;
1095 if (!a[OVS_FLOW_ATTR_KEY])
1097 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1101 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1105 table = genl_dereference(dp->table);
1106 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1110 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1111 info->snd_seq, OVS_FLOW_CMD_NEW);
1113 return PTR_ERR(reply);
1115 return genlmsg_reply(reply, info);
1118 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1120 struct nlattr **a = info->attrs;
1121 struct ovs_header *ovs_header = info->userhdr;
1122 struct sw_flow_key key;
1123 struct sk_buff *reply;
1124 struct sw_flow *flow;
1125 struct datapath *dp;
1126 struct flow_table *table;
1130 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1134 if (!a[OVS_FLOW_ATTR_KEY])
1135 return flush_flows(dp);
1137 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1141 table = genl_dereference(dp->table);
1142 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1146 reply = ovs_flow_cmd_alloc_info(flow);
1150 ovs_flow_tbl_remove(table, flow);
1152 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1153 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1156 ovs_flow_deferred_free(flow);
1158 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1162 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1164 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1165 struct datapath *dp;
1166 struct flow_table *table;
1168 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1172 table = genl_dereference(dp->table);
1175 struct sw_flow *flow;
1178 bucket = cb->args[0];
1180 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1184 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1185 NETLINK_CB(cb->skb).portid,
1186 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1187 OVS_FLOW_CMD_NEW) < 0)
1190 cb->args[0] = bucket;
1196 static struct genl_ops dp_flow_genl_ops[] = {
1197 { .cmd = OVS_FLOW_CMD_NEW,
1198 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1199 .policy = flow_policy,
1200 .doit = ovs_flow_cmd_new_or_set
1202 { .cmd = OVS_FLOW_CMD_DEL,
1203 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1204 .policy = flow_policy,
1205 .doit = ovs_flow_cmd_del
1207 { .cmd = OVS_FLOW_CMD_GET,
1208 .flags = 0, /* OK for unprivileged users. */
1209 .policy = flow_policy,
1210 .doit = ovs_flow_cmd_get,
1211 .dumpit = ovs_flow_cmd_dump
1213 { .cmd = OVS_FLOW_CMD_SET,
1214 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1215 .policy = flow_policy,
1216 .doit = ovs_flow_cmd_new_or_set,
1220 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1221 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1222 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1225 static struct genl_family dp_datapath_genl_family = {
1226 .id = GENL_ID_GENERATE,
1227 .hdrsize = sizeof(struct ovs_header),
1228 .name = OVS_DATAPATH_FAMILY,
1229 .version = OVS_DATAPATH_VERSION,
1230 .maxattr = OVS_DP_ATTR_MAX,
1234 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1235 .name = OVS_DATAPATH_MCGROUP
1238 static size_t ovs_dp_cmd_msg_size(void)
1240 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1242 msgsize += nla_total_size(IFNAMSIZ);
1243 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1248 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1249 u32 portid, u32 seq, u32 flags, u8 cmd)
1251 struct ovs_header *ovs_header;
1252 struct ovs_dp_stats dp_stats;
1255 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1260 ovs_header->dp_ifindex = get_dpifindex(dp);
1263 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1266 goto nla_put_failure;
1268 get_dp_stats(dp, &dp_stats);
1269 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1270 goto nla_put_failure;
1272 return genlmsg_end(skb, ovs_header);
1275 genlmsg_cancel(skb, ovs_header);
1280 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1283 struct sk_buff *skb;
1286 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1288 return ERR_PTR(-ENOMEM);
1290 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1293 return ERR_PTR(retval);
1298 /* Called with genl_mutex and optionally with RTNL lock also. */
1299 static struct datapath *lookup_datapath(struct net *net,
1300 struct ovs_header *ovs_header,
1301 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1303 struct datapath *dp;
1305 if (!a[OVS_DP_ATTR_NAME])
1306 dp = get_dp(net, ovs_header->dp_ifindex);
1308 struct vport *vport;
1311 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1312 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1315 return dp ? dp : ERR_PTR(-ENODEV);
1318 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1320 struct nlattr **a = info->attrs;
1321 struct vport_parms parms;
1322 struct sk_buff *reply;
1323 struct datapath *dp;
1324 struct vport *vport;
1325 struct ovs_net *ovs_net;
1329 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1335 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1337 goto err_unlock_rtnl;
1339 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1341 /* Allocate table. */
1343 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1347 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1348 if (!dp->stats_percpu) {
1350 goto err_destroy_table;
1353 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1357 goto err_destroy_percpu;
1360 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1361 INIT_HLIST_HEAD(&dp->ports[i]);
1363 /* Set up our datapath device. */
1364 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1365 parms.type = OVS_VPORT_TYPE_INTERNAL;
1366 parms.options = NULL;
1368 parms.port_no = OVSP_LOCAL;
1369 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1371 vport = new_vport(&parms);
1372 if (IS_ERR(vport)) {
1373 err = PTR_ERR(vport);
1377 goto err_destroy_ports_array;
1380 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1381 info->snd_seq, OVS_DP_CMD_NEW);
1382 err = PTR_ERR(reply);
1384 goto err_destroy_local_port;
1386 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1387 list_add_tail(&dp->list_node, &ovs_net->dps);
1390 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1393 err_destroy_local_port:
1394 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1395 err_destroy_ports_array:
1398 free_percpu(dp->stats_percpu);
1400 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1402 release_net(ovs_dp_get_net(dp));
1410 /* Called with genl_mutex. */
1411 static void __dp_destroy(struct datapath *dp)
1417 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1418 struct vport *vport;
1419 struct hlist_node *n;
1421 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1422 if (vport->port_no != OVSP_LOCAL)
1423 ovs_dp_detach_port(vport);
1426 list_del(&dp->list_node);
1427 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1429 /* rtnl_unlock() will wait until all the references to devices that
1430 * are pending unregistration have been dropped. We do it here to
1431 * ensure that any internal devices (which contain DP pointers) are
1432 * fully destroyed before freeing the datapath.
1436 call_rcu(&dp->rcu, destroy_dp_rcu);
1439 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1441 struct sk_buff *reply;
1442 struct datapath *dp;
1445 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1450 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1451 info->snd_seq, OVS_DP_CMD_DEL);
1452 err = PTR_ERR(reply);
1458 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1463 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1465 struct sk_buff *reply;
1466 struct datapath *dp;
1469 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1473 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1474 info->snd_seq, OVS_DP_CMD_NEW);
1475 if (IS_ERR(reply)) {
1476 err = PTR_ERR(reply);
1477 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1478 ovs_dp_datapath_multicast_group.id, err);
1482 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1487 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1489 struct sk_buff *reply;
1490 struct datapath *dp;
1492 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1496 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1497 info->snd_seq, OVS_DP_CMD_NEW);
1499 return PTR_ERR(reply);
1501 return genlmsg_reply(reply, info);
1504 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1506 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1507 struct datapath *dp;
1508 int skip = cb->args[0];
1511 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1513 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1514 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1515 OVS_DP_CMD_NEW) < 0)
1525 static struct genl_ops dp_datapath_genl_ops[] = {
1526 { .cmd = OVS_DP_CMD_NEW,
1527 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1528 .policy = datapath_policy,
1529 .doit = ovs_dp_cmd_new
1531 { .cmd = OVS_DP_CMD_DEL,
1532 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1533 .policy = datapath_policy,
1534 .doit = ovs_dp_cmd_del
1536 { .cmd = OVS_DP_CMD_GET,
1537 .flags = 0, /* OK for unprivileged users. */
1538 .policy = datapath_policy,
1539 .doit = ovs_dp_cmd_get,
1540 .dumpit = ovs_dp_cmd_dump
1542 { .cmd = OVS_DP_CMD_SET,
1543 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1544 .policy = datapath_policy,
1545 .doit = ovs_dp_cmd_set,
1549 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1550 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1551 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1552 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1553 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1554 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1555 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1558 static struct genl_family dp_vport_genl_family = {
1559 .id = GENL_ID_GENERATE,
1560 .hdrsize = sizeof(struct ovs_header),
1561 .name = OVS_VPORT_FAMILY,
1562 .version = OVS_VPORT_VERSION,
1563 .maxattr = OVS_VPORT_ATTR_MAX,
1567 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1568 .name = OVS_VPORT_MCGROUP
1571 /* Called with RTNL lock or RCU read lock. */
1572 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1573 u32 portid, u32 seq, u32 flags, u8 cmd)
1575 struct ovs_header *ovs_header;
1576 struct ovs_vport_stats vport_stats;
1579 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1584 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1586 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1587 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1588 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1589 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1590 goto nla_put_failure;
1592 ovs_vport_get_stats(vport, &vport_stats);
1593 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1595 goto nla_put_failure;
1597 err = ovs_vport_get_options(vport, skb);
1598 if (err == -EMSGSIZE)
1601 return genlmsg_end(skb, ovs_header);
1606 genlmsg_cancel(skb, ovs_header);
1610 /* Called with RTNL lock or RCU read lock. */
1611 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1614 struct sk_buff *skb;
1617 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1619 return ERR_PTR(-ENOMEM);
1621 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1624 return ERR_PTR(retval);
1629 /* Called with RTNL lock or RCU read lock. */
1630 static struct vport *lookup_vport(struct net *net,
1631 struct ovs_header *ovs_header,
1632 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1634 struct datapath *dp;
1635 struct vport *vport;
1637 if (a[OVS_VPORT_ATTR_NAME]) {
1638 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1640 return ERR_PTR(-ENODEV);
1641 if (ovs_header->dp_ifindex &&
1642 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1643 return ERR_PTR(-ENODEV);
1645 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1646 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1648 if (port_no >= DP_MAX_PORTS)
1649 return ERR_PTR(-EFBIG);
1651 dp = get_dp(net, ovs_header->dp_ifindex);
1653 return ERR_PTR(-ENODEV);
1655 vport = ovs_vport_rtnl_rcu(dp, port_no);
1657 return ERR_PTR(-ENODEV);
1660 return ERR_PTR(-EINVAL);
1663 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1665 struct nlattr **a = info->attrs;
1666 struct ovs_header *ovs_header = info->userhdr;
1667 struct vport_parms parms;
1668 struct sk_buff *reply;
1669 struct vport *vport;
1670 struct datapath *dp;
1675 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1676 !a[OVS_VPORT_ATTR_UPCALL_PID])
1680 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1685 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1686 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1689 if (port_no >= DP_MAX_PORTS)
1692 vport = ovs_vport_rtnl_rcu(dp, port_no);
1697 for (port_no = 1; ; port_no++) {
1698 if (port_no >= DP_MAX_PORTS) {
1702 vport = ovs_vport_rtnl(dp, port_no);
1708 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1709 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1710 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1712 parms.port_no = port_no;
1713 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1715 vport = new_vport(&parms);
1716 err = PTR_ERR(vport);
1721 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1723 if (IS_ERR(reply)) {
1724 err = PTR_ERR(reply);
1725 ovs_dp_detach_port(vport);
1729 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1737 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1739 struct nlattr **a = info->attrs;
1740 struct sk_buff *reply;
1741 struct vport *vport;
1745 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1746 err = PTR_ERR(vport);
1751 if (a[OVS_VPORT_ATTR_TYPE] &&
1752 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1755 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1756 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1759 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1760 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1762 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1764 if (IS_ERR(reply)) {
1765 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1766 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1770 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1777 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1779 struct nlattr **a = info->attrs;
1780 struct sk_buff *reply;
1781 struct vport *vport;
1785 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1786 err = PTR_ERR(vport);
1790 if (vport->port_no == OVSP_LOCAL) {
1795 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1797 err = PTR_ERR(reply);
1802 ovs_dp_detach_port(vport);
1804 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1811 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1813 struct nlattr **a = info->attrs;
1814 struct ovs_header *ovs_header = info->userhdr;
1815 struct sk_buff *reply;
1816 struct vport *vport;
1820 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1821 err = PTR_ERR(vport);
1825 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1827 err = PTR_ERR(reply);
1833 return genlmsg_reply(reply, info);
1840 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1842 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1843 struct datapath *dp;
1844 int bucket = cb->args[0], skip = cb->args[1];
1847 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1852 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1853 struct vport *vport;
1856 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1858 ovs_vport_cmd_fill_info(vport, skb,
1859 NETLINK_CB(cb->skb).portid,
1862 OVS_VPORT_CMD_NEW) < 0)
1878 static struct genl_ops dp_vport_genl_ops[] = {
1879 { .cmd = OVS_VPORT_CMD_NEW,
1880 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1881 .policy = vport_policy,
1882 .doit = ovs_vport_cmd_new
1884 { .cmd = OVS_VPORT_CMD_DEL,
1885 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1886 .policy = vport_policy,
1887 .doit = ovs_vport_cmd_del
1889 { .cmd = OVS_VPORT_CMD_GET,
1890 .flags = 0, /* OK for unprivileged users. */
1891 .policy = vport_policy,
1892 .doit = ovs_vport_cmd_get,
1893 .dumpit = ovs_vport_cmd_dump
1895 { .cmd = OVS_VPORT_CMD_SET,
1896 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1897 .policy = vport_policy,
1898 .doit = ovs_vport_cmd_set,
1902 struct genl_family_and_ops {
1903 struct genl_family *family;
1904 struct genl_ops *ops;
1906 struct genl_multicast_group *group;
1909 static const struct genl_family_and_ops dp_genl_families[] = {
1910 { &dp_datapath_genl_family,
1911 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1912 &ovs_dp_datapath_multicast_group },
1913 { &dp_vport_genl_family,
1914 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1915 &ovs_dp_vport_multicast_group },
1916 { &dp_flow_genl_family,
1917 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1918 &ovs_dp_flow_multicast_group },
1919 { &dp_packet_genl_family,
1920 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1924 static void dp_unregister_genl(int n_families)
1928 for (i = 0; i < n_families; i++)
1929 genl_unregister_family(dp_genl_families[i].family);
1932 static int dp_register_genl(void)
1939 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1940 const struct genl_family_and_ops *f = &dp_genl_families[i];
1942 err = genl_register_family_with_ops(f->family, f->ops,
1949 err = genl_register_mc_group(f->family, f->group);
1958 dp_unregister_genl(n_registered);
1962 static void rehash_flow_table(struct work_struct *work)
1964 struct datapath *dp;
1970 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1972 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1973 struct flow_table *old_table = genl_dereference(dp->table);
1974 struct flow_table *new_table;
1976 new_table = ovs_flow_tbl_rehash(old_table);
1977 if (!IS_ERR(new_table)) {
1978 rcu_assign_pointer(dp->table, new_table);
1979 ovs_flow_tbl_deferred_destroy(old_table);
1986 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
1989 static int __net_init ovs_init_net(struct net *net)
1991 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1993 INIT_LIST_HEAD(&ovs_net->dps);
1997 static void __net_exit ovs_exit_net(struct net *net)
1999 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2000 struct datapath *dp, *dp_next;
2003 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2008 static struct pernet_operations ovs_net_ops = {
2009 .init = ovs_init_net,
2010 .exit = ovs_exit_net,
2012 .size = sizeof(struct ovs_net),
2015 static int __init dp_init(void)
2019 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2021 pr_info("Open vSwitch switching datapath\n");
2023 err = ovs_flow_init();
2027 err = ovs_vport_init();
2029 goto error_flow_exit;
2031 err = register_pernet_device(&ovs_net_ops);
2033 goto error_vport_exit;
2035 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2037 goto error_netns_exit;
2039 err = dp_register_genl();
2041 goto error_unreg_notifier;
2043 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2047 error_unreg_notifier:
2048 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2050 unregister_pernet_device(&ovs_net_ops);
2059 static void dp_cleanup(void)
2061 cancel_delayed_work_sync(&rehash_flow_wq);
2062 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2063 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2064 unregister_pernet_device(&ovs_net_ops);
2070 module_init(dp_init);
2071 module_exit(dp_cleanup);
2073 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2074 MODULE_LICENSE("GPL");