2 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 /* Functions for managing the dp interface/device. */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
14 #include <linux/module.h>
16 #include <linux/if_arp.h>
17 #include <linux/if_vlan.h>
20 #include <linux/delay.h>
21 #include <linux/time.h>
22 #include <linux/etherdevice.h>
23 #include <linux/kernel.h>
24 #include <linux/kthread.h>
25 #include <linux/mutex.h>
26 #include <linux/percpu.h>
27 #include <linux/rcupdate.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/version.h>
31 #include <linux/ethtool.h>
32 #include <linux/wait.h>
33 #include <asm/system.h>
34 #include <asm/div64.h>
36 #include <linux/highmem.h>
37 #include <linux/netfilter_bridge.h>
38 #include <linux/netfilter_ipv4.h>
39 #include <linux/inetdevice.h>
40 #include <linux/list.h>
41 #include <linux/rculist.h>
42 #include <linux/dmi.h>
43 #include <net/inet_ecn.h>
44 #include <net/genetlink.h>
45 #include <linux/compat.h>
47 #include "openvswitch/datapath-protocol.h"
52 #include "loop_counter.h"
53 #include "odp-compat.h"
55 #include "vport-internal_dev.h"
57 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
58 EXPORT_SYMBOL(dp_ioctl_hook);
60 /* Datapaths. Protected on the read side by rcu_read_lock, on the write side
63 * dp_mutex nests inside the RTNL lock: if you need both you must take the RTNL
66 * It is safe to access the datapath and vport structures with just
69 static struct datapath __rcu *dps[ODP_MAX];
70 static DEFINE_MUTEX(dp_mutex);
72 static struct vport *new_vport(const struct vport_parms *);
74 /* Must be called with rcu_read_lock or dp_mutex. */
75 struct datapath *get_dp(int dp_idx)
77 if (dp_idx < 0 || dp_idx >= ODP_MAX)
79 return rcu_dereference_check(dps[dp_idx], rcu_read_lock_held() ||
80 lockdep_is_held(&dp_mutex));
82 EXPORT_SYMBOL_GPL(get_dp);
84 static struct datapath *get_dp_locked(int dp_idx)
88 mutex_lock(&dp_mutex);
91 mutex_lock(&dp->mutex);
92 mutex_unlock(&dp_mutex);
96 static struct tbl *get_table_protected(struct datapath *dp)
98 return rcu_dereference_protected(dp->table,
99 lockdep_is_held(&dp->mutex));
102 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
104 return rcu_dereference_protected(dp->ports[port_no],
105 lockdep_is_held(&dp->mutex));
108 /* Must be called with rcu_read_lock or RTNL lock. */
109 const char *dp_name(const struct datapath *dp)
111 return vport_get_name(rcu_dereference_rtnl(dp->ports[ODPP_LOCAL]));
114 static inline size_t br_nlmsg_size(void)
116 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
117 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
118 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
119 + nla_total_size(4) /* IFLA_MASTER */
120 + nla_total_size(4) /* IFLA_MTU */
121 + nla_total_size(4) /* IFLA_LINK */
122 + nla_total_size(1); /* IFLA_OPERSTATE */
125 static int dp_fill_ifinfo(struct sk_buff *skb,
126 const struct vport *port,
127 int event, unsigned int flags)
129 struct datapath *dp = port->dp;
130 int ifindex = vport_get_ifindex(port);
131 int iflink = vport_get_iflink(port);
132 struct ifinfomsg *hdr;
133 struct nlmsghdr *nlh;
141 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
145 hdr = nlmsg_data(nlh);
146 hdr->ifi_family = AF_BRIDGE;
148 hdr->ifi_type = ARPHRD_ETHER;
149 hdr->ifi_index = ifindex;
150 hdr->ifi_flags = vport_get_flags(port);
153 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
154 NLA_PUT_U32(skb, IFLA_MASTER,
155 vport_get_ifindex(get_vport_protected(dp, ODPP_LOCAL)));
156 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
157 #ifdef IFLA_OPERSTATE
158 NLA_PUT_U8(skb, IFLA_OPERSTATE,
159 vport_is_running(port)
160 ? vport_get_operstate(port)
164 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
166 if (ifindex != iflink)
167 NLA_PUT_U32(skb, IFLA_LINK,iflink);
169 return nlmsg_end(skb, nlh);
172 nlmsg_cancel(skb, nlh);
176 static void dp_ifinfo_notify(int event, struct vport *port)
181 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
185 err = dp_fill_ifinfo(skb, port, event, 0);
187 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
188 WARN_ON(err == -EMSGSIZE);
192 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
196 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
199 static void release_dp(struct kobject *kobj)
201 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
205 static struct kobj_type dp_ktype = {
206 .release = release_dp
209 static int create_dp(int dp_idx, const char __user *devnamep)
211 struct vport_parms parms;
212 char devname[IFNAMSIZ];
219 int retval = strncpy_from_user(devname, devnamep, IFNAMSIZ);
223 } else if (retval >= IFNAMSIZ) {
228 snprintf(devname, sizeof(devname), "of%d", dp_idx);
232 mutex_lock(&dp_mutex);
234 if (!try_module_get(THIS_MODULE))
237 /* Exit early if a datapath with that number already exists.
238 * (We don't use -EEXIST because that's ambiguous with 'devname'
239 * conflicting with an existing network device name.) */
245 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
248 INIT_LIST_HEAD(&dp->port_list);
249 mutex_init(&dp->mutex);
250 mutex_lock(&dp->mutex);
252 for (i = 0; i < DP_N_QUEUES; i++)
253 skb_queue_head_init(&dp->queues[i]);
254 init_waitqueue_head(&dp->waitqueue);
256 /* Initialize kobject for bridge. This will be added as
257 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
258 dp->ifobj.kset = NULL;
259 kobject_init(&dp->ifobj, &dp_ktype);
261 /* Allocate table. */
263 rcu_assign_pointer(dp->table, tbl_create(TBL_MIN_BUCKETS));
267 /* Set up our datapath device. */
268 parms.name = devname;
269 parms.type = ODP_VPORT_TYPE_INTERNAL;
270 parms.options = NULL;
272 parms.port_no = ODPP_LOCAL;
273 vport = new_vport(&parms);
275 err = PTR_ERR(vport);
279 goto err_destroy_table;
283 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
284 if (!dp->stats_percpu) {
286 goto err_destroy_local_port;
289 rcu_assign_pointer(dps[dp_idx], dp);
292 mutex_unlock(&dp->mutex);
293 mutex_unlock(&dp_mutex);
298 err_destroy_local_port:
299 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
301 tbl_destroy(get_table_protected(dp), NULL);
303 mutex_unlock(&dp->mutex);
306 module_put(THIS_MODULE);
308 mutex_unlock(&dp_mutex);
314 static void destroy_dp_rcu(struct rcu_head *rcu)
316 struct datapath *dp = container_of(rcu, struct datapath, rcu);
319 for (i = 0; i < DP_N_QUEUES; i++)
320 skb_queue_purge(&dp->queues[i]);
322 tbl_destroy((struct tbl __force *)dp->table, flow_free_tbl);
323 free_percpu(dp->stats_percpu);
324 kobject_put(&dp->ifobj);
327 static int destroy_dp(int dp_idx)
334 mutex_lock(&dp_mutex);
341 mutex_lock(&dp->mutex);
343 list_for_each_entry_safe (p, n, &dp->port_list, node)
344 if (p->port_no != ODPP_LOCAL)
348 rcu_assign_pointer(dps[dp->dp_idx], NULL);
349 dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
351 mutex_unlock(&dp->mutex);
352 call_rcu(&dp->rcu, destroy_dp_rcu);
353 module_put(THIS_MODULE);
356 mutex_unlock(&dp_mutex);
361 /* Called with RTNL lock and dp->mutex. */
362 static struct vport *new_vport(const struct vport_parms *parms)
367 vport = vport_add(parms);
368 if (!IS_ERR(vport)) {
369 struct datapath *dp = parms->dp;
371 rcu_assign_pointer(dp->ports[parms->port_no], vport);
372 list_add_rcu(&vport->node, &dp->port_list);
374 dp_ifinfo_notify(RTM_NEWLINK, vport);
381 int dp_detach_port(struct vport *p)
387 if (p->port_no != ODPP_LOCAL)
389 dp_ifinfo_notify(RTM_DELLINK, p);
391 /* First drop references to device. */
392 list_del_rcu(&p->node);
393 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
395 /* Then destroy it. */
403 /* Must be called with rcu_read_lock. */
404 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
406 struct datapath *dp = p->dp;
407 struct dp_stats_percpu *stats;
408 int stats_counter_off;
409 struct sw_flow_actions *acts;
410 struct loop_counter *loop;
413 OVS_CB(skb)->vport = p;
415 if (!OVS_CB(skb)->flow) {
416 struct sw_flow_key key;
417 struct tbl_node *flow_node;
420 /* Extract flow from 'skb' into 'key'. */
421 error = flow_extract(skb, p ? p->port_no : ODPP_NONE, &key, &is_frag);
422 if (unlikely(error)) {
427 if (is_frag && dp->drop_frags) {
429 stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
434 flow_node = tbl_lookup(rcu_dereference(dp->table), &key,
435 flow_hash(&key), flow_cmp);
436 if (unlikely(!flow_node)) {
437 struct dp_upcall_info upcall;
439 upcall.type = _ODPL_MISS_NR;
442 upcall.sample_pool = 0;
443 upcall.actions = NULL;
444 upcall.actions_len = 0;
445 dp_upcall(dp, skb, &upcall);
446 stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
450 OVS_CB(skb)->flow = flow_cast(flow_node);
453 stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
454 flow_used(OVS_CB(skb)->flow, skb);
456 acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
458 /* Check whether we've looped too much. */
459 loop = loop_get_counter();
460 if (unlikely(++loop->count > MAX_LOOPS))
461 loop->looping = true;
462 if (unlikely(loop->looping)) {
463 loop_suppress(dp, acts);
468 /* Execute actions. */
469 execute_actions(dp, skb, &OVS_CB(skb)->flow->key, acts->actions,
472 /* Check whether sub-actions looped too much. */
473 if (unlikely(loop->looping))
474 loop_suppress(dp, acts);
477 /* Decrement loop counter. */
479 loop->looping = false;
483 /* Update datapath statistics. */
485 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
487 write_seqcount_begin(&stats->seqlock);
488 (*(u64 *)((u8 *)stats + stats_counter_off))++;
489 write_seqcount_end(&stats->seqlock);
494 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
496 u16 csum_start, csum_offset;
499 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
500 csum_start -= skb_headroom(skb);
501 BUG_ON(csum_start >= skb_headlen(skb));
503 skb_copy_bits(skb, 0, to, csum_start);
505 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
506 skb->len - csum_start, 0);
507 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
510 /* Append each packet in 'skb' list to 'queue'. There will be only one packet
511 * unless we broke up a GSO packet. */
512 static int queue_control_packets(struct datapath *dp, struct sk_buff *skb,
513 const struct dp_upcall_info *upcall_info)
515 struct sk_buff *nskb;
519 if (OVS_CB(skb)->vport)
520 port_no = OVS_CB(skb)->vport->port_no;
522 port_no = ODPP_LOCAL;
525 struct odp_packet *upcall;
526 struct sk_buff *user_skb; /* to be queued to userspace */
533 len = sizeof(struct odp_packet);
534 len += nla_total_size(4); /* ODP_PACKET_ATTR_TYPE. */
535 len += nla_total_size(skb->len);
536 len += nla_total_size(FLOW_BUFSIZE);
537 if (upcall_info->userdata)
538 len += nla_total_size(8);
539 if (upcall_info->sample_pool)
540 len += nla_total_size(4);
541 if (upcall_info->actions_len)
542 len += nla_total_size(upcall_info->actions_len);
544 user_skb = alloc_skb(len, GFP_ATOMIC);
548 upcall = (struct odp_packet *)__skb_put(user_skb, sizeof(*upcall));
549 upcall->dp_idx = dp->dp_idx;
551 nla_put_u32(user_skb, ODP_PACKET_ATTR_TYPE, upcall_info->type);
553 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_KEY);
554 flow_to_nlattrs(upcall_info->key, user_skb);
555 nla_nest_end(user_skb, nla);
557 if (upcall_info->userdata)
558 nla_put_u64(user_skb, ODP_PACKET_ATTR_USERDATA, upcall_info->userdata);
559 if (upcall_info->sample_pool)
560 nla_put_u32(user_skb, ODP_PACKET_ATTR_SAMPLE_POOL, upcall_info->sample_pool);
561 if (upcall_info->actions_len) {
562 const struct nlattr *actions = upcall_info->actions;
563 u32 actions_len = upcall_info->actions_len;
565 nla = nla_nest_start(user_skb, ODP_PACKET_ATTR_ACTIONS);
566 memcpy(__skb_put(user_skb, actions_len), actions, actions_len);
567 nla_nest_end(user_skb, nla);
570 nla = __nla_reserve(user_skb, ODP_PACKET_ATTR_PACKET, skb->len);
571 if (skb->ip_summed == CHECKSUM_PARTIAL)
572 copy_and_csum_skb(skb, nla_data(nla));
574 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
576 upcall->len = user_skb->len;
577 skb_queue_tail(&dp->queues[upcall_info->type], user_skb);
586 while ((skb = nskb) != NULL) {
593 int dp_upcall(struct datapath *dp, struct sk_buff *skb, const struct dp_upcall_info *upcall_info)
595 struct dp_stats_percpu *stats;
596 struct sk_buff_head *queue;
599 WARN_ON_ONCE(skb_shared(skb));
600 BUG_ON(upcall_info->type >= DP_N_QUEUES);
602 queue = &dp->queues[upcall_info->type];
604 if (skb_queue_len(queue) >= DP_MAX_QUEUE_LEN)
607 forward_ip_summed(skb);
609 err = vswitch_skb_checksum_setup(skb);
613 /* Break apart GSO packets into their component pieces. Otherwise
614 * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
615 if (skb_is_gso(skb)) {
616 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
626 err = queue_control_packets(dp, skb, upcall_info);
627 wake_up_interruptible(&dp->waitqueue);
634 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
636 write_seqcount_begin(&stats->seqlock);
638 write_seqcount_end(&stats->seqlock);
645 static int flush_flows(struct datapath *dp)
647 struct tbl *old_table = get_table_protected(dp);
648 struct tbl *new_table;
650 new_table = tbl_create(TBL_MIN_BUCKETS);
654 rcu_assign_pointer(dp->table, new_table);
656 tbl_deferred_destroy(old_table, flow_free_tbl);
661 static int validate_actions(const struct nlattr *actions, u32 actions_len)
663 const struct nlattr *a;
666 nla_for_each_attr(a, actions, actions_len, rem) {
667 static const u32 action_lens[ODPAT_MAX + 1] = {
669 [ODPAT_CONTROLLER] = 8,
670 [ODPAT_SET_DL_TCI] = 2,
671 [ODPAT_STRIP_VLAN] = 0,
672 [ODPAT_SET_DL_SRC] = ETH_ALEN,
673 [ODPAT_SET_DL_DST] = ETH_ALEN,
674 [ODPAT_SET_NW_SRC] = 4,
675 [ODPAT_SET_NW_DST] = 4,
676 [ODPAT_SET_NW_TOS] = 1,
677 [ODPAT_SET_TP_SRC] = 2,
678 [ODPAT_SET_TP_DST] = 2,
679 [ODPAT_SET_TUNNEL] = 8,
680 [ODPAT_SET_PRIORITY] = 4,
681 [ODPAT_POP_PRIORITY] = 0,
682 [ODPAT_DROP_SPOOFED_ARP] = 0,
684 int type = nla_type(a);
686 if (type > ODPAT_MAX || nla_len(a) != action_lens[type])
693 case ODPAT_CONTROLLER:
694 case ODPAT_STRIP_VLAN:
695 case ODPAT_SET_DL_SRC:
696 case ODPAT_SET_DL_DST:
697 case ODPAT_SET_NW_SRC:
698 case ODPAT_SET_NW_DST:
699 case ODPAT_SET_TP_SRC:
700 case ODPAT_SET_TP_DST:
701 case ODPAT_SET_TUNNEL:
702 case ODPAT_SET_PRIORITY:
703 case ODPAT_POP_PRIORITY:
704 case ODPAT_DROP_SPOOFED_ARP:
705 /* No validation needed. */
709 if (nla_get_u32(a) >= DP_MAX_PORTS)
713 case ODPAT_SET_DL_TCI:
714 if (nla_get_be16(a) & htons(VLAN_CFI_MASK))
718 case ODPAT_SET_NW_TOS:
719 if (nla_get_u8(a) & INET_ECN_MASK)
734 static struct sw_flow_actions *get_actions(const struct odp_flow *flow)
736 struct sw_flow_actions *actions;
739 actions = flow_actions_alloc(flow->actions_len);
740 error = PTR_ERR(actions);
745 if (copy_from_user(actions->actions,
746 (struct nlattr __user __force *)flow->actions,
748 goto error_free_actions;
749 error = validate_actions(actions->actions, actions->actions_len);
751 goto error_free_actions;
758 return ERR_PTR(error);
761 static void get_stats(struct sw_flow *flow, struct odp_flow_stats *stats)
764 struct timespec offset_ts, used, now_mono;
766 ktime_get_ts(&now_mono);
767 jiffies_to_timespec(jiffies - flow->used, &offset_ts);
768 set_normalized_timespec(&used, now_mono.tv_sec - offset_ts.tv_sec,
769 now_mono.tv_nsec - offset_ts.tv_nsec);
771 stats->used_sec = used.tv_sec;
772 stats->used_nsec = used.tv_nsec;
775 stats->used_nsec = 0;
778 stats->n_packets = flow->packet_count;
779 stats->n_bytes = flow->byte_count;
781 stats->tcp_flags = flow->tcp_flags;
784 static void clear_stats(struct sw_flow *flow)
788 flow->packet_count = 0;
789 flow->byte_count = 0;
792 static int expand_table(struct datapath *dp)
794 struct tbl *old_table = get_table_protected(dp);
795 struct tbl *new_table;
797 new_table = tbl_expand(old_table);
798 if (IS_ERR(new_table))
799 return PTR_ERR(new_table);
801 rcu_assign_pointer(dp->table, new_table);
802 tbl_deferred_destroy(old_table, NULL);
807 static int do_put_flow(struct datapath *dp, struct odp_flow_put *uf,
808 struct odp_flow_stats *stats)
810 struct tbl_node *flow_node;
811 struct sw_flow_key key;
812 struct sw_flow *flow;
814 struct sw_flow_actions *acts = NULL;
818 error = flow_copy_from_user(&key, (const struct nlattr __force __user *)uf->flow.key,
823 hash = flow_hash(&key);
824 table = get_table_protected(dp);
825 flow_node = tbl_lookup(table, &key, hash, flow_cmp);
829 if (!(uf->flags & ODPPF_CREATE))
832 /* Expand table, if necessary, to make room. */
833 if (tbl_count(table) >= tbl_n_buckets(table)) {
834 error = expand_table(dp);
837 table = get_table_protected(dp);
843 error = PTR_ERR(flow);
849 /* Obtain actions. */
850 acts = get_actions(&uf->flow);
851 error = PTR_ERR(acts);
853 goto error_free_flow;
854 rcu_assign_pointer(flow->sf_acts, acts);
856 /* Put flow in bucket. */
857 error = tbl_insert(table, &flow->tbl_node, hash);
859 goto error_free_flow_acts;
861 memset(stats, 0, sizeof(struct odp_flow_stats));
863 /* We found a matching flow. */
864 struct sw_flow_actions *old_acts, *new_acts;
866 flow = flow_cast(flow_node);
868 /* Bail out if we're not allowed to modify an existing flow. */
870 if (!(uf->flags & ODPPF_MODIFY))
874 new_acts = get_actions(&uf->flow);
875 error = PTR_ERR(new_acts);
876 if (IS_ERR(new_acts))
879 old_acts = rcu_dereference_protected(flow->sf_acts,
880 lockdep_is_held(&dp->mutex));
881 if (old_acts->actions_len != new_acts->actions_len ||
882 memcmp(old_acts->actions, new_acts->actions,
883 old_acts->actions_len)) {
884 rcu_assign_pointer(flow->sf_acts, new_acts);
885 flow_deferred_free_acts(old_acts);
890 /* Fetch stats, then clear them if necessary. */
891 spin_lock_bh(&flow->lock);
892 get_stats(flow, stats);
893 if (uf->flags & ODPPF_ZERO_STATS)
895 spin_unlock_bh(&flow->lock);
900 error_free_flow_acts:
903 flow->sf_acts = NULL;
909 static int put_flow(struct datapath *dp, struct odp_flow_put __user *ufp)
911 struct odp_flow_stats stats;
912 struct odp_flow_put uf;
915 if (copy_from_user(&uf, ufp, sizeof(struct odp_flow_put)))
918 error = do_put_flow(dp, &uf, &stats);
922 if (copy_to_user(&ufp->flow.stats, &stats,
923 sizeof(struct odp_flow_stats)))
929 static int do_answer_query(struct datapath *dp, struct sw_flow *flow,
931 struct odp_flow_stats __user *ustats,
932 struct nlattr __user *actions,
933 u32 __user *actions_lenp)
935 struct sw_flow_actions *sf_acts;
936 struct odp_flow_stats stats;
939 spin_lock_bh(&flow->lock);
940 get_stats(flow, &stats);
941 if (query_flags & ODPFF_ZERO_TCP_FLAGS)
944 spin_unlock_bh(&flow->lock);
946 if (copy_to_user(ustats, &stats, sizeof(struct odp_flow_stats)) ||
947 get_user(actions_len, actions_lenp))
953 sf_acts = rcu_dereference_protected(flow->sf_acts,
954 lockdep_is_held(&dp->mutex));
955 if (put_user(sf_acts->actions_len, actions_lenp) ||
956 (actions && copy_to_user(actions, sf_acts->actions,
957 min(sf_acts->actions_len, actions_len))))
963 static int answer_query(struct datapath *dp, struct sw_flow *flow,
964 u32 query_flags, struct odp_flow __user *ufp)
966 struct nlattr __user *actions;
968 if (get_user(actions, (struct nlattr __user * __user *)&ufp->actions))
971 return do_answer_query(dp, flow, query_flags,
972 &ufp->stats, actions, &ufp->actions_len);
975 static struct sw_flow *do_del_flow(struct datapath *dp, const struct nlattr __user *key, u32 key_len)
977 struct tbl *table = get_table_protected(dp);
978 struct tbl_node *flow_node;
979 struct sw_flow_key swkey;
982 error = flow_copy_from_user(&swkey, key, key_len);
984 return ERR_PTR(error);
986 flow_node = tbl_lookup(table, &swkey, flow_hash(&swkey), flow_cmp);
988 return ERR_PTR(-ENOENT);
990 error = tbl_remove(table, flow_node);
992 return ERR_PTR(error);
994 /* XXX Returned flow_node's statistics might lose a few packets, since
995 * other CPUs can be using this flow. We used to synchronize_rcu() to
996 * make sure that we get completely accurate stats, but that blows our
997 * performance, badly. */
998 return flow_cast(flow_node);
1001 static int del_flow(struct datapath *dp, struct odp_flow __user *ufp)
1003 struct sw_flow *flow;
1007 if (copy_from_user(&uf, ufp, sizeof(uf)))
1010 flow = do_del_flow(dp, (const struct nlattr __force __user *)uf.key, uf.key_len);
1012 return PTR_ERR(flow);
1014 error = answer_query(dp, flow, 0, ufp);
1015 flow_deferred_free(flow);
1019 static int query_flow(struct datapath *dp, struct odp_flow __user *uflow)
1021 struct tbl *table = get_table_protected(dp);
1022 struct tbl_node *flow_node;
1023 struct sw_flow_key key;
1024 struct odp_flow flow;
1027 if (copy_from_user(&flow, uflow, sizeof(flow)))
1030 error = flow_copy_from_user(&key, (const struct nlattr __force __user *)flow.key, flow.key_len);
1034 flow_node = tbl_lookup(table, &flow.key, flow_hash(&key), flow_cmp);
1037 return answer_query(dp, flow_cast(flow_node), flow.flags, uflow);
1040 static struct sw_flow *do_dump_flow(struct datapath *dp, u32 __user *state)
1042 struct tbl *table = get_table_protected(dp);
1043 struct tbl_node *tbl_node;
1046 if (get_user(bucket, &state[0]) || get_user(obj, &state[1]))
1047 return ERR_PTR(-EFAULT);
1049 tbl_node = tbl_next(table, &bucket, &obj);
1051 if (put_user(bucket, &state[0]) || put_user(obj, &state[1]))
1052 return ERR_PTR(-EFAULT);
1054 return tbl_node ? flow_cast(tbl_node) : NULL;
1057 static int dump_flow(struct datapath *dp, struct odp_flow_dump __user *udumpp)
1059 struct odp_flow __user *uflowp;
1060 struct nlattr __user *ukey;
1061 struct sw_flow *flow;
1064 flow = do_dump_flow(dp, udumpp->state);
1066 return PTR_ERR(flow);
1068 if (get_user(uflowp, (struct odp_flow __user *__user*)&udumpp->flow))
1072 return put_user(ODPFF_EOF, &uflowp->flags);
1074 if (put_user(0, &uflowp->flags) ||
1075 get_user(ukey, (struct nlattr __user * __user*)&uflowp->key) ||
1076 get_user(key_len, &uflowp->key_len))
1079 key_len = flow_copy_to_user(ukey, &flow->key, key_len);
1082 if (put_user(key_len, &uflowp->key_len))
1085 return answer_query(dp, flow, 0, uflowp);
1088 static int do_execute(struct datapath *dp, const struct odp_execute *execute)
1090 struct sw_flow_key key;
1091 struct sk_buff *skb;
1092 struct sw_flow_actions *actions;
1098 if (execute->length < ETH_HLEN || execute->length > 65535)
1101 actions = flow_actions_alloc(execute->actions_len);
1102 if (IS_ERR(actions)) {
1103 err = PTR_ERR(actions);
1108 if (copy_from_user(actions->actions,
1109 (struct nlattr __user __force *)execute->actions, execute->actions_len))
1110 goto error_free_actions;
1112 err = validate_actions(actions->actions, execute->actions_len);
1114 goto error_free_actions;
1117 skb = alloc_skb(execute->length, GFP_KERNEL);
1119 goto error_free_actions;
1122 if (copy_from_user(skb_put(skb, execute->length),
1123 (const void __user __force *)execute->data,
1125 goto error_free_skb;
1127 skb_reset_mac_header(skb);
1130 /* Normally, setting the skb 'protocol' field would be handled by a
1131 * call to eth_type_trans(), but it assumes there's a sending
1132 * device, which we may not have. */
1133 if (ntohs(eth->h_proto) >= 1536)
1134 skb->protocol = eth->h_proto;
1136 skb->protocol = htons(ETH_P_802_2);
1138 err = flow_extract(skb, -1, &key, &is_frag);
1140 goto error_free_skb;
1143 err = execute_actions(dp, skb, &key, actions->actions, actions->actions_len);
1157 static int execute_packet(struct datapath *dp, const struct odp_execute __user *executep)
1159 struct odp_execute execute;
1161 if (copy_from_user(&execute, executep, sizeof(execute)))
1164 return do_execute(dp, &execute);
1167 static int get_dp_stats(struct datapath *dp, struct odp_stats __user *statsp)
1169 struct odp_stats stats;
1172 stats.n_frags = stats.n_hit = stats.n_missed = stats.n_lost = 0;
1173 for_each_possible_cpu(i) {
1174 const struct dp_stats_percpu *percpu_stats;
1175 struct dp_stats_percpu local_stats;
1178 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
1181 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
1182 local_stats = *percpu_stats;
1183 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
1185 stats.n_frags += local_stats.n_frags;
1186 stats.n_hit += local_stats.n_hit;
1187 stats.n_missed += local_stats.n_missed;
1188 stats.n_lost += local_stats.n_lost;
1190 return copy_to_user(statsp, &stats, sizeof(stats)) ? -EFAULT : 0;
1193 /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports */
1194 int dp_min_mtu(const struct datapath *dp)
1201 list_for_each_entry_rcu (p, &dp->port_list, node) {
1204 /* Skip any internal ports, since that's what we're trying to
1206 if (is_internal_vport(p))
1209 dev_mtu = vport_get_mtu(p);
1210 if (!mtu || dev_mtu < mtu)
1214 return mtu ? mtu : ETH_DATA_LEN;
1217 /* Sets the MTU of all datapath devices to the minimum of the ports. Must
1218 * be called with RTNL lock. */
1219 void set_internal_devs_mtu(const struct datapath *dp)
1226 mtu = dp_min_mtu(dp);
1228 list_for_each_entry_rcu (p, &dp->port_list, node) {
1229 if (is_internal_vport(p))
1230 vport_set_mtu(p, mtu);
1234 static int get_listen_mask(const struct file *f)
1236 return (long)f->private_data;
1239 static void set_listen_mask(struct file *f, int listen_mask)
1241 f->private_data = (void*)(long)listen_mask;
1244 static const struct nla_policy vport_policy[ODP_VPORT_ATTR_MAX + 1] = {
1245 [ODP_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1246 [ODP_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1247 [ODP_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1248 [ODP_VPORT_ATTR_STATS] = { .len = sizeof(struct rtnl_link_stats64) },
1249 [ODP_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1250 [ODP_VPORT_ATTR_MTU] = { .type = NLA_U32 },
1251 [ODP_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1254 static int copy_vport_to_user(void __user *dst, struct vport *vport, uint32_t total_len)
1256 struct odp_vport *odp_vport;
1257 struct sk_buff *skb;
1259 int ifindex, iflink;
1262 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1268 odp_vport = (struct odp_vport*)__skb_put(skb, sizeof(struct odp_vport));
1269 odp_vport->dp_idx = vport->dp->dp_idx;
1270 odp_vport->total_len = total_len;
1272 NLA_PUT_U32(skb, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1273 NLA_PUT_U32(skb, ODP_VPORT_ATTR_TYPE, vport_get_type(vport));
1274 NLA_PUT_STRING(skb, ODP_VPORT_ATTR_NAME, vport_get_name(vport));
1276 nla = nla_reserve(skb, ODP_VPORT_ATTR_STATS, sizeof(struct rtnl_link_stats64));
1278 goto nla_put_failure;
1279 if (vport_get_stats(vport, nla_data(nla)))
1280 __skb_trim(skb, skb->len - nla->nla_len);
1282 NLA_PUT(skb, ODP_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1284 NLA_PUT_U32(skb, ODP_VPORT_ATTR_MTU, vport_get_mtu(vport));
1286 err = vport_get_options(vport, skb);
1288 ifindex = vport_get_ifindex(vport);
1290 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFINDEX, ifindex);
1292 iflink = vport_get_iflink(vport);
1294 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFLINK, iflink);
1297 if (skb->len > total_len)
1300 odp_vport->len = skb->len;
1301 err = copy_to_user(dst, skb->data, skb->len) ? -EFAULT : 0;
1313 static struct sk_buff *copy_vport_from_user(struct odp_vport __user *uodp_vport,
1314 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1316 struct odp_vport *odp_vport;
1317 struct sk_buff *skb;
1321 if (get_user(len, &uodp_vport->len))
1322 return ERR_PTR(-EFAULT);
1323 if (len < sizeof(struct odp_vport))
1324 return ERR_PTR(-EINVAL);
1326 skb = alloc_skb(len, GFP_KERNEL);
1328 return ERR_PTR(-ENOMEM);
1331 if (copy_from_user(__skb_put(skb, len), uodp_vport, len))
1332 goto error_free_skb;
1334 odp_vport = (struct odp_vport *)skb->data;
1336 if (odp_vport->len != len)
1337 goto error_free_skb;
1339 err = nla_parse(a, ODP_VPORT_ATTR_MAX, (struct nlattr *)(skb->data + sizeof(struct odp_vport)),
1340 skb->len - sizeof(struct odp_vport), vport_policy);
1342 goto error_free_skb;
1344 err = VERIFY_NUL_STRING(a[ODP_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1346 goto error_free_skb;
1352 return ERR_PTR(err);
1356 /* Called without any locks (or with RTNL lock).
1357 * Returns holding vport->dp->mutex.
1359 static struct vport *lookup_vport(struct odp_vport *odp_vport,
1360 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1362 struct datapath *dp;
1363 struct vport *vport;
1365 if (a[ODP_VPORT_ATTR_NAME]) {
1366 int dp_idx, port_no;
1370 vport = vport_locate(nla_data(a[ODP_VPORT_ATTR_NAME]));
1373 return ERR_PTR(-ENODEV);
1375 dp_idx = vport->dp->dp_idx;
1376 port_no = vport->port_no;
1379 dp = get_dp_locked(dp_idx);
1383 vport = get_vport_protected(dp, port_no);
1385 strcmp(vport_get_name(vport), nla_data(a[ODP_VPORT_ATTR_NAME]))) {
1386 mutex_unlock(&dp->mutex);
1391 } else if (a[ODP_VPORT_ATTR_PORT_NO]) {
1392 u32 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1394 if (port_no >= DP_MAX_PORTS)
1395 return ERR_PTR(-EINVAL);
1397 dp = get_dp_locked(odp_vport->dp_idx);
1399 return ERR_PTR(-ENODEV);
1401 vport = get_vport_protected(dp, port_no);
1403 mutex_unlock(&dp->mutex);
1404 return ERR_PTR(-ENOENT);
1408 return ERR_PTR(-EINVAL);
1411 static int change_vport(struct vport *vport, struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1414 if (a[ODP_VPORT_ATTR_STATS])
1415 err = vport_set_stats(vport, nla_data(a[ODP_VPORT_ATTR_STATS]));
1416 if (!err && a[ODP_VPORT_ATTR_ADDRESS])
1417 err = vport_set_addr(vport, nla_data(a[ODP_VPORT_ATTR_ADDRESS]));
1418 if (!err && a[ODP_VPORT_ATTR_MTU])
1419 err = vport_set_mtu(vport, nla_get_u32(a[ODP_VPORT_ATTR_MTU]));
1423 static int attach_vport(struct odp_vport __user *uodp_vport)
1425 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1];
1426 struct odp_vport *odp_vport;
1427 struct vport_parms parms;
1428 struct vport *vport;
1429 struct sk_buff *skb;
1430 struct datapath *dp;
1434 skb = copy_vport_from_user(uodp_vport, a);
1438 odp_vport = (struct odp_vport *)skb->data;
1441 if (!a[ODP_VPORT_ATTR_NAME] || !a[ODP_VPORT_ATTR_TYPE])
1442 goto exit_kfree_skb;
1446 dp = get_dp_locked(odp_vport->dp_idx);
1449 goto exit_unlock_rtnl;
1451 if (a[ODP_VPORT_ATTR_PORT_NO]) {
1452 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1455 if (port_no >= DP_MAX_PORTS)
1456 goto exit_unlock_dp;
1458 vport = get_vport_protected(dp, port_no);
1461 goto exit_unlock_dp;
1463 for (port_no = 1; ; port_no++) {
1464 if (port_no >= DP_MAX_PORTS) {
1466 goto exit_unlock_dp;
1468 vport = get_vport_protected(dp, port_no);
1474 parms.name = nla_data(a[ODP_VPORT_ATTR_NAME]);
1475 parms.type = nla_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1476 parms.options = a[ODP_VPORT_ATTR_OPTIONS];
1478 parms.port_no = port_no;
1480 vport = new_vport(&parms);
1481 err = PTR_ERR(vport);
1483 goto exit_unlock_dp;
1485 set_internal_devs_mtu(dp);
1486 dp_sysfs_add_if(vport);
1488 err = change_vport(vport, a);
1490 dp_detach_port(vport);
1491 goto exit_unlock_dp;
1494 err = copy_vport_to_user(uodp_vport, vport, odp_vport->total_len);
1497 mutex_unlock(&dp->mutex);
1506 static int set_vport(unsigned int cmd, struct odp_vport __user *uodp_vport)
1508 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1];
1509 struct vport *vport;
1510 struct sk_buff *skb;
1513 skb = copy_vport_from_user(uodp_vport, a);
1519 vport = lookup_vport((struct odp_vport *)skb->data, a);
1520 err = PTR_ERR(vport);
1525 if (a[ODP_VPORT_ATTR_OPTIONS])
1526 err = vport_set_options(vport, a[ODP_VPORT_ATTR_OPTIONS]);
1528 err = change_vport(vport, a);
1530 mutex_unlock(&vport->dp->mutex);
1538 static int del_vport(unsigned int cmd, struct odp_vport __user *uodp_vport)
1540 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1];
1541 struct datapath *dp;
1542 struct vport *vport;
1543 struct sk_buff *skb;
1546 skb = copy_vport_from_user(uodp_vport, a);
1552 vport = lookup_vport((struct odp_vport *)skb->data, a);
1553 err = PTR_ERR(vport);
1559 if (vport->port_no == ODPP_LOCAL)
1562 err = dp_detach_port(vport);
1563 mutex_unlock(&dp->mutex);
1571 static int get_vport(struct odp_vport __user *uodp_vport)
1573 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1];
1574 struct odp_vport *odp_vport;
1575 struct vport *vport;
1576 struct sk_buff *skb;
1579 skb = copy_vport_from_user(uodp_vport, a);
1583 odp_vport = (struct odp_vport *)skb->data;
1585 vport = lookup_vport(odp_vport, a);
1586 err = PTR_ERR(vport);
1590 err = copy_vport_to_user(uodp_vport, vport, odp_vport->total_len);
1591 mutex_unlock(&vport->dp->mutex);
1598 static int dump_vport(struct odp_vport __user *uodp_vport)
1600 struct nlattr *a[ODP_VPORT_ATTR_MAX + 1];
1601 struct odp_vport *odp_vport;
1602 struct sk_buff *skb;
1603 struct datapath *dp;
1607 skb = copy_vport_from_user(uodp_vport, a);
1611 odp_vport = (struct odp_vport *)skb->data;
1613 dp = get_dp_locked(odp_vport->dp_idx);
1619 if (a[ODP_VPORT_ATTR_PORT_NO])
1620 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1621 for (; port_no < DP_MAX_PORTS; port_no++) {
1622 struct vport *vport = get_vport_protected(dp, port_no);
1624 err = copy_vport_to_user(uodp_vport, vport, odp_vport->total_len);
1625 goto exit_unlock_dp;
1631 mutex_unlock(&dp->mutex);
1638 static long openvswitch_ioctl(struct file *f, unsigned int cmd,
1641 int dp_idx = iminor(f->f_dentry->d_inode);
1642 struct datapath *dp;
1643 int drop_frags, listeners;
1644 unsigned int sflow_probability;
1647 /* Handle commands with special locking requirements up front. */
1650 err = create_dp(dp_idx, (char __user *)argp);
1653 case ODP_DP_DESTROY:
1654 err = destroy_dp(dp_idx);
1658 err = attach_vport((struct odp_vport __user *)argp);
1662 err = get_vport((struct odp_vport __user *)argp);
1666 err = del_vport(cmd, (struct odp_vport __user *)argp);
1670 err = set_vport(cmd, (struct odp_vport __user *)argp);
1673 case ODP_VPORT_DUMP:
1674 err = dump_vport((struct odp_vport __user *)argp);
1678 dp = get_dp_locked(dp_idx);
1685 err = get_dp_stats(dp, (struct odp_stats __user *)argp);
1688 case ODP_GET_DROP_FRAGS:
1689 err = put_user(dp->drop_frags, (int __user *)argp);
1692 case ODP_SET_DROP_FRAGS:
1693 err = get_user(drop_frags, (int __user *)argp);
1697 if (drop_frags != 0 && drop_frags != 1)
1699 dp->drop_frags = drop_frags;
1703 case ODP_GET_LISTEN_MASK:
1704 err = put_user(get_listen_mask(f), (int __user *)argp);
1707 case ODP_SET_LISTEN_MASK:
1708 err = get_user(listeners, (int __user *)argp);
1712 if (listeners & ~ODPL_ALL)
1715 set_listen_mask(f, listeners);
1718 case ODP_GET_SFLOW_PROBABILITY:
1719 err = put_user(dp->sflow_probability, (unsigned int __user *)argp);
1722 case ODP_SET_SFLOW_PROBABILITY:
1723 err = get_user(sflow_probability, (unsigned int __user *)argp);
1725 dp->sflow_probability = sflow_probability;
1728 case ODP_FLOW_FLUSH:
1729 err = flush_flows(dp);
1733 err = put_flow(dp, (struct odp_flow_put __user *)argp);
1737 err = del_flow(dp, (struct odp_flow __user *)argp);
1741 err = query_flow(dp, (struct odp_flow __user *)argp);
1745 err = dump_flow(dp, (struct odp_flow_dump __user *)argp);
1749 err = execute_packet(dp, (struct odp_execute __user *)argp);
1756 mutex_unlock(&dp->mutex);
1761 static int dp_has_packet_of_interest(struct datapath *dp, int listeners)
1764 for (i = 0; i < DP_N_QUEUES; i++) {
1765 if (listeners & (1 << i) && !skb_queue_empty(&dp->queues[i]))
1771 #ifdef CONFIG_COMPAT
1772 static int compat_get_flow(struct odp_flow *flow, const struct compat_odp_flow __user *compat)
1774 compat_uptr_t key, actions;
1776 if (!access_ok(VERIFY_READ, compat, sizeof(struct compat_odp_flow)) ||
1777 __copy_from_user(&flow->stats, &compat->stats, sizeof(struct odp_flow_stats)) ||
1778 __get_user(key, &compat->key) ||
1779 __get_user(flow->key_len, &compat->key_len) ||
1780 __get_user(actions, &compat->actions) ||
1781 __get_user(flow->actions_len, &compat->actions_len) ||
1782 __get_user(flow->flags, &compat->flags))
1785 flow->key = (struct nlattr __force *)compat_ptr(key);
1786 flow->actions = (struct nlattr __force *)compat_ptr(actions);
1790 static int compat_put_flow(struct datapath *dp, struct compat_odp_flow_put __user *ufp)
1792 struct odp_flow_stats stats;
1793 struct odp_flow_put fp;
1796 if (compat_get_flow(&fp.flow, &ufp->flow) ||
1797 get_user(fp.flags, &ufp->flags))
1800 error = do_put_flow(dp, &fp, &stats);
1804 if (copy_to_user(&ufp->flow.stats, &stats,
1805 sizeof(struct odp_flow_stats)))
1811 static int compat_answer_query(struct datapath *dp, struct sw_flow *flow,
1813 struct compat_odp_flow __user *ufp)
1815 compat_uptr_t actions;
1817 if (get_user(actions, &ufp->actions))
1820 return do_answer_query(dp, flow, query_flags, &ufp->stats,
1821 compat_ptr(actions), &ufp->actions_len);
1824 static int compat_del_flow(struct datapath *dp, struct compat_odp_flow __user *ufp)
1826 struct sw_flow *flow;
1830 if (compat_get_flow(&uf, ufp))
1833 flow = do_del_flow(dp, (const struct nlattr __force __user *)uf.key, uf.key_len);
1835 return PTR_ERR(flow);
1837 error = compat_answer_query(dp, flow, 0, ufp);
1838 flow_deferred_free(flow);
1842 static int compat_query_flow(struct datapath *dp, struct compat_odp_flow __user *uflow)
1844 struct tbl *table = get_table_protected(dp);
1845 struct tbl_node *flow_node;
1846 struct sw_flow_key key;
1847 struct odp_flow flow;
1850 if (compat_get_flow(&flow, uflow))
1853 error = flow_copy_from_user(&key, (const struct nlattr __force __user *)flow.key, flow.key_len);
1857 flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
1860 return compat_answer_query(dp, flow_cast(flow_node), flow.flags, uflow);
1863 static int compat_dump_flow(struct datapath *dp, struct compat_odp_flow_dump __user *udumpp)
1865 struct compat_odp_flow __user *uflowp;
1866 compat_uptr_t compat_ufp;
1867 struct sw_flow *flow;
1871 flow = do_dump_flow(dp, udumpp->state);
1873 return PTR_ERR(flow);
1875 if (get_user(compat_ufp, &udumpp->flow))
1877 uflowp = compat_ptr(compat_ufp);
1880 return put_user(ODPFF_EOF, &uflowp->flags);
1882 if (put_user(0, &uflowp->flags) ||
1883 get_user(ukey, &uflowp->key) ||
1884 get_user(key_len, &uflowp->key_len))
1887 key_len = flow_copy_to_user(compat_ptr(ukey), &flow->key, key_len);
1890 if (put_user(key_len, &uflowp->key_len))
1893 return compat_answer_query(dp, flow, 0, uflowp);
1896 static int compat_execute(struct datapath *dp, const struct compat_odp_execute __user *uexecute)
1898 struct odp_execute execute;
1899 compat_uptr_t actions;
1902 if (!access_ok(VERIFY_READ, uexecute, sizeof(struct compat_odp_execute)) ||
1903 __get_user(actions, &uexecute->actions) ||
1904 __get_user(execute.actions_len, &uexecute->actions_len) ||
1905 __get_user(data, &uexecute->data) ||
1906 __get_user(execute.length, &uexecute->length))
1909 execute.actions = (struct nlattr __force *)compat_ptr(actions);
1910 execute.data = (const void __force *)compat_ptr(data);
1912 return do_execute(dp, &execute);
1915 static long openvswitch_compat_ioctl(struct file *f, unsigned int cmd, unsigned long argp)
1917 int dp_idx = iminor(f->f_dentry->d_inode);
1918 struct datapath *dp;
1922 case ODP_DP_DESTROY:
1923 case ODP_FLOW_FLUSH:
1924 /* Ioctls that don't need any translation at all. */
1925 return openvswitch_ioctl(f, cmd, argp);
1932 case ODP_VPORT_DUMP:
1934 case ODP_GET_DROP_FRAGS:
1935 case ODP_SET_DROP_FRAGS:
1936 case ODP_SET_LISTEN_MASK:
1937 case ODP_GET_LISTEN_MASK:
1938 case ODP_SET_SFLOW_PROBABILITY:
1939 case ODP_GET_SFLOW_PROBABILITY:
1940 /* Ioctls that just need their pointer argument extended. */
1941 return openvswitch_ioctl(f, cmd, (unsigned long)compat_ptr(argp));
1944 dp = get_dp_locked(dp_idx);
1950 case ODP_FLOW_PUT32:
1951 err = compat_put_flow(dp, compat_ptr(argp));
1954 case ODP_FLOW_DEL32:
1955 err = compat_del_flow(dp, compat_ptr(argp));
1958 case ODP_FLOW_GET32:
1959 err = compat_query_flow(dp, compat_ptr(argp));
1962 case ODP_FLOW_DUMP32:
1963 err = compat_dump_flow(dp, compat_ptr(argp));
1967 err = compat_execute(dp, compat_ptr(argp));
1974 mutex_unlock(&dp->mutex);
1980 static ssize_t openvswitch_read(struct file *f, char __user *buf,
1981 size_t nbytes, loff_t *ppos)
1983 int listeners = get_listen_mask(f);
1984 int dp_idx = iminor(f->f_dentry->d_inode);
1985 struct datapath *dp = get_dp_locked(dp_idx);
1986 struct sk_buff *skb;
1993 if (nbytes == 0 || !listeners)
1999 for (i = 0; i < DP_N_QUEUES; i++) {
2000 if (listeners & (1 << i)) {
2001 skb = skb_dequeue(&dp->queues[i]);
2007 if (f->f_flags & O_NONBLOCK) {
2012 wait_event_interruptible(dp->waitqueue,
2013 dp_has_packet_of_interest(dp,
2016 if (signal_pending(current)) {
2017 retval = -ERESTARTSYS;
2022 mutex_unlock(&dp->mutex);
2025 iov.iov_len = min_t(size_t, skb->len, nbytes);
2026 retval = skb_copy_datagram_iovec(skb, 0, &iov, iov.iov_len);
2034 mutex_unlock(&dp->mutex);
2038 static unsigned int openvswitch_poll(struct file *file, poll_table *wait)
2040 int dp_idx = iminor(file->f_dentry->d_inode);
2041 struct datapath *dp = get_dp_locked(dp_idx);
2046 poll_wait(file, &dp->waitqueue, wait);
2047 if (dp_has_packet_of_interest(dp, get_listen_mask(file)))
2048 mask |= POLLIN | POLLRDNORM;
2049 mutex_unlock(&dp->mutex);
2051 mask = POLLIN | POLLRDNORM | POLLHUP;
2056 static struct file_operations openvswitch_fops = {
2057 .owner = THIS_MODULE,
2058 .read = openvswitch_read,
2059 .poll = openvswitch_poll,
2060 .unlocked_ioctl = openvswitch_ioctl,
2061 #ifdef CONFIG_COMPAT
2062 .compat_ioctl = openvswitch_compat_ioctl,
2068 static int __init dp_init(void)
2070 struct sk_buff *dummy_skb;
2073 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2075 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2083 goto error_flow_exit;
2085 err = register_netdevice_notifier(&dp_device_notifier);
2087 goto error_vport_exit;
2089 major = register_chrdev(0, "openvswitch", &openvswitch_fops);
2091 goto error_unreg_notifier;
2095 error_unreg_notifier:
2096 unregister_netdevice_notifier(&dp_device_notifier);
2105 static void dp_cleanup(void)
2108 unregister_chrdev(major, "openvswitch");
2109 unregister_netdevice_notifier(&dp_device_notifier);
2114 module_init(dp_init);
2115 module_exit(dp_cleanup);
2117 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2118 MODULE_LICENSE("GPL");