dc4df2aaaaa8f34ae654cee35750de74ce07c7e6
[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 <linux/string.h>
10 #include <linux/types.h>
11 #include <linux/percpu.h>
12 #include <linux/init.h>
13 #include <linux/ratelimit.h>
14
15 #include <net/sock.h>
16
17 #include <asm/byteorder.h>
18 #include <asm/uaccess.h>
19
20 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
21 void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,
22                                const __be32 *from, const __be32 *to,
23                                int pseudohdr)
24 {
25         __be32 diff[] = {
26                 ~from[0], ~from[1], ~from[2], ~from[3],
27                 to[0], to[1], to[2], to[3],
28         };
29         if (skb->ip_summed != CHECKSUM_PARTIAL) {
30                 *sum = csum_fold(csum_partial(diff, sizeof(diff),
31                                         ~csum_unfold(*sum)));
32                 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
33                         skb->csum = ~csum_partial(diff, sizeof(diff),
34                                         ~skb->csum);
35         } else if (pseudohdr)
36                 *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
37                                         csum_unfold(*sum)));
38 }
39 #endif
40
41 bool __net_get_random_once(void *buf, int nbytes, bool *done,
42                            atomic_t *done_key)
43 {
44         static DEFINE_SPINLOCK(lock);
45         unsigned long flags;
46
47         spin_lock_irqsave(&lock, flags);
48         if (*done) {
49                 spin_unlock_irqrestore(&lock, flags);
50                 return false;
51         }
52
53         get_random_bytes(buf, nbytes);
54         *done = true;
55         spin_unlock_irqrestore(&lock, flags);
56
57         atomic_set(done_key, 1);
58
59         return true;
60 }