datapath: Add support for kernel 3.14.
[sliver-openvswitch.git] / datapath / compat.h
1 /*
2  * Copyright (c) 2007-2012 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #ifndef COMPAT_H
20 #define COMPAT_H 1
21
22 #include <linux/in.h>
23 #include <linux/in_route.h>
24 #include <linux/netlink.h>
25 #include <net/route.h>
26 #include <net/xfrm.h>
27
28 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
29 #define GROUP_ID(grp)   0
30 #else
31 #define GROUP_ID(grp)   ((grp)->id)
32 #endif
33
34 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
35 #define rt_dst(rt) (rt->dst)
36 #else
37 #define rt_dst(rt) (rt->u.dst)
38 #endif
39
40 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
41 #define inet_sport(sk)  (inet_sk(sk)->sport)
42 #else
43 #define inet_sport(sk)  (inet_sk(sk)->inet_sport)
44 #endif
45
46 static inline struct rtable *find_route(struct net *net,
47                                         __be32 *saddr, __be32 daddr,
48                                         u8 ipproto, u8 tos, u32 skb_mark)
49 {
50         struct rtable *rt;
51         /* Tunnel configuration keeps DSCP part of TOS bits, But Linux
52          * router expect RT_TOS bits only. */
53
54 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
55         struct flowi fl = { .nl_u = { .ip4_u = {
56                                         .daddr = daddr,
57                                         .saddr = *saddr,
58                                         .tos   = RT_TOS(tos) } },
59                                         .mark = skb_mark,
60                                         .proto = ipproto };
61
62         if (unlikely(ip_route_output_key(net, &rt, &fl)))
63                 return ERR_PTR(-EADDRNOTAVAIL);
64         *saddr = fl.nl_u.ip4_u.saddr;
65         return rt;
66 #else
67         struct flowi4 fl = { .daddr = daddr,
68                              .saddr = *saddr,
69                              .flowi4_tos = RT_TOS(tos),
70                              .flowi4_mark = skb_mark,
71                              .flowi4_proto = ipproto };
72
73         rt = ip_route_output_key(net, &fl);
74         *saddr = fl.saddr;
75         return rt;
76 #endif
77 }
78 #endif /* compat.h */