switch->netjig patch 4
Jeff, this patch (also http://www.sandelman.ca/tmp/jig4.patch ) adds contextualization to the switch functions. I hope that this patch is small enough to chew.
for hash.[ch], this is relatively simple, as it just adds struct nethub
* to all functions, and gets rid of the global "h[]" array.
for nethub.c's cleanup_nh(), it cleans up some additional items that may get created, such as per-switch directories.
add_fd() gets context argument, and changes its allocation technique to grow the array, doubling in size each time.
A new function, init_nethub() gathers all initialization stuff that was done by uml_switch's main() into one place.
In port.c, the linked list is replaced with one from queue.h, which has nice macros to cope.
nethub.h grows the new nethub and netjig_state structures.
for uml_switch, we don't need multiple netjig_state's, but we will later.
in port.c, some printf()'s are changed to fprintf(stderr,). Also some of the code is just unwrapped to be more readable.new_port becomes handle_new_port(), and gets made more readable. A new function handle_port() takes over logic from uml_switch.
This is probably the biggest patch that there is.
uml_router/hash.c +49 -56
@@ -12,12 +12,11 @@#include <sys/types.h> #include <sys/time.h> #include <sys/signal.h> + +#include "nethub.h" #include "switch.h" #include "hash.h" -#define HASH_SIZE 128 -#define HASH_MOD 11 -struct hash_entry {struct hash_entry *next;struct hash_entry *prev;
@@ -26,37 +25,38 @@unsigned char dst[ETH_ALEN ]; }; -static struct hash_entry *h[HASH_SIZE ]; -static int calc_hash (char *src) {return ((*(u_int32_t *) &src[0 ] %HASH_MOD ) ^ src[4 ] ^ src[5 ] ) %HASH_SIZE ; } -static struct hash_entry *find_entry (char *dst) +static struct hash_entry *find_entry (struct nethub *nh, +char *dst) {struct hash_entry *e;int k =calc_hash (dst); -for (e = h[k]; e; e = e->next){ +for (e = nh->h[k]; e; e = e->next){if (!memcmp (&e->dst, dst,ETH_ALEN ))return (e); }return (NULL ); } -void *find_in_hash (char *dst) +void *find_in_hash (struct nethub *nh, +char *dst) { -struct hash_entry *e =find_entry (dst); +struct hash_entry *e =find_entry (nh, dst);if (e ==NULL )return (NULL );return (e->port); } -void insert_into_hash (char *src,void *port) +void insert_into_hash (struct nethub *nh, +char *src,void *port) {struct hash_entry *new;int k =calc_hash (src); - new =find_in_hash (src); + new =find_in_hash (nh, src);if (new !=NULL )return ; new =malloc (sizeof (*new));
@@ -66,50 +66,57 @@ }memcpy (&new->dst, src,ETH_ALEN ); -if (h[k] !=NULL ) h[k]->prev = new; - new->next = h[k]; +if (nh->h[k] !=NULL ) nh->h[k]->prev = new; + new->next = nh->h[k]; new->prev =NULL ; new->port = port; new->last_seen =0 ; - h[k] = new; + nh->h[k] = new; } -void update_entry_time (char *src) +void update_entry_time (struct nethub *nh, +char *src) {struct hash_entry *e; - e =find_entry (src); + e =find_entry (nh, src);if (e ==NULL )return ; e->last_seen =time (NULL ); } -static void delete_hash_entry (struct hash_entry *old) +static void delete_hash_entry (struct nethub *nh, +struct hash_entry *old) {int k =calc_hash (old->dst);if (old->prev !=NULL ) old->prev->next = old->next;if (old->next !=NULL ) old->next->prev = old->prev; -if (h[k] == old) h[k] = old->next; +if (nh->h[k] == old) nh->h[k] = old->next;free (old); } -void delete_hash (char *dst) +void delete_hash (struct nethub *nh, +char *dst) { -struct hash_entry *old =find_entry (dst); +struct hash_entry *old =find_entry (nh, dst);if (old ==NULL )return ; -delete_hash_entry (old); +delete_hash_entry (nh, old); } -static void for_all_hash (void (*f)(struct hash_entry *,void *),void *arg) +static void for_all_hash (struct nethub *nh, +void (*f)(struct nethub *nh, +struct hash_entry *, +void *), +void *arg) {int i;struct hash_entry *e, *next;for (i =0 ; i <HASH_SIZE ; i++){ -for (e = h[i]; e; e = next){ +for (e = nh->h[i]; e; e = next){ next = e->next; - (*f)(e, arg); + (*f)(nh, e, arg); } } }
@@ -119,57 +126,43 @@char *(*port_id)(void *); }; -static void print_hash_entry (struct hash_entry *e,void *arg) +static void print_hash_entry (struct nethub *nh, +struct hash_entry *e,void *arg) {struct printer *p = arg; -printf ("Hash: %d Addr: %02x:%02x:%02x:%02x:%02x:%02x to port: %s " -"age %ld secs\n" ,calc_hash (e->dst), - e->dst[0 ], e->dst[1 ], e->dst[2 ], e->dst[3 ], e->dst[4 ], e->dst[5 ], - (*p->port_id)(e->port), (int ) p->now - e->last_seen); +fprintf (stderr,"Hash: %d Addr: %02x:%02x:%02x:%02x:%02x:%02x to port: %s " +"age %ld secs\n" ,calc_hash (e->dst), + e->dst[0 ], e->dst[1 ], e->dst[2 ], e->dst[3 ], e->dst[4 ], e->dst[5 ], + (*p->port_id)(e->port), (int ) p->now - e->last_seen); } -void print_hash (char *(*port_id)(void *)) +void print_hash (struct nethub *nh, +char *(*port_id)(void *)) {struct printer p = ((struct printer ) { now :time (NULL ), port_id : port_id }); -for_all_hash (print_hash_entry, &p); +for_all_hash (nh, print_hash_entry, &p); } -#define GC_INTERVAL 2 -#define GC_EXPIRE 100 - -static void gc (struct hash_entry *e,void *now) +static void gc (struct nethub *nh, +struct hash_entry *e, +void *now) {time_t t = *(time_t *) now;if (e->last_seen +GC_EXPIRE < t) -delete_hash_entry (e); +delete_hash_entry (nh, e); } -static void sig_alarm (int sig) +void hash_periodic (struct nethub *nh) { -struct itimerval it;time_t t =time (NULL ); -for_all_hash (&gc, &t); - - it.it_value.tv_sec =GC_INTERVAL ; - it.it_value.tv_usec =0 ; - it.it_interval.tv_sec =0 ; - it.it_interval.tv_usec =0 ; -setitimer (ITIMER_REAL , &it,NULL ); +for_all_hash (nh, &gc, &t); } - -void hash_init (void ) + +void hash_init (struct nethub *nh) { -struct sigaction sa; - - sa.sa_handler = sig_alarm; - sa.sa_flags =SA_RESTART ; -if (sigaction (SIGALRM , &sa,NULL ) <0 ){ -perror ("Setting handler for SIGALRM" ); -return ; - } -kill (getpid (),SIGALRM ); +/* nothing right now */ }
uml_router/hash.h +13 -6
@@ -5,11 +5,18 @@#ifndef __HASH_H__#define __HASH_H__ -extern void *find_in_hash (char *dst); -extern void insert_into_hash (char *src,void *port); -extern void delete_hash (char *dst); -extern void print_hash (char *(*port_id)(void *)); -extern void update_entry_time (char *src); -extern void hash_init (void ); +struct nethub ; + +extern void *find_in_hash (struct nethub *nh,char *dst); +extern void insert_into_hash (struct nethub *nh,char *src,void *port); +extern void delete_hash (struct nethub *nh,char *dst); +extern void print_hash (struct nethub *nh, +char *(*port_id)(void *)); +extern void update_entry_time (struct nethub *nh,char *src); +extern void hash_init (struct nethub *nh); +extern void hash_periodic (struct nethub *nh); + +#define GC_INTERVAL 2 +#define GC_EXPIRE 100#endif
Binary files uml_tools.jig3/uml_router/hash.o and uml_tools.jig4/uml_router/hash.o differ
uml_router/nethub.c +335 -56
515 lines not on this page · raw message
uml_router/nethub.h +94 -15
127 lines not on this page · raw message
Binary files uml_tools.jig3/uml_router/nethub.o and uml_tools.jig4/uml_router/nethub.o differ
uml_router/netjig.h +29 -0
@@ -0,0 +1,29 @@ +extern char *progname; +extern jmp_buf getMeOut; + +#define ETH_ALEN 6 + +#define MAX (x,y) ((x) > (y) ? (x) : (y)) + +/* prototypes */ +extern void sig_handler (int sig); +extern void create_socket_dir (struct netjig_state *ns); +extern void cmdprompt (struct netjig_state *ns); +extern void forward_data (struct nethub *nh,struct packet *p,int len); +extern int cmdread (struct netjig_state *ns,char *buf,int len); +extern void *xmalloc1 (size_t size,char *file,int linenum); + +/* from cmdmode.c */ + +extern void finish_waitplay (struct netjig_state *ns); +extern int cmdread (struct netjig_state *ns,char *buf,int len); + +/* +* $Log: netjig.h,v $ +* +* Local variables: +* c-file-style: "linux" +* c-basic-offset: 8 +* End: +* +*/
uml_router/port.c +293 -133
613 lines not on this page · raw message
uml_router/port.h +59 -9
74 lines not on this page · raw message
Binary files uml_tools.jig3/uml_router/port.o and uml_tools.jig4/uml_router/port.o differ
uml_router/tuntap.c +10 -2
@@ -6,6 +6,8 @@#include <sys/ioctl.h> #include <net/if.h> #include <linux/if_tun.h> + +#include "nethub.h" #include "port.h" static void send_tap (int fd,void *packet,int len,void *unused)
@@ -18,7 +20,9 @@ } } -int open_tap (char *dev) +int open_tap (struct netjig_state *ns, +struct nethub *nh, +char *dev) {struct ifreq ifr;int fd, err;
@@ -35,8 +39,12 @@close (fd);return (-1 ); } - err =setup_port (fd, send_tap,NULL ,0 ); + + err =setup_sock_tap (ns, nh, fd, send_tap);if (err)return (err); + +add_fd (ns, fd); +return (fd); }
uml_router/tuntap.h +4 -1
@@ -5,7 +5,10 @@#ifndef __TUNTAP_H__#define __TUNTAP_H__ -extern int open_tap (char *dev); +extern int open_tap (struct netjig_state *ns, +struct nethub *nh, +char *dev); +extern void handle_tap (int fd,int hub);#endif
Binary files uml_tools.jig3/uml_router/tuntap.o and uml_tools.jig4/uml_router/tuntap.o differ Binary files uml_tools.jig3/uml_router/uml_switch and uml_tools.jig4/uml_router/uml_switch differ
uml_router/uml_switch.c +152 -98
357 lines not on this page · raw message
Binary files uml_tools.jig3/uml_router/uml_switch.o and uml_tools.jig4/uml_router/uml_switch.o differ
-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X. From Windows to Linux, servers to mobile, InstallShield X is the one installation-authoring solution that does it all. Learn more and evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
User-mode-linux-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel