PERFORCE change 100507 for review
Paolo Pisati
piso at FreeBSD.org
Mon Jul 3 15:22:55 UTC 2006
http://perforce.freebsd.org/chv.cgi?CH=100507
Change 100507 by piso at piso_newluxor on 2006/07/03 15:22:38
Revert previous changes: i axed too much stuff.
Affected files ...
.. //depot/projects/soc2005/libalias/sys/netinet/libalias/alias_mod.c#4 edit
.. //depot/projects/soc2005/libalias/sys/netinet/libalias/alias_mod.h#3 edit
Differences ...
==== //depot/projects/soc2005/libalias/sys/netinet/libalias/alias_mod.c#4 (text+ko) ====
@@ -56,15 +56,20 @@
#include <sys/types.h>
+#if __FreeBSD_version >= 500000
/* XXX - make the compiler happy... */
int strncmp(const char *s1, const char *s2, size_t len);
+#endif
-/* Protocol and userland module handlers chains. */
+/* protocol and userland module handlers chains */
struct chain handler_chain, dll_chain;
-// XXX - use queue(3) and errno.h
#ifdef _KERNEL
+#if __FreeBSD_version >= 500000
+
+/* Fine grained locking for 5.x, 6.x and 7.x */
+
#define LIBALIAS_LOCK_INIT(_chain) \
mtx_init(&(_chain)->mtx, "libalias list of proto-handlers", NULL, \
MTX_DEF | MTX_RECURSE)
@@ -122,8 +127,58 @@
if (mtx_initialized(&c->mtx))
LIBALIAS_LOCK_DESTROY(c);
}
+#else
+
+/* Good old spl*() locking for 4.x */
+/*
+ * XXX - i'm not sure about mutex & conditional var
+ * conversion that i did using spl*()...
+ */
+
+#define LIBALIAS_LOCK_INIT(_chain) (_chain)->spl = 0
+#define LIBALIAS_LOCK_DESTROY(_chain)
+#define LIBALIAS_WLOCK_ASSERT(_chain) do { \
+ KASSERT(_chain->spl != 0, ("chain not locked")); \
+} while (0)
+
+static __inline void
+LIBALIAS_RLOCK(struct chain *chain)
+{
+ chain->spl = splimp();
+}
-#else
+static __inline void
+LIBALIAS_RUNLOCK(struct chain *chain)
+{
+ splx(chain->spl);
+}
+
+static __inline void
+LIBALIAS_WLOCK(struct chain *chain)
+{
+ LIBALIAS_RLOCK(chain);
+}
+
+static __inline void
+LIBALIAS_WUNLOCK(struct chain *chain)
+{
+ LIBALIAS_RUNLOCK(chain);
+}
+
+static void
+_handler_chain_init(struct chain *c) {
+
+ c->spl = 0;
+}
+
+static void
+_handler_chain_destroy(struct chain *c) {
+
+ ;
+}
+
+#endif
+#else
#define LIBALIAS_LOCK_INIT(_chain) ;
#define LIBALIAS_LOCK_DESTROY(_chain) ;
@@ -182,16 +237,16 @@
LIBALIAS_WLOCK_ASSERT(c);
b = (struct proto_handler **)&c->chain;
- p->next = NULL; /* I'm paranoid... */
+ p->next = NULL; /* i'm paranoid... */
for(; *b != NULL; b = &((*b)->next), i++) {
if (((*b)->pri == p->pri) && ((*b)->dir == p->dir) &&
((*b)->proto == p->proto))
- return (EHDCON); /* Priority conflict. */
+ return (EHDCON); /* priority conflict */
if ((*b)->pri > p->pri) {
p->next = *b; break;
}
}
- /* End of list or got right position, insert here. */
+ /* end of list or got right position, insert here */
*b = p;
return (OK);
}
@@ -205,62 +260,62 @@
for(; (*b != NULL) && (*b != p); b = &((*b)->next))
;
if (*b == p) *b = p->next;
- else return (EHDNOF); /* Handler not found. */
+ else return (EHDNOF); /* handler not found */
return (OK);
}
int
attach_handlers(struct proto_handler *_p) {
- int i, error = NOK;
+ int i, res = NOK;
LIBALIAS_WLOCK(&handler_chain);
for (i=0; 1; i++) {
if (*((int *)&_p[i]) == EOH) break;
- error = _attach_handler(&handler_chain, &_p[i]);
- if (error != OK) break;
+ res = _attach_handler(&handler_chain, &_p[i]);
+ if (res != OK) break;
}
LIBALIAS_WUNLOCK(&handler_chain);
- return (error);
+ return (res);
}
int
detach_handlers(struct proto_handler *_p) {
- int i, error = NOK;
+ int i, res = NOK;
LIBALIAS_WLOCK(&handler_chain);
for (i=0; 1; i++) {
if (*((int *)&_p[i]) == EOH) break;
- error = _detach_handler(&handler_chain, &_p[i]);
- if (error != OK) break;
+ res = _detach_handler(&handler_chain, &_p[i]);
+ if (res != OK) break;
}
LIBALIAS_WUNLOCK(&handler_chain);
- return (error);
+ return (res);
}
int
detach_handler(struct proto_handler *_p) {
- int error = NOK;
+ int res = NOK;
LIBALIAS_WLOCK(&handler_chain);
- error = _detach_handler(&handler_chain, _p);
+ res = _detach_handler(&handler_chain, _p);
LIBALIAS_WUNLOCK(&handler_chain);
- return (error);
+ return (res);
}
int
find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *pip, struct alias_data *ad) {
struct proto_handler *p;
- int error;
+ int err;
LIBALIAS_RLOCK(&handler_chain);
- for (p = handler_chain.chain, error = EHDNOF; p != NULL; p = p->next)
+ for (p = handler_chain.chain, err = EHDNOF; p != NULL; p = p->next)
if ((p->dir & dir) && (p->proto & proto))
if (p->fingerprint(la, pip, ad) == OK) {
- error = p->protohandler(la, pip, ad);
+ err = p->protohandler(la, pip, ad);
break;
}
LIBALIAS_RUNLOCK(&handler_chain);
- return (error);
+ return (err);
}
struct proto_handler *
@@ -276,11 +331,11 @@
LIBALIAS_WLOCK_ASSERT(c);
b = (struct dll **)&c->chain;
- p->next = NULL; /* I'm paranoid... */
+ p->next = NULL; /* i'm paranoid... */
for(; *b != NULL; b = &((*b)->next), i++)
if (!strncmp((*b)->name, p->name, DLL_LEN))
- return (EHDCON); /* Dll name conflict. */
- /* End of list, insert here. */
+ return (EHDCON); /* dll name conflict */
+ /* end of list, insert here */
*b = p;
return (OK);
}
@@ -288,17 +343,17 @@
static void *
_detach_dll(struct chain *c, char *p) {
struct dll **b;
- void *error = NULL;
+ void *err = NULL;
LIBALIAS_WLOCK_ASSERT(c);
b = (struct dll **)&c->chain;
for(; *b != NULL; b = &((*b)->next))
if (!strncmp((*b)->name, p, DLL_LEN)) {
- error = *b;
+ err = *b;
*b = (*b)->next;
break;
}
- return (error);
+ return (err);
}
int
==== //depot/projects/soc2005/libalias/sys/netinet/libalias/alias_mod.h#3 (text+ko) ====
@@ -24,7 +24,7 @@
* SUCH DAMAGE.
*/
-/*
+/*-
* Alias_mod.h defines the outside world interfaces for the packet aliasing
* modular framework
*/
@@ -32,14 +32,13 @@
#ifndef _ALIAS_MOD_H_
#define _ALIAS_MOD_H_
-/* Protocol handlers struct & function. */
+/* protocol handlers struct & function*/
-/* Packet flow direction. */
+/* packet flow direction */
#define IN 1
#define OUT 2
-// XXX - use ip.h?
-/* Working protocol. */
+/* working protocol */
#define IP 1
#define TCP 2
#define UDP 4
@@ -48,16 +47,17 @@
/*
* Data passed to protocol handler module, it must be filled
* right before calling find_handler() to determine which
- * module is elegible to be called.
+ * module is elegible to be called
*/
-struct alias_data {
+struct alias_data {
+
struct alias_link *lnk;
struct in_addr *original_address;
struct in_addr *alias_address;
- uint16_t *alias_port;
- uint16_t *sport, *dport;
- uint16_t maxpacketsize;
+ u_short *alias_port;
+ u_int16_t *sport, *dport;
+ int maxpacketsize;
};
/*
@@ -65,8 +65,8 @@
* a protocol handler correctly work.
*/
-// XXX - use queue(3)
struct proto_handler {
+
u_int pri; /* handler priority */
int16_t dir; /* flow direction */
int16_t proto; /* working protocol */
@@ -77,14 +77,22 @@
struct proto_handler *next;
};
-// XXX - use rwlock(9)
+#if __FreeBSD_version >= 500000
struct chain {
+
void *chain;
struct mtx mtx; /* lock guarding list */
int busy_count; /* busy count for rw locks */
int want_write;
struct cv cv;
};
+#else
+struct chain {
+
+ void *chain;
+ int spl;
+};
+#endif
/*
* Used only in userland when libalias needs to keep track of all
@@ -93,7 +101,8 @@
*/
#define DLL_LEN 32
-struct dll {
+struct dll {
+
char name[DLL_LEN]; /* name of module */
void *handle; /*
* ptr to shared obj obtained through
@@ -127,7 +136,6 @@
#define OK 1
#define NOK -1
-// XXX - convert all this stuff to use errno.h
/* end of handlers */
#define EOH -1
More information about the p4-projects
mailing list