Whamcloud - gitweb
LU-12452 socklnd: allow setting IP ToS value
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-net.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22
23 #ifndef __LIBCFS_LINUX_NET_H__
24 #define __LIBCFS_LINUX_NET_H__
25
26 #include <net/netlink.h>
27 #include <net/genetlink.h>
28
29 /* NL_SET_ERR_MSG macros is already defined in kernels
30  * 3.10.0-1160 and above. For older kernels (3.10.0-957)
31  * where this is not defined we put the message to the
32  * system log as a workaround
33  */
34 #ifndef NL_SET_ERR_MSG
35 #define NL_SET_ERR_MSG(unused, msg) do {              \
36        static const char __msg[] = msg;               \
37        pr_debug("%s\n", __msg);                       \
38 } while (0)
39 #endif
40
41 #ifndef NLM_F_DUMP_FILTERED
42 #define NLM_F_DUMP_FILTERED   0x20    /* Dump was filtered as requested */
43 #endif
44
45 #ifndef HAVE_NLA_STRDUP
46 char *nla_strdup(const struct nlattr *nla, gfp_t flags);
47 #endif /* !HAVE_NLA_STRDUP */
48
49 #ifdef HAVE_NLA_STRLCPY
50 #define nla_strscpy     nla_strlcpy
51 #endif /* HAVE_NLA_STRLCPY */
52
53 #ifndef HAVE_NL_PARSE_WITH_EXT_ACK
54
55 #define NL_SET_BAD_ATTR(extack, attr)
56
57 /* this can be increased when necessary - don't expose to userland */
58 #define NETLINK_MAX_COOKIE_LEN  20
59
60 /**
61  * struct netlink_ext_ack - netlink extended ACK report struct
62  * @_msg: message string to report - don't access directly, use
63  *      %NL_SET_ERR_MSG
64  * @bad_attr: attribute with error
65  * @cookie: cookie data to return to userspace (for success)
66  * @cookie_len: actual cookie data length
67  */
68 struct netlink_ext_ack {
69         const char *_msg;
70         const struct nlattr *bad_attr;
71         u8 cookie[NETLINK_MAX_COOKIE_LEN];
72         u8 cookie_len;
73 };
74
75 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG(NULL, msg)
76
77 static inline int cfs_nla_parse(struct nlattr **tb, int maxtype,
78                                 const struct nlattr *head, int len,
79                                 const struct nla_policy *policy,
80                                 struct netlink_ext_ack *extack)
81 {
82         return nla_parse(tb, maxtype, head, len, policy);
83 }
84
85 static inline int cfs_nla_parse_nested(struct nlattr *tb[], int maxtype,
86                                        const struct nlattr *nla,
87                                        const struct nla_policy *policy,
88                                        struct netlink_ext_ack *extack)
89 {
90         return nla_parse_nested(tb, maxtype, nla, policy);
91 }
92
93 #else /* !HAVE_NL_PARSE_WITH_EXT_ACK */
94
95 #define cfs_nla_parse_nested    nla_parse_nested
96 #define cfs_nla_parse           nla_parse
97
98 #endif
99
100 #ifndef HAVE_GENL_DUMPIT_INFO
101 struct cfs_genl_dumpit_info {
102         const struct genl_family *family;
103         const struct genl_ops *ops;
104         struct nlattr **attrs;
105 };
106
107 static inline const struct cfs_genl_dumpit_info *
108 lnet_genl_dumpit_info(struct netlink_callback *cb)
109 {
110         return (const struct cfs_genl_dumpit_info *)cb->args[1];
111 }
112 #else
113 #define cfs_genl_dumpit_info    genl_dumpit_info
114
115 static inline const struct cfs_genl_dumpit_info *
116 lnet_genl_dumpit_info(struct netlink_callback *cb)
117 {
118         return (const struct cfs_genl_dumpit_info *)genl_dumpit_info(cb);
119 }
120 #endif /* HAVE_GENL_DUMPIT_INFO */
121
122 #ifdef HAVE_KERNEL_SETSOCKOPT
123
124 #include <net/tcp.h>
125
126 #if !defined(HAVE_TCP_SOCK_SET_QUICKACK)
127 static inline void tcp_sock_set_quickack(struct sock *sk, int opt)
128 {
129         struct socket *sock = sk->sk_socket;
130
131         kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
132                           (char *)&opt, sizeof(opt));
133 }
134 #endif /* HAVE_TCP_SOCK_SET_QUICKACK */
135
136 #if !defined(HAVE_TCP_SOCK_SET_NODELAY)
137 static inline void tcp_sock_set_nodelay(struct sock *sk)
138 {
139         int opt = 1;
140         struct socket *sock = sk->sk_socket;
141
142         kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
143                           (char *)&opt, sizeof(opt));
144 }
145 #endif /* HAVE_TCP_SOCK_SET_NODELAY */
146
147 #if !defined(HAVE_TCP_SOCK_SET_KEEPIDLE)
148 static inline int tcp_sock_set_keepidle(struct sock *sk, int opt)
149 {
150         struct socket *sock = sk->sk_socket;
151
152         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
153                                  (char *)&opt, sizeof(opt));
154 }
155 #endif /* HAVE_TCP_SOCK_SET_KEEPIDLE */
156
157 #if !defined(HAVE_TCP_SOCK_SET_KEEPINTVL)
158 static inline int tcp_sock_set_keepintvl(struct sock *sk, int opt)
159 {
160         struct socket *sock = sk->sk_socket;
161
162         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
163                                  (char *)&opt, sizeof(opt));
164 }
165 #endif /* HAVE_TCP_SOCK_SET_KEEPINTVL */
166
167 #if !defined(HAVE_TCP_SOCK_SET_KEEPCNT)
168 static inline int tcp_sock_set_keepcnt(struct sock *sk, int opt)
169 {
170         struct socket *sock = sk->sk_socket;
171
172         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
173                                  (char *)&opt, sizeof(opt));
174 }
175 #endif /* HAVE_TCP_SOCK_SET_KEEPCNT */
176
177 #if !defined(HAVE_IP6_SET_PREF)
178 static inline void ip6_sock_set_addr_preferences(struct sock *sk,
179                                                  unsigned int pref)
180 {
181         kernel_setsockopt(sk->sk_socket, SOL_IPV6, IPV6_ADDR_PREFERENCES,
182                           (char *)&pref, sizeof(pref));
183 }
184 #endif /* HAVE_IP6_SET_PREF */
185
186 #if !defined(HAVE_IP_SET_TOS)
187 static inline void ip_sock_set_tos(struct sock *sk, int val)
188 {
189         kernel_setsockopt(sk->sk_socket, IPPROTO_IP, IP_TOS,
190                           (char *)&val, sizeof(val));
191 }
192 #endif /* HAVE_IP_SET_TOS */
193 #endif /* HAVE_KERNEL_SETSOCKOPT */
194
195 #endif /* __LIBCFS_LINUX_NET_H__ */