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