2 * netlink-private/netlink.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-2013 Thomas Graf <tgraf@suug.ch>
12 #ifndef NETLINK_LOCAL_H_
13 #define NETLINK_LOCAL_H_
25 #include <sys/types.h>
27 #include <sys/socket.h>
33 #include <arpa/inet.h>
39 #define SOL_NETLINK 270
42 #include <linux/types.h>
44 /* local header copies */
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>
58 #ifndef DISABLE_PTHREADS
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>
75 #define __ADD(id, name) { .i = id, .a = #name },
80 struct nl_list_head list;
84 #define NL_DBG(LVL,FMT,ARG...) \
86 if (LVL <= nl_debug) \
88 "DBG<" #LVL ">%20s:%-4u %s: " FMT, \
90 __PRETTY_FUNCTION__, ##ARG); \
93 #define NL_DBG(LVL,FMT,ARG...) do { } while(0)
98 fprintf(stderr, "BUG at file position %s:%d:%s\n", \
99 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
103 #define APPBUG(msg) \
105 fprintf(stderr, "APPLICATION BUG: %s:%d:%s: %s\n", \
106 __FILE__, __LINE__, __PRETTY_FUNCTION__, msg); \
110 extern int __nl_read_num_str_file(const char *path,
111 int (*cb)(long, const char *));
113 extern int __trans_list_add(int, const char *, struct nl_list_head *);
114 extern void __trans_list_clear(struct nl_list_head *);
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);
119 extern char *__list_type2str(int, char *, size_t, struct nl_list_head *);
120 extern int __list_str2type(const char *, struct nl_list_head *);
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);
125 extern void dump_from_ops(struct nl_object *, struct nl_dump_params *);
127 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
129 return cb->cb_set[type](msg, cb->cb_args[type]);
132 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
134 /* This is also defined in stddef.h */
136 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
139 #define __init __attribute__ ((constructor))
140 #define __exit __attribute__ ((destructor))
142 #define __deprecated __attribute__ ((deprecated))
144 #define min(x,y) ({ \
145 typeof(x) _x = (x); \
146 typeof(y) _y = (y); \
147 (void) (&_x == &_y); \
148 _x < _y ? _x : _y; })
150 #define max(x,y) ({ \
151 typeof(x) _x = (x); \
152 typeof(y) _y = (y); \
153 (void) (&_x == &_y); \
154 _x > _y ? _x : _y; })
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; })
161 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
162 struct nlmsghdr *, struct nl_parser_param *);
165 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
166 struct tc_ratespec *src)
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;
175 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
176 struct rtnl_ratespec *src)
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;
185 static inline char *nl_cache_name(struct nl_cache *cache)
187 return cache->c_ops ? cache->c_ops->co_name : "unknown";
190 #define GENL_FAMILY(id, name) \
192 { id, NL_ACT_UNSPEC, name }, \
193 END_OF_MSGTYPES_LIST, \
196 static inline int wait_for_ack(struct nl_sock *sk)
198 if (sk->s_flags & NL_NO_AUTO_ACK)
201 return nl_wait_for_ack(sk);
204 static inline int build_sysconf_path(char **strp, const char *filename)
208 sysconfdir = getenv("NLSYSCONFDIR");
211 sysconfdir = SYSCONFDIR;
213 return asprintf(strp, "%s/%s", sysconfdir, filename);
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
220 static inline void nl_lock(pthread_mutex_t *lock)
222 pthread_mutex_lock(lock);
225 static inline void nl_unlock(pthread_mutex_t *lock)
227 pthread_mutex_unlock(lock);
230 static inline void nl_read_lock(pthread_rwlock_t *lock)
232 pthread_rwlock_rdlock(lock);
235 static inline void nl_read_unlock(pthread_rwlock_t *lock)
237 pthread_rwlock_unlock(lock);
240 static inline void nl_write_lock(pthread_rwlock_t *lock)
242 pthread_rwlock_wrlock(lock);
245 static inline void nl_write_unlock(pthread_rwlock_t *lock)
247 pthread_rwlock_unlock(lock);
251 #define NL_LOCK(NAME) int __unused_lock_ ##NAME __attribute__((unused))
252 #define NL_RW_LOCK(NAME) int __unused_lock_ ##NAME __attribute__((unused))
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)