datapath: Report memory allocation errors in flow_extract().
[sliver-openvswitch.git] / datapath / datapath.c
index 491f98a..a34049f 100644 (file)
@@ -32,7 +32,7 @@
 #include <asm/system.h>
 #include <asm/div64.h>
 #include <asm/bug.h>
-#include <asm/highmem.h>
+#include <linux/highmem.h>
 #include <linux/netfilter_bridge.h>
 #include <linux/netfilter_ipv4.h>
 #include <linux/inetdevice.h>
@@ -69,8 +69,22 @@ EXPORT_SYMBOL(dp_ioctl_hook);
 static struct datapath *dps[ODP_MAX];
 static DEFINE_MUTEX(dp_mutex);
 
-/* Number of milliseconds between runs of the maintenance thread. */
-#define MAINT_SLEEP_MSECS 1000
+/* We limit the number of times that we pass into dp_process_received_packet()
+ * to avoid blowing out the stack in the event that we have a loop. */
+struct loop_counter {
+       int count;              /* Count. */
+       bool looping;           /* Loop detected? */
+};
+
+#define DP_MAX_LOOPS 5
+
+/* We use a separate counter for each CPU for both interrupt and non-interrupt
+ * context in order to keep the limit deterministic for a given packet. */
+struct percpu_loop_counters {
+       struct loop_counter counters[2];
+};
+
+static DEFINE_PER_CPU(struct percpu_loop_counters, dp_loop_counters);
 
 static int new_dp_port(struct datapath *, struct odp_port *, int port_no);
 
@@ -380,6 +394,7 @@ static int new_dp_port(struct datapath *dp, struct odp_port *odp_port, int port_
 
        p->port_no = port_no;
        p->dp = dp;
+       p->vport = vport;
        atomic_set(&p->sflow_pool, 0);
 
        err = vport_attach(vport, p);
@@ -513,6 +528,14 @@ out:
        return err;
 }
 
