2 * Copyright (c) 2007-2011 Nicira Networks.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/system.h>
43 #include <asm/div64.h>
44 #include <linux/highmem.h>
45 #include <linux/netfilter_bridge.h>
46 #include <linux/netfilter_ipv4.h>
47 #include <linux/inetdevice.h>
48 #include <linux/list.h>
49 #include <linux/openvswitch.h>
50 #include <linux/rculist.h>
51 #include <linux/dmi.h>
52 #include <linux/workqueue.h>
53 #include <net/genetlink.h>
57 #include "vport-internal_dev.h"
62 * Writes to device state (add/remove datapath, port, set operations on vports,
63 * etc.) are protected by RTNL.
65 * Writes to other state (flow table modifications, set miscellaneous datapath
66 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
69 * Reads are protected by RCU.
71 * There are a few special cases (mostly stats) that have their own
72 * synchronization but they nest under all of above and don't interact with
76 /* Global list of datapaths to enable dumping them all out.
77 * Protected by genl_mutex.
79 static LIST_HEAD(dps);
81 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
82 static void rehash_flow_table(struct work_struct *work);
83 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
85 static struct vport *new_vport(const struct vport_parms *);
86 static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
87 const struct dp_upcall_info *);
88 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
89 const struct dp_upcall_info *);
91 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
92 static struct datapath *get_dp(int dp_ifindex)
94 struct datapath *dp = NULL;
95 struct net_device *dev;
98 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
100 struct vport *vport = ovs_internal_dev_get_vport(dev);
109 /* Must be called with rcu_read_lock or RTNL lock. */
110 const char *ovs_dp_name(const struct datapath *dp)
112 struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
113 return vport->ops->get_name(vport);
116 static int get_dpifindex(struct datapath *dp)
123 local = rcu_dereference(dp->ports[OVSP_LOCAL]);
125 ifindex = local->ops->get_ifindex(local);
134 static void destroy_dp_rcu(struct rcu_head *rcu)
136 struct datapath *dp = container_of(rcu, struct datapath, rcu);
138 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
139 free_percpu(dp->stats_percpu);
143 /* Called with RTNL lock and genl_lock. */
144 static struct vport *new_vport(const struct vport_parms *parms)
148 vport = ovs_vport_add(parms);
149 if (!IS_ERR(vport)) {
150 struct datapath *dp = parms->dp;
152 rcu_assign_pointer(dp->ports[parms->port_no], vport);
153 list_add(&vport->node, &dp->port_list);
159 /* Called with RTNL lock. */
160 void ovs_dp_detach_port(struct vport *p)
164 /* First drop references to device. */
166 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
168 /* Then destroy it. */
172 /* Must be called with rcu_read_lock. */
173 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
175 struct datapath *dp = p->dp;
176 struct sw_flow *flow;
177 struct dp_stats_percpu *stats;
178 struct sw_flow_key key;
183 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
185 /* Extract flow from 'skb' into 'key'. */
186 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
187 if (unlikely(error)) {
193 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
194 if (unlikely(!flow)) {
195 struct dp_upcall_info upcall;
197 upcall.cmd = OVS_PACKET_CMD_MISS;
199 upcall.userdata = NULL;
200 upcall.pid = p->upcall_pid;
201 ovs_dp_upcall(dp, skb, &upcall);
203 stats_counter = &stats->n_missed;
207 OVS_CB(skb)->flow = flow;
209 stats_counter = &stats->n_hit;
210 ovs_flow_used(OVS_CB(skb)->flow, skb);
211 ovs_execute_actions(dp, skb);
214 /* Update datapath statistics. */
215 u64_stats_update_begin(&stats->sync);
217 u64_stats_update_end(&stats->sync);
220 static struct genl_family dp_packet_genl_family = {
221 .id = GENL_ID_GENERATE,
222 .hdrsize = sizeof(struct ovs_header),
223 .name = OVS_PACKET_FAMILY,
224 .version = OVS_PACKET_VERSION,
225 .maxattr = OVS_PACKET_ATTR_MAX
228 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
229 const struct dp_upcall_info *upcall_info)
231 struct dp_stats_percpu *stats;
235 if (upcall_info->pid == 0) {
240 dp_ifindex = get_dpifindex(dp);
246 if (!skb_is_gso(skb))
247 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
249 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
256 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
258 u64_stats_update_begin(&stats->sync);
260 u64_stats_update_end(&stats->sync);
265 static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
266 const struct dp_upcall_info *upcall_info)
268 struct dp_upcall_info later_info;
269 struct sw_flow_key later_key;
270 struct sk_buff *segs, *nskb;
273 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
277 /* Queue all of the segments. */
280 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
284 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
285 /* The initial flow key extracted by ovs_flow_extract()
286 * in this case is for a first fragment, so we need to
287 * properly mark later fragments.
289 later_key = *upcall_info->key;
290 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
292 later_info = *upcall_info;
293 later_info.key = &later_key;
294 upcall_info = &later_info;
296 } while ((skb = skb->next));
298 /* Free all of the segments. */
306 } while ((skb = nskb));
310 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
311 const struct dp_upcall_info *upcall_info)
313 struct ovs_header *upcall;
314 struct sk_buff *nskb = NULL;
315 struct sk_buff *user_skb; /* to be queued to userspace */
320 if (vlan_tx_tag_present(skb)) {
321 nskb = skb_clone(skb, GFP_ATOMIC);
325 nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
333 if (nla_attr_size(skb->len) > USHRT_MAX) {
338 len = sizeof(struct ovs_header);
339 len += nla_total_size(skb->len);
340 len += nla_total_size(FLOW_BUFSIZE);
341 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
342 len += nla_total_size(8);
344 user_skb = genlmsg_new(len, GFP_ATOMIC);
350 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
351 0, upcall_info->cmd);
352 upcall->dp_ifindex = dp_ifindex;
354 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
355 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
356 nla_nest_end(user_skb, nla);
358 if (upcall_info->userdata)
359 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
360 nla_get_u64(upcall_info->userdata));
362 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
364 skb_copy_and_csum_dev(skb, nla_data(nla));
366 err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
373 /* Called with genl_mutex. */
374 static int flush_flows(int dp_ifindex)
376 struct flow_table *old_table;
377 struct flow_table *new_table;
380 dp = get_dp(dp_ifindex);
384 old_table = genl_dereference(dp->table);
385 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
389 rcu_assign_pointer(dp->table, new_table);
391 ovs_flow_tbl_deferred_destroy(old_table);
395 static int validate_actions(const struct nlattr *attr,
396 const struct sw_flow_key *key, int depth);
398 static int validate_sample(const struct nlattr *attr,
399 const struct sw_flow_key *key, int depth)
401 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
402 const struct nlattr *probability, *actions;
403 const struct nlattr *a;
406 memset(attrs, 0, sizeof(attrs));
407 nla_for_each_nested(a, attr, rem) {
408 int type = nla_type(a);
409 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
416 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
417 if (!probability || nla_len(probability) != sizeof(u32))
420 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
421 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
423 return validate_actions(actions, key, depth + 1);
426 static int validate_set(const struct nlattr *a,
427 const struct sw_flow_key *flow_key)
429 const struct nlattr *ovs_key = nla_data(a);
430 int key_type = nla_type(ovs_key);
432 /* There can be only one key in a action */
433 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
436 if (key_type > OVS_KEY_ATTR_MAX ||
437 nla_len(ovs_key) != ovs_key_lens[key_type])
441 const struct ovs_key_ipv4 *ipv4_key;
443 case OVS_KEY_ATTR_PRIORITY:
444 case OVS_KEY_ATTR_ETHERNET:
447 case OVS_KEY_ATTR_IPV4:
448 if (flow_key->eth.type != htons(ETH_P_IP))
451 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
454 ipv4_key = nla_data(ovs_key);
455 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
458 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
463 case OVS_KEY_ATTR_TCP:
464 if (flow_key->ip.proto != IPPROTO_TCP)
467 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
472 case OVS_KEY_ATTR_UDP:
473 if (flow_key->ip.proto != IPPROTO_UDP)
476 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
487 static int validate_userspace(const struct nlattr *attr)
489 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
490 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
491 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
493 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
496 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
497 attr, userspace_policy);
501 if (!a[OVS_USERSPACE_ATTR_PID] ||
502 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
508 static int validate_actions(const struct nlattr *attr,
509 const struct sw_flow_key *key, int depth)
511 const struct nlattr *a;
514 if (depth >= SAMPLE_ACTION_DEPTH)
517 nla_for_each_nested(a, attr, rem) {
518 /* Expected argument lengths, (u32)-1 for variable length. */
519 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
520 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
521 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
522 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
523 [OVS_ACTION_ATTR_POP_VLAN] = 0,
524 [OVS_ACTION_ATTR_SET] = (u32)-1,
525 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
527 const struct ovs_action_push_vlan *vlan;
528 int type = nla_type(a);
530 if (type > OVS_ACTION_ATTR_MAX ||
531 (action_lens[type] != nla_len(a) &&
532 action_lens[type] != (u32)-1))
536 case OVS_ACTION_ATTR_UNSPEC:
539 case OVS_ACTION_ATTR_USERSPACE:
540 err = validate_userspace(a);
545 case OVS_ACTION_ATTR_OUTPUT:
546 if (nla_get_u32(a) >= DP_MAX_PORTS)
551 case OVS_ACTION_ATTR_POP_VLAN:
554 case OVS_ACTION_ATTR_PUSH_VLAN:
556 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
558 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
562 case OVS_ACTION_ATTR_SET:
563 err = validate_set(a, key);
568 case OVS_ACTION_ATTR_SAMPLE:
569 err = validate_sample(a, key, depth);
585 static void clear_stats(struct sw_flow *flow)
589 flow->packet_count = 0;
590 flow->byte_count = 0;
593 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
595 struct ovs_header *ovs_header = info->userhdr;
596 struct nlattr **a = info->attrs;
597 struct sw_flow_actions *acts;
598 struct sk_buff *packet;
599 struct sw_flow *flow;
607 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
608 !a[OVS_PACKET_ATTR_ACTIONS] ||
609 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
612 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
613 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
617 skb_reserve(packet, NET_IP_ALIGN);
619 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
621 skb_reset_mac_header(packet);
622 eth = eth_hdr(packet);
624 /* Normally, setting the skb 'protocol' field would be handled by a
625 * call to eth_type_trans(), but it assumes there's a sending
626 * device, which we may not have. */
627 if (ntohs(eth->h_proto) >= 1536)
628 packet->protocol = eth->h_proto;
630 packet->protocol = htons(ETH_P_802_2);
632 /* Build an sw_flow for sending this packet. */
633 flow = ovs_flow_alloc();
638 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
642 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
643 &flow->key.phy.in_port,
644 a[OVS_PACKET_ATTR_KEY]);
648 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
652 flow->hash = ovs_flow_hash(&flow->key, key_len);
654 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
658 rcu_assign_pointer(flow->sf_acts, acts);
660 OVS_CB(packet)->flow = flow;
661 packet->priority = flow->key.phy.priority;
664 dp = get_dp(ovs_header->dp_ifindex);
670 err = ovs_execute_actions(dp, packet);
687 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
688 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
689 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
690 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
693 static struct genl_ops dp_packet_genl_ops[] = {
694 { .cmd = OVS_PACKET_CMD_EXECUTE,
695 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
696 .policy = packet_policy,
697 .doit = ovs_packet_cmd_execute
701 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
704 struct flow_table *table = genl_dereference(dp->table);
706 stats->n_flows = ovs_flow_tbl_count(table);
708 stats->n_hit = stats->n_missed = stats->n_lost = 0;
709 for_each_possible_cpu(i) {
710 const struct dp_stats_percpu *percpu_stats;
711 struct dp_stats_percpu local_stats;
714 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
717 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
718 local_stats = *percpu_stats;
719 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
721 stats->n_hit += local_stats.n_hit;
722 stats->n_missed += local_stats.n_missed;
723 stats->n_lost += local_stats.n_lost;
727 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
728 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
729 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
730 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
733 static struct genl_family dp_flow_genl_family = {
734 .id = GENL_ID_GENERATE,
735 .hdrsize = sizeof(struct ovs_header),
736 .name = OVS_FLOW_FAMILY,
737 .version = OVS_FLOW_VERSION,
738 .maxattr = OVS_FLOW_ATTR_MAX
741 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
742 .name = OVS_FLOW_MCGROUP
745 /* Called with genl_lock. */
746 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
747 struct sk_buff *skb, u32 pid,
748 u32 seq, u32 flags, u8 cmd)
750 const int skb_orig_len = skb->len;
751 const struct sw_flow_actions *sf_acts;
752 struct ovs_flow_stats stats;
753 struct ovs_header *ovs_header;
759 sf_acts = rcu_dereference_protected(flow->sf_acts,
760 lockdep_genl_is_held());
762 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
766 ovs_header->dp_ifindex = get_dpifindex(dp);
768 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
770 goto nla_put_failure;
771 err = ovs_flow_to_nlattrs(&flow->key, skb);
774 nla_nest_end(skb, nla);
776 spin_lock_bh(&flow->lock);
778 stats.n_packets = flow->packet_count;
779 stats.n_bytes = flow->byte_count;
780 tcp_flags = flow->tcp_flags;
781 spin_unlock_bh(&flow->lock);
784 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used));
787 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
788 sizeof(struct ovs_flow_stats), &stats);
791 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
793 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
794 * this is the first flow to be dumped into 'skb'. This is unusual for
795 * Netlink but individual action lists can be longer than
796 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
797 * The userspace caller can always fetch the actions separately if it
798 * really wants them. (Most userspace callers in fact don't care.)
800 * This can only fail for dump operations because the skb is always
801 * properly sized for single flows.
803 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
805 if (err < 0 && skb_orig_len)
808 return genlmsg_end(skb, ovs_header);
813 genlmsg_cancel(skb, ovs_header);
817 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
819 const struct sw_flow_actions *sf_acts;
822 sf_acts = rcu_dereference_protected(flow->sf_acts,
823 lockdep_genl_is_held());
825 /* OVS_FLOW_ATTR_KEY */
826 len = nla_total_size(FLOW_BUFSIZE);
827 /* OVS_FLOW_ATTR_ACTIONS */
828 len += nla_total_size(sf_acts->actions_len);
829 /* OVS_FLOW_ATTR_STATS */
830 len += nla_total_size(sizeof(struct ovs_flow_stats));
831 /* OVS_FLOW_ATTR_TCP_FLAGS */
832 len += nla_total_size(1);
833 /* OVS_FLOW_ATTR_USED */
834 len += nla_total_size(8);
836 len += NLMSG_ALIGN(sizeof(struct ovs_header));
838 return genlmsg_new(len, GFP_KERNEL);
841 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
843 u32 pid, u32 seq, u8 cmd)
848 skb = ovs_flow_cmd_alloc_info(flow);
850 return ERR_PTR(-ENOMEM);
852 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
857 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
859 struct nlattr **a = info->attrs;
860 struct ovs_header *ovs_header = info->userhdr;
861 struct sw_flow_key key;
862 struct sw_flow *flow;
863 struct sk_buff *reply;
865 struct flow_table *table;
871 if (!a[OVS_FLOW_ATTR_KEY])
873 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
877 /* Validate actions. */
878 if (a[OVS_FLOW_ATTR_ACTIONS]) {
879 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
882 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
887 dp = get_dp(ovs_header->dp_ifindex);
892 table = genl_dereference(dp->table);
893 flow = ovs_flow_tbl_lookup(table, &key, key_len);
895 struct sw_flow_actions *acts;
897 /* Bail out if we're not allowed to create a new flow. */
899 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
902 /* Expand table, if necessary, to make room. */
903 if (ovs_flow_tbl_need_to_expand(table)) {
904 struct flow_table *new_table;
906 new_table = ovs_flow_tbl_expand(table);
907 if (!IS_ERR(new_table)) {
908 rcu_assign_pointer(dp->table, new_table);
909 ovs_flow_tbl_deferred_destroy(table);
910 table = genl_dereference(dp->table);
915 flow = ovs_flow_alloc();
917 error = PTR_ERR(flow);
923 /* Obtain actions. */
924 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
925 error = PTR_ERR(acts);
927 goto error_free_flow;
928 rcu_assign_pointer(flow->sf_acts, acts);
930 /* Put flow in bucket. */
931 flow->hash = ovs_flow_hash(&key, key_len);
932 ovs_flow_tbl_insert(table, flow);
934 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
938 /* We found a matching flow. */
939 struct sw_flow_actions *old_acts;
940 struct nlattr *acts_attrs;
942 /* Bail out if we're not allowed to modify an existing flow.
943 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
944 * because Generic Netlink treats the latter as a dump
945 * request. We also accept NLM_F_EXCL in case that bug ever
949 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
950 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
953 /* Update actions. */
954 old_acts = rcu_dereference_protected(flow->sf_acts,
955 lockdep_genl_is_held());
956 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
958 (old_acts->actions_len != nla_len(acts_attrs) ||
959 memcmp(old_acts->actions, nla_data(acts_attrs),
960 old_acts->actions_len))) {
961 struct sw_flow_actions *new_acts;
963 new_acts = ovs_flow_actions_alloc(acts_attrs);
964 error = PTR_ERR(new_acts);
965 if (IS_ERR(new_acts))
968 rcu_assign_pointer(flow->sf_acts, new_acts);
969 ovs_flow_deferred_free_acts(old_acts);
972 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
973 info->snd_seq, OVS_FLOW_CMD_NEW);
976 if (a[OVS_FLOW_ATTR_CLEAR]) {
977 spin_lock_bh(&flow->lock);
979 spin_unlock_bh(&flow->lock);
984 genl_notify(reply, genl_info_net(info), info->snd_pid,
985 ovs_dp_flow_multicast_group.id, info->nlhdr,
988 netlink_set_err(init_net.genl_sock, 0,
989 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
998 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1000 struct nlattr **a = info->attrs;
1001 struct ovs_header *ovs_header = info->userhdr;
1002 struct sw_flow_key key;
1003 struct sk_buff *reply;
1004 struct sw_flow *flow;
1005 struct datapath *dp;
1006 struct flow_table *table;
1010 if (!a[OVS_FLOW_ATTR_KEY])
1012 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1016 dp = get_dp(ovs_header->dp_ifindex);
1020 table = genl_dereference(dp->table);
1021 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1025 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1026 info->snd_seq, OVS_FLOW_CMD_NEW);
1028 return PTR_ERR(reply);
1030 return genlmsg_reply(reply, info);
1033 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1035 struct nlattr **a = info->attrs;
1036 struct ovs_header *ovs_header = info->userhdr;
1037 struct sw_flow_key key;
1038 struct sk_buff *reply;
1039 struct sw_flow *flow;
1040 struct datapath *dp;
1041 struct flow_table *table;
1045 if (!a[OVS_FLOW_ATTR_KEY])
1046 return flush_flows(ovs_header->dp_ifindex);
1047 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1051 dp = get_dp(ovs_header->dp_ifindex);
1055 table = genl_dereference(dp->table);
1056 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1060 reply = ovs_flow_cmd_alloc_info(flow);
1064 ovs_flow_tbl_remove(table, flow);
1066 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1067 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1070 ovs_flow_deferred_free(flow);
1072 genl_notify(reply, genl_info_net(info), info->snd_pid,
1073 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1077 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1079 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1080 struct datapath *dp;
1081 struct flow_table *table;
1083 dp = get_dp(ovs_header->dp_ifindex);
1087 table = genl_dereference(dp->table);
1090 struct sw_flow *flow;
1093 bucket = cb->args[0];
1095 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1099 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1100 NETLINK_CB(cb->skb).pid,
1101 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1102 OVS_FLOW_CMD_NEW) < 0)
1105 cb->args[0] = bucket;
1111 static struct genl_ops dp_flow_genl_ops[] = {
1112 { .cmd = OVS_FLOW_CMD_NEW,
1113 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1114 .policy = flow_policy,
1115 .doit = ovs_flow_cmd_new_or_set
1117 { .cmd = OVS_FLOW_CMD_DEL,
1118 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1119 .policy = flow_policy,
1120 .doit = ovs_flow_cmd_del
1122 { .cmd = OVS_FLOW_CMD_GET,
1123 .flags = 0, /* OK for unprivileged users. */
1124 .policy = flow_policy,
1125 .doit = ovs_flow_cmd_get,
1126 .dumpit = ovs_flow_cmd_dump
1128 { .cmd = OVS_FLOW_CMD_SET,
1129 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1130 .policy = flow_policy,
1131 .doit = ovs_flow_cmd_new_or_set,
1135 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1136 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1137 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1140 static struct genl_family dp_datapath_genl_family = {
1141 .id = GENL_ID_GENERATE,
1142 .hdrsize = sizeof(struct ovs_header),
1143 .name = OVS_DATAPATH_FAMILY,
1144 .version = OVS_DATAPATH_VERSION,
1145 .maxattr = OVS_DP_ATTR_MAX
1148 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1149 .name = OVS_DATAPATH_MCGROUP
1152 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1153 u32 pid, u32 seq, u32 flags, u8 cmd)
1155 struct ovs_header *ovs_header;
1156 struct ovs_dp_stats dp_stats;
1159 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1164 ovs_header->dp_ifindex = get_dpifindex(dp);
1167 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1170 goto nla_put_failure;
1172 get_dp_stats(dp, &dp_stats);
1173 NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
1175 return genlmsg_end(skb, ovs_header);
1178 genlmsg_cancel(skb, ovs_header);
1183 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1186 struct sk_buff *skb;
1189 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1191 return ERR_PTR(-ENOMEM);
1193 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1196 return ERR_PTR(retval);
1201 /* Called with genl_mutex and optionally with RTNL lock also. */
1202 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1203 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1205 struct datapath *dp;
1207 if (!a[OVS_DP_ATTR_NAME])
1208 dp = get_dp(ovs_header->dp_ifindex);
1210 struct vport *vport;
1213 vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1214 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1217 return dp ? dp : ERR_PTR(-ENODEV);
1220 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1222 struct nlattr **a = info->attrs;
1223 struct vport_parms parms;
1224 struct sk_buff *reply;
1225 struct datapath *dp;
1226 struct vport *vport;
1230 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1235 if (!try_module_get(THIS_MODULE))
1236 goto err_unlock_rtnl;
1239 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1241 goto err_put_module;
1242 INIT_LIST_HEAD(&dp->port_list);
1244 /* Allocate table. */
1246 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1250 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1251 if (!dp->stats_percpu) {
1253 goto err_destroy_table;
1256 /* Set up our datapath device. */
1257 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1258 parms.type = OVS_VPORT_TYPE_INTERNAL;
1259 parms.options = NULL;
1261 parms.port_no = OVSP_LOCAL;
1262 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1264 vport = new_vport(&parms);
1265 if (IS_ERR(vport)) {
1266 err = PTR_ERR(vport);
1270 goto err_destroy_percpu;
1273 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1274 info->snd_seq, OVS_DP_CMD_NEW);
1275 err = PTR_ERR(reply);
1277 goto err_destroy_local_port;
1279 list_add_tail(&dp->list_node, &dps);
1282 genl_notify(reply, genl_info_net(info), info->snd_pid,
1283 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1287 err_destroy_local_port:
1288 ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1290 free_percpu(dp->stats_percpu);
1292 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1296 module_put(THIS_MODULE);
1303 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1305 struct vport *vport, *next_vport;
1306 struct sk_buff *reply;
1307 struct datapath *dp;
1311 dp = lookup_datapath(info->userhdr, info->attrs);
1316 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1317 info->snd_seq, OVS_DP_CMD_DEL);
1318 err = PTR_ERR(reply);
1322 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1323 if (vport->port_no != OVSP_LOCAL)
1324 ovs_dp_detach_port(vport);
1326 list_del(&dp->list_node);
1327 ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1329 /* rtnl_unlock() will wait until all the references to devices that
1330 * are pending unregistration have been dropped. We do it here to
1331 * ensure that any internal devices (which contain DP pointers) are
1332 * fully destroyed before freeing the datapath.
1336 call_rcu(&dp->rcu, destroy_dp_rcu);
1337 module_put(THIS_MODULE);
1339 genl_notify(reply, genl_info_net(info), info->snd_pid,
1340 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1350 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1352 struct sk_buff *reply;
1353 struct datapath *dp;
1356 dp = lookup_datapath(info->userhdr, info->attrs);
1360 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1361 info->snd_seq, OVS_DP_CMD_NEW);
1362 if (IS_ERR(reply)) {
1363 err = PTR_ERR(reply);
1364 netlink_set_err(init_net.genl_sock, 0,
1365 ovs_dp_datapath_multicast_group.id, err);
1369 genl_notify(reply, genl_info_net(info), info->snd_pid,
1370 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1376 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1378 struct sk_buff *reply;
1379 struct datapath *dp;
1381 dp = lookup_datapath(info->userhdr, info->attrs);
1385 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1386 info->snd_seq, OVS_DP_CMD_NEW);
1388 return PTR_ERR(reply);
1390 return genlmsg_reply(reply, info);
1393 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1395 struct datapath *dp;
1396 int skip = cb->args[0];
1399 list_for_each_entry(dp, &dps, list_node) {
1402 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1403 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1404 OVS_DP_CMD_NEW) < 0)
1414 static struct genl_ops dp_datapath_genl_ops[] = {
1415 { .cmd = OVS_DP_CMD_NEW,
1416 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1417 .policy = datapath_policy,
1418 .doit = ovs_dp_cmd_new
1420 { .cmd = OVS_DP_CMD_DEL,
1421 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1422 .policy = datapath_policy,
1423 .doit = ovs_dp_cmd_del
1425 { .cmd = OVS_DP_CMD_GET,
1426 .flags = 0, /* OK for unprivileged users. */
1427 .policy = datapath_policy,
1428 .doit = ovs_dp_cmd_get,
1429 .dumpit = ovs_dp_cmd_dump
1431 { .cmd = OVS_DP_CMD_SET,
1432 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1433 .policy = datapath_policy,
1434 .doit = ovs_dp_cmd_set,
1438 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1439 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1440 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1441 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1442 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1443 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1444 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1447 static struct genl_family dp_vport_genl_family = {
1448 .id = GENL_ID_GENERATE,
1449 .hdrsize = sizeof(struct ovs_header),
1450 .name = OVS_VPORT_FAMILY,
1451 .version = OVS_VPORT_VERSION,
1452 .maxattr = OVS_VPORT_ATTR_MAX
1455 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1456 .name = OVS_VPORT_MCGROUP
1459 /* Called with RTNL lock or RCU read lock. */
1460 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1461 u32 pid, u32 seq, u32 flags, u8 cmd)
1463 struct ovs_header *ovs_header;
1464 struct ovs_vport_stats vport_stats;
1467 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1472 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1474 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1475 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1476 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1477 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1479 ovs_vport_get_stats(vport, &vport_stats);
1480 NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1483 err = ovs_vport_get_options(vport, skb);
1484 if (err == -EMSGSIZE)
1487 return genlmsg_end(skb, ovs_header);
1492 genlmsg_cancel(skb, ovs_header);
1496 /* Called with RTNL lock or RCU read lock. */
1497 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1500 struct sk_buff *skb;
1503 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1505 return ERR_PTR(-ENOMEM);
1507 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1510 return ERR_PTR(retval);
1515 /* Called with RTNL lock or RCU read lock. */
1516 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1517 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1519 struct datapath *dp;
1520 struct vport *vport;
1522 if (a[OVS_VPORT_ATTR_NAME]) {
1523 vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1525 return ERR_PTR(-ENODEV);
1527 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1528 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1530 if (port_no >= DP_MAX_PORTS)
1531 return ERR_PTR(-EFBIG);
1533 dp = get_dp(ovs_header->dp_ifindex);
1535 return ERR_PTR(-ENODEV);
1537 vport = rcu_dereference_rtnl(dp->ports[port_no]);
1539 return ERR_PTR(-ENOENT);
1542 return ERR_PTR(-EINVAL);
1545 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1547 struct nlattr **a = info->attrs;
1548 struct ovs_header *ovs_header = info->userhdr;
1549 struct vport_parms parms;
1550 struct sk_buff *reply;
1551 struct vport *vport;
1552 struct datapath *dp;
1557 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1558 !a[OVS_VPORT_ATTR_UPCALL_PID])
1562 dp = get_dp(ovs_header->dp_ifindex);
1567 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1568 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1571 if (port_no >= DP_MAX_PORTS)
1574 vport = rtnl_dereference(dp->ports[port_no]);
1579 for (port_no = 1; ; port_no++) {
1580 if (port_no >= DP_MAX_PORTS) {
1584 vport = rtnl_dereference(dp->ports[port_no]);
1590 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1591 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1592 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1594 parms.port_no = port_no;
1595 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1597 vport = new_vport(&parms);
1598 err = PTR_ERR(vport);
1602 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1604 if (IS_ERR(reply)) {
1605 err = PTR_ERR(reply);
1606 ovs_dp_detach_port(vport);
1609 genl_notify(reply, genl_info_net(info), info->snd_pid,
1610 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1618 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1620 struct nlattr **a = info->attrs;
1621 struct sk_buff *reply;
1622 struct vport *vport;
1626 vport = lookup_vport(info->userhdr, a);
1627 err = PTR_ERR(vport);
1632 if (a[OVS_VPORT_ATTR_TYPE] &&
1633 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1636 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1637 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1638 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1639 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1641 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1643 if (IS_ERR(reply)) {
1644 err = PTR_ERR(reply);
1645 netlink_set_err(init_net.genl_sock, 0,
1646 ovs_dp_vport_multicast_group.id, err);
1650 genl_notify(reply, genl_info_net(info), info->snd_pid,
1651 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1658 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1660 struct nlattr **a = info->attrs;
1661 struct sk_buff *reply;
1662 struct vport *vport;
1666 vport = lookup_vport(info->userhdr, a);
1667 err = PTR_ERR(vport);
1671 if (vport->port_no == OVSP_LOCAL) {
1676 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1678 err = PTR_ERR(reply);
1682 ovs_dp_detach_port(vport);
1684 genl_notify(reply, genl_info_net(info), info->snd_pid,
1685 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1692 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1694 struct nlattr **a = info->attrs;
1695 struct ovs_header *ovs_header = info->userhdr;
1696 struct sk_buff *reply;
1697 struct vport *vport;
1701 vport = lookup_vport(ovs_header, a);
1702 err = PTR_ERR(vport);
1706 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1708 err = PTR_ERR(reply);
1714 return genlmsg_reply(reply, info);
1721 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1723 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1724 struct datapath *dp;
1728 dp = get_dp(ovs_header->dp_ifindex);
1733 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1734 struct vport *vport;
1736 vport = rcu_dereference(dp->ports[port_no]);
1740 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1741 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1742 OVS_VPORT_CMD_NEW) < 0)
1747 cb->args[0] = port_no;
1753 static void rehash_flow_table(struct work_struct *work)
1755 struct datapath *dp;
1759 list_for_each_entry(dp, &dps, list_node) {
1760 struct flow_table *old_table = genl_dereference(dp->table);
1761 struct flow_table *new_table;
1763 new_table = ovs_flow_tbl_rehash(old_table);
1764 if (!IS_ERR(new_table)) {
1765 rcu_assign_pointer(dp->table, new_table);
1766 ovs_flow_tbl_deferred_destroy(old_table);
1772 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
1775 static struct genl_ops dp_vport_genl_ops[] = {
1776 { .cmd = OVS_VPORT_CMD_NEW,
1777 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1778 .policy = vport_policy,
1779 .doit = ovs_vport_cmd_new
1781 { .cmd = OVS_VPORT_CMD_DEL,
1782 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1783 .policy = vport_policy,
1784 .doit = ovs_vport_cmd_del
1786 { .cmd = OVS_VPORT_CMD_GET,
1787 .flags = 0, /* OK for unprivileged users. */
1788 .policy = vport_policy,
1789 .doit = ovs_vport_cmd_get,
1790 .dumpit = ovs_vport_cmd_dump
1792 { .cmd = OVS_VPORT_CMD_SET,
1793 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1794 .policy = vport_policy,
1795 .doit = ovs_vport_cmd_set,
1799 struct genl_family_and_ops {
1800 struct genl_family *family;
1801 struct genl_ops *ops;
1803 struct genl_multicast_group *group;
1806 static const struct genl_family_and_ops dp_genl_families[] = {
1807 { &dp_datapath_genl_family,
1808 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1809 &ovs_dp_datapath_multicast_group },
1810 { &dp_vport_genl_family,
1811 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1812 &ovs_dp_vport_multicast_group },
1813 { &dp_flow_genl_family,
1814 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1815 &ovs_dp_flow_multicast_group },
1816 { &dp_packet_genl_family,
1817 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1821 static void dp_unregister_genl(int n_families)
1825 for (i = 0; i < n_families; i++)
1826 genl_unregister_family(dp_genl_families[i].family);
1829 static int dp_register_genl(void)
1836 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1837 const struct genl_family_and_ops *f = &dp_genl_families[i];
1839 err = genl_register_family_with_ops(f->family, f->ops,
1846 err = genl_register_mc_group(f->family, f->group);
1855 dp_unregister_genl(n_registered);
1859 static int __init dp_init(void)
1861 struct sk_buff *dummy_skb;
1864 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
1866 pr_info("Open vSwitch switching datapath\n");
1868 err = ovs_flow_init();
1872 err = ovs_vport_init();
1874 goto error_flow_exit;
1876 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1878 goto error_vport_exit;
1880 err = dp_register_genl();
1882 goto error_unreg_notifier;
1884 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
1888 error_unreg_notifier:
1889 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1898 static void dp_cleanup(void)
1900 cancel_delayed_work_sync(&rehash_flow_wq);
1902 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1903 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1908 module_init(dp_init);
1909 module_exit(dp_cleanup);
1911 MODULE_DESCRIPTION("Open vSwitch switching datapath");
1912 MODULE_LICENSE("GPL");