2 * Copyright (c) 2007, 2008, 2009, 2010 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 <linux/compat.h>
46 #include "openvswitch/datapath-protocol.h"
50 #include "loop_counter.h"
51 #include "odp-compat.h"
53 #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 *dps[ODP_MAX];
70 static DEFINE_MUTEX(dp_mutex);
72 static int new_vport(struct datapath *, struct odp_port *, int port_no);
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(dps[dp_idx]);
81 EXPORT_SYMBOL_GPL(get_dp);
83 static struct datapath *get_dp_locked(int dp_idx)
87 mutex_lock(&dp_mutex);
90 mutex_lock(&dp->mutex);
91 mutex_unlock(&dp_mutex);
95 /* Must be called with rcu_read_lock or RTNL lock. */
96 const char *dp_name(const struct datapath *dp)
98 return vport_get_name(dp->ports[ODPP_LOCAL]);
101 static inline size_t br_nlmsg_size(void)
103 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
104 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
105 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
106 + nla_total_size(4) /* IFLA_MASTER */
107 + nla_total_size(4) /* IFLA_MTU */
108 + nla_total_size(4) /* IFLA_LINK */
109 + nla_total_size(1); /* IFLA_OPERSTATE */
112 static int dp_fill_ifinfo(struct sk_buff *skb,
113 const struct vport *port,
114 int event, unsigned int flags)
116 const struct datapath *dp = port->dp;
117 int ifindex = vport_get_ifindex(port);
118 int iflink = vport_get_iflink(port);
119 struct ifinfomsg *hdr;
120 struct nlmsghdr *nlh;
128 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
132 hdr = nlmsg_data(nlh);
133 hdr->ifi_family = AF_BRIDGE;
135 hdr->ifi_type = ARPHRD_ETHER;
136 hdr->ifi_index = ifindex;
137 hdr->ifi_flags = vport_get_flags(port);
140 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
141 NLA_PUT_U32(skb, IFLA_MASTER, vport_get_ifindex(dp->ports[ODPP_LOCAL]));
142 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
143 #ifdef IFLA_OPERSTATE
144 NLA_PUT_U8(skb, IFLA_OPERSTATE,
145 vport_is_running(port)
146 ? vport_get_operstate(port)
150 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
152 if (ifindex != iflink)
153 NLA_PUT_U32(skb, IFLA_LINK,iflink);
155 return nlmsg_end(skb, nlh);
158 nlmsg_cancel(skb, nlh);
162 static void dp_ifinfo_notify(int event, struct vport *port)
167 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
171 err = dp_fill_ifinfo(skb, port, event, 0);
173 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
174 WARN_ON(err == -EMSGSIZE);
178 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
182 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
185 static void release_dp(struct kobject *kobj)
187 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
191 static struct kobj_type dp_ktype = {
192 .release = release_dp
195 static int create_dp(int dp_idx, const char __user *devnamep)
197 struct odp_port internal_dev_port;
198 char devname[IFNAMSIZ];
204 int retval = strncpy_from_user(devname, devnamep, IFNAMSIZ);
208 } else if (retval >= IFNAMSIZ) {
213 snprintf(devname, sizeof devname, "of%d", dp_idx);
217 mutex_lock(&dp_mutex);
219 if (!try_module_get(THIS_MODULE))
222 /* Exit early if a datapath with that number already exists.
223 * (We don't use -EEXIST because that's ambiguous with 'devname'
224 * conflicting with an existing network device name.) */
230 dp = kzalloc(sizeof *dp, GFP_KERNEL);
233 INIT_LIST_HEAD(&dp->port_list);
234 mutex_init(&dp->mutex);
236 for (i = 0; i < DP_N_QUEUES; i++)
237 skb_queue_head_init(&dp->queues[i]);
238 init_waitqueue_head(&dp->waitqueue);
240 /* Initialize kobject for bridge. This will be added as
241 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
242 dp->ifobj.kset = NULL;
243 kobject_init(&dp->ifobj, &dp_ktype);
245 /* Allocate table. */
247 rcu_assign_pointer(dp->table, tbl_create(0));
251 /* Set up our datapath device. */
252 BUILD_BUG_ON(sizeof(internal_dev_port.devname) != sizeof(devname));
253 strcpy(internal_dev_port.devname, devname);
254 strcpy(internal_dev_port.type, "internal");
255 err = new_vport(dp, &internal_dev_port, ODPP_LOCAL);
260 goto err_destroy_table;
264 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
265 if (!dp->stats_percpu)
266 goto err_destroy_local_port;
268 rcu_assign_pointer(dps[dp_idx], dp);
269 mutex_unlock(&dp_mutex);
276 err_destroy_local_port:
277 dp_detach_port(dp->ports[ODPP_LOCAL]);
279 tbl_destroy(dp->table, NULL);
283 module_put(THIS_MODULE);
285 mutex_unlock(&dp_mutex);
291 static void do_destroy_dp(struct datapath *dp)
296 list_for_each_entry_safe (p, n, &dp->port_list, node)
297 if (p->port_no != ODPP_LOCAL)
302 rcu_assign_pointer(dps[dp->dp_idx], NULL);
304 dp_detach_port(dp->ports[ODPP_LOCAL]);
306 tbl_destroy(dp->table, flow_free_tbl);
308 for (i = 0; i < DP_N_QUEUES; i++)
309 skb_queue_purge(&dp->queues[i]);
310 free_percpu(dp->stats_percpu);
311 kobject_put(&dp->ifobj);
312 module_put(THIS_MODULE);
315 static int destroy_dp(int dp_idx)
321 mutex_lock(&dp_mutex);
331 mutex_unlock(&dp_mutex);
336 /* Called with RTNL lock and dp_mutex. */
337 static int new_vport(struct datapath *dp, struct odp_port *odp_port, int port_no)
339 struct vport_parms parms;
343 parms.name = odp_port->devname;
344 parms.type = odp_port->type;
345 parms.config = odp_port->config;
347 parms.port_no = port_no;
350 vport = vport_add(&parms);
354 return PTR_ERR(vport);
356 err = vport_attach(vport);
362 rcu_assign_pointer(dp->ports[port_no], vport);
363 list_add_rcu(&vport->node, &dp->port_list);
366 dp_ifinfo_notify(RTM_NEWLINK, vport);
371 static int attach_port(int dp_idx, struct odp_port __user *portp)
374 struct odp_port port;
379 if (copy_from_user(&port, portp, sizeof port))
381 port.devname[IFNAMSIZ - 1] = '\0';
382 port.type[VPORT_TYPE_SIZE - 1] = '\0';
385 dp = get_dp_locked(dp_idx);
388 goto out_unlock_rtnl;
390 for (port_no = 1; port_no < DP_MAX_PORTS; port_no++)
391 if (!dp->ports[port_no])
397 err = new_vport(dp, &port, port_no);
401 set_internal_devs_mtu(dp);
402 dp_sysfs_add_if(dp->ports[port_no]);
404 err = put_user(port_no, &portp->port);
407 mutex_unlock(&dp->mutex);
414 int dp_detach_port(struct vport *p)
420 if (p->port_no != ODPP_LOCAL)
422 dp_ifinfo_notify(RTM_DELLINK, p);
424 /* First drop references to device. */
426 list_del_rcu(&p->node);
427 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
429 err = vport_detach(p);
433 /* Then wait until no one is still using it, and destroy it. */
443 static int detach_port(int dp_idx, int port_no)
450 if (port_no < 0 || port_no >= DP_MAX_PORTS || port_no == ODPP_LOCAL)
454 dp = get_dp_locked(dp_idx);
457 goto out_unlock_rtnl;
459 p = dp->ports[port_no];
464 err = dp_detach_port(p);
467 mutex_unlock(&dp->mutex);
474 /* Must be called with rcu_read_lock. */
475 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
477 struct datapath *dp = p->dp;
478 struct dp_stats_percpu *stats;
479 int stats_counter_off;
480 struct sw_flow_actions *acts;
481 struct loop_counter *loop;
484 OVS_CB(skb)->vport = p;
486 if (!OVS_CB(skb)->flow) {
487 struct odp_flow_key key;
488 struct tbl_node *flow_node;
491 /* Extract flow from 'skb' into 'key'. */
492 error = flow_extract(skb, p ? p->port_no : ODPP_NONE, &key, &is_frag);
493 if (unlikely(error)) {
498 if (is_frag && dp->drop_frags) {
500 stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
505 flow_node = tbl_lookup(rcu_dereference(dp->table), &key,
506 flow_hash(&key), flow_cmp);
507 if (unlikely(!flow_node)) {
508 dp_output_control(dp, skb, _ODPL_MISS_NR, OVS_CB(skb)->tun_id);
509 stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
513 OVS_CB(skb)->flow = flow_cast(flow_node);
516 flow_used(OVS_CB(skb)->flow, skb);
518 acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
520 /* Check whether we've looped too much. */
521 loop = loop_get_counter();
522 if (unlikely(++loop->count > MAX_LOOPS))
523 loop->looping = true;
524 if (unlikely(loop->looping)) {
525 loop_suppress(dp, acts);
529 /* Execute actions. */
530 execute_actions(dp, skb, &OVS_CB(skb)->flow->key, acts->actions,
532 stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
534 /* Check whether sub-actions looped too much. */
535 if (unlikely(loop->looping))
536 loop_suppress(dp, acts);
539 /* Decrement loop counter. */
541 loop->looping = false;
545 /* Update datapath statistics. */
547 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
549 write_seqcount_begin(&stats->seqlock);
550 (*(u64 *)((u8 *)stats + stats_counter_off))++;
551 write_seqcount_end(&stats->seqlock);
556 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
557 /* This code is based on skb_checksum_setup() from Xen's net/dev/core.c. We
558 * can't call this function directly because it isn't exported in all
560 int vswitch_skb_checksum_setup(struct sk_buff *skb)
565 __u16 csum_start, csum_offset;
567 if (!skb->proto_csum_blank)
570 if (skb->protocol != htons(ETH_P_IP))
573 if (!pskb_may_pull(skb, skb_network_header(skb) + sizeof(struct iphdr) - skb->data))
577 th = skb_network_header(skb) + 4 * iph->ihl;
579 csum_start = th - skb->head;
580 switch (iph->protocol) {
582 csum_offset = offsetof(struct tcphdr, check);
585 csum_offset = offsetof(struct udphdr, check);
589 pr_err("Attempting to checksum a non-TCP/UDP packet, "
590 "dropping a protocol %d packet",
595 if (!pskb_may_pull(skb, th + csum_offset + 2 - skb->data))
598 skb->ip_summed = CHECKSUM_PARTIAL;
599 skb->proto_csum_blank = 0;
601 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
602 skb->csum_start = csum_start;
603 skb->csum_offset = csum_offset;
605 skb_set_transport_header(skb, csum_start - skb_headroom(skb));
606 skb->csum = csum_offset;
614 #endif /* CONFIG_XEN && HAVE_PROTO_DATA_VALID */
616 /* Types of checksums that we can receive (these all refer to L4 checksums):
617 * 1. CHECKSUM_NONE: Device that did not compute checksum, contains full
618 * (though not verified) checksum in packet but not in skb->csum. Packets
619 * from the bridge local port will also have this type.
620 * 2. CHECKSUM_COMPLETE (CHECKSUM_HW): Good device that computes checksums,
621 * also the GRE module. This is the same as CHECKSUM_NONE, except it has
622 * a valid skb->csum. Importantly, both contain a full checksum (not
623 * verified) in the packet itself. The only difference is that if the
624 * packet gets to L4 processing on this machine (not in DomU) we won't
625 * have to recompute the checksum to verify. Most hardware devices do not
626 * produce packets with this type, even if they support receive checksum
627 * offloading (they produce type #5).
628 * 3. CHECKSUM_PARTIAL (CHECKSUM_HW): Packet without full checksum and needs to
629 * be computed if it is sent off box. Unfortunately on earlier kernels,
630 * this case is impossible to distinguish from #2, despite having opposite
631 * meanings. Xen adds an extra field on earlier kernels (see #4) in order
632 * to distinguish the different states.
633 * 4. CHECKSUM_UNNECESSARY (with proto_csum_blank true): This packet was
634 * generated locally by a Xen DomU and has a partial checksum. If it is
635 * handled on this machine (Dom0 or DomU), then the checksum will not be
636 * computed. If it goes off box, the checksum in the packet needs to be
637 * completed. Calling skb_checksum_setup converts this to CHECKSUM_HW
638 * (CHECKSUM_PARTIAL) so that the checksum can be completed. In later
639 * kernels, this combination is replaced with CHECKSUM_PARTIAL.
640 * 5. CHECKSUM_UNNECESSARY (with proto_csum_blank false): Packet with a correct
641 * full checksum or using a protocol without a checksum. skb->csum is
642 * undefined. This is common from devices with receive checksum
643 * offloading. This is somewhat similar to CHECKSUM_NONE, except that
644 * nobody will try to verify the checksum with CHECKSUM_UNNECESSARY.
646 * Note that on earlier kernels, CHECKSUM_COMPLETE and CHECKSUM_PARTIAL are
647 * both defined as CHECKSUM_HW. Normally the meaning of CHECKSUM_HW is clear
648 * based on whether it is on the transmit or receive path. After the datapath
649 * it will be intepreted as CHECKSUM_PARTIAL. If the packet already has a
650 * checksum, we will panic. Since we can receive packets with checksums, we
651 * assume that all CHECKSUM_HW packets have checksums and map them to
652 * CHECKSUM_NONE, which has a similar meaning (the it is only different if the
653 * packet is processed by the local IP stack, in which case it will need to
654 * be reverified). If we receive a packet with CHECKSUM_HW that really means
655 * CHECKSUM_PARTIAL, it will be sent with the wrong checksum. However, there
656 * shouldn't be any devices that do this with bridging. */
657 void compute_ip_summed(struct sk_buff *skb, bool xmit)
659 /* For our convenience these defines change repeatedly between kernel
660 * versions, so we can't just copy them over... */
661 switch (skb->ip_summed) {
663 OVS_CB(skb)->ip_summed = OVS_CSUM_NONE;
665 case CHECKSUM_UNNECESSARY:
666 OVS_CB(skb)->ip_summed = OVS_CSUM_UNNECESSARY;
669 /* In theory this could be either CHECKSUM_PARTIAL or CHECKSUM_COMPLETE.
670 * However, on the receive side we should only get CHECKSUM_PARTIAL
671 * packets from Xen, which uses some special fields to represent this
672 * (see below). Since we can only make one type work, pick the one
673 * that actually happens in practice.
675 * On the transmit side (basically after skb_checksum_setup()
676 * has been run or on internal dev transmit), packets with
677 * CHECKSUM_COMPLETE aren't generated, so assume CHECKSUM_PARTIAL. */
680 OVS_CB(skb)->ip_summed = OVS_CSUM_COMPLETE;
682 OVS_CB(skb)->ip_summed = OVS_CSUM_PARTIAL;
686 case CHECKSUM_COMPLETE:
687 OVS_CB(skb)->ip_summed = OVS_CSUM_COMPLETE;
689 case CHECKSUM_PARTIAL:
690 OVS_CB(skb)->ip_summed = OVS_CSUM_PARTIAL;
694 pr_err("unknown checksum type %d\n", skb->ip_summed);
695 /* None seems the safest... */
696 OVS_CB(skb)->ip_summed = OVS_CSUM_NONE;
699 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
700 /* Xen has a special way of representing CHECKSUM_PARTIAL on older
701 * kernels. It should not be set on the transmit path though. */
702 if (skb->proto_csum_blank)
703 OVS_CB(skb)->ip_summed = OVS_CSUM_PARTIAL;
705 WARN_ON_ONCE(skb->proto_csum_blank && xmit);
709 /* This function closely resembles skb_forward_csum() used by the bridge. It
710 * is slightly different because we are only concerned with bridging and not
711 * other types of forwarding and can get away with slightly more optimal
713 void forward_ip_summed(struct sk_buff *skb)
716 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
717 skb->ip_summed = CHECKSUM_NONE;
721 /* Append each packet in 'skb' list to 'queue'. There will be only one packet
722 * unless we broke up a GSO packet. */
723 static int queue_control_packets(struct sk_buff *skb, struct sk_buff_head *queue,
724 int queue_no, u32 arg)
726 struct sk_buff *nskb;
730 if (OVS_CB(skb)->vport)
731 port_no = OVS_CB(skb)->vport->port_no;
733 port_no = ODPP_LOCAL;
736 struct odp_msg *header;
741 err = skb_cow(skb, sizeof *header);
745 header = (struct odp_msg*)__skb_push(skb, sizeof *header);
746 header->type = queue_no;
747 header->length = skb->len;
748 header->port = port_no;
749 header->reserved = 0;
751 skb_queue_tail(queue, skb);
759 while ((skb = nskb) != NULL) {
766 int dp_output_control(struct datapath *dp, struct sk_buff *skb, int queue_no,
769 struct dp_stats_percpu *stats;
770 struct sk_buff_head *queue;
773 WARN_ON_ONCE(skb_shared(skb));
774 BUG_ON(queue_no != _ODPL_MISS_NR && queue_no != _ODPL_ACTION_NR && queue_no != _ODPL_SFLOW_NR);
775 queue = &dp->queues[queue_no];
777 if (skb_queue_len(queue) >= DP_MAX_QUEUE_LEN)
780 forward_ip_summed(skb);
782 err = vswitch_skb_checksum_setup(skb);
786 /* Break apart GSO packets into their component pieces. Otherwise
787 * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
788 if (skb_is_gso(skb)) {
789 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
793 if (unlikely(IS_ERR(skb))) {
798 /* XXX This case might not be possible. It's hard to
799 * tell from the skb_gso_segment() code and comment. */
803 err = queue_control_packets(skb, queue, queue_no, arg);
804 wake_up_interruptible(&dp->waitqueue);
811 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
813 write_seqcount_begin(&stats->seqlock);
815 write_seqcount_end(&stats->seqlock);
822 static int flush_flows(struct datapath *dp)
824 struct tbl *old_table = rcu_dereference(dp->table);
825 struct tbl *new_table;
827 new_table = tbl_create(0);
831 rcu_assign_pointer(dp->table, new_table);
833 tbl_deferred_destroy(old_table, flow_free_tbl);
838 static int validate_actions(const struct sw_flow_actions *actions)
842 for (i = 0; i < actions->n_actions; i++) {
843 const union odp_action *a = &actions->actions[i];
846 case ODPAT_CONTROLLER:
847 case ODPAT_STRIP_VLAN:
848 case ODPAT_SET_DL_SRC:
849 case ODPAT_SET_DL_DST:
850 case ODPAT_SET_NW_SRC:
851 case ODPAT_SET_NW_DST:
852 case ODPAT_SET_TP_SRC:
853 case ODPAT_SET_TP_DST:
854 case ODPAT_SET_TUNNEL:
855 case ODPAT_SET_PRIORITY:
856 case ODPAT_POP_PRIORITY:
857 case ODPAT_DROP_SPOOFED_ARP:
858 /* No validation needed. */
862 if (a->output.port >= DP_MAX_PORTS)
866 case ODPAT_SET_DL_TCI:
867 if (a->dl_tci.tci & htons(VLAN_CFI_MASK))
871 case ODPAT_SET_NW_TOS:
872 if (a->nw_tos.nw_tos & INET_ECN_MASK)
884 static struct sw_flow_actions *get_actions(const struct odp_flow *flow)
886 struct sw_flow_actions *actions;
889 actions = flow_actions_alloc(flow->n_actions);
890 error = PTR_ERR(actions);
895 if (copy_from_user(actions->actions, flow->actions,
896 flow->n_actions * sizeof(union odp_action)))
897 goto error_free_actions;
898 error = validate_actions(actions);
900 goto error_free_actions;
907 return ERR_PTR(error);
910 static void get_stats(struct sw_flow *flow, struct odp_flow_stats *stats)
913 struct timespec offset_ts, used, now_mono;
915 ktime_get_ts(&now_mono);
916 jiffies_to_timespec(jiffies - flow->used, &offset_ts);
917 set_normalized_timespec(&used, now_mono.tv_sec - offset_ts.tv_sec,
918 now_mono.tv_nsec - offset_ts.tv_nsec);
920 stats->used_sec = used.tv_sec;
921 stats->used_nsec = used.tv_nsec;
924 stats->used_nsec = 0;
927 stats->n_packets = flow->packet_count;
928 stats->n_bytes = flow->byte_count;
930 stats->tcp_flags = flow->tcp_flags;
934 static void clear_stats(struct sw_flow *flow)
938 flow->packet_count = 0;
939 flow->byte_count = 0;
942 static int expand_table(struct datapath *dp)
944 struct tbl *old_table = rcu_dereference(dp->table);
945 struct tbl *new_table;
947 new_table = tbl_expand(old_table);
948 if (IS_ERR(new_table))
949 return PTR_ERR(new_table);
951 rcu_assign_pointer(dp->table, new_table);
952 tbl_deferred_destroy(old_table, NULL);
957 static int do_put_flow(struct datapath *dp, struct odp_flow_put *uf,
958 struct odp_flow_stats *stats)
960 struct tbl_node *flow_node;
961 struct sw_flow *flow;
965 table = rcu_dereference(dp->table);
966 flow_node = tbl_lookup(table, &uf->flow.key, flow_hash(&uf->flow.key), flow_cmp);
969 struct sw_flow_actions *acts;
972 if (!(uf->flags & ODPPF_CREATE))
975 /* Expand table, if necessary, to make room. */
976 if (tbl_count(table) >= tbl_n_buckets(table)) {
977 error = expand_table(dp);
980 table = rcu_dereference(dp->table);
986 error = PTR_ERR(flow);
989 flow->key = uf->flow.key;
992 /* Obtain actions. */
993 acts = get_actions(&uf->flow);
994 error = PTR_ERR(acts);
996 goto error_free_flow;
997 rcu_assign_pointer(flow->sf_acts, acts);
999 /* Put flow in bucket. */
1000 error = tbl_insert(table, &flow->tbl_node, flow_hash(&flow->key));
1002 goto error_free_flow_acts;
1004 memset(stats, 0, sizeof(struct odp_flow_stats));
1006 /* We found a matching flow. */
1007 struct sw_flow_actions *old_acts, *new_acts;
1009 flow = flow_cast(flow_node);
1011 /* Bail out if we're not allowed to modify an existing flow. */
1013 if (!(uf->flags & ODPPF_MODIFY))
1017 new_acts = get_actions(&uf->flow);
1018 error = PTR_ERR(new_acts);
1019 if (IS_ERR(new_acts))
1021 old_acts = rcu_dereference(flow->sf_acts);
1022 if (old_acts->n_actions != new_acts->n_actions ||
1023 memcmp(old_acts->actions, new_acts->actions,
1024 sizeof(union odp_action) * old_acts->n_actions)) {
1025 rcu_assign_pointer(flow->sf_acts, new_acts);
1026 flow_deferred_free_acts(old_acts);
1031 /* Fetch stats, then clear them if necessary. */
1032 spin_lock_bh(&flow->lock);
1033 get_stats(flow, stats);
1034 if (uf->flags & ODPPF_ZERO_STATS)
1036 spin_unlock_bh(&flow->lock);
1041 error_free_flow_acts:
1042 kfree(flow->sf_acts);
1044 flow->sf_acts = NULL;
1050 static int put_flow(struct datapath *dp, struct odp_flow_put __user *ufp)
1052 struct odp_flow_stats stats;
1053 struct odp_flow_put uf;
1056 if (copy_from_user(&uf, ufp, sizeof(struct odp_flow_put)))
1059 error = do_put_flow(dp, &uf, &stats);
1063 if (copy_to_user(&ufp->flow.stats, &stats,
1064 sizeof(struct odp_flow_stats)))
1070 static int do_answer_query(struct sw_flow *flow, u32 query_flags,
1071 struct odp_flow_stats __user *ustats,
1072 union odp_action __user *actions,
1073 u32 __user *n_actionsp)
1075 struct sw_flow_actions *sf_acts;
1076 struct odp_flow_stats stats;
1079 spin_lock_bh(&flow->lock);
1080 get_stats(flow, &stats);
1081 if (query_flags & ODPFF_ZERO_TCP_FLAGS)
1082 flow->tcp_flags = 0;
1084 spin_unlock_bh(&flow->lock);
1086 if (copy_to_user(ustats, &stats, sizeof(struct odp_flow_stats)) ||
1087 get_user(n_actions, n_actionsp))
1093 sf_acts = rcu_dereference(flow->sf_acts);
1094 if (put_user(sf_acts->n_actions, n_actionsp) ||
1095 (actions && copy_to_user(actions, sf_acts->actions,
1096 sizeof(union odp_action) *
1097 min(sf_acts->n_actions, n_actions))))
1103 static int answer_query(struct sw_flow *flow, u32 query_flags,
1104 struct odp_flow __user *ufp)
1106 union odp_action *actions;
1108 if (get_user(actions, &ufp->actions))
1111 return do_answer_query(flow, query_flags,
1112 &ufp->stats, actions, &ufp->n_actions);
1115 static struct sw_flow *do_del_flow(struct datapath *dp, struct odp_flow_key *key)
1117 struct tbl *table = rcu_dereference(dp->table);
1118 struct tbl_node *flow_node;
1121 flow_node = tbl_lookup(table, key, flow_hash(key), flow_cmp);
1123 return ERR_PTR(-ENOENT);
1125 error = tbl_remove(table, flow_node);
1127 return ERR_PTR(error);
1129 /* XXX Returned flow_node's statistics might lose a few packets, since
1130 * other CPUs can be using this flow. We used to synchronize_rcu() to
1131 * make sure that we get completely accurate stats, but that blows our
1132 * performance, badly. */
1133 return flow_cast(flow_node);
1136 static int del_flow(struct datapath *dp, struct odp_flow __user *ufp)
1138 struct sw_flow *flow;
1142 if (copy_from_user(&uf, ufp, sizeof uf))
1145 flow = do_del_flow(dp, &uf.key);
1147 return PTR_ERR(flow);
1149 error = answer_query(flow, 0, ufp);
1150 flow_deferred_free(flow);
1154 static int do_query_flows(struct datapath *dp, const struct odp_flowvec *flowvec)
1156 struct tbl *table = rcu_dereference(dp->table);
1159 for (i = 0; i < flowvec->n_flows; i++) {
1160 struct odp_flow __user *ufp = &flowvec->flows[i];
1162 struct tbl_node *flow_node;
1165 if (copy_from_user(&uf, ufp, sizeof uf))
1168 flow_node = tbl_lookup(table, &uf.key, flow_hash(&uf.key), flow_cmp);
1170 error = put_user(ENOENT, &ufp->stats.error);
1172 error = answer_query(flow_cast(flow_node), uf.flags, ufp);
1176 return flowvec->n_flows;
1179 struct list_flows_cbdata {
1180 struct odp_flow __user *uflows;
1185 static int list_flow(struct tbl_node *node, void *cbdata_)
1187 struct sw_flow *flow = flow_cast(node);
1188 struct list_flows_cbdata *cbdata = cbdata_;
1189 struct odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++];
1192 if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key))
1194 error = answer_query(flow, 0, ufp);
1198 if (cbdata->listed_flows >= cbdata->n_flows)
1199 return cbdata->listed_flows;
1203 static int do_list_flows(struct datapath *dp, const struct odp_flowvec *flowvec)
1205 struct list_flows_cbdata cbdata;
1208 if (!flowvec->n_flows)
1211 cbdata.uflows = flowvec->flows;
1212 cbdata.n_flows = flowvec->n_flows;
1213 cbdata.listed_flows = 0;
1215 error = tbl_foreach(rcu_dereference(dp->table), list_flow, &cbdata);
1216 return error ? error : cbdata.listed_flows;
1219 static int do_flowvec_ioctl(struct datapath *dp, unsigned long argp,
1220 int (*function)(struct datapath *,
1221 const struct odp_flowvec *))
1223 struct odp_flowvec __user *uflowvec;
1224 struct odp_flowvec flowvec;
1227 uflowvec = (struct odp_flowvec __user *)argp;
1228 if (copy_from_user(&flowvec, uflowvec, sizeof flowvec))
1231 if (flowvec.n_flows > INT_MAX / sizeof(struct odp_flow))
1234 retval = function(dp, &flowvec);
1235 return (retval < 0 ? retval
1236 : retval == flowvec.n_flows ? 0
1237 : put_user(retval, &uflowvec->n_flows));
1240 static int do_execute(struct datapath *dp, const struct odp_execute *execute)
1242 struct odp_flow_key key;
1243 struct sk_buff *skb;
1244 struct sw_flow_actions *actions;
1250 if (execute->length < ETH_HLEN || execute->length > 65535)
1253 actions = flow_actions_alloc(execute->n_actions);
1254 if (IS_ERR(actions)) {
1255 err = PTR_ERR(actions);
1260 if (copy_from_user(actions->actions, execute->actions,
1261 execute->n_actions * sizeof *execute->actions))
1262 goto error_free_actions;
1264 err = validate_actions(actions);
1266 goto error_free_actions;
1269 skb = alloc_skb(execute->length, GFP_KERNEL);
1271 goto error_free_actions;
1274 if (copy_from_user(skb_put(skb, execute->length), execute->data,
1276 goto error_free_skb;
1278 skb_reset_mac_header(skb);
1281 /* Normally, setting the skb 'protocol' field would be handled by a
1282 * call to eth_type_trans(), but it assumes there's a sending
1283 * device, which we may not have. */
1284 if (ntohs(eth->h_proto) >= 1536)
1285 skb->protocol = eth->h_proto;
1287 skb->protocol = htons(ETH_P_802_2);
1289 err = flow_extract(skb, -1, &key, &is_frag);
1291 goto error_free_skb;
1294 err = execute_actions(dp, skb, &key, actions->actions, actions->n_actions);
1308 static int execute_packet(struct datapath *dp, const struct odp_execute __user *executep)
1310 struct odp_execute execute;
1312 if (copy_from_user(&execute, executep, sizeof execute))
1315 return do_execute(dp, &execute);
1318 static int get_dp_stats(struct datapath *dp, struct odp_stats __user *statsp)
1320 struct tbl *table = rcu_dereference(dp->table);
1321 struct odp_stats stats;
1324 stats.n_flows = tbl_count(table);
1325 stats.cur_capacity = tbl_n_buckets(table);
1326 stats.max_capacity = TBL_MAX_BUCKETS;
1327 stats.n_ports = dp->n_ports;
1328 stats.max_ports = DP_MAX_PORTS;
1329 stats.n_frags = stats.n_hit = stats.n_missed = stats.n_lost = 0;
1330 for_each_possible_cpu(i) {
1331 const struct dp_stats_percpu *percpu_stats;
1332 struct dp_stats_percpu local_stats;
1335 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
1338 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
1339 local_stats = *percpu_stats;
1340 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
1342 stats.n_frags += local_stats.n_frags;
1343 stats.n_hit += local_stats.n_hit;
1344 stats.n_missed += local_stats.n_missed;
1345 stats.n_lost += local_stats.n_lost;
1347 stats.max_miss_queue = DP_MAX_QUEUE_LEN;
1348 stats.max_action_queue = DP_MAX_QUEUE_LEN;
1349 return copy_to_user(statsp, &stats, sizeof stats) ? -EFAULT : 0;
1352 /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports */
1353 int dp_min_mtu(const struct datapath *dp)
1360 list_for_each_entry_rcu (p, &dp->port_list, node) {
1363 /* Skip any internal ports, since that's what we're trying to
1365 if (is_internal_vport(p))
1368 dev_mtu = vport_get_mtu(p);
1369 if (!mtu || dev_mtu < mtu)
1373 return mtu ? mtu : ETH_DATA_LEN;
1376 /* Sets the MTU of all datapath devices to the minimum of the ports. Must
1377 * be called with RTNL lock. */
1378 void set_internal_devs_mtu(const struct datapath *dp)
1385 mtu = dp_min_mtu(dp);
1387 list_for_each_entry_rcu (p, &dp->port_list, node) {
1388 if (is_internal_vport(p))
1389 vport_set_mtu(p, mtu);
1393 static int put_port(const struct vport *p, struct odp_port __user *uop)
1397 memset(&op, 0, sizeof op);
1400 strncpy(op.devname, vport_get_name(p), sizeof op.devname);
1401 strncpy(op.type, vport_get_type(p), sizeof op.type);
1404 op.port = p->port_no;
1406 return copy_to_user(uop, &op, sizeof op) ? -EFAULT : 0;
1409 static int query_port(struct datapath *dp, struct odp_port __user *uport)
1411 struct odp_port port;
1413 if (copy_from_user(&port, uport, sizeof port))
1416 if (port.devname[0]) {
1417 struct vport *vport;
1420 port.devname[IFNAMSIZ - 1] = '\0';
1425 vport = vport_locate(port.devname);
1430 if (vport->dp != dp) {
1435 port.port = vport->port_no;
1444 if (port.port >= DP_MAX_PORTS)
1446 if (!dp->ports[port.port])
1450 return put_port(dp->ports[port.port], uport);
1453 static int do_list_ports(struct datapath *dp, struct odp_port __user *uports,
1460 list_for_each_entry_rcu (p, &dp->port_list, node) {
1461 if (put_port(p, &uports[idx]))
1463 if (idx++ >= n_ports)
1470 static int list_ports(struct datapath *dp, struct odp_portvec __user *upv)
1472 struct odp_portvec pv;
1475 if (copy_from_user(&pv, upv, sizeof pv))
1478 retval = do_list_ports(dp, pv.ports, pv.n_ports);
1482 return put_user(retval, &upv->n_ports);
1485 static int get_listen_mask(const struct file *f)
1487 return (long)f->private_data;
1490 static void set_listen_mask(struct file *f, int listen_mask)
1492 f->private_data = (void*)(long)listen_mask;
1495 static long openvswitch_ioctl(struct file *f, unsigned int cmd,
1498 int dp_idx = iminor(f->f_dentry->d_inode);
1499 struct datapath *dp;
1500 int drop_frags, listeners, port_no;
1501 unsigned int sflow_probability;
1504 /* Handle commands with special locking requirements up front. */
1507 err = create_dp(dp_idx, (char __user *)argp);
1510 case ODP_DP_DESTROY:
1511 err = destroy_dp(dp_idx);
1514 case ODP_VPORT_ATTACH:
1515 err = attach_port(dp_idx, (struct odp_port __user *)argp);
1518 case ODP_VPORT_DETACH:
1519 err = get_user(port_no, (int __user *)argp);
1521 err = detach_port(dp_idx, port_no);
1525 err = vport_user_mod((struct odp_port __user *)argp);
1528 case ODP_VPORT_STATS_GET:
1529 err = vport_user_stats_get((struct odp_vport_stats_req __user *)argp);
1532 case ODP_VPORT_STATS_SET:
1533 err = vport_user_stats_set((struct odp_vport_stats_req __user *)argp);
1536 case ODP_VPORT_ETHER_GET:
1537 err = vport_user_ether_get((struct odp_vport_ether __user *)argp);
1540 case ODP_VPORT_ETHER_SET:
1541 err = vport_user_ether_set((struct odp_vport_ether __user *)argp);
1544 case ODP_VPORT_MTU_GET:
1545 err = vport_user_mtu_get((struct odp_vport_mtu __user *)argp);
1548 case ODP_VPORT_MTU_SET:
1549 err = vport_user_mtu_set((struct odp_vport_mtu __user *)argp);
1553 dp = get_dp_locked(dp_idx);
1560 err = get_dp_stats(dp, (struct odp_stats __user *)argp);
1563 case ODP_GET_DROP_FRAGS:
1564 err = put_user(dp->drop_frags, (int __user *)argp);
1567 case ODP_SET_DROP_FRAGS:
1568 err = get_user(drop_frags, (int __user *)argp);
1572 if (drop_frags != 0 && drop_frags != 1)
1574 dp->drop_frags = drop_frags;
1578 case ODP_GET_LISTEN_MASK:
1579 err = put_user(get_listen_mask(f), (int __user *)argp);
1582 case ODP_SET_LISTEN_MASK:
1583 err = get_user(listeners, (int __user *)argp);
1587 if (listeners & ~ODPL_ALL)
1590 set_listen_mask(f, listeners);
1593 case ODP_GET_SFLOW_PROBABILITY:
1594 err = put_user(dp->sflow_probability, (unsigned int __user *)argp);
1597 case ODP_SET_SFLOW_PROBABILITY:
1598 err = get_user(sflow_probability, (unsigned int __user *)argp);
1600 dp->sflow_probability = sflow_probability;
1603 case ODP_VPORT_QUERY:
1604 err = query_port(dp, (struct odp_port __user *)argp);
1607 case ODP_VPORT_LIST:
1608 err = list_ports(dp, (struct odp_portvec __user *)argp);
1611 case ODP_FLOW_FLUSH:
1612 err = flush_flows(dp);
1616 err = put_flow(dp, (struct odp_flow_put __user *)argp);
1620 err = del_flow(dp, (struct odp_flow __user *)argp);
1624 err = do_flowvec_ioctl(dp, argp, do_query_flows);
1628 err = do_flowvec_ioctl(dp, argp, do_list_flows);
1632 err = execute_packet(dp, (struct odp_execute __user *)argp);
1639 mutex_unlock(&dp->mutex);
1644 static int dp_has_packet_of_interest(struct datapath *dp, int listeners)
1647 for (i = 0; i < DP_N_QUEUES; i++) {
1648 if (listeners & (1 << i) && !skb_queue_empty(&dp->queues[i]))
1654 #ifdef CONFIG_COMPAT
1655 static int compat_list_ports(struct datapath *dp, struct compat_odp_portvec __user *upv)
1657 struct compat_odp_portvec pv;
1660 if (copy_from_user(&pv, upv, sizeof pv))
1663 retval = do_list_ports(dp, compat_ptr(pv.ports), pv.n_ports);
1667 return put_user(retval, &upv->n_ports);
1670 static int compat_get_flow(struct odp_flow *flow, const struct compat_odp_flow __user *compat)
1672 compat_uptr_t actions;
1674 if (!access_ok(VERIFY_READ, compat, sizeof(struct compat_odp_flow)) ||
1675 __copy_from_user(&flow->stats, &compat->stats, sizeof(struct odp_flow_stats)) ||
1676 __copy_from_user(&flow->key, &compat->key, sizeof(struct odp_flow_key)) ||
1677 __get_user(actions, &compat->actions) ||
1678 __get_user(flow->n_actions, &compat->n_actions) ||
1679 __get_user(flow->flags, &compat->flags))
1682 flow->actions = compat_ptr(actions);
1686 static int compat_put_flow(struct datapath *dp, struct compat_odp_flow_put __user *ufp)
1688 struct odp_flow_stats stats;
1689 struct odp_flow_put fp;
1692 if (compat_get_flow(&fp.flow, &ufp->flow) ||
1693 get_user(fp.flags, &ufp->flags))
1696 error = do_put_flow(dp, &fp, &stats);
1700 if (copy_to_user(&ufp->flow.stats, &stats,
1701 sizeof(struct odp_flow_stats)))
1707 static int compat_answer_query(struct sw_flow *flow, u32 query_flags,
1708 struct compat_odp_flow __user *ufp)
1710 compat_uptr_t actions;
1712 if (get_user(actions, &ufp->actions))
1715 return do_answer_query(flow, query_flags, &ufp->stats,
1716 compat_ptr(actions), &ufp->n_actions);
1719 static int compat_del_flow(struct datapath *dp, struct compat_odp_flow __user *ufp)
1721 struct sw_flow *flow;
1725 if (compat_get_flow(&uf, ufp))
1728 flow = do_del_flow(dp, &uf.key);
1730 return PTR_ERR(flow);
1732 error = compat_answer_query(flow, 0, ufp);
1733 flow_deferred_free(flow);
1737 static int compat_query_flows(struct datapath *dp, struct compat_odp_flow *flows, u32 n_flows)
1739 struct tbl *table = rcu_dereference(dp->table);
1742 for (i = 0; i < n_flows; i++) {
1743 struct compat_odp_flow __user *ufp = &flows[i];
1745 struct tbl_node *flow_node;
1748 if (compat_get_flow(&uf, ufp))
1751 flow_node = tbl_lookup(table, &uf.key, flow_hash(&uf.key), flow_cmp);
1753 error = put_user(ENOENT, &ufp->stats.error);
1755 error = compat_answer_query(flow_cast(flow_node), uf.flags, ufp);
1762 struct compat_list_flows_cbdata {
1763 struct compat_odp_flow __user *uflows;
1768 static int compat_list_flow(struct tbl_node *node, void *cbdata_)
1770 struct sw_flow *flow = flow_cast(node);
1771 struct compat_list_flows_cbdata *cbdata = cbdata_;
1772 struct compat_odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++];
1775 if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key))
1777 error = compat_answer_query(flow, 0, ufp);
1781 if (cbdata->listed_flows >= cbdata->n_flows)
1782 return cbdata->listed_flows;
1786 static int compat_list_flows(struct datapath *dp, struct compat_odp_flow *flows, u32 n_flows)
1788 struct compat_list_flows_cbdata cbdata;
1794 cbdata.uflows = flows;
1795 cbdata.n_flows = n_flows;
1796 cbdata.listed_flows = 0;
1798 error = tbl_foreach(rcu_dereference(dp->table), compat_list_flow, &cbdata);
1799 return error ? error : cbdata.listed_flows;
1802 static int compat_flowvec_ioctl(struct datapath *dp, unsigned long argp,
1803 int (*function)(struct datapath *,
1804 struct compat_odp_flow *,
1807 struct compat_odp_flowvec __user *uflowvec;
1808 struct compat_odp_flow __user *flows;
1809 struct compat_odp_flowvec flowvec;
1812 uflowvec = compat_ptr(argp);
1813 if (!access_ok(VERIFY_WRITE, uflowvec, sizeof *uflowvec) ||
1814 copy_from_user(&flowvec, uflowvec, sizeof flowvec))
1817 if (flowvec.n_flows > INT_MAX / sizeof(struct compat_odp_flow))
1820 flows = compat_ptr(flowvec.flows);
1821 if (!access_ok(VERIFY_WRITE, flows,
1822 flowvec.n_flows * sizeof(struct compat_odp_flow)))
1825 retval = function(dp, flows, flowvec.n_flows);
1826 return (retval < 0 ? retval
1827 : retval == flowvec.n_flows ? 0
1828 : put_user(retval, &uflowvec->n_flows));
1831 static int compat_execute(struct datapath *dp, const struct compat_odp_execute __user *uexecute)
1833 struct odp_execute execute;
1834 compat_uptr_t actions;
1837 if (!access_ok(VERIFY_READ, uexecute, sizeof(struct compat_odp_execute)) ||
1838 __get_user(actions, &uexecute->actions) ||
1839 __get_user(execute.n_actions, &uexecute->n_actions) ||
1840 __get_user(data, &uexecute->data) ||
1841 __get_user(execute.length, &uexecute->length))
1844 execute.actions = compat_ptr(actions);
1845 execute.data = compat_ptr(data);
1847 return do_execute(dp, &execute);
1850 static long openvswitch_compat_ioctl(struct file *f, unsigned int cmd, unsigned long argp)
1852 int dp_idx = iminor(f->f_dentry->d_inode);
1853 struct datapath *dp;
1857 case ODP_DP_DESTROY:
1858 case ODP_FLOW_FLUSH:
1859 /* Ioctls that don't need any translation at all. */
1860 return openvswitch_ioctl(f, cmd, argp);
1863 case ODP_VPORT_ATTACH:
1864 case ODP_VPORT_DETACH:
1866 case ODP_VPORT_MTU_SET:
1867 case ODP_VPORT_MTU_GET:
1868 case ODP_VPORT_ETHER_SET:
1869 case ODP_VPORT_ETHER_GET:
1870 case ODP_VPORT_STATS_SET:
1871 case ODP_VPORT_STATS_GET:
1873 case ODP_GET_DROP_FRAGS:
1874 case ODP_SET_DROP_FRAGS:
1875 case ODP_SET_LISTEN_MASK:
1876 case ODP_GET_LISTEN_MASK:
1877 case ODP_SET_SFLOW_PROBABILITY:
1878 case ODP_GET_SFLOW_PROBABILITY:
1879 case ODP_VPORT_QUERY:
1880 /* Ioctls that just need their pointer argument extended. */
1881 return openvswitch_ioctl(f, cmd, (unsigned long)compat_ptr(argp));
1884 dp = get_dp_locked(dp_idx);
1890 case ODP_VPORT_LIST32:
1891 err = compat_list_ports(dp, compat_ptr(argp));
1894 case ODP_FLOW_PUT32:
1895 err = compat_put_flow(dp, compat_ptr(argp));
1898 case ODP_FLOW_DEL32:
1899 err = compat_del_flow(dp, compat_ptr(argp));
1902 case ODP_FLOW_GET32:
1903 err = compat_flowvec_ioctl(dp, argp, compat_query_flows);
1906 case ODP_FLOW_LIST32:
1907 err = compat_flowvec_ioctl(dp, argp, compat_list_flows);
1911 err = compat_execute(dp, compat_ptr(argp));
1918 mutex_unlock(&dp->mutex);
1924 /* Unfortunately this function is not exported so this is a verbatim copy
1925 * from net/core/datagram.c in 2.6.30. */
1926 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
1927 u8 __user *to, int len,
1930 int start = skb_headlen(skb);
1932 int i, copy = start - offset;
1939 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
1943 if ((len -= copy) == 0)
1950 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1953 WARN_ON(start > offset + len);
1955 end = start + skb_shinfo(skb)->frags[i].size;
1956 if ((copy = end - offset) > 0) {
1960 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1961 struct page *page = frag->page;
1966 csum2 = csum_and_copy_to_user(vaddr +
1973 *csump = csum_block_add(*csump, csum2, pos);
1983 if (skb_shinfo(skb)->frag_list) {
1984 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1986 for (; list; list=list->next) {
1989 WARN_ON(start > offset + len);
1991 end = start + list->len;
1992 if ((copy = end - offset) > 0) {
1996 if (skb_copy_and_csum_datagram(list,
2001 *csump = csum_block_add(*csump, csum2, pos);
2002 if ((len -= copy) == 0)
2018 ssize_t openvswitch_read(struct file *f, char __user *buf, size_t nbytes,
2021 /* XXX is there sufficient synchronization here? */
2022 int listeners = get_listen_mask(f);
2023 int dp_idx = iminor(f->f_dentry->d_inode);
2024 struct datapath *dp = get_dp(dp_idx);
2025 struct sk_buff *skb;
2026 size_t copy_bytes, tot_copy_bytes;
2032 if (nbytes == 0 || !listeners)
2038 for (i = 0; i < DP_N_QUEUES; i++) {
2039 if (listeners & (1 << i)) {
2040 skb = skb_dequeue(&dp->queues[i]);
2046 if (f->f_flags & O_NONBLOCK) {
2051 wait_event_interruptible(dp->waitqueue,
2052 dp_has_packet_of_interest(dp,
2055 if (signal_pending(current)) {
2056 retval = -ERESTARTSYS;
2061 copy_bytes = tot_copy_bytes = min_t(size_t, skb->len, nbytes);
2064 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2065 if (copy_bytes == skb->len) {
2067 unsigned int csum_start, csum_offset;
2069 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
2070 csum_start = skb->csum_start - skb_headroom(skb);
2071 csum_offset = skb->csum_offset;
2073 csum_start = skb_transport_header(skb) - skb->data;
2074 csum_offset = skb->csum;
2076 BUG_ON(csum_start >= skb_headlen(skb));
2077 retval = skb_copy_and_csum_datagram(skb, csum_start, buf + csum_start,
2078 copy_bytes - csum_start, &csum);
2080 __sum16 __user *csump;
2082 copy_bytes = csum_start;
2083 csump = (__sum16 __user *)(buf + csum_start + csum_offset);
2085 BUG_ON((char *)csump + sizeof(__sum16) > buf + nbytes);
2086 put_user(csum_fold(csum), csump);
2089 retval = skb_checksum_help(skb);
2093 struct iovec __user iov;
2096 iov.iov_len = copy_bytes;
2097 retval = skb_copy_datagram_iovec(skb, 0, &iov, iov.iov_len);
2101 retval = tot_copy_bytes;
2109 static unsigned int openvswitch_poll(struct file *file, poll_table *wait)
2111 /* XXX is there sufficient synchronization here? */
2112 int dp_idx = iminor(file->f_dentry->d_inode);
2113 struct datapath *dp = get_dp(dp_idx);
2118 poll_wait(file, &dp->waitqueue, wait);
2119 if (dp_has_packet_of_interest(dp, get_listen_mask(file)))
2120 mask |= POLLIN | POLLRDNORM;
2122 mask = POLLIN | POLLRDNORM | POLLHUP;
2127 struct file_operations openvswitch_fops = {
2128 /* XXX .aio_read = openvswitch_aio_read, */
2129 .read = openvswitch_read,
2130 .poll = openvswitch_poll,
2131 .unlocked_ioctl = openvswitch_ioctl,
2132 #ifdef CONFIG_COMPAT
2133 .compat_ioctl = openvswitch_compat_ioctl,
2135 /* XXX .fasync = openvswitch_fasync, */
2140 static int __init dp_init(void)
2142 struct sk_buff *dummy_skb;
2145 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2147 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2155 goto error_flow_exit;
2157 err = register_netdevice_notifier(&dp_device_notifier);
2159 goto error_vport_exit;
2161 major = register_chrdev(0, "openvswitch", &openvswitch_fops);
2163 goto error_unreg_notifier;
2167 error_unreg_notifier:
2168 unregister_netdevice_notifier(&dp_device_notifier);
2177 static void dp_cleanup(void)
2180 unregister_chrdev(major, "openvswitch");
2181 unregister_netdevice_notifier(&dp_device_notifier);
2186 module_init(dp_init);
2187 module_exit(dp_cleanup);
2189 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2190 MODULE_LICENSE("GPL");