+static void suppress_loop(struct datapath *dp, struct sw_flow_actions *actions)
+{
+       if (net_ratelimit())
+               printk(KERN_WARNING "%s: flow looped %d times, dropping\n",
+                      dp_name(dp), DP_MAX_LOOPS);
+       actions->n_actions = 0;
+}
+
 /* Must be called with rcu_read_lock. */
 void dp_process_received_packet(struct dp_port *p, struct sk_buff *skb)
 {
@@ -521,37 +544,71 @@ void dp_process_received_packet(struct dp_port *p, struct sk_buff *skb)
        int stats_counter_off;
        struct odp_flow_key key;
        struct tbl_node *flow_node;
-
-       WARN_ON_ONCE(skb_shared(skb));
-       skb_warn_if_lro(skb);
+       struct sw_flow *flow;
+       struct sw_flow_actions *acts;
+       struct loop_counter *loop;
+       int error;
 
        OVS_CB(skb)->dp_port = p;
 
-       if (flow_extract(skb, p ? p->port_no : ODPP_NONE, &key)) {
-               if (dp->drop_frags) {
-                       kfree_skb(skb);
-                       stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
-                       goto out;
-               }
+       /* Extract flow from 'skb' into 'key'. */
+       error = flow_extract(skb, p ? p->port_no : ODPP_NONE, &key);
+       if (unlikely(error)) {
+               kfree_skb(skb);
+               return;
        }
 
+       if (OVS_CB(skb)->is_frag && dp->drop_frags) {
+               kfree_skb(skb);
+               stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
+               goto out;
+       }
+
+       /* Look up flow. */
        flow_node = tbl_lookup(rcu_dereference(dp->table), &key, flow_hash(&key), flow_cmp);
-       if (flow_node) {
-               struct sw_flow *flow = flow_cast(flow_node);
-               struct sw_flow_actions *acts = rcu_dereference(flow->sf_acts);
-               flow_used(flow, skb);
-               execute_actions(dp, skb, &key, acts->actions, acts->n_actions,
-                               GFP_ATOMIC);
-               stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
-       } else {
-               stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
+       if (unlikely(!flow_node)) {
                dp_output_control(dp, skb, _ODPL_MISS_NR, OVS_CB(skb)->tun_id);
+               stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
+               goto out;
        }
 
+       flow = flow_cast(flow_node);
+       flow_used(flow, skb);
+
+       acts = rcu_dereference(flow->sf_acts);
+
+       /* Check whether we've looped too much. */
+       loop = &get_cpu_var(dp_loop_counters).counters[!!in_interrupt()];
+       if (unlikely(++loop->count > DP_MAX_LOOPS))
+               loop->looping = true;
+       if (unlikely(loop->looping)) {
+               suppress_loop(dp, acts);
+               goto out_loop;
+       }
+
+       /* Execute actions. */
+       execute_actions(dp, skb, &key, acts->actions, acts->n_actions, GFP_ATOMIC);
+       stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
+
+       /* Check whether sub-actions looped too much. */
+       if (unlikely(loop->looping))
+               suppress_loop(dp, acts);
+
+out_loop:
+       /* Decrement loop counter. */
+       if (!--loop->count)
+               loop->looping = false;
+       put_cpu_var(dp_loop_counters);
+
 out:
+       /* Update datapath statistics. */
        local_bh_disable();
        stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
+
+       write_seqcount_begin(&stats->seqlock);
        (*(u64 *)((u8 *)stats + stats_counter_off))++;
+       write_seqcount_end(&stats->seqlock);
+
        local_bh_enable();
 }
 
@@ -656,8 +713,7 @@ out:
  * be reverified).  If we receive a packet with CHECKSUM_HW that really means
  * CHECKSUM_PARTIAL, it will be sent with the wrong checksum.  However, there
  * shouldn't be any devices that do this with bridging. */
-void
-compute_ip_summed(struct sk_buff *skb, bool xmit)
+void compute_ip_summed(struct sk_buff *skb, bool xmit)
 {
        /* For our convenience these defines change repeatedly between kernel
         * versions, so we can't just copy them over... */
@@ -714,8 +770,7 @@ compute_ip_summed(struct sk_buff *skb, bool xmit)
  * is slightly different because we are only concerned with bridging and not
  * other types of forwarding and can get away with slightly more optimal
  * behavior.*/
-void
-forward_ip_summed(struct sk_buff *skb)
+void forward_ip_summed(struct sk_buff *skb)
 {
 #ifdef CHECKSUM_HW
        if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
@@ -725,9 +780,8 @@ forward_ip_summed(struct sk_buff *skb)
 
 /* Append each packet in 'skb' list to 'queue'.  There will be only one packet
  * unless we broke up a GSO packet. */
-static int
-queue_control_packets(struct sk_buff *skb, struct sk_buff_head *queue,
-                     int queue_no, u32 arg)
+static int queue_control_packets(struct sk_buff *skb, struct sk_buff_head *queue,
+                                int queue_no, u32 arg)
 {
        struct sk_buff *nskb;
        int port_no;
@@ -744,16 +798,6 @@ queue_control_packets(struct sk_buff *skb, struct sk_buff_head *queue,
                nskb = skb->next;
                skb->next = NULL;
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
-               /* Until 2.6.22, the start of the transport header was
-                * also the start of data to be checksummed.  Linux
-                * 2.6.22 introduced the csum_start field for this
-                * purpose, but we should point the transport header to
-                * it anyway for backward compatibility, as
-                * dev_queue_xmit() does even in 2.6.28. */
-               skb_set_transport_header(skb, skb->csum_start - skb_headroom(skb));
-#endif
-
                err = skb_cow(skb, sizeof *header);
                if (err)
                        goto err_kfree_skbs;
@@ -779,9 +823,8 @@ err_kfree_skbs:
        return err;
 }
 
-int
-dp_output_control(struct datapath *dp, struct sk_buff *skb, int queue_no,
-                 u32 arg)
+int dp_output_control(struct datapath *dp, struct sk_buff *skb, int queue_no,
+                     u32 arg)
 {
        struct dp_stats_percpu *stats;
        struct sk_buff_head *queue;
@@ -826,7 +869,11 @@ err_kfree_skb:
 err:
        local_bh_disable();
        stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
+
+       write_seqcount_begin(&stats->seqlock);
        stats->n_lost++;
+       write_seqcount_end(&stats->seqlock);
+
        local_bh_enable();
 
        return err;
@@ -917,27 +964,43 @@ error:
        return ERR_PTR(error);
 }
 
-static void get_stats(struct sw_flow *flow, struct odp_flow_stats *stats)
+static struct timespec get_time_offset(void)
 {
-       if (flow->used.tv_sec) {
-               stats->used_sec = flow->used.tv_sec;
-               stats->used_nsec = flow->used.tv_nsec;
+       struct timespec now_mono, now_jiffies;
+
+       ktime_get_ts(&now_mono);
+       jiffies_to_timespec(jiffies, &now_jiffies);
+       return timespec_sub(now_mono, now_jiffies);
+}
+
+static void get_stats(struct sw_flow *flow, struct odp_flow_stats *stats,
+                     struct timespec time_offset)
+{
+       if (flow->used) {
+               struct timespec flow_ts, used;
+
+               jiffies_to_timespec(flow->used, &flow_ts);
+               set_normalized_timespec(&used, flow_ts.tv_sec + time_offset.tv_sec,
+                                       flow_ts.tv_nsec + time_offset.tv_nsec);
+
+               stats->used_sec = used.tv_sec;
+               stats->used_nsec = used.tv_nsec;
        } else {
                stats->used_sec = 0;
                stats->used_nsec = 0;
        }
+
        stats->n_packets = flow->packet_count;
        stats->n_bytes = flow->byte_count;
-       stats->ip_tos = flow->ip_tos;
+       stats->reserved = 0;
        stats->tcp_flags = flow->tcp_flags;
        stats->error = 0;
 }
 
 static void clear_stats(struct sw_flow *flow)
 {
-       flow->used.tv_sec = flow->used.tv_nsec = 0;
+       flow->used = 0;
        flow->tcp_flags = 0;
-       flow->ip_tos = 0;
        flow->packet_count = 0;
        flow->byte_count = 0;
 }
@@ -1035,7 +1098,7 @@ static int do_put_flow(struct datapath *dp, struct odp_flow_put *uf,
 
                /* Fetch stats, then clear them if necessary. */
                spin_lock_bh(&flow->lock);
-               get_stats(flow, stats);
+               get_stats(flow, stats, get_time_offset());
                if (uf->flags & ODPPF_ZERO_STATS)
                        clear_stats(flow);
                spin_unlock_bh(&flow->lock);
@@ -1072,6 +1135,7 @@ static int put_flow(struct datapath *dp, struct odp_flow_put __user *ufp)
 }
 
 static int do_answer_query(struct sw_flow *flow, u32 query_flags,
+                          struct timespec time_offset,
                           struct odp_flow_stats __user *ustats,
                           union odp_action __user *actions,
                           u32 __user *n_actionsp)
@@ -1081,7 +1145,7 @@ static int do_answer_query(struct sw_flow *flow, u32 query_flags,
        u32 n_actions;
 
        spin_lock_bh(&flow->lock);
-       get_stats(flow, &stats);
+       get_stats(flow, &stats, time_offset);
        if (query_flags & ODPFF_ZERO_TCP_FLAGS)
                flow->tcp_flags = 0;
 
@@ -1105,6 +1169,7 @@ static int do_answer_query(struct sw_flow *flow, u32 query_flags,
 }
 
 static int answer_query(struct sw_flow *flow, u32 query_flags,
+                       struct timespec time_offset,
                        struct odp_flow __user *ufp)
 {
        union odp_action *actions;
@@ -1112,7 +1177,7 @@ static int answer_query(struct sw_flow *flow, u32 query_flags,
        if (get_user(actions, &ufp->actions))
                return -EFAULT;
 
-       return do_answer_query(flow, query_flags,
+       return do_answer_query(flow, query_flags, time_offset,
                               &ufp->stats, actions, &ufp->n_actions);
 }
 
@@ -1151,7 +1216,7 @@ static int del_flow(struct datapath *dp, struct odp_flow __user *ufp)
        if (IS_ERR(flow))
                return PTR_ERR(flow);
 
-       error = answer_query(flow, 0, ufp);
+       error = answer_query(flow, 0, get_time_offset(), ufp);
        flow_deferred_free(flow);
        return error;
 }
@@ -1159,8 +1224,11 @@ static int del_flow(struct datapath *dp, struct odp_flow __user *ufp)
 static int do_query_flows(struct datapath *dp, const struct odp_flowvec *flowvec)
 {
        struct tbl *table = rcu_dereference(dp->table);
+       struct timespec time_offset;
        u32 i;
 
+       time_offset = get_time_offset();
+
        for (i = 0; i < flowvec->n_flows; i++) {
                struct odp_flow __user *ufp = &flowvec->flows[i];
                struct odp_flow uf;
@@ -1175,7 +1243,7 @@ static int do_query_flows(struct datapath *dp, const struct odp_flowvec *flowvec
                if (!flow_node)
                        error = put_user(ENOENT, &ufp->stats.error);
                else
-                       error = answer_query(flow_cast(flow_node), uf.flags, ufp);
+                       error = answer_query(flow_cast(flow_node), uf.flags, time_offset, ufp);
                if (error)
                        return -EFAULT;
        }
@@ -1186,6 +1254,7 @@ struct list_flows_cbdata {
        struct odp_flow __user *uflows;
        u32 n_flows;
        u32 listed_flows;
+       struct timespec time_offset;
 };
 
 static int list_flow(struct tbl_node *node, void *cbdata_)
@@ -1197,7 +1266,7 @@ static int list_flow(struct tbl_node *node, void *cbdata_)
 
        if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key))
                return -EFAULT;
-       error = answer_query(flow, 0, ufp);
+       error = answer_query(flow, 0, cbdata->time_offset, ufp);
        if (error)
                return error;
 
@@ -1217,6 +1286,8 @@ static int do_list_flows(struct datapath *dp, const struct odp_flowvec *flowvec)
        cbdata.uflows = flowvec->flows;
        cbdata.n_flows = flowvec->n_flows;
        cbdata.listed_flows = 0;
+       cbdata.time_offset = get_time_offset();
+
        error = tbl_foreach(rcu_dereference(dp->table), list_flow, &cbdata);
        return error ? error : cbdata.listed_flows;
 }
@@ -1294,7 +1365,9 @@ static int do_execute(struct datapath *dp, const struct odp_execute *execute)
        else
                skb->protocol = htons(ETH_P_802_2);
 
-       flow_extract(skb, execute->in_port, &key);
+       err = flow_extract(skb, execute->in_port, &key);
+       if (err)
+               goto error_free_skb;
 
        rcu_read_lock();
        err = execute_actions(dp, skb, &key, actions->actions,
@@ -1336,12 +1409,21 @@ static int get_dp_stats(struct datapath *dp, struct odp_stats __user *statsp)
        stats.max_groups = DP_MAX_GROUPS;
        stats.n_frags = stats.n_hit = stats.n_missed = stats.n_lost = 0;
        for_each_possible_cpu(i) {
-               const struct dp_stats_percpu *s;
-               s = per_cpu_ptr(dp->stats_percpu, i);
-               stats.n_frags += s->n_frags;
-               stats.n_hit += s->n_hit;
-               stats.n_missed += s->n_missed;
-               stats.n_lost += s->n_lost;
+               const struct dp_stats_percpu *percpu_stats;
+               struct dp_stats_percpu local_stats;
+               unsigned seqcount;
+
+               percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
+
+               do {
+                       seqcount = read_seqcount_begin(&percpu_stats->seqlock);
+                       local_stats = *percpu_stats;
+               } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
+
+               stats.n_frags += local_stats.n_frags;
+               stats.n_hit += local_stats.n_hit;
+               stats.n_missed += local_stats.n_missed;
+               stats.n_lost += local_stats.n_lost;
        }
        stats.max_miss_queue = DP_MAX_QUEUE_LEN;
        stats.max_action_queue = DP_MAX_QUEUE_LEN;
@@ -1389,8 +1471,7 @@ void set_internal_devs_mtu(const struct datapath *dp)
        }
 }
 
-static int
-put_port(const struct dp_port *p, struct odp_port __user *uop)
+static int put_port(const struct dp_port *p, struct odp_port __user *uop)
 {
        struct odp_port op;
 
@@ -1406,8 +1487,7 @@ put_port(const struct dp_port *p, struct odp_port __user *uop)
        return copy_to_user(uop, &op, sizeof op) ? -EFAULT : 0;
 }
 
-static int
-query_port(struct datapath *dp, struct odp_port __user *uport)
+static int query_port(struct datapath *dp, struct odp_port __user *uport)
 {
        struct odp_port port;
 
@@ -1454,8 +1534,8 @@ error_unlock:
        return put_port(dp->ports[port.port], uport);
 }
 
-static int
-do_list_ports(struct datapath *dp, struct odp_port __user *uports, int n_ports)
+static int do_list_ports(struct datapath *dp, struct odp_port __user *uports,
+                        int n_ports)
 {
        int idx = 0;
        if (n_ports) {
@@ -1471,8 +1551,7 @@ do_list_ports(struct datapath *dp, struct odp_port __user *uports, int n_ports)
        return idx;
 }
 
-static int
-list_ports(struct datapath *dp, struct odp_portvec __user *upv)
+static int list_ports(struct datapath *dp, struct odp_portvec __user *upv)
 {
        struct odp_portvec pv;
        int retval;
@@ -1494,8 +1573,8 @@ static void free_port_group(struct rcu_head *rcu)
        kfree(g);
 }
 
-static int
-do_set_port_group(struct datapath *dp, u16 __user *ports, int n_ports, int group)
+static int do_set_port_group(struct datapath *dp, u16 __user *ports,
+                            int n_ports, int group)
 {
        struct dp_port_group *new_group, *old_group;
        int error;
@@ -1526,8 +1605,8 @@ error:
        return error;
 }
 
-static int
-set_port_group(struct datapath *dp, const struct odp_port_group __user *upg)
+static int set_port_group(struct datapath *dp,
+                         const struct odp_port_group __user *upg)
 {
        struct odp_port_group pg;
 
@@ -1537,10 +1616,9 @@ set_port_group(struct datapath *dp, const struct odp_port_group __user *upg)
        return do_set_port_group(dp, pg.ports, pg.n_ports, pg.group);
 }
 
-static int
-do_get_port_group(struct datapath *dp,
-                 u16 __user *ports, int n_ports, int group,
-                 u16 __user *n_portsp)
+static int do_get_port_group(struct datapath *dp,
+                            u16 __user *ports, int n_ports, int group,
+                            u16 __user *n_portsp)
 {
        struct dp_port_group *g;
        u16 n_copy;
@@ -1829,6 +1907,7 @@ static int compat_put_flow(struct datapath *dp, struct compat_odp_flow_put __use
 }
 
 static int compat_answer_query(struct sw_flow *flow, u32 query_flags,
+                              struct timespec time_offset,
                               struct compat_odp_flow __user *ufp)
 {
        compat_uptr_t actions;
@@ -1836,7 +1915,7 @@ static int compat_answer_query(struct sw_flow *flow, u32 query_flags,
        if (get_user(actions, &ufp->actions))
                return -EFAULT;
 
-       return do_answer_query(flow, query_flags, &ufp->stats,
+       return do_answer_query(flow, query_flags, time_offset, &ufp->stats,
                               compat_ptr(actions), &ufp->n_actions);
 }
 
@@ -1853,7 +1932,7 @@ static int compat_del_flow(struct datapath *dp, struct compat_odp_flow __user *u
        if (IS_ERR(flow))
                return PTR_ERR(flow);
 
-       error = compat_answer_query(flow, 0, ufp);
+       error = compat_answer_query(flow, 0, get_time_offset(), ufp);
        flow_deferred_free(flow);
        return error;
 }
@@ -1861,8 +1940,11 @@ static int compat_del_flow(struct datapath *dp, struct compat_odp_flow __user *u
 static int compat_query_flows(struct datapath *dp, struct compat_odp_flow *flows, u32 n_flows)
 {
        struct tbl *table = rcu_dereference(dp->table);
+       struct timespec time_offset;
        u32 i;
 
+       time_offset = get_time_offset();
+
        for (i = 0; i < n_flows; i++) {
                struct compat_odp_flow __user *ufp = &flows[i];
                struct odp_flow uf;
@@ -1877,7 +1959,7 @@ static int compat_query_flows(struct datapath *dp, struct compat_odp_flow *flows
                if (!flow_node)
                        error = put_user(ENOENT, &ufp->stats.error);
                else
-                       error = compat_answer_query(flow_cast(flow_node), uf.flags, ufp);
+                       error = compat_answer_query(flow_cast(flow_node), uf.flags, time_offset, ufp);
                if (error)
                        return -EFAULT;
        }
@@ -1888,6 +1970,7 @@ struct compat_list_flows_cbdata {
        struct compat_odp_flow __user *uflows;
        u32 n_flows;
        u32 listed_flows;
+       struct timespec time_offset;
 };
 
 static int compat_list_flow(struct tbl_node *node, void *cbdata_)
@@ -1899,7 +1982,7 @@ static int compat_list_flow(struct tbl_node *node, void *cbdata_)
 
        if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key))
                return -EFAULT;
-       error = compat_answer_query(flow, 0, ufp);
+       error = compat_answer_query(flow, 0, cbdata->time_offset, ufp);
        if (error)
                return error;
 
@@ -1919,6 +2002,8 @@ static int compat_list_flows(struct datapath *dp, struct compat_odp_flow *flows,
        cbdata.uflows = flows;
        cbdata.n_flows = n_flows;
        cbdata.listed_flows = 0;
+       cbdata.time_offset = get_time_offset();
+
        error = tbl_foreach(rcu_dereference(dp->table), compat_list_flow, &cbdata);
        return error ? error : cbdata.listed_flows;
 }
@@ -2201,27 +2286,31 @@ success:
        
        retval = 0;
        if (skb->ip_summed == CHECKSUM_PARTIAL) {
-               __wsum csum = 0;
-               int csum_start, csum_offset;
+               if (copy_bytes == skb->len) {
+                       __wsum csum = 0;
+                       unsigned int csum_start, csum_offset;
 
-               csum_start = skb_transport_header(skb) - skb->data;
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
-               csum_offset = skb->csum_offset;
+                       csum_start = skb->csum_start - skb_headroom(skb);
+                       csum_offset = skb->csum_offset;
 #else
-               csum_offset = skb->csum;
+                       csum_start = skb_transport_header(skb) - skb->data;
+                       csum_offset = skb->csum;
 #endif
-               if (csum_start + csum_offset + sizeof(__sum16) <= copy_bytes) {
+                       BUG_ON(csum_start >= skb_headlen(skb));
                        retval = skb_copy_and_csum_datagram(skb, csum_start, buf + csum_start,
                                                            copy_bytes - csum_start, &csum);
-
                        if (!retval) {
                                __sum16 __user *csump;
 
                                copy_bytes = csum_start;
                                csump = (__sum16 __user *)(buf + csum_start + csum_offset);
+
+                               BUG_ON((char *)csump + sizeof(__sum16) > buf + nbytes);
                                put_user(csum_fold(csum), csump);
                        }
-               }
+               } else
+                       retval = skb_checksum_help(skb);
        }
 
        if (!retval) {