added comment
[ana-net.git] / src / xt_fblock.h
blob0c2f85269ddc083b2bc4fded0ff4e57ca6439877
1 /*
2 * Lightweight Autonomic Network Architecture
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL.
7 */
9 #ifndef XT_FBLOCK_H
10 #define XT_FBLOCK_H
12 #ifdef __KERNEL__
14 #include <linux/proc_fs.h>
15 #include <linux/if.h>
16 #include <linux/cpu.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/skbuff.h>
20 #include <linux/notifier.h>
21 #include <linux/radix-tree.h>
23 #include "xt_idp.h"
25 enum path_type {
26 TYPE_INGRESS = 0,
27 #define TYPE_INGRESS TYPE_INGRESS
28 TYPE_EGRESS,
29 #define TYPE_EGRESS TYPE_EGRESS
30 _TYPE_MAX,
33 extern const char *path_names[];
35 enum fblock_mode {
36 MODE_SOURCE = 0,
37 #define MODE_SOURCE MODE_SOURCE
38 MODE_SINK,
39 #define MODE_SINK MODE_SINK
40 MODE_DUAL,
41 #define MODE_DUAL MODE_DUAL
44 #define NUM_TYPES _TYPE_MAX
46 #define FBLOCK_BIND_IDP 0x0001
47 #define FBLOCK_UNBIND_IDP 0x0002
48 #define FBLOCK_SET_OPT 0x0003
49 #define FBLOCK_DOWN_PREPARE 0x0004
50 #define FBLOCK_DOWN 0x0005
52 #endif /* __KERNEL__ */
54 #define FBNAMSIZ IFNAMSIZ
55 #define TYPNAMSIZ FBNAMSIZ
57 #ifdef __KERNEL__
59 extern struct proc_dir_entry *fblock_proc_dir;
61 struct fblock_bind_msg {
62 enum path_type dir;
63 idp_t idp;
66 struct fblock_opt_msg {
67 char *key;
68 char *val;
71 struct fblock;
73 struct fblock_factory {
74 char type[TYPNAMSIZ];
75 enum fblock_mode mode;
76 struct module *owner;
77 struct fblock *(*ctor)(char *name);
78 void (*dtor)(struct fblock *fb);
79 } ____cacheline_aligned;
81 struct fblock_notifier {
82 struct fblock *self;
83 struct notifier_block nb;
84 struct fblock_notifier *next;
85 idp_t remote;
88 struct fblock_subscrib {
89 struct atomic_notifier_head subscribers;
92 struct fblock {
93 char name[FBNAMSIZ];
94 void __percpu *private_data;
95 int (*netfb_rx)(const struct fblock * const fb,
96 struct sk_buff * const skb,
97 enum path_type * const dir);
98 int (*event_rx)(struct notifier_block *self, unsigned long cmd,
99 void *args);
100 struct fblock_factory *factory;
101 struct fblock_notifier *notifiers;
102 struct fblock_subscrib *others;
103 struct rcu_head rcu;
104 atomic_t refcnt;
105 idp_t idp;
106 spinlock_t lock; /* Used in notifiers */
107 } ____cacheline_aligned;
109 extern void free_fblock_rcu(struct rcu_head *rp);
111 static inline void get_fblock(struct fblock *fb)
113 atomic_inc(&fb->refcnt);
116 static inline void put_fblock(struct fblock *fb)
118 if (likely(!atomic_dec_and_test(&fb->refcnt)))
119 return;
120 call_rcu(&fb->rcu, free_fblock_rcu);
124 * Note: __* variants do not hold the rcu_read_lock!
127 /* Allocate/free a new fblock object. */
128 extern struct fblock *alloc_fblock(gfp_t flags);
129 extern void kfree_fblock(struct fblock *p);
131 /* Initialize/cleanup a fblock object. */
132 extern int init_fblock(struct fblock *fb, char *name, void __percpu *priv);
133 extern void cleanup_fblock(struct fblock *fb);
134 extern void cleanup_fblock_ctor(struct fblock *fb);
137 * Registers a fblock object to the stack. Latter variant allocates
138 * a new unused idp, former uses a given _free_ idp.
140 extern int register_fblock(struct fblock *p, idp_t idp);
141 extern int register_fblock_namespace(struct fblock *p);
144 * Unregisters a fblock object from the stack. Former variant does not
145 * release the idp to name mapping, latter variant frees it, too.
147 extern void unregister_fblock(struct fblock *p);
148 extern void unregister_fblock_namespace(struct fblock *p);
149 extern void unregister_fblock_namespace_no_rcu(struct fblock *p);
151 extern struct radix_tree_root fblmap;
153 /* Caller needs to do a put_fblock() after his work is done! */
154 /* Called within RCU read lock! */
155 #define __search_fblock(idp) \
156 ({ \
157 struct fblock *ret = radix_tree_lookup(&fblmap, idp); \
158 if (likely(ret)) \
159 get_fblock(ret); \
160 ret; \
163 /* Returns fblock object specified by idp or name. */
164 extern struct fblock *search_fblock(idp_t idp);
165 extern struct fblock *search_fblock_n(char *name);
167 /* Migrate state from src to dst and drop of dst states */
168 extern void fblock_migrate_p(struct fblock *dst, struct fblock *src);
169 extern void fblock_migrate_r(struct fblock *dst, struct fblock *src);
171 /* Notify fblock of new option. */
172 extern int fblock_set_option(struct fblock *fb, char *opt_string);
173 extern int __fblock_set_option(struct fblock *fb, char *opt_string);
175 /* Binds two fblock objects, increments refcount each. */
176 extern int fblock_bind(struct fblock *fb1, struct fblock *fb2);
177 extern int __fblock_bind(struct fblock *fb1, struct fblock *fb2);
179 /* Unbinds two fblock objects, decrements refcount each. */
180 extern int fblock_unbind(struct fblock *fb1, struct fblock *fb2);
181 extern int __fblock_unbind(struct fblock *fb1, struct fblock *fb2);
183 /* Lookup idp by fblock name. */
184 extern idp_t get_fblock_namespace_mapping(char *name);
185 extern idp_t __get_fblock_namespace_mapping(char *name);
188 * Maps existing fblock name to a new idp, can be used if object has been
189 * removed via unregister_fblock.
191 extern int change_fblock_namespace_mapping(char *name, idp_t new);
192 extern int __change_fblock_namespace_mapping(char *name, idp_t new);
194 extern int subscribe_to_remote_fblock(struct fblock *us,
195 struct fblock *remote);
196 extern void unsubscribe_from_remote_fblock(struct fblock *us,
197 struct fblock *remote);
199 static inline void init_fblock_subscriber(struct fblock *fb,
200 struct notifier_block *nb)
202 nb->priority = 0;
203 nb->notifier_call = fb->event_rx;
204 nb->next = NULL;
207 static inline int
208 fblock_register_foreign_subscriber(struct fblock *us,
209 struct notifier_block *remote)
211 return atomic_notifier_chain_register(&rcu_dereference_raw(us->others)->subscribers,
212 remote);
215 static inline void
216 fblock_unregister_foreign_subscriber(struct fblock *us,
217 struct notifier_block *remote)
219 atomic_notifier_chain_unregister(&rcu_dereference_raw(us->others)->subscribers,
220 remote);
223 static inline int notify_fblock_subscribers(struct fblock *us,
224 unsigned long cmd, void *arg)
226 if (unlikely(!rcu_dereference_raw(us->others)))
227 return -ENOENT;
228 return atomic_notifier_call_chain(&rcu_dereference_raw(us->others)->subscribers,
229 cmd, arg);
232 extern int init_fblock_tables(void);
233 extern void cleanup_fblock_tables(void);
235 /* here is the address, e.g. __builtin_return_address(0) */
236 static inline void fblock_over_panic(struct fblock *fb, void *here)
238 printk(KERN_EMERG "fblock_over_panic: text:%p ptr:%p idp:%u refs:%d "
239 "name:%s priv:%p fac:%p not:%p others: %p\n",
240 here, fb, fb->idp, atomic_read(&fb->refcnt), fb->name,
241 fb->private_data, fb->factory, fb->notifiers, fb->others);
242 BUG();
245 #endif /* __KERNEL__ */
246 #endif /* XT_FBLOCK_H */