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);
77 * Writes to device state (add/remove datapath, port, set operations on vports,
78 * etc.) are protected by RTNL.
80 * Writes to other state (flow table modifications, set miscellaneous datapath
81 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
84 * Reads are protected by RCU.
86 * There are a few special cases (mostly stats) that have their own
87 * synchronization but they nest under all of above and don't interact with
91 static struct vport *new_vport(const struct vport_parms *);
92 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
93 const struct dp_upcall_info *);
94 static int queue_userspace_packet(struct net *, int dp_ifindex,
96 const struct dp_upcall_info *);
98 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
99 static struct datapath *get_dp(struct net *net, int dp_ifindex)
101 struct datapath *dp = NULL;
102 struct net_device *dev;
105 dev = dev_get_by_index_rcu(net, dp_ifindex);
107 struct vport *vport = ovs_internal_dev_get_vport(dev);
116 /* Must be called with rcu_read_lock or RTNL lock. */
117 const char *ovs_dp_name(const struct datapath *dp)
119 struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
120 return vport->ops->get_name(vport);
123 static int get_dpifindex(struct datapath *dp)
130 local = rcu_dereference(dp->ports[OVSP_LOCAL]);
132 ifindex = local->ops->get_ifindex(local);
141 static void destroy_dp_rcu(struct rcu_head *rcu)
143 struct datapath *dp = container_of(rcu, struct datapath, rcu);
145 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
146 free_percpu(dp->stats_percpu);
147 release_net(ovs_dp_get_net(dp));
151 /* Called with RTNL lock and genl_lock. */
152 static struct vport *new_vport(const struct vport_parms *parms)
156 vport = ovs_vport_add(parms);
157 if (!IS_ERR(vport)) {
158 struct datapath *dp = parms->dp;
160 rcu_assign_pointer(dp->ports[parms->port_no], vport);
161 list_add(&vport->node, &dp->port_list);
167 /* Called with RTNL lock. */
168 void ovs_dp_detach_port(struct vport *p)
172 /* First drop references to device. */
174 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
176 /* Then destroy it. */
180 /* Must be called with rcu_read_lock. */
181 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
183 struct datapath *dp = p->dp;
184 struct sw_flow *flow;
185 struct dp_stats_percpu *stats;
186 struct sw_flow_key key;
191 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
193 /* Extract flow from 'skb' into 'key'. */
194 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
195 if (unlikely(error)) {
201 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
202 if (unlikely(!flow)) {
203 struct dp_upcall_info upcall;
205 upcall.cmd = OVS_PACKET_CMD_MISS;
207 upcall.userdata = NULL;
208 upcall.pid = p->upcall_pid;
209 ovs_dp_upcall(dp, skb, &upcall);
211 stats_counter = &stats->n_missed;
215 OVS_CB(skb)->flow = flow;
217 stats_counter = &stats->n_hit;
218 ovs_flow_used(OVS_CB(skb)->flow, skb);
219 ovs_execute_actions(dp, skb);
222 /* Update datapath statistics. */
223 u64_stats_update_begin(&stats->sync);
225 u64_stats_update_end(&stats->sync);
228 static struct genl_family dp_packet_genl_family = {
229 .id = GENL_ID_GENERATE,
230 .hdrsize = sizeof(struct ovs_header),
231 .name = OVS_PACKET_FAMILY,
232 .version = OVS_PACKET_VERSION,
233 .maxattr = OVS_PACKET_ATTR_MAX,
237 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
238 const struct dp_upcall_info *upcall_info)
240 struct dp_stats_percpu *stats;
244 if (upcall_info->pid == 0) {
249 dp_ifindex = get_dpifindex(dp);
255 if (!skb_is_gso(skb))
256 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
258 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
265 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
267 u64_stats_update_begin(&stats->sync);
269 u64_stats_update_end(&stats->sync);
274 static int queue_gso_packets(struct net *net, int dp_ifindex,
276 const struct dp_upcall_info *upcall_info)
278 unsigned short gso_type = skb_shinfo(skb)->gso_type;
279 struct dp_upcall_info later_info;
280 struct sw_flow_key later_key;
281 struct sk_buff *segs, *nskb;
284 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
286 return PTR_ERR(segs);
288 /* Queue all of the segments. */
291 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
295 if (skb == segs && gso_type & SKB_GSO_UDP) {
296 /* The initial flow key extracted by ovs_flow_extract()
297 * in this case is for a first fragment, so we need to
298 * properly mark later fragments.
300 later_key = *upcall_info->key;
301 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
303 later_info = *upcall_info;
304 later_info.key = &later_key;
305 upcall_info = &later_info;
307 } while ((skb = skb->next));
309 /* Free all of the segments. */
317 } while ((skb = nskb));
321 static int queue_userspace_packet(struct net *net, int dp_ifindex,
323 const struct dp_upcall_info *upcall_info)
325 struct ovs_header *upcall;
326 struct sk_buff *nskb = NULL;
327 struct sk_buff *user_skb; /* to be queued to userspace */
332 if (vlan_tx_tag_present(skb)) {
333 nskb = skb_clone(skb, GFP_ATOMIC);
337 nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
345 if (nla_attr_size(skb->len) > USHRT_MAX) {
350 len = sizeof(struct ovs_header);
351 len += nla_total_size(skb->len);
352 len += nla_total_size(FLOW_BUFSIZE);
353 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
354 len += nla_total_size(8);
356 user_skb = genlmsg_new(len, GFP_ATOMIC);
362 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
363 0, upcall_info->cmd);
364 upcall->dp_ifindex = dp_ifindex;
366 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
367 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
368 nla_nest_end(user_skb, nla);
370 if (upcall_info->userdata)
371 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
372 nla_get_u64(upcall_info->userdata));
374 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
376 skb_copy_and_csum_dev(skb, nla_data(nla));
378 err = genlmsg_unicast(net, user_skb, upcall_info->pid);
385 /* Called with genl_mutex. */
386 static int flush_flows(struct datapath *dp)
388 struct flow_table *old_table;
389 struct flow_table *new_table;
391 old_table = genl_dereference(dp->table);
392 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
396 rcu_assign_pointer(dp->table, new_table);
398 ovs_flow_tbl_deferred_destroy(old_table);
402 static int validate_actions(const struct nlattr *attr,
403 const struct sw_flow_key *key, int depth);
405 static int validate_sample(const struct nlattr *attr,
406 const struct sw_flow_key *key, int depth)
408 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
409 const struct nlattr *probability, *actions;
410 const struct nlattr *a;
413 memset(attrs, 0, sizeof(attrs));
414 nla_for_each_nested(a, attr, rem) {
415 int type = nla_type(a);
416 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
423 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
424 if (!probability || nla_len(probability) != sizeof(u32))
427 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
428 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
430 return validate_actions(actions, key, depth + 1);
433 static int validate_tp_port(const struct sw_flow_key *flow_key)
435 if (flow_key->eth.type == htons(ETH_P_IP)) {
436 if (flow_key->ipv4.tp.src && flow_key->ipv4.tp.dst)
438 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
439 if (flow_key->ipv6.tp.src && flow_key->ipv6.tp.dst)
446 static int validate_set(const struct nlattr *a,
447 const struct sw_flow_key *flow_key)
449 const struct nlattr *ovs_key = nla_data(a);
450 int key_type = nla_type(ovs_key);
452 /* There can be only one key in a action */
453 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
456 if (key_type > OVS_KEY_ATTR_MAX ||
457 nla_len(ovs_key) != ovs_key_lens[key_type])
461 const struct ovs_key_ipv4 *ipv4_key;
463 case OVS_KEY_ATTR_PRIORITY:
464 case OVS_KEY_ATTR_ETHERNET:
467 case OVS_KEY_ATTR_IPV4:
468 if (flow_key->eth.type != htons(ETH_P_IP))
471 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
474 ipv4_key = nla_data(ovs_key);
475 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
478 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
483 case OVS_KEY_ATTR_TCP:
484 if (flow_key->ip.proto != IPPROTO_TCP)
487 return validate_tp_port(flow_key);
489 case OVS_KEY_ATTR_UDP:
490 if (flow_key->ip.proto != IPPROTO_UDP)
493 return validate_tp_port(flow_key);
502 static int validate_userspace(const struct nlattr *attr)
504 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
505 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
506 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
508 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
511 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
512 attr, userspace_policy);
516 if (!a[OVS_USERSPACE_ATTR_PID] ||
517 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
523 static int validate_actions(const struct nlattr *attr,
524 const struct sw_flow_key *key, int depth)
526 const struct nlattr *a;
529 if (depth >= SAMPLE_ACTION_DEPTH)
532 nla_for_each_nested(a, attr, rem) {
533 /* Expected argument lengths, (u32)-1 for variable length. */
534 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
535 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
536 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
537 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
538 [OVS_ACTION_ATTR_POP_VLAN] = 0,
539 [OVS_ACTION_ATTR_SET] = (u32)-1,
540 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
542 const struct ovs_action_push_vlan *vlan;
543 int type = nla_type(a);
545 if (type > OVS_ACTION_ATTR_MAX ||
546 (action_lens[type] != nla_len(a) &&
547 action_lens[type] != (u32)-1))
551 case OVS_ACTION_ATTR_UNSPEC:
554 case OVS_ACTION_ATTR_USERSPACE:
555 err = validate_userspace(a);
560 case OVS_ACTION_ATTR_OUTPUT:
561 if (nla_get_u32(a) >= DP_MAX_PORTS)
566 case OVS_ACTION_ATTR_POP_VLAN:
569 case OVS_ACTION_ATTR_PUSH_VLAN:
571 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
573 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
577 case OVS_ACTION_ATTR_SET:
578 err = validate_set(a, key);
583 case OVS_ACTION_ATTR_SAMPLE:
584 err = validate_sample(a, key, depth);
600 static void clear_stats(struct sw_flow *flow)
604 flow->packet_count = 0;
605 flow->byte_count = 0;
608 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
610 struct ovs_header *ovs_header = info->userhdr;
611 struct nlattr **a = info->attrs;
612 struct sw_flow_actions *acts;
613 struct sk_buff *packet;
614 struct sw_flow *flow;
622 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
623 !a[OVS_PACKET_ATTR_ACTIONS] ||
624 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
627 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
628 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
632 skb_reserve(packet, NET_IP_ALIGN);
634 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
636 skb_reset_mac_header(packet);
637 eth = eth_hdr(packet);
639 /* Normally, setting the skb 'protocol' field would be handled by a
640 * call to eth_type_trans(), but it assumes there's a sending
641 * device, which we may not have. */
642 if (ntohs(eth->h_proto) >= 1536)
643 packet->protocol = eth->h_proto;
645 packet->protocol = htons(ETH_P_802_2);
647 /* Build an sw_flow for sending this packet. */
648 flow = ovs_flow_alloc();
653 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
657 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
658 &flow->key.phy.in_port,
659 a[OVS_PACKET_ATTR_KEY]);
663 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
667 flow->hash = ovs_flow_hash(&flow->key, key_len);
669 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
673 rcu_assign_pointer(flow->sf_acts, acts);
675 OVS_CB(packet)->flow = flow;
676 packet->priority = flow->key.phy.priority;
679 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
685 err = ovs_execute_actions(dp, packet);
702 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
703 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
704 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
705 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
708 static struct genl_ops dp_packet_genl_ops[] = {
709 { .cmd = OVS_PACKET_CMD_EXECUTE,
710 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
711 .policy = packet_policy,
712 .doit = ovs_packet_cmd_execute
716 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
719 struct flow_table *table = genl_dereference(dp->table);
721 stats->n_flows = ovs_flow_tbl_count(table);
723 stats->n_hit = stats->n_missed = stats->n_lost = 0;
724 for_each_possible_cpu(i) {
725 const struct dp_stats_percpu *percpu_stats;
726 struct dp_stats_percpu local_stats;
729 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
732 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
733 local_stats = *percpu_stats;
734 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
736 stats->n_hit += local_stats.n_hit;
737 stats->n_missed += local_stats.n_missed;
738 stats->n_lost += local_stats.n_lost;
742 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
743 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
744 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
745 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
748 static struct genl_family dp_flow_genl_family = {
749 .id = GENL_ID_GENERATE,
750 .hdrsize = sizeof(struct ovs_header),
751 .name = OVS_FLOW_FAMILY,
752 .version = OVS_FLOW_VERSION,
753 .maxattr = OVS_FLOW_ATTR_MAX,
757 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
758 .name = OVS_FLOW_MCGROUP
761 /* Called with genl_lock. */
762 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
763 struct sk_buff *skb, u32 pid,
764 u32 seq, u32 flags, u8 cmd)
766 const int skb_orig_len = skb->len;
767 const struct sw_flow_actions *sf_acts;
768 struct ovs_flow_stats stats;
769 struct ovs_header *ovs_header;
775 sf_acts = rcu_dereference_protected(flow->sf_acts,
776 lockdep_genl_is_held());
778 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
782 ovs_header->dp_ifindex = get_dpifindex(dp);
784 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
786 goto nla_put_failure;
787 err = ovs_flow_to_nlattrs(&flow->key, skb);
790 nla_nest_end(skb, nla);
792 spin_lock_bh(&flow->lock);
794 stats.n_packets = flow->packet_count;
795 stats.n_bytes = flow->byte_count;
796 tcp_flags = flow->tcp_flags;
797 spin_unlock_bh(&flow->lock);
800 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
801 goto nla_put_failure;
803 if (stats.n_packets &&
804 nla_put(skb, OVS_FLOW_ATTR_STATS,
805 sizeof(struct ovs_flow_stats), &stats))
806 goto nla_put_failure;
809 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
810 goto nla_put_failure;
812 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
813 * this is the first flow to be dumped into 'skb'. This is unusual for
814 * Netlink but individual action lists can be longer than
815 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
816 * The userspace caller can always fetch the actions separately if it
817 * really wants them. (Most userspace callers in fact don't care.)
819 * This can only fail for dump operations because the skb is always
820 * properly sized for single flows.
822 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
824 if (err < 0 && skb_orig_len)
827 return genlmsg_end(skb, ovs_header);
832 genlmsg_cancel(skb, ovs_header);
836 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
838 const struct sw_flow_actions *sf_acts;
841 sf_acts = rcu_dereference_protected(flow->sf_acts,
842 lockdep_genl_is_held());
844 /* OVS_FLOW_ATTR_KEY */
845 len = nla_total_size(FLOW_BUFSIZE);
846 /* OVS_FLOW_ATTR_ACTIONS */
847 len += nla_total_size(sf_acts->actions_len);
848 /* OVS_FLOW_ATTR_STATS */
849 len += nla_total_size(sizeof(struct ovs_flow_stats));
850 /* OVS_FLOW_ATTR_TCP_FLAGS */
851 len += nla_total_size(1);
852 /* OVS_FLOW_ATTR_USED */
853 len += nla_total_size(8);
855 len += NLMSG_ALIGN(sizeof(struct ovs_header));
857 return genlmsg_new(len, GFP_KERNEL);
860 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
862 u32 pid, u32 seq, u8 cmd)
867 skb = ovs_flow_cmd_alloc_info(flow);
869 return ERR_PTR(-ENOMEM);
871 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
876 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
878 struct nlattr **a = info->attrs;
879 struct ovs_header *ovs_header = info->userhdr;
880 struct sw_flow_key key;
881 struct sw_flow *flow;
882 struct sk_buff *reply;
884 struct flow_table *table;
890 if (!a[OVS_FLOW_ATTR_KEY])
892 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
896 /* Validate actions. */
897 if (a[OVS_FLOW_ATTR_ACTIONS]) {
898 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
901 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
906 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
911 table = genl_dereference(dp->table);
912 flow = ovs_flow_tbl_lookup(table, &key, key_len);
914 struct sw_flow_actions *acts;
916 /* Bail out if we're not allowed to create a new flow. */
918 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
921 /* Expand table, if necessary, to make room. */
922 if (ovs_flow_tbl_need_to_expand(table)) {
923 struct flow_table *new_table;
925 new_table = ovs_flow_tbl_expand(table);
926 if (!IS_ERR(new_table)) {
927 rcu_assign_pointer(dp->table, new_table);
928 ovs_flow_tbl_deferred_destroy(table);
929 table = genl_dereference(dp->table);
934 flow = ovs_flow_alloc();
936 error = PTR_ERR(flow);
942 /* Obtain actions. */
943 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
944 error = PTR_ERR(acts);
946 goto error_free_flow;
947 rcu_assign_pointer(flow->sf_acts, acts);
949 /* Put flow in bucket. */
950 flow->hash = ovs_flow_hash(&key, key_len);
951 ovs_flow_tbl_insert(table, flow);
953 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
957 /* We found a matching flow. */
958 struct sw_flow_actions *old_acts;
959 struct nlattr *acts_attrs;
961 /* Bail out if we're not allowed to modify an existing flow.
962 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
963 * because Generic Netlink treats the latter as a dump
964 * request. We also accept NLM_F_EXCL in case that bug ever
968 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
969 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
972 /* Update actions. */
973 old_acts = rcu_dereference_protected(flow->sf_acts,
974 lockdep_genl_is_held());
975 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
977 (old_acts->actions_len != nla_len(acts_attrs) ||
978 memcmp(old_acts->actions, nla_data(acts_attrs),
979 old_acts->actions_len))) {
980 struct sw_flow_actions *new_acts;
982 new_acts = ovs_flow_actions_alloc(acts_attrs);
983 error = PTR_ERR(new_acts);
984 if (IS_ERR(new_acts))
987 rcu_assign_pointer(flow->sf_acts, new_acts);
988 ovs_flow_deferred_free_acts(old_acts);
991 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
992 info->snd_seq, OVS_FLOW_CMD_NEW);
995 if (a[OVS_FLOW_ATTR_CLEAR]) {
996 spin_lock_bh(&flow->lock);
998 spin_unlock_bh(&flow->lock);
1003 genl_notify(reply, genl_info_net(info), info->snd_pid,
1004 ovs_dp_flow_multicast_group.id, info->nlhdr,
1007 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1008 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1012 ovs_flow_free(flow);
1017 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1019 struct nlattr **a = info->attrs;
1020 struct ovs_header *ovs_header = info->userhdr;
1021 struct sw_flow_key key;
1022 struct sk_buff *reply;
1023 struct sw_flow *flow;
1024 struct datapath *dp;
1025 struct flow_table *table;
1029 if (!a[OVS_FLOW_ATTR_KEY])
1031 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1035 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1039 table = genl_dereference(dp->table);
1040 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1044 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1045 info->snd_seq, OVS_FLOW_CMD_NEW);
1047 return PTR_ERR(reply);
1049 return genlmsg_reply(reply, info);
1052 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1054 struct nlattr **a = info->attrs;
1055 struct ovs_header *ovs_header = info->userhdr;
1056 struct sw_flow_key key;
1057 struct sk_buff *reply;
1058 struct sw_flow *flow;
1059 struct datapath *dp;
1060 struct flow_table *table;
1064 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1068 if (!a[OVS_FLOW_ATTR_KEY])
1069 return flush_flows(dp);
1071 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1075 table = genl_dereference(dp->table);
1076 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1080 reply = ovs_flow_cmd_alloc_info(flow);
1084 ovs_flow_tbl_remove(table, flow);
1086 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1087 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1090 ovs_flow_deferred_free(flow);
1092 genl_notify(reply, genl_info_net(info), info->snd_pid,
1093 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1097 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1099 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1100 struct datapath *dp;
1101 struct flow_table *table;
1103 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1107 table = genl_dereference(dp->table);
1110 struct sw_flow *flow;
1113 bucket = cb->args[0];
1115 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1119 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1120 NETLINK_CB(cb->skb).pid,
1121 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1122 OVS_FLOW_CMD_NEW) < 0)
1125 cb->args[0] = bucket;
1131 static struct genl_ops dp_flow_genl_ops[] = {
1132 { .cmd = OVS_FLOW_CMD_NEW,
1133 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1134 .policy = flow_policy,
1135 .doit = ovs_flow_cmd_new_or_set
1137 { .cmd = OVS_FLOW_CMD_DEL,
1138 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1139 .policy = flow_policy,
1140 .doit = ovs_flow_cmd_del
1142 { .cmd = OVS_FLOW_CMD_GET,
1143 .flags = 0, /* OK for unprivileged users. */
1144 .policy = flow_policy,
1145 .doit = ovs_flow_cmd_get,
1146 .dumpit = ovs_flow_cmd_dump
1148 { .cmd = OVS_FLOW_CMD_SET,
1149 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1150 .policy = flow_policy,
1151 .doit = ovs_flow_cmd_new_or_set,
1155 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1156 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1157 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1160 static struct genl_family dp_datapath_genl_family = {
1161 .id = GENL_ID_GENERATE,
1162 .hdrsize = sizeof(struct ovs_header),
1163 .name = OVS_DATAPATH_FAMILY,
1164 .version = OVS_DATAPATH_VERSION,
1165 .maxattr = OVS_DP_ATTR_MAX,
1169 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1170 .name = OVS_DATAPATH_MCGROUP
1173 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1174 u32 pid, u32 seq, u32 flags, u8 cmd)
1176 struct ovs_header *ovs_header;
1177 struct ovs_dp_stats dp_stats;
1180 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1185 ovs_header->dp_ifindex = get_dpifindex(dp);
1188 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1191 goto nla_put_failure;
1193 get_dp_stats(dp, &dp_stats);
1194 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1195 goto nla_put_failure;
1197 return genlmsg_end(skb, ovs_header);
1200 genlmsg_cancel(skb, ovs_header);
1205 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1208 struct sk_buff *skb;
1211 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1213 return ERR_PTR(-ENOMEM);
1215 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1218 return ERR_PTR(retval);
1223 /* Called with genl_mutex and optionally with RTNL lock also. */
1224 static struct datapath *lookup_datapath(struct net *net,
1225 struct ovs_header *ovs_header,
1226 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1228 struct datapath *dp;
1230 if (!a[OVS_DP_ATTR_NAME])
1231 dp = get_dp(net, ovs_header->dp_ifindex);
1233 struct vport *vport;
1236 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1237 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1240 return dp ? dp : ERR_PTR(-ENODEV);
1243 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1245 struct nlattr **a = info->attrs;
1246 struct vport_parms parms;
1247 struct sk_buff *reply;
1248 struct datapath *dp;
1249 struct vport *vport;
1250 struct ovs_net *ovs_net;
1254 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1260 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1262 goto err_unlock_rtnl;
1264 INIT_LIST_HEAD(&dp->port_list);
1265 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1267 /* Allocate table. */
1269 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1273 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1274 if (!dp->stats_percpu) {
1276 goto err_destroy_table;
1279 /* Set up our datapath device. */
1280 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1281 parms.type = OVS_VPORT_TYPE_INTERNAL;
1282 parms.options = NULL;
1284 parms.port_no = OVSP_LOCAL;
1285 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1287 vport = new_vport(&parms);
1288 if (IS_ERR(vport)) {
1289 err = PTR_ERR(vport);
1293 goto err_destroy_percpu;
1296 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1297 info->snd_seq, OVS_DP_CMD_NEW);
1298 err = PTR_ERR(reply);
1300 goto err_destroy_local_port;
1302 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1303 list_add_tail(&dp->list_node, &ovs_net->dps);
1306 genl_notify(reply, genl_info_net(info), info->snd_pid,
1307 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1311 err_destroy_local_port:
1312 ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1314 free_percpu(dp->stats_percpu);
1316 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1318 release_net(ovs_dp_get_net(dp));
1326 /* Called with genl_mutex. */
1327 static void __dp_destroy(struct datapath *dp)
1329 struct vport *vport, *next_vport;
1332 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1333 if (vport->port_no != OVSP_LOCAL)
1334 ovs_dp_detach_port(vport);
1336 list_del(&dp->list_node);
1337 ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1339 /* rtnl_unlock() will wait until all the references to devices that
1340 * are pending unregistration have been dropped. We do it here to
1341 * ensure that any internal devices (which contain DP pointers) are
1342 * fully destroyed before freeing the datapath.
1346 call_rcu(&dp->rcu, destroy_dp_rcu);
1349 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1351 struct sk_buff *reply;
1352 struct datapath *dp;
1355 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1360 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1361 info->snd_seq, OVS_DP_CMD_DEL);
1362 err = PTR_ERR(reply);
1368 genl_notify(reply, genl_info_net(info), info->snd_pid,
1369 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1375 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1377 struct sk_buff *reply;
1378 struct datapath *dp;
1381 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1385 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1386 info->snd_seq, OVS_DP_CMD_NEW);
1387 if (IS_ERR(reply)) {
1388 err = PTR_ERR(reply);
1389 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1390 ovs_dp_datapath_multicast_group.id, err);
1394 genl_notify(reply, genl_info_net(info), info->snd_pid,
1395 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1401 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1403 struct sk_buff *reply;
1404 struct datapath *dp;
1406 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1410 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1411 info->snd_seq, OVS_DP_CMD_NEW);
1413 return PTR_ERR(reply);
1415 return genlmsg_reply(reply, info);
1418 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1420 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1421 struct datapath *dp;
1422 int skip = cb->args[0];
1425 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1427 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1428 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1429 OVS_DP_CMD_NEW) < 0)
1439 static struct genl_ops dp_datapath_genl_ops[] = {
1440 { .cmd = OVS_DP_CMD_NEW,
1441 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1442 .policy = datapath_policy,
1443 .doit = ovs_dp_cmd_new
1445 { .cmd = OVS_DP_CMD_DEL,
1446 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1447 .policy = datapath_policy,
1448 .doit = ovs_dp_cmd_del
1450 { .cmd = OVS_DP_CMD_GET,
1451 .flags = 0, /* OK for unprivileged users. */
1452 .policy = datapath_policy,
1453 .doit = ovs_dp_cmd_get,
1454 .dumpit = ovs_dp_cmd_dump
1456 { .cmd = OVS_DP_CMD_SET,
1457 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1458 .policy = datapath_policy,
1459 .doit = ovs_dp_cmd_set,
1463 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1464 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1465 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1466 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1467 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1468 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1469 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1472 static struct genl_family dp_vport_genl_family = {
1473 .id = GENL_ID_GENERATE,
1474 .hdrsize = sizeof(struct ovs_header),
1475 .name = OVS_VPORT_FAMILY,
1476 .version = OVS_VPORT_VERSION,
1477 .maxattr = OVS_VPORT_ATTR_MAX,
1481 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1482 .name = OVS_VPORT_MCGROUP
1485 /* Called with RTNL lock or RCU read lock. */
1486 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1487 u32 pid, u32 seq, u32 flags, u8 cmd)
1489 struct ovs_header *ovs_header;
1490 struct ovs_vport_stats vport_stats;
1493 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1498 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1500 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1501 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1502 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1503 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid))
1504 goto nla_put_failure;
1506 ovs_vport_get_stats(vport, &vport_stats);
1507 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1509 goto nla_put_failure;
1511 err = ovs_vport_get_options(vport, skb);
1512 if (err == -EMSGSIZE)
1515 return genlmsg_end(skb, ovs_header);
1520 genlmsg_cancel(skb, ovs_header);
1524 /* Called with RTNL lock or RCU read lock. */
1525 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1528 struct sk_buff *skb;
1531 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1533 return ERR_PTR(-ENOMEM);
1535 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1538 return ERR_PTR(retval);
1543 /* Called with RTNL lock or RCU read lock. */
1544 static struct vport *lookup_vport(struct net *net,
1545 struct ovs_header *ovs_header,
1546 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1548 struct datapath *dp;
1549 struct vport *vport;
1551 if (a[OVS_VPORT_ATTR_NAME]) {
1552 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1554 return ERR_PTR(-ENODEV);
1555 if (ovs_header->dp_ifindex &&
1556 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1557 return ERR_PTR(-ENODEV);
1559 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1560 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1562 if (port_no >= DP_MAX_PORTS)
1563 return ERR_PTR(-EFBIG);
1565 dp = get_dp(net, ovs_header->dp_ifindex);
1567 return ERR_PTR(-ENODEV);
1569 vport = rcu_dereference_rtnl(dp->ports[port_no]);
1571 return ERR_PTR(-ENOENT);
1574 return ERR_PTR(-EINVAL);
1577 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1579 struct nlattr **a = info->attrs;
1580 struct ovs_header *ovs_header = info->userhdr;
1581 struct vport_parms parms;
1582 struct sk_buff *reply;
1583 struct vport *vport;
1584 struct datapath *dp;
1589 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1590 !a[OVS_VPORT_ATTR_UPCALL_PID])
1594 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1599 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1600 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1603 if (port_no >= DP_MAX_PORTS)
1606 vport = rtnl_dereference(dp->ports[port_no]);
1611 for (port_no = 1; ; port_no++) {
1612 if (port_no >= DP_MAX_PORTS) {
1616 vport = rtnl_dereference(dp->ports[port_no]);
1622 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1623 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1624 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1626 parms.port_no = port_no;
1627 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1629 vport = new_vport(&parms);
1630 err = PTR_ERR(vport);
1634 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1636 if (IS_ERR(reply)) {
1637 err = PTR_ERR(reply);
1638 ovs_dp_detach_port(vport);
1641 genl_notify(reply, genl_info_net(info), info->snd_pid,
1642 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1650 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1652 struct nlattr **a = info->attrs;
1653 struct sk_buff *reply;
1654 struct vport *vport;
1658 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1659 err = PTR_ERR(vport);
1664 if (a[OVS_VPORT_ATTR_TYPE] &&
1665 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1668 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1669 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1672 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1673 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1675 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1677 if (IS_ERR(reply)) {
1678 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1679 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1683 genl_notify(reply, genl_info_net(info), info->snd_pid,
1684 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1691 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1693 struct nlattr **a = info->attrs;
1694 struct sk_buff *reply;
1695 struct vport *vport;
1699 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1700 err = PTR_ERR(vport);
1704 if (vport->port_no == OVSP_LOCAL) {
1709 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1711 err = PTR_ERR(reply);
1715 ovs_dp_detach_port(vport);
1717 genl_notify(reply, genl_info_net(info), info->snd_pid,
1718 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1725 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1727 struct nlattr **a = info->attrs;
1728 struct ovs_header *ovs_header = info->userhdr;
1729 struct sk_buff *reply;
1730 struct vport *vport;
1734 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1735 err = PTR_ERR(vport);
1739 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1741 err = PTR_ERR(reply);
1747 return genlmsg_reply(reply, info);
1754 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1756 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1757 struct datapath *dp;
1761 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1766 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1767 struct vport *vport;
1769 vport = rcu_dereference(dp->ports[port_no]);
1773 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1774 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1775 OVS_VPORT_CMD_NEW) < 0)
1780 cb->args[0] = port_no;
1786 static struct genl_ops dp_vport_genl_ops[] = {
1787 { .cmd = OVS_VPORT_CMD_NEW,
1788 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1789 .policy = vport_policy,
1790 .doit = ovs_vport_cmd_new
1792 { .cmd = OVS_VPORT_CMD_DEL,
1793 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1794 .policy = vport_policy,
1795 .doit = ovs_vport_cmd_del
1797 { .cmd = OVS_VPORT_CMD_GET,
1798 .flags = 0, /* OK for unprivileged users. */
1799 .policy = vport_policy,
1800 .doit = ovs_vport_cmd_get,
1801 .dumpit = ovs_vport_cmd_dump
1803 { .cmd = OVS_VPORT_CMD_SET,
1804 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1805 .policy = vport_policy,
1806 .doit = ovs_vport_cmd_set,
1810 struct genl_family_and_ops {
1811 struct genl_family *family;
1812 struct genl_ops *ops;
1814 struct genl_multicast_group *group;
1817 static const struct genl_family_and_ops dp_genl_families[] = {
1818 { &dp_datapath_genl_family,
1819 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1820 &ovs_dp_datapath_multicast_group },
1821 { &dp_vport_genl_family,
1822 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1823 &ovs_dp_vport_multicast_group },
1824 { &dp_flow_genl_family,
1825 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1826 &ovs_dp_flow_multicast_group },
1827 { &dp_packet_genl_family,
1828 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1832 static void dp_unregister_genl(int n_families)
1836 for (i = 0; i < n_families; i++)
1837 genl_unregister_family(dp_genl_families[i].family);
1840 static int dp_register_genl(void)
1847 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1848 const struct genl_family_and_ops *f = &dp_genl_families[i];
1850 err = genl_register_family_with_ops(f->family, f->ops,
1857 err = genl_register_mc_group(f->family, f->group);
1866 dp_unregister_genl(n_registered);
1870 static void rehash_flow_table(struct work_struct *work)
1872 struct datapath *dp;
1878 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1880 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1881 struct flow_table *old_table = genl_dereference(dp->table);
1882 struct flow_table *new_table;
1884 new_table = ovs_flow_tbl_rehash(old_table);
1885 if (!IS_ERR(new_table)) {
1886 rcu_assign_pointer(dp->table, new_table);
1887 ovs_flow_tbl_deferred_destroy(old_table);
1894 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
1897 static int __net_init ovs_init_net(struct net *net)
1899 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1901 INIT_LIST_HEAD(&ovs_net->dps);
1905 static void __net_exit ovs_exit_net(struct net *net)
1907 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1908 struct datapath *dp, *dp_next;
1911 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1916 static struct pernet_operations ovs_net_ops = {
1917 .init = ovs_init_net,
1918 .exit = ovs_exit_net,
1920 .size = sizeof(struct ovs_net),
1923 static int __init dp_init(void)
1925 struct sk_buff *dummy_skb;
1928 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
1930 pr_info("Open vSwitch switching datapath\n");
1932 err = ovs_flow_init();
1936 err = ovs_vport_init();
1938 goto error_flow_exit;
1940 err = register_pernet_device(&ovs_net_ops);
1942 goto error_vport_exit;
1944 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1946 goto error_netns_exit;
1948 err = dp_register_genl();
1950 goto error_unreg_notifier;
1952 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
1956 error_unreg_notifier:
1957 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1959 unregister_pernet_device(&ovs_net_ops);
1968 static void dp_cleanup(void)
1970 cancel_delayed_work_sync(&rehash_flow_wq);
1971 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1972 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1973 unregister_pernet_device(&ovs_net_ops);
1979 module_init(dp_init);
1980 module_exit(dp_cleanup);
1982 MODULE_DESCRIPTION("Open vSwitch switching datapath");
1983 MODULE_LICENSE("GPL");