1 /* Copyright 2005 Renzo Davoli VDE-2
2 * Copyright 2002 Yon Uriarte and Jeff Dike (uml_switch)
3 * Licensed under the GPLv2
4 * Modified 2003 Renzo Davoli
15 #include <sys/types.h>
17 #include <sys/signal.h>
21 #include <vdecommon.h>
29 #define MIN_PERSISTENCE_DFL 3
30 static int min_persistence
=MIN_PERSISTENCE_DFL
;
31 #define HASH_INIT_BITS 7
34 #define HASH_SIZE (1 << hash_bits)
37 #define DBGHASHNEW (dl)
38 #define DBGHASHDEL (dl+1)
39 static struct dbgcl dl
[]= {
40 {"hash/+","hash: new element",D_HASH
|D_PLUS
},
41 {"hash/-","hash: discarded element",D_HASH
|D_MINUS
},
45 struct hash_entry
*next
;
46 struct hash_entry
**prev
;
52 static struct hash_entry
**h
;
54 static int calc_hash(u_int64_t src
)
57 src
*= 0xff51afd7ed558ccd;
59 src
*= 0xc4ceb9fe1a85ec53;
60 return src
& hash_mask
;
63 #if BYTE_ORDER == LITTLE_ENDIAN
64 #define EMAC2MAC6(X) \
65 (u_int)((X)&0xff), (u_int)(((X)>>8)&0xff), (u_int)(((X)>>16)&0xff), \
66 (u_int)(((X)>>24)&0xff), (u_int)(((X)>>32)&0xff), (u_int)(((X)>>40)&0xff)
67 #elif BYTE_ORDER == BIG_ENDIAN
68 #define EMAC2MAC6(X) \
69 (u_int)(((X)>>24)&0xff), (u_int)(((X)>>16)&0xff), (u_int)(((X)>>8)&0xff), \
70 (u_int)((X)&0xff), (u_int)(((X)>>40)&0xff), (u_int)(((X)>>32)&0xff)
72 #error Unknown Endianess
75 #define EMAC2VLAN(X) ((u_int16_t) ((X)>>48))
76 #define EMAC2VLAN2(X) ((u_int) (((X)>>48) &0xff)), ((u_int) (((X)>>56) &0xff))
78 #define find_entry(MAC) \
79 ({struct hash_entry *e; \
80 int k = calc_hash(MAC);\
81 for(e = h[k]; e && e->dst != (MAC); e = e->next)\
86 #define extmac(MAC,VLAN) \
87 ((*(u_int32_t *) &((MAC)[0])) + ((u_int64_t) ((*(u_int16_t *) &((MAC)[4]))+ ((u_int64_t) (VLAN) << 16)) << 32))
89 /* looks in global hash table 'h' for given address, and return associated
91 int find_in_hash(unsigned char *dst
,int vlan
)
93 struct hash_entry
*e
= find_entry(extmac(dst
,vlan
));
94 if(e
== NULL
) return -1;
99 int find_in_hash_update(unsigned char *src
,int vlan
,int port
)
101 struct hash_entry
*e
;
102 u_int64_t esrc
=extmac(src
,vlan
);
103 int k
= calc_hash(esrc
);
106 for(e
= h
[k
]; e
&& e
->dst
!= esrc
; e
= e
->next
)
109 e
= (struct hash_entry
*) malloc(sizeof(*e
));
111 printlog(LOG_WARNING
,"Failed to malloc hash entry %s",strerror(errno
));
115 DBGOUT(DBGHASHNEW
,"%02x:%02x:%02x:%02x:%02x:%02x VLAN %02x:%02x Port %d",
116 EMAC2MAC6(esrc
), EMAC2VLAN2(esrc
), port
);
117 EVENTOUT(DBGHASHNEW
,esrc
);
119 if(h
[k
] != NULL
) h
[k
]->prev
= &(e
->next
);
128 if ((now
- e
->last_seen
) > min_persistence
) {
138 #define delete_hash_entry(OLD) \
140 DBGOUT(DBGHASHDEL,"%02x:%02x:%02x:%02x:%02x:%02x VLAN %02x:%02x Port %d", EMAC2MAC6(OLD->dst), EMAC2VLAN2(OLD->dst), OLD->port); \
141 EVENTOUT(DBGHASHDEL,OLD->dst);\
142 *((OLD)->prev)=(OLD)->next; \
143 if((OLD)->next != NULL) (OLD)->next->prev = (OLD)->prev; \
148 void delete_hash(unsigned char *dst
,int vlan
)
150 struct hash_entry
*old
= find_entry(extmac(dst
,vlan
));
152 if(old
== NULL
) return;
154 delete_hash_entry(old
);
158 /* for each entry of the global hash table 'h', calls function f, passing to it
159 * the hash entry and the additional arg 'arg' */
160 static void for_all_hash(void (*f
)(struct hash_entry
*, void *), void *arg
)
163 struct hash_entry
*e
, *next
;
165 for(i
= 0; i
< HASH_SIZE
; i
++){
166 for(e
= h
[i
]; e
; e
= next
){
173 static void delete_port_iterator (struct hash_entry
*e
, void *arg
)
175 int *pport
=(int *)arg
;
176 if (e
->port
== *pport
)
177 delete_hash_entry(e
);
180 void hash_delete_port (int port
)
183 for_all_hash(delete_port_iterator
,&port
);
187 static void delete_vlan_iterator (struct hash_entry
*e
, void *arg
)
189 int *vlan
=(int *)arg
;
190 if (EMAC2VLAN(e
->dst
) == (u_int16_t
)(*vlan
))
191 delete_hash_entry(e
);
194 void hash_delete_vlan (int vlan
)
197 for_all_hash(delete_vlan_iterator
,&vlan
);
201 struct vlanport
{int vlan
; int port
;};
203 static void delete_vlanport_iterator (struct hash_entry
*e
, void *arg
)
205 struct vlanport
*vp
=(struct vlanport
*)arg
;
206 if ((EMAC2VLAN(e
->dst
)) == (u_int16_t
)(vp
->vlan
) &&
208 delete_hash_entry(e
);
211 void hash_delete_vlanport (int vlan
,int port
)
213 struct vlanport vp
={vlan
,port
};
215 for_all_hash(delete_vlanport_iterator
,&vp
);
219 struct vlansetofports
{int vlan
; bitarray setofports
;};
221 static void delete_vlansetofports_iterator (struct hash_entry
*e
, void *arg
)
223 struct vlansetofports
*vp
=(struct vlansetofports
*)arg
;
224 if ((EMAC2VLAN(e
->dst
)) == (u_int16_t
)(vp
->vlan
) &&
225 ba_check(vp
->setofports
,e
->port
))
226 delete_hash_entry(e
);
229 void hash_delete_vlanports (int vlan
,bitarray setofports
)
231 struct vlansetofports vp
={vlan
,setofports
};
233 for_all_hash(delete_vlansetofports_iterator
,&vp
);
237 static void flush_iterator (struct hash_entry
*e
, void *arg
)
239 delete_hash_entry(e
);
245 for_all_hash(flush_iterator
,NULL
);
250 #define GC_INTERVAL 2
251 #define GC_EXPIRE 100
252 static int gc_interval
;
253 static int gc_expire
;
254 static unsigned int gc_timerno
;
256 /* clean from the hash table entries older than GC_EXPIRE seconds, given that
257 * 'now' points to a time_t structure describing the current time */
258 static void gc(struct hash_entry
*e
, void *now
)
260 time_t t
= *(time_t *) now
;
262 if(e
->last_seen
+ gc_expire
< t
)
263 delete_hash_entry(e
);
266 /* clean old entries in the hash table 'h', and prepare the timer to be called
267 * again between GC_INTERVAL seconds */
268 static void hash_gc(void *arg
)
271 for_all_hash(&gc
, &t
);
274 #define HASH_INIT(BIT) \
276 hash_mask=HASH_SIZE-1;\
277 if ((h=(struct hash_entry **) calloc (HASH_SIZE,sizeof (struct hash_entry *))) == NULL) {\
278 printlog(LOG_WARNING,"Failed to malloc hash table %s",strerror(errno));\
283 static inline int po2round(int vx
)
290 while (x
) { x
>>=1; i
++; }
292 printlog(LOG_WARNING
,"Hash size must be a power of 2. %d rounded to %d",vx
,1<<i
);
297 int hash_resize(int hash_size
)
303 HASH_INIT(po2round(hash_size
));
310 int hash_set_gc_interval(int p
)
312 qtimer_del(gc_timerno
);
314 gc_timerno
=qtimer_add(gc_interval
,0,hash_gc
,NULL
);
318 int hash_set_gc_expire(int e
)
324 int hash_set_minper(int e
)
330 int hash_get_gc_interval()
335 int hash_get_gc_expire()
340 static int find_hash(FILE *fd
,char *strmac
)
343 unsigned char macv
[ETH_ALEN
];
344 unsigned char *mac
=macv
;
347 struct hash_entry
*e
;
348 if (index(strmac
,':') != NULL
)
349 rv
=sscanf(strmac
,"%x:%x:%x:%x:%x:%x %d", maci
+0, maci
+1, maci
+2, maci
+3, maci
+4, maci
+5, &vlan
);
351 rv
=sscanf(strmac
,"%x.%x.%x.%x.%x.%x %d", maci
+0, maci
+1, maci
+2, maci
+3, maci
+4, maci
+5, &vlan
);
356 for (i
=0;i
<ETH_ALEN
;i
++)
358 e
=find_entry(extmac(mac
,vlan
));
362 printoutc(fd
,"Hash: %04d Addr: %02x:%02x:%02x:%02x:%02x:%02x VLAN %04d to port: %03d "
363 "age %ld secs", calc_hash(e
->dst
),
364 EMAC2MAC6(e
->dst
),EMAC2VLAN(e
->dst
), e
->port
+1, qtime() - e
->last_seen
);
370 static void print_hash_entry(struct hash_entry
*e
, void *arg
)
374 printoutc(pfd
,"Hash: %04d Addr: %02x:%02x:%02x:%02x:%02x:%02x VLAN %04d to port: %03d "
375 "age %ld secs", calc_hash(e
->dst
),
376 EMAC2MAC6(e
->dst
),EMAC2VLAN(e
->dst
), e
->port
, qtime() - e
->last_seen
);
379 static int print_hash(FILE *fd
)
382 for_all_hash(print_hash_entry
, fd
);
387 static int showinfo(FILE *fd
)
389 printoutc(fd
,"Hash size %d",HASH_SIZE
);
390 printoutc(fd
,"GC interval %d secs",gc_interval
);
391 printoutc(fd
,"GC expire %d secs",gc_expire
);
392 printoutc(fd
,"Min persistence %d secs",min_persistence
);
396 static struct comlist cl
[]={
397 {"hash","============","HASH TABLE MENU",NULL
,NOARG
},
398 {"hash/showinfo","","show hash info",showinfo
,NOARG
|WITHFILE
},
399 {"hash/setsize","N","change hash size",hash_resize
,INTARG
},
400 {"hash/setgcint","N","change garbage collector interval",hash_set_gc_interval
,INTARG
},
401 {"hash/setexpire","N","change hash entries expire time",hash_set_gc_expire
,INTARG
},
402 {"hash/setminper","N","minimum persistence time",hash_set_minper
,INTARG
},
403 {"hash/print","","print the hash table",print_hash
,NOARG
|WITHFILE
},
404 {"hash/find","MAC [VLAN]","MAC lookup",find_hash
,STRARG
|WITHFILE
},
407 /* sets sig_alarm as handler for SIGALRM, and run it a first time */
408 void hash_init(int hash_size
)
410 HASH_INIT(po2round(hash_size
));
412 gc_interval
=GC_INTERVAL
;
414 gc_timerno
=qtimer_add(gc_interval
,0,hash_gc
,NULL
);