Whamcloud - gitweb
LU-17888 osd-ldiskfs: osd_scrub_refresh_mapping deadlock
[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 <linux/netdevice.h>
27 #include <net/netlink.h>
28 #include <net/genetlink.h>
29
30 #ifndef HAVE_NETDEV_CMD_TO_NAME
31 static inline const char *netdev_cmd_to_name(unsigned long cmd)
32 {
33 #define N(val)                                                 \
34         case NETDEV_##val:                              \
35                 return "NETDEV_" __stringify(val);
36         switch (cmd) {
37         N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
38         N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
39         N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
40         N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
41         N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
42         N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
43         N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
44         };
45 #undef N
46         return "UNKNOWN_NETDEV_EVENT";
47 }
48 #endif
49
50 /* NL_SET_ERR_MSG macros is already defined in kernels
51  * 3.10.0-1160 and above. For older kernels (3.10.0-957)
52  * where this is not defined we put the message to the
53  * system log as a workaround
54  */
55 #ifndef NL_SET_ERR_MSG
56 #define NL_SET_ERR_MSG(unused, msg) do {              \
57        static const char __msg[] = msg;               \
58        pr_debug("%s\n", __msg);                       \
59 } while (0)
60 #endif
61
62 #ifndef NLM_F_DUMP_FILTERED
63 #define NLM_F_DUMP_FILTERED   0x20    /* Dump was filtered as requested */
64 #endif
65
66 #ifndef HAVE_NLA_STRDUP
67 char *nla_strdup(const struct nlattr *nla, gfp_t flags);
68 #endif /* !HAVE_NLA_STRDUP */
69
70 #ifdef HAVE_NLA_STRLCPY
71 #define nla_strscpy     nla_strlcpy
72 #endif /* HAVE_NLA_STRLCPY */
73
74 #ifndef HAVE_NL_PARSE_WITH_EXT_ACK
75
76 #define NL_SET_BAD_ATTR(extack, attr)
77
78 /* this can be increased when necessary - don't expose to userland */
79 #define NETLINK_MAX_COOKIE_LEN  20
80
81 /**
82  * struct netlink_ext_ack - netlink extended ACK report struct
83  * @_msg: message string to report - don't access directly, use
84  *      %NL_SET_ERR_MSG
85  * @bad_attr: attribute with error
86  * @cookie: cookie data to return to userspace (for success)
87  * @cookie_len: actual cookie data length
88  */
89 struct netlink_ext_ack {
90         const char *_msg;
91         const struct nlattr *bad_attr;
92         u8 cookie[NETLINK_MAX_COOKIE_LEN];
93         u8 cookie_len;
94 };
95
96 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG(NULL, msg)
97
98 static inline int cfs_nla_parse(struct nlattr **tb, int maxtype,
99                                 const struct nlattr *head, int len,
100                                 const struct nla_policy *policy,
101                                 struct netlink_ext_ack *extack)
102 {
103         return nla_parse(tb, maxtype, head, len, policy);
104 }
105
106 static inline int cfs_nla_parse_nested(struct nlattr *tb[], int maxtype,
107                                        const struct nlattr *nla,
108                                        const struct nla_policy *policy,
109                                        struct netlink_ext_ack *extack)
110 {
111         return nla_parse_nested(tb, maxtype, nla, policy);
112 }
113
114 #else /* !HAVE_NL_PARSE_WITH_EXT_ACK */
115
116 #define cfs_nla_parse_nested    nla_parse_nested
117 #define cfs_nla_parse           nla_parse
118
119 #endif
120
121 #ifndef HAVE_GENL_DUMPIT_INFO
122 struct cfs_genl_dumpit_info {
123         const struct genl_family *family;
124         const struct genl_ops *ops;
125         struct nlattr **attrs;
126 };
127
128 static inline const struct cfs_genl_dumpit_info *
129 lnet_genl_dumpit_info(struct netlink_callback *cb)
130 {
131         return (const struct cfs_genl_dumpit_info *)cb->args[1];
132 }
133 #else
134 #define cfs_genl_dumpit_info    genl_dumpit_info
135
136 static inline const struct cfs_genl_dumpit_info *
137 lnet_genl_dumpit_info(struct netlink_callback *cb)
138 {
139         return (const struct cfs_genl_dumpit_info *)genl_dumpit_info(cb);
140 }
141 #endif /* HAVE_GENL_DUMPIT_INFO */
142
143 #ifdef HAVE_KERNEL_SETSOCKOPT
144
145 #include <net/tcp.h>
146
147 #if !defined(HAVE_TCP_SOCK_SET_QUICKACK)
148 static inline void tcp_sock_set_quickack(struct sock *sk, int opt)
149 {
150         struct socket *sock = sk->sk_socket;
151
152         kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
153                           (char *)&opt, sizeof(opt));
154 }
155 #endif /* HAVE_TCP_SOCK_SET_QUICKACK */
156
157 #if !defined(HAVE_TCP_SOCK_SET_NODELAY)
158 static inline void tcp_sock_set_nodelay(struct sock *sk)
159 {
160         int opt = 1;
161         struct socket *sock = sk->sk_socket;
162
163         kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
164                           (char *)&opt, sizeof(opt));
165 }
166 #endif /* HAVE_TCP_SOCK_SET_NODELAY */
167
168 #if !defined(HAVE_TCP_SOCK_SET_KEEPIDLE)
169 static inline int tcp_sock_set_keepidle(struct sock *sk, int opt)
170 {
171         struct socket *sock = sk->sk_socket;
172
173         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
174                                  (char *)&opt, sizeof(opt));
175 }
176 #endif /* HAVE_TCP_SOCK_SET_KEEPIDLE */
177
178 #if !defined(HAVE_TCP_SOCK_SET_KEEPINTVL)
179 static inline int tcp_sock_set_keepintvl(struct sock *sk, int opt)
180 {
181         struct socket *sock = sk->sk_socket;
182
183         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
184                                  (char *)&opt, sizeof(opt));
185 }
186 #endif /* HAVE_TCP_SOCK_SET_KEEPINTVL */
187
188 #if !defined(HAVE_TCP_SOCK_SET_KEEPCNT)
189 static inline int tcp_sock_set_keepcnt(struct sock *sk, int opt)
190 {
191         struct socket *sock = sk->sk_socket;
192
193         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
194                                  (char *)&opt, sizeof(opt));
195 }
196 #endif /* HAVE_TCP_SOCK_SET_KEEPCNT */
197
198 #if !defined(HAVE_IP6_SET_PREF)
199 static inline void ip6_sock_set_addr_preferences(struct sock *sk,
200                                                  unsigned int pref)
201 {
202         kernel_setsockopt(sk->sk_socket, SOL_IPV6, IPV6_ADDR_PREFERENCES,
203                           (char *)&pref, sizeof(pref));
204 }
205 #endif /* HAVE_IP6_SET_PREF */
206
207 #if !defined(HAVE_IP_SET_TOS)
208 static inline void ip_sock_set_tos(struct sock *sk, int val)
209 {
210         kernel_setsockopt(sk->sk_socket, IPPROTO_IP, IP_TOS,
211                           (char *)&val, sizeof(val));
212 }
213 #endif /* HAVE_IP_SET_TOS */
214 #endif /* HAVE_KERNEL_SETSOCKOPT */
215
216 #endif /* __LIBCFS_LINUX_NET_H__ */