Imported Upstream version 3.2.22
[platform/upstream/libnl3.git] / include / netlink-private / netlink.h
1 /*
2  * netlink-private/netlink.h    Local Netlink Interface
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #ifndef NETLINK_LOCAL_H_
13 #define NETLINK_LOCAL_H_
14
15 #include <stdio.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <math.h>
22 #include <time.h>
23 #include <stdarg.h>
24 #include <ctype.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <inttypes.h>
29 #include <assert.h>
30 #include <limits.h>
31 #include <search.h>
32
33 #include <arpa/inet.h>
34 #include <netdb.h>
35
36 #include <defs.h>
37
38 #ifndef SOL_NETLINK
39 #define SOL_NETLINK 270
40 #endif
41
42 #include <linux/types.h>
43
44 /* local header copies */
45 #include <linux/if.h>
46 #include <linux/if_arp.h>
47 #include <linux/if_ether.h>
48 #include <linux/ethtool.h>
49 #include <linux/pkt_sched.h>
50 #include <linux/pkt_cls.h>
51 #include <linux/gen_stats.h>
52 #include <linux/ip_mp_alg.h>
53 #include <linux/atm.h>
54 #include <linux/inetdevice.h>
55 #include <linux/ipv6.h>
56 #include <linux/snmp.h>
57
58 #ifndef DISABLE_PTHREADS
59 #include <pthread.h>
60 #endif
61
62 #include <netlink/netlink.h>
63 #include <netlink/handlers.h>
64 #include <netlink/cache.h>
65 #include <netlink/route/tc.h>
66 #include <netlink-private/object-api.h>
67 #include <netlink-private/cache-api.h>
68 #include <netlink-private/types.h>
69
70 struct trans_tbl {
71         int i;
72         const char *a;
73 };
74
75 #define __ADD(id, name) { .i = id, .a = #name },
76
77 struct trans_list {
78         int i;
79         char *a;
80         struct nl_list_head list;
81 };
82
83 #ifdef NL_DEBUG
84 #define NL_DBG(LVL,FMT,ARG...)                                          \
85         do {                                                            \
86                 if (LVL <= nl_debug)                                    \
87                         fprintf(stderr,                                 \
88                                 "DBG<" #LVL ">%20s:%-4u %s: " FMT,      \
89                                 __FILE__, __LINE__,                     \
90                                 __PRETTY_FUNCTION__, ##ARG);            \
91         } while (0)
92 #else /* NL_DEBUG */
93 #define NL_DBG(LVL,FMT,ARG...) do { } while(0)
94 #endif /* NL_DEBUG */
95
96 #define BUG()                                                           \
97         do {                                                            \
98                 fprintf(stderr, "BUG at file position %s:%d:%s\n",      \
99                         __FILE__, __LINE__, __PRETTY_FUNCTION__);       \
100                 assert(0);                                              \
101         } while (0)
102
103 #define APPBUG(msg)                                                     \
104         do {                                                            \
105                 fprintf(stderr, "APPLICATION BUG: %s:%d:%s: %s\n",      \
106                         __FILE__, __LINE__, __PRETTY_FUNCTION__, msg);  \
107                 assert(0);                                              \
108         } while(0)
109
110 extern int __nl_read_num_str_file(const char *path,
111                                   int (*cb)(long, const char *));
112
113 extern int __trans_list_add(int, const char *, struct nl_list_head *);
114 extern void __trans_list_clear(struct nl_list_head *);
115
116 extern char *__type2str(int, char *, size_t, const struct trans_tbl *, size_t);
117 extern int __str2type(const char *, const struct trans_tbl *, size_t);
118
119 extern char *__list_type2str(int, char *, size_t, struct nl_list_head *);
120 extern int __list_str2type(const char *, struct nl_list_head *);
121
122 extern char *__flags2str(int, char *, size_t, const struct trans_tbl *, size_t);
123 extern int __str2flags(const char *, const struct trans_tbl *, size_t);
124
125 extern void dump_from_ops(struct nl_object *, struct nl_dump_params *);
126
127 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
128 {
129         return cb->cb_set[type](msg, cb->cb_args[type]);
130 }
131
132 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
133
134 /* This is also defined in stddef.h */
135 #ifndef offsetof
136 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
137 #endif
138
139 #define __init __attribute__ ((constructor))
140 #define __exit __attribute__ ((destructor))
141 #undef __deprecated
142 #define __deprecated __attribute__ ((deprecated))
143
144 #define min(x,y) ({ \
145         typeof(x) _x = (x);     \
146         typeof(y) _y = (y);     \
147         (void) (&_x == &_y);            \
148         _x < _y ? _x : _y; })
149
150 #define max(x,y) ({ \
151         typeof(x) _x = (x);     \
152         typeof(y) _y = (y);     \
153         (void) (&_x == &_y);            \
154         _x > _y ? _x : _y; })
155
156 #define min_t(type,x,y) \
157         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
158 #define max_t(type,x,y) \
159         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
160
161 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
162                           struct nlmsghdr *, struct nl_parser_param *);
163
164
165 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
166                                       struct tc_ratespec *src)
167 {
168         dst->rs_cell_log = src->cell_log;
169         dst->rs_overhead = src->overhead;
170         dst->rs_cell_align = src->cell_align;
171         dst->rs_mpu = src->mpu;
172         dst->rs_rate = src->rate;
173 }
174
175 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
176                                        struct rtnl_ratespec *src)
177 {
178         dst->cell_log = src->rs_cell_log;
179         dst->overhead = src->rs_overhead;
180         dst->cell_align = src->rs_cell_align;
181         dst->mpu = src->rs_mpu;
182         dst->rate = src->rs_rate;
183 }
184
185 static inline char *nl_cache_name(struct nl_cache *cache)
186 {
187         return cache->c_ops ? cache->c_ops->co_name : "unknown";
188 }
189
190 #define GENL_FAMILY(id, name) \
191         { \
192                 { id, NL_ACT_UNSPEC, name }, \
193                 END_OF_MSGTYPES_LIST, \
194         }
195
196 static inline int wait_for_ack(struct nl_sock *sk)
197 {
198         if (sk->s_flags & NL_NO_AUTO_ACK)
199                 return 0;
200         else
201                 return nl_wait_for_ack(sk);
202 }
203
204 static inline int build_sysconf_path(char **strp, const char *filename)
205 {
206         char *sysconfdir;
207
208         sysconfdir = getenv("NLSYSCONFDIR");
209
210         if (!sysconfdir)
211                 sysconfdir = SYSCONFDIR;
212
213         return asprintf(strp, "%s/%s", sysconfdir, filename);
214 }
215
216 #ifndef DISABLE_PTHREADS
217 #define NL_LOCK(NAME) pthread_mutex_t (NAME) = PTHREAD_MUTEX_INITIALIZER
218 #define NL_RW_LOCK(NAME) pthread_rwlock_t (NAME) = PTHREAD_RWLOCK_INITIALIZER
219
220 static inline void nl_lock(pthread_mutex_t *lock)
221 {
222         pthread_mutex_lock(lock);
223 }
224
225 static inline void nl_unlock(pthread_mutex_t *lock)
226 {
227         pthread_mutex_unlock(lock);
228 }
229
230 static inline void nl_read_lock(pthread_rwlock_t *lock)
231 {
232         pthread_rwlock_rdlock(lock);
233 }
234
235 static inline void nl_read_unlock(pthread_rwlock_t *lock)
236 {
237         pthread_rwlock_unlock(lock);
238 }
239
240 static inline void nl_write_lock(pthread_rwlock_t *lock)
241 {
242         pthread_rwlock_wrlock(lock);
243 }
244
245 static inline void nl_write_unlock(pthread_rwlock_t *lock)
246 {
247         pthread_rwlock_unlock(lock);
248 }
249
250 #else
251 #define NL_LOCK(NAME) int __unused_lock_ ##NAME __attribute__((unused))
252 #define NL_RW_LOCK(NAME) int __unused_lock_ ##NAME __attribute__((unused))
253
254 #define nl_lock(LOCK) do { } while(0)
255 #define nl_unlock(LOCK) do { } while(0)
256 #define nl_read_lock(LOCK) do { } while(0)
257 #define nl_read_unlock(LOCK) do { } while(0)
258 #define nl_write_lock(LOCK) do { } while(0)
259 #define nl_write_unlock(LOCK) do { } while(0)
260 #endif
261
262 #endif