fb migration, part 2
[ana-net.git] / src / xt_fblock.h
blobc33e20eee4bfd748265cc15907afd024e33b8f62
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/rwlock.h>
19 #include <linux/skbuff.h>
20 #include <linux/notifier.h>
22 #include "xt_idp.h"
24 enum path_type {
25 TYPE_INGRESS = 0,
26 #define TYPE_INGRESS TYPE_INGRESS
27 TYPE_EGRESS,
28 #define TYPE_EGRESS TYPE_EGRESS
29 _TYPE_MAX,
32 #define NUM_TYPES _TYPE_MAX
34 #define FBLOCK_BIND_IDP 0x0001
35 #define FBLOCK_UNBIND_IDP 0x0002
36 #define FBLOCK_SET_OPT 0x0003
37 #define FBLOCK_DOWN_PREPARE 0x0004
38 #define FBLOCK_DOWN 0x0005
40 #endif /* __KERNEL__ */
42 #define FBNAMSIZ IFNAMSIZ
43 #define TYPNAMSIZ FBNAMSIZ
45 #ifdef __KERNEL__
47 extern struct proc_dir_entry *fblock_proc_dir;
49 struct fblock_bind_msg {
50 enum path_type dir;
51 idp_t idp;
54 struct fblock_opt_msg {
55 char *key;
56 char *val;
59 struct fblock;
61 struct fblock_ops {
62 int (*netfb_rx)(struct fblock *fb, struct sk_buff *skb,
63 enum path_type *dir);
64 int (*event_rx)(struct notifier_block *self, unsigned long cmd,
65 void *args);
68 struct fblock_factory {
69 char type[TYPNAMSIZ];
70 struct module *owner;
71 struct fblock *(*ctor)(char *name);
72 void (*dtor)(struct fblock *fb);
73 } ____cacheline_aligned;
75 struct fblock_notifier {
76 struct fblock *self;
77 struct fblock *remote;
78 struct notifier_block nb;
79 struct fblock_notifier *next;
80 } ____cacheline_aligned;
82 struct fblock_subscrib {
83 struct atomic_notifier_head subscribers;
86 struct fblock {
87 char name[FBNAMSIZ];
88 void *private_data;
89 struct fblock_ops *ops;
90 struct fblock_factory *factory;
91 struct fblock_notifier *notifiers;
92 struct fblock_subscrib *others;
93 struct fblock *next;
94 struct rcu_head rcu;
95 atomic_t refcnt;
96 idp_t idp;
97 rwlock_t lock; /* Used in notifiers */
98 } ____cacheline_aligned;
101 * Note: __* variants do not hold the rcu_read_lock!
104 /* Allocate/free a new fblock object. */
105 extern struct fblock *alloc_fblock(gfp_t flags);
106 extern void kfree_fblock(struct fblock *p);
108 enum cleanup_mode {
109 CLEAN_FREE_PRIV,
110 CLEAN_NOFREE_PRIV,
113 /* Initialize/cleanup a fblock object. */
114 extern int init_fblock(struct fblock *fb, char *name, void *priv,
115 struct fblock_ops *ops);
116 extern void cleanup_fblock(struct fblock *fb);
117 extern void cleanup_fblock_ctor(struct fblock *fb);
120 * Registers a fblock object to the stack. Latter variant allocates
121 * a new unused idp, former uses a given _free_ idp.
123 extern int register_fblock(struct fblock *p, idp_t idp);
124 extern int register_fblock_namespace(struct fblock *p);
127 * Unregisters a fblock object from the stack. Former variant does not
128 * release the idp to name mapping, latter variant frees it, too.
130 extern int unregister_fblock(struct fblock *p);
131 extern void unregister_fblock_namespace(struct fblock *p);
132 extern void unregister_fblock_namespace_no_rcu(struct fblock *p);
134 /* Returns fblock object specified by idp or name. */
135 extern struct fblock *search_fblock(idp_t idp);
136 extern struct fblock *__search_fblock(idp_t idp);
137 extern struct fblock *search_fblock_n(char *name);
138 extern struct fblock *__search_fblock_n(char *name);
140 /* Migrate state from src to dst and drop of dst states */
141 extern int fblock_migrate(struct fblock *dst, struct fblock *src);
143 /* Notify fblock of new option. */
144 extern int fblock_set_option(struct fblock *fb, char *opt_string);
145 extern int __fblock_set_option(struct fblock *fb, char *opt_string);
147 /* Binds two fblock objects, increments refcount each. */
148 extern int fblock_bind(struct fblock *fb1, struct fblock *fb2);
149 extern int __fblock_bind(struct fblock *fb1, struct fblock *fb2);
151 /* Unbinds two fblock objects, decrements refcount each. */
152 extern int fblock_unbind(struct fblock *fb1, struct fblock *fb2);
153 extern int __fblock_unbind(struct fblock *fb1, struct fblock *fb2);
155 /* Lookup idp by fblock name. */
156 extern idp_t get_fblock_namespace_mapping(char *name);
157 extern idp_t __get_fblock_namespace_mapping(char *name);
160 * Maps existing fblock name to a new idp, can be used if object has been
161 * removed via unregister_fblock.
163 extern int change_fblock_namespace_mapping(char *name, idp_t new);
164 extern int __change_fblock_namespace_mapping(char *name, idp_t new);
166 extern int subscribe_to_remote_fblock(struct fblock *us,
167 struct fblock *remote);
168 extern void unsubscribe_from_remote_fblock(struct fblock *us,
169 struct fblock *remote);
171 static inline void init_fblock_subscriber(struct fblock *fb,
172 struct notifier_block *nb)
174 nb->priority = 0;
175 nb->notifier_call = fb->ops->event_rx;
176 nb->next = NULL;
179 static inline int
180 fblock_register_foreign_subscriber(struct fblock *us,
181 struct notifier_block *remote)
183 return atomic_notifier_chain_register(&rcu_dereference_raw(us->others)->subscribers,
184 remote);
187 static inline void
188 fblock_unregister_foreign_subscriber(struct fblock *us,
189 struct notifier_block *remote)
191 atomic_notifier_chain_unregister(&rcu_dereference_raw(us->others)->subscribers,
192 remote);
195 static inline int notify_fblock_subscribers(struct fblock *us,
196 unsigned long cmd, void *arg)
198 if (unlikely(!rcu_dereference_raw(us->others)))
199 return -ENOENT;
200 return atomic_notifier_call_chain(&rcu_dereference_raw(us->others)->subscribers,
201 cmd, arg);
204 static inline void get_fblock(struct fblock *fb)
206 atomic_inc(&fb->refcnt);
209 static inline void put_fblock(struct fblock *fb)
211 if (likely(!atomic_dec_and_test(&fb->refcnt)))
212 return;
213 cleanup_fblock(fb);
214 kfree_fblock(fb);
217 extern int init_fblock_tables(void);
218 extern void cleanup_fblock_tables(void);
220 #endif /* __KERNEL__ */
221 #endif /* XT_FBLOCK_H */