Whamcloud - gitweb
a9502a381811b21f9b73e2771b3f5e57b14fd9e0
[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 #ifndef HAVE_NLA_STRDUP
30 char *nla_strdup(const struct nlattr *nla, gfp_t flags);
31 #endif /* !HAVE_NLA_STRDUP */
32
33 #ifndef HAVE_NL_PARSE_WITH_EXT_ACK
34
35 #define NL_SET_BAD_ATTR(extack, attr)
36
37 /* this can be increased when necessary - don't expose to userland */
38 #define NETLINK_MAX_COOKIE_LEN  20
39
40 /**
41  * struct netlink_ext_ack - netlink extended ACK report struct
42  * @_msg: message string to report - don't access directly, use
43  *      %NL_SET_ERR_MSG
44  * @bad_attr: attribute with error
45  * @cookie: cookie data to return to userspace (for success)
46  * @cookie_len: actual cookie data length
47  */
48 struct netlink_ext_ack {
49         const char *_msg;
50         const struct nlattr *bad_attr;
51         u8 cookie[NETLINK_MAX_COOKIE_LEN];
52         u8 cookie_len;
53 };
54
55 #define GENL_SET_ERR_MSG(info, msg) NL_SET_ERR_MSG(NULL, msg)
56
57 static inline int cfs_nla_parse(struct nlattr **tb, int maxtype,
58                                 const struct nlattr *head, int len,
59                                 const struct nla_policy *policy,
60                                 struct netlink_ext_ack *extack)
61 {
62         return nla_parse(tb, maxtype, head, len, policy);
63 }
64
65 static inline int cfs_nla_parse_nested(struct nlattr *tb[], int maxtype,
66                                        const struct nlattr *nla,
67                                        const struct nla_policy *policy,
68                                        struct netlink_ext_ack *extack)
69 {
70         return nla_parse_nested(tb, maxtype, nla, policy);
71 }
72
73 #else /* !HAVE_NL_PARSE_WITH_EXT_ACK */
74
75 #define cfs_nla_parse_nested    nla_parse_nested
76 #define cfs_nla_parse           nla_parse
77
78 #endif
79
80 #ifndef HAVE_GENL_DUMPIT_INFO
81 struct cfs_genl_dumpit_info {
82         const struct genl_family *family;
83         const struct genl_ops *ops;
84         struct nlattr **attrs;
85 };
86
87 static inline const struct cfs_genl_dumpit_info *
88 lnet_genl_dumpit_info(struct netlink_callback *cb)
89 {
90         return (const struct cfs_genl_dumpit_info *)cb->args[1];
91 }
92 #else
93 #define cfs_genl_dumpit_info    genl_dumpit_info
94
95 static inline const struct cfs_genl_dumpit_info *
96 lnet_genl_dumpit_info(struct netlink_callback *cb)
97 {
98         return (const struct cfs_genl_dumpit_info *)genl_dumpit_info(cb);
99 }
100 #endif /* HAVE_GENL_DUMPIT_INFO */
101
102 #ifdef HAVE_KERNEL_SETSOCKOPT
103
104 #include <net/tcp.h>
105
106 #if !defined(HAVE_TCP_SOCK_SET_QUICKACK)
107 static inline void tcp_sock_set_quickack(struct sock *sk, int opt)
108 {
109         struct socket *sock = sk->sk_socket;
110
111         kernel_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
112                           (char *)&opt, sizeof(opt));
113 }
114 #endif /* HAVE_TCP_SOCK_SET_QUICKACK */
115
116 #if !defined(HAVE_TCP_SOCK_SET_NODELAY)
117 static inline void tcp_sock_set_nodelay(struct sock *sk)
118 {
119         int opt = 1;
120         struct socket *sock = sk->sk_socket;
121
122         kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
123                           (char *)&opt, sizeof(opt));
124 }
125 #endif /* HAVE_TCP_SOCK_SET_NODELAY */
126
127 #if !defined(HAVE_TCP_SOCK_SET_KEEPIDLE)
128 static inline int tcp_sock_set_keepidle(struct sock *sk, int opt)
129 {
130         struct socket *sock = sk->sk_socket;
131
132         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
133                                  (char *)&opt, sizeof(opt));
134 }
135 #endif /* HAVE_TCP_SOCK_SET_KEEPIDLE */
136
137 #if !defined(HAVE_TCP_SOCK_SET_KEEPINTVL)
138 static inline int tcp_sock_set_keepintvl(struct sock *sk, int opt)
139 {
140         struct socket *sock = sk->sk_socket;
141
142         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
143                                  (char *)&opt, sizeof(opt));
144 }
145 #endif /* HAVE_TCP_SOCK_SET_KEEPINTVL */
146
147 #if !defined(HAVE_TCP_SOCK_SET_KEEPCNT)
148 static inline int tcp_sock_set_keepcnt(struct sock *sk, int opt)
149 {
150         struct socket *sock = sk->sk_socket;
151
152         return kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
153                                  (char *)&opt, sizeof(opt));
154 }
155 #endif /* HAVE_TCP_SOCK_SET_KEEPCNT */
156 #endif /* HAVE_KERNEL_SETSOCKOPT */
157
158 #endif /* __LIBCFS_LINUX_NET_H__ */