datapath: Add support for kernels 3.13
[sliver-openvswitch.git] / datapath / linux / compat / utils.c
1 #include <linux/module.h>
2 #include <linux/jiffies.h>
3 #include <linux/kernel.h>
4 #include <linux/ctype.h>
5 #include <linux/inet.h>
6 #include <linux/mm.h>
7 #include <linux/net.h>
8 #include <net/checksum.h>
9 #include <net/ip.h>
10 #include <linux/string.h>
11 #include <linux/types.h>
12 #include <linux/percpu.h>
13 #include <linux/init.h>
14 #include <linux/ratelimit.h>
15
16 #include <net/sock.h>
17
18 #include <asm/byteorder.h>
19 #include <asm/uaccess.h>
20
21 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
22 void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,
23                                const __be32 *from, const __be32 *to,
24                                int pseudohdr)
25 {
26         __be32 diff[] = {
27                 ~from[0], ~from[1], ~from[2], ~from[3],
28                 to[0], to[1], to[2], to[3],
29         };
30         if (skb->ip_summed != CHECKSUM_PARTIAL) {
31                 *sum = csum_fold(csum_partial(diff, sizeof(diff),
32                                         ~csum_unfold(*sum)));
33                 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
34                         skb->csum = ~csum_partial(diff, sizeof(diff),
35                                         ~skb->csum);
36         } else if (pseudohdr)
37                 *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
38                                         csum_unfold(*sum)));
39 }
40 #endif
41
42 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0)
43 bool __net_get_random_once(void *buf, int nbytes, bool *done,
44                            atomic_t *done_key)
45 {
46         static DEFINE_SPINLOCK(lock);
47         unsigned long flags;
48
49         spin_lock_irqsave(&lock, flags);
50         if (*done) {
51                 spin_unlock_irqrestore(&lock, flags);
52                 return false;
53         }
54
55         get_random_bytes(buf, nbytes);
56         *done = true;
57         spin_unlock_irqrestore(&lock, flags);
58
59         atomic_set(done_key, 1);
60
61         return true;
62 }
63 #endif