2 * Lightweight Autonomic Network Architecture
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
14 #include <linux/proc_fs.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>
27 #define TYPE_INGRESS TYPE_INGRESS
29 #define TYPE_EGRESS TYPE_EGRESS
33 extern const char *path_names
[];
37 #define MODE_SOURCE MODE_SOURCE
39 #define MODE_SINK MODE_SINK
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
59 extern struct proc_dir_entry
*fblock_proc_dir
;
61 struct fblock_bind_msg
{
66 struct fblock_opt_msg
{
73 struct fblock_factory
{
75 enum fblock_mode mode
;
77 struct fblock
*(*ctor
)(char *name
);
78 void (*dtor
)(struct fblock
*fb
);
79 } ____cacheline_aligned
;
81 struct fblock_notifier
{
83 struct notifier_block nb
;
84 struct fblock_notifier
*next
;
88 struct fblock_subscrib
{
89 struct atomic_notifier_head subscribers
;
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
,
100 struct fblock_factory
*factory
;
101 struct fblock_notifier
*notifiers
;
102 struct fblock_subscrib
*others
;
107 spinlock_t lock
; /* Used in notifiers */
108 } ____cacheline_aligned
;
110 extern void free_fblock_rcu(struct rcu_head
*rp
);
112 static inline void get_fblock(struct fblock
*fb
)
114 atomic_inc(&fb
->refcnt
);
117 static inline void put_fblock(struct fblock
*fb
)
119 if (likely(!atomic_dec_and_test(&fb
->refcnt
)))
121 call_rcu(&fb
->rcu
, free_fblock_rcu
);
125 * Note: __* variants do not hold the rcu_read_lock!
128 /* Allocate/free a new fblock object. */
129 extern struct fblock
*alloc_fblock(gfp_t flags
);
130 extern void kfree_fblock(struct fblock
*p
);
132 /* Initialize/cleanup a fblock object. */
133 extern int init_fblock(struct fblock
*fb
, char *name
, void __percpu
*priv
);
134 extern void cleanup_fblock(struct fblock
*fb
);
135 extern void cleanup_fblock_ctor(struct fblock
*fb
);
138 * Registers a fblock object to the stack. Latter variant allocates
139 * a new unused idp, former uses a given _free_ idp.
141 extern int register_fblock(struct fblock
*p
, idp_t idp
);
142 extern int register_fblock_namespace(struct fblock
*p
);
145 * Unregisters a fblock object from the stack. Former variant does not
146 * release the idp to name mapping, latter variant frees it, too.
148 extern void unregister_fblock(struct fblock
*p
);
149 extern void unregister_fblock_namespace(struct fblock
*p
);
150 extern void unregister_fblock_namespace_no_rcu(struct fblock
*p
);
152 extern struct radix_tree_root fblmap
;
154 /* Caller needs to do a put_fblock() after his work is done! */
155 /* Called within RCU read lock! */
156 #define __search_fblock(idp) \
158 struct fblock *ret = radix_tree_lookup(&fblmap, idp); \
164 /* Returns fblock object specified by idp or name. */
165 extern struct fblock
*search_fblock(idp_t idp
);
166 extern struct fblock
*search_fblock_n(char *name
);
168 /* Migrate state from src to dst and drop of dst states */
169 extern void fblock_migrate_p(struct fblock
*dst
, struct fblock
*src
);
170 extern void fblock_migrate_r(struct fblock
*dst
, struct fblock
*src
);
172 /* Notify fblock of new option. */
173 extern int fblock_set_option(struct fblock
*fb
, char *opt_string
);
174 extern int __fblock_set_option(struct fblock
*fb
, char *opt_string
);
176 /* Binds two fblock objects, increments refcount each. */
177 extern int fblock_bind(struct fblock
*fb1
, struct fblock
*fb2
);
178 extern int __fblock_bind(struct fblock
*fb1
, struct fblock
*fb2
);
180 /* Unbinds two fblock objects, decrements refcount each. */
181 extern int fblock_unbind(struct fblock
*fb1
, struct fblock
*fb2
);
182 extern int __fblock_unbind(struct fblock
*fb1
, struct fblock
*fb2
);
184 /* Lookup idp by fblock name. */
185 extern idp_t
get_fblock_namespace_mapping(char *name
);
186 extern idp_t
__get_fblock_namespace_mapping(char *name
);
189 * Maps existing fblock name to a new idp, can be used if object has been
190 * removed via unregister_fblock.
192 extern int change_fblock_namespace_mapping(char *name
, idp_t
new);
193 extern int __change_fblock_namespace_mapping(char *name
, idp_t
new);
195 extern int subscribe_to_remote_fblock(struct fblock
*us
,
196 struct fblock
*remote
);
197 extern void unsubscribe_from_remote_fblock(struct fblock
*us
,
198 struct fblock
*remote
);
200 static inline void init_fblock_subscriber(struct fblock
*fb
,
201 struct notifier_block
*nb
)
204 nb
->notifier_call
= fb
->event_rx
;
209 fblock_register_foreign_subscriber(struct fblock
*us
,
210 struct notifier_block
*remote
)
212 return atomic_notifier_chain_register(&rcu_dereference_raw(us
->others
)->subscribers
,
217 fblock_unregister_foreign_subscriber(struct fblock
*us
,
218 struct notifier_block
*remote
)
220 atomic_notifier_chain_unregister(&rcu_dereference_raw(us
->others
)->subscribers
,
224 static inline int notify_fblock_subscribers(struct fblock
*us
,
225 unsigned long cmd
, void *arg
)
227 if (unlikely(!rcu_dereference_raw(us
->others
)))
229 return atomic_notifier_call_chain(&rcu_dereference_raw(us
->others
)->subscribers
,
233 extern int init_fblock_tables(void);
234 extern void cleanup_fblock_tables(void);
236 /* here is the address, e.g. __builtin_return_address(0) */
237 static inline void fblock_over_panic(struct fblock
*fb
, void *here
)
239 printk(KERN_EMERG
"fblock_over_panic: text:%p ptr:%p idp:%u refs:%d "
240 "name:%s next:%p priv:%p fac:%p not:%p others: %p\n",
241 here
, fb
, fb
->idp
, atomic_read(&fb
->refcnt
), fb
->name
, fb
->next
,
242 fb
->private_data
, fb
->factory
, fb
->notifiers
, fb
->others
);
246 #endif /* __KERNEL__ */
247 #endif /* XT_FBLOCK_H */