2 * netlink-local.h Local Netlink Interface
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
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
12 #ifndef NETLINK_LOCAL_H_
13 #define NETLINK_LOCAL_H_
25 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include <arpa/inet.h>
33 #include <asm/types.h>
35 /* local header copies */
37 #include <linux/if_arp.h>
38 #include <linux/if_ether.h>
39 #include <linux/pkt_sched.h>
40 #include <linux/pkt_cls.h>
41 #include <linux/gen_stats.h>
43 #include <netlink/netlink-compat.h>
44 #include <netlink/netlink-kernel.h>
45 #include <netlink/rtnetlink-kernel.h>
47 #include <netlink/types.h>
48 #include <netlink-types.h>
50 #include <netlink/handlers.h>
51 #include <netlink/cache.h>
52 #include <netlink/route/tc.h>
59 #define __ADD(id, name) { .i = id, .a = #name },
61 #define NL_DBG(LVL,FMT,ARG...) \
63 if (LVL <= nl_debug) \
64 fprintf(stderr, "DBG<" #LVL ">: " FMT, ##ARG); \
69 fprintf(stderr, "BUG: %s:%d\n", \
70 __FILE__, __LINE__); \
74 #define RET_ERR(R, E) \
80 extern int __nl_error(int, const char *, unsigned int,
81 const char *, const char *, ...);
83 #ifdef NL_ERROR_ASSERT
85 static inline int __assert_error(const char *file, int line, char *func,
89 fprintf(stderr, "%s:%d:%s: ", file, line, func);
91 vfprintf(stderr, fmt, args);
93 fprintf(stderr, "\n");
97 #define nl_error(E, FMT,ARG...) \
98 __assert_error(__FILE__, __LINE__, __FUNCTION__, FMT, ##ARG)
101 #define nl_error(E, FMT,ARG...) \
102 __nl_error(E, __FILE__, __LINE__, __FUNCTION__, FMT, ##ARG);
106 #define nl_errno(E) nl_error(E, NULL)
109 static inline char *__type2str(int type, char *buf, size_t len,
110 struct trans_tbl *tbl, size_t tbl_len)
113 for (i = 0; i < tbl_len; i++) {
114 if (tbl[i].i == type) {
115 snprintf(buf, len, "%s", tbl[i].a);
120 snprintf(buf, len, "0x%x", type);
124 static inline char *__flags2str(int flags, char *buf, size_t len,
125 struct trans_tbl *tbl, size_t tbl_len)
132 for (i = 0; i < tbl_len; i++) {
133 if (tbl[i].i & tmp) {
135 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
137 strncat(buf, ",", len - strlen(buf) - 1);
144 static inline int __str2type(const char *buf, struct trans_tbl *tbl,
154 for (i = 0; i < tbl_len; i++)
155 if (!strcasecmp(tbl[i].a, buf))
158 l = strtoul(buf, &end, 0);
159 if (l == ULONG_MAX || *end != '\0')
165 static inline int __str2flags(const char *buf, struct trans_tbl *tbl,
168 int i, flags = 0, len;
169 char *p = (char *) buf, *t;
176 len = t ? t - p : strlen(p);
177 for (i = 0; i < tbl_len; i++)
178 if (!strncasecmp(tbl[i].a, p, len))
191 static inline void dp_new_line(struct nl_dump_params *params,
194 if (params->dp_prefix) {
196 for (i = 0; i < params->dp_prefix; i++) {
198 fprintf(params->dp_fd, " ");
199 else if (params->dp_buf)
200 strncat(params->dp_buf, " ",
202 sizeof(params->dp_buf) - 1);
206 if (params->dp_nl_cb)
207 params->dp_nl_cb(params, line_nr);
210 static inline void __dp_dump(struct nl_dump_params *parms, const char *fmt,
214 vfprintf(parms->dp_fd, fmt, args);
215 else if (parms->dp_buf || parms->dp_cb) {
217 vasprintf(&buf, fmt, args);
219 parms->dp_cb(parms, buf);
221 strncat(parms->dp_buf, buf,
222 parms->dp_buflen - strlen(parms->dp_buf) - 1);
227 static inline void dp_dump(struct nl_dump_params *parms, const char *fmt, ...)
232 __dp_dump(parms, fmt, args);
236 static inline void dp_dump_line(struct nl_dump_params *parms, int line,
237 const char *fmt, ...)
241 dp_new_line(parms, line);
244 __dp_dump(parms, fmt, args);
248 static inline void dump_from_ops(struct nl_object *obj,
249 struct nl_dump_params *params)
251 int type = params->dp_type;
254 if (type < 0 || type > NL_DUMP_MAX)
257 if (params->dp_dump_msgtype) {
258 dp_dump_line(params, 0, "%s ",
259 nl_cache_mngt_type2name(obj->ce_ops,
262 params->dp_pre_dump = 1;
264 dp_new_line(params, 0);
266 if (obj->ce_ops->co_dump[type])
267 obj->ce_ops->co_dump[type](obj, params);
270 static inline struct nl_cache *dp_cache(struct nl_object *obj)
272 if (obj->ce_cache == NULL)
273 return nl_cache_mngt_require(obj->ce_ops->co_name);
275 return obj->ce_cache;
278 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
280 return cb->cb_set[type](msg, cb->cb_args[type]);
283 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
284 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
286 #define __init __attribute__ ((constructor))
287 #define __exit __attribute__ ((destructor))
292 #define NL_COPY_TEMPLATE(TB, PREFIX, BASE, FLAGBASE, IDX, VAR) \
293 ({ if (TB[PREFIX ##IDX]) { \
294 err = NL_COPY_DATA(BASE ##VAR, TB[PREFIX ##IDX]); \
297 BASE ##mask |= FLAGBASE ##IDX; \
300 #define min(x,y) ({ \
301 typeof(x) _x = (x); \
302 typeof(y) _y = (y); \
303 (void) (&_x == &_y); \
304 _x < _y ? _x : _y; })
306 #define max(x,y) ({ \
307 typeof(x) _x = (x); \
308 typeof(y) _y = (y); \
309 (void) (&_x == &_y); \
310 _x > _y ? _x : _y; })
312 #define min_t(type,x,y) \
313 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
314 #define max_t(type,x,y) \
315 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
317 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
318 struct nlmsghdr *, struct nl_parser_param *);
321 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
322 struct tc_ratespec *src)
324 dst->rs_cell_log = src->cell_log;
325 dst->rs_feature = src->feature;
326 dst->rs_addend = src->addend;
327 dst->rs_mpu = src->mpu;
328 dst->rs_rate = src->rate;
331 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
332 struct rtnl_ratespec *src)
334 dst->cell_log = src->rs_cell_log;
335 dst->feature = src->rs_feature;
336 dst->addend = src->rs_addend;
337 dst->mpu = src->rs_mpu;
338 dst->rate = src->rs_rate;