/* * BIRD -- Route Attribute Cache * * (c) 1998--2000 Martin Mares * * Can be freely distributed and used under the terms of the GNU GPL. */ /** * DOC: Route attribute cache * * Each route entry carries a set of route attributes. Several of them * vary from route to route, but most attributes are usually common * for a large number of routes. To conserve memory, we've decided to * store only the varying ones directly in the &rte and hold the rest * in a special structure called &rta which is shared among all the * &rte's with these attributes. * * Each &rta contains all the static attributes of the route (i.e., * those which are always present) as structure members and a list of * dynamic attributes represented by a linked list of &ea_list * structures, each of them consisting of an array of &eattr's containing * the individual attributes. An attribute can be specified more than once * in the &ea_list chain and in such case the first occurrence overrides * the others. This semantics is used especially when someone (for example * a filter) wishes to alter values of several dynamic attributes, but * it wants to preserve the original attribute lists maintained by * another module. * * Each &eattr contains an attribute identifier (split to protocol ID and * per-protocol attribute ID), protocol dependent flags, a type code (consisting * of several bit fields describing attribute characteristics) and either an * embedded 32-bit value or a pointer to a &adata structure holding attribute * contents. * * There exist two variants of &rta's -- cached and un-cached ones. Un-cached * &rta's can have arbitrarily complex structure of &ea_list's and they * can be modified by any module in the route processing chain. Cached * &rta's have their attribute lists normalized (that means at most one * &ea_list is present and its values are sorted in order to speed up * searching), they are stored in a hash table to make fast lookup possible * and they are provided with a use count to allow sharing. * * Routing tables always contain only cached &rta's. */ #include "nest/bird.h" #include "nest/rt.h" #include "nest/protocol.h" #include "nest/iface.h" #include "nest/cli.h" #include "lib/attrs.h" #include "lib/alloca.h" #include "lib/hash.h" #include "lib/idm.h" #include "lib/resource.h" #include "lib/string.h" #include #include const adata null_adata; /* adata of length 0 */ struct ea_class ea_gen_igp_metric = { .name = "igp_metric", .type = T_INT, }; struct ea_class ea_gen_preference = { .name = "preference", .type = T_INT, }; struct ea_class ea_gen_from = { .name = "from", .type = T_IP, }; const char * const rta_src_names[RTS_MAX] = { [RTS_STATIC] = "static", [RTS_INHERIT] = "inherit", [RTS_DEVICE] = "device", [RTS_STATIC_DEVICE] = "static-device", [RTS_REDIRECT] = "redirect", [RTS_RIP] = "RIP", [RTS_OSPF] = "OSPF", [RTS_OSPF_IA] = "OSPF-IA", [RTS_OSPF_EXT1] = "OSPF-E1", [RTS_OSPF_EXT2] = "OSPF-E2", [RTS_BGP] = "BGP", [RTS_PIPE] = "pipe", [RTS_BABEL] = "Babel", [RTS_RPKI] = "RPKI", }; static void ea_gen_source_format(const eattr *a, byte *buf, uint size) { if ((a->u.data >= RTS_MAX) || !rta_src_names[a->u.data]) bsnprintf(buf, size, "unknown"); else bsnprintf(buf, size, "%s", rta_src_names[a->u.data]); } struct ea_class ea_gen_source = { .name = "source", .type = T_ENUM_RTS, .readonly = 1, .format = ea_gen_source_format, }; struct ea_class ea_gen_nexthop = { .name = "nexthop", .type = T_NEXTHOP_LIST, }; struct ea_class ea_mpls_labels = { .name = "mpls_labels", .type = T_CLIST, .readonly = 1, }; const char * rta_dest_names[RTD_MAX] = { [RTD_NONE] = "", [RTD_UNICAST] = "unicast", [RTD_BLACKHOLE] = "blackhole", [RTD_UNREACHABLE] = "unreachable", [RTD_PROHIBIT] = "prohibited", }; pool *rta_pool; static slab *rta_slab; static slab *rte_src_slab; static struct idm src_ids; #define SRC_ID_INIT_SIZE 4 /* rte source hash */ #define RSH_KEY(n) n->proto, n->private_id #define RSH_NEXT(n) n->next #define RSH_EQ(p1,n1,p2,n2) p1 == p2 && n1 == n2 #define RSH_FN(p,n) p->hash_key ^ u32_hash(n) #define RSH_REHASH rte_src_rehash #define RSH_PARAMS /2, *2, 1, 1, 8, 20 #define RSH_INIT_ORDER 6 static HASH(struct rte_src) src_hash; static void rte_src_init(void) { rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src)); idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE); HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER); } HASH_DEFINE_REHASH_FN(RSH, struct rte_src) struct rte_src * rt_find_source(struct proto *p, u32 id) { return HASH_FIND(src_hash, RSH, p, id); } struct rte_src * rt_get_source(struct proto *p, u32 id) { struct rte_src *src = rt_find_source(p, id); if (src) return src; src = sl_allocz(rte_src_slab); src->proto = p; src->private_id = id; src->global_id = idm_alloc(&src_ids); src->uc = 0; HASH_INSERT2(src_hash, RSH, rta_pool, src); return src; } void rt_prune_sources(void) { HASH_WALK_FILTER(src_hash, next, src, sp) { if (src->uc == 0) { HASH_DO_REMOVE(src_hash, RSH, sp); idm_free(&src_ids, src->global_id); sl_free(src); } } HASH_WALK_FILTER_END; HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool); } /* * Multipath Next Hop */ static int nexthop_compare_node(const struct nexthop *x, const struct nexthop *y) { int r; /* Should we also compare flags ? */ r = ((int) y->weight) - ((int) x->weight); if (r) return r; r = ipa_compare(x->gw, y->gw); if (r) return r; r = ((int) y->labels) - ((int) x->labels); if (r) return r; for (int i = 0; i < y->labels; i++) { r = ((int) y->label[i]) - ((int) x->label[i]); if (r) return r; } return ((int) x->iface->index) - ((int) y->iface->index); } static int nexthop_compare_qsort(const void *x, const void *y) { return nexthop_compare_node( *(const struct nexthop **) x, *(const struct nexthop **) y ); } /** * nexthop_merge - merge nexthop lists * @x: list 1 * @y: list 2 * @max: max number of nexthops * @lp: linpool for allocating nexthops * * The nexthop_merge() function takes two nexthop lists @x and @y and merges them, * eliminating possible duplicates. The input lists must be sorted and the * result is sorted too. The number of nexthops in result is limited by @max. * New nodes are allocated from linpool @lp. * * The arguments @rx and @ry specify whether corresponding input lists may be * consumed by the function (i.e. their nodes reused in the resulting list), in * that case the caller should not access these lists after that. To eliminate * issues with deallocation of these lists, the caller should use some form of * bulk deallocation (e.g. stack or linpool) to free these nodes when the * resulting list is no longer needed. When reusability is not set, the * corresponding lists are not modified nor linked from the resulting list. */ struct nexthop_adata * nexthop_merge(struct nexthop_adata *xin, struct nexthop_adata *yin, int max, linpool *lp) { uint outlen = ADATA_SIZE(xin->ad.length) + ADATA_SIZE(yin->ad.length); struct nexthop_adata *out = lp_alloc(lp, outlen); out->ad.length = outlen - sizeof (struct adata); struct nexthop *x = &xin->nh, *y = &yin->nh, *cur = &out->nh; int xvalid, yvalid; while (max--) { xvalid = NEXTHOP_VALID(x, xin); yvalid = NEXTHOP_VALID(y, yin); if (!xvalid && !yvalid) break; ASSUME(NEXTHOP_VALID(cur, out)); int cmp = !xvalid ? 1 : !yvalid ? -1 : nexthop_compare_node(x, y); if (cmp < 0) { ASSUME(NEXTHOP_VALID(x, xin)); memcpy(cur, x, nexthop_size(x)); x = NEXTHOP_NEXT(x); } else if (cmp > 0) { ASSUME(NEXTHOP_VALID(y, yin)); memcpy(cur, y, nexthop_size(y)); y = NEXTHOP_NEXT(y); } else { ASSUME(NEXTHOP_VALID(x, xin)); memcpy(cur, x, nexthop_size(x)); x = NEXTHOP_NEXT(x); ASSUME(NEXTHOP_VALID(y, yin)); y = NEXTHOP_NEXT(y); } cur = NEXTHOP_NEXT(cur); } out->ad.length = (void *) cur - (void *) out->ad.data; return out; } struct nexthop_adata * nexthop_sort(struct nexthop_adata *nhad, linpool *lp) { /* Count the nexthops */ uint cnt = 0; NEXTHOP_WALK(nh, nhad) cnt++; if (cnt <= 1) return nhad; /* Get pointers to them */ struct nexthop **sptr = tmp_alloc(cnt * sizeof(struct nexthop *)); uint i = 0; NEXTHOP_WALK(nh, nhad) sptr[i++] = nh; /* Sort the pointers */ qsort(sptr, cnt, sizeof(struct nexthop *), nexthop_compare_qsort); /* Allocate the output */ struct nexthop_adata *out = (struct nexthop_adata *) lp_alloc_adata(lp, nhad->ad.length); struct nexthop *dest = &out->nh; /* Deduplicate nexthops while storing them */ for (uint i = 0; i < cnt; i++) { if (i && !nexthop_compare_node(sptr[i], sptr[i-1])) continue; memcpy(dest, sptr[i], NEXTHOP_SIZE(sptr[i])); dest = NEXTHOP_NEXT(dest); } out->ad.length = (void *) dest - (void *) out->ad.data; return out; } int nexthop_is_sorted(struct nexthop_adata *nhad) { struct nexthop *prev = NULL; NEXTHOP_WALK(nh, nhad) { if (prev && (nexthop_compare_node(prev, nh) >= 0)) return 0; prev = nh; } return 1; } /* * Extended Attributes */ #define EA_CLASS_INITIAL_MAX 128 static struct ea_class **ea_class_global = NULL; static uint ea_class_max; static struct idm ea_class_idm; /* Config parser lex register function */ void ea_lex_register(struct ea_class *def); void ea_lex_unregister(struct ea_class *def); static void ea_class_free(struct ea_class *cl) { /* No more ea class references. Unregister the attribute. */ idm_free(&ea_class_idm, cl->id); ea_class_global[cl->id] = NULL; ea_lex_unregister(cl); } static void ea_class_ref_free(resource *r) { struct ea_class_ref *ref = SKIP_BACK(struct ea_class_ref, r, r); if (!--ref->class->uc) ea_class_free(ref->class); } static void ea_class_ref_dump(resource *r) { struct ea_class_ref *ref = SKIP_BACK(struct ea_class_ref, r, r); debug("name \"%s\", type=%d\n", ref->class->name, ref->class->type); } static struct resclass ea_class_ref_class = { .name = "Attribute class reference", .size = sizeof(struct ea_class_ref), .free = ea_class_ref_free, .dump = ea_class_ref_dump, .lookup = NULL, .memsize = NULL, }; static void ea_class_init(void) { idm_init(&ea_class_idm, rta_pool, EA_CLASS_INITIAL_MAX); ea_class_global = mb_allocz(rta_pool, sizeof(*ea_class_global) * (ea_class_max = EA_CLASS_INITIAL_MAX)); } static struct ea_class_ref * ea_ref_class(pool *p, struct ea_class *def) { def->uc++; struct ea_class_ref *ref = ralloc(p, &ea_class_ref_class); ref->class = def; return ref; } static struct ea_class_ref * ea_register(pool *p, struct ea_class *def) { def->id = idm_alloc(&ea_class_idm); ASSERT_DIE(ea_class_global); while (def->id >= ea_class_max) ea_class_global = mb_realloc(ea_class_global, sizeof(*ea_class_global) * (ea_class_max *= 2)); ASSERT_DIE(def->id < ea_class_max); ea_class_global[def->id] = def; ea_lex_register(def); return ea_ref_class(p, def); } struct ea_class_ref * ea_register_alloc(pool *p, struct ea_class cl) { struct ea_class *clp = ea_class_find_by_name(cl.name); if (clp && clp->type == cl.type) return ea_ref_class(p, clp); uint namelen = strlen(cl.name) + 1; struct { struct ea_class cl; char name[0]; } *cla = mb_alloc(rta_pool, sizeof(struct ea_class) + namelen); cla->cl = cl; memcpy(cla->name, cl.name, namelen); cla->cl.name = cla->name; return ea_register(p, &cla->cl); } void ea_register_init(struct ea_class *clp) { ASSERT_DIE(!ea_class_find_by_name(clp->name)); ea_register(&root_pool, clp); } struct ea_class * ea_class_find_by_id(uint id) { ASSERT_DIE(id < ea_class_max); ASSERT_DIE(ea_class_global[id]); return ea_class_global[id]; } static inline eattr * ea__find(ea_list *e, unsigned id) { eattr *a; int l, r, m; while (e) { if (e->flags & EALF_BISECT) { l = 0; r = e->count - 1; while (l <= r) { m = (l+r) / 2; a = &e->attrs[m]; if (a->id == id) return a; else if (a->id < id) l = m+1; else r = m-1; } } else for(m=0; mcount; m++) if (e->attrs[m].id == id) return &e->attrs[m]; e = e->next; } return NULL; } /** * ea_find - find an extended attribute * @e: attribute list to search in * @id: attribute ID to search for * * Given an extended attribute list, ea_find() searches for a first * occurrence of an attribute with specified ID, returning either a pointer * to its &eattr structure or %NULL if no such attribute exists. */ eattr * ea_find_by_id(ea_list *e, unsigned id) { eattr *a = ea__find(e, id & EA_CODE_MASK); if (a && a->undef && !(id & EA_ALLOW_UNDEF)) return NULL; return a; } /** * ea_walk - walk through extended attributes * @s: walk state structure * @id: start of attribute ID interval * @max: length of attribute ID interval * * Given an extended attribute list, ea_walk() walks through the list looking * for first occurrences of attributes with ID in specified interval from @id to * (@id + @max - 1), returning pointers to found &eattr structures, storing its * walk state in @s for subsequent calls. * * The function ea_walk() is supposed to be called in a loop, with initially * zeroed walk state structure @s with filled the initial extended attribute * list, returning one found attribute in each call or %NULL when no other * attribute exists. The extended attribute list or the arguments should not be * modified between calls. The maximum value of @max is 128. */ eattr * ea_walk(struct ea_walk_state *s, uint id, uint max) { ea_list *e = s->eattrs; eattr *a = s->ea; eattr *a_max; max = id + max; if (a) goto step; for (; e; e = e->next) { if (e->flags & EALF_BISECT) { int l, r, m; l = 0; r = e->count - 1; while (l < r) { m = (l+r) / 2; if (e->attrs[m].id < id) l = m + 1; else r = m; } a = e->attrs + l; } else a = e->attrs; step: a_max = e->attrs + e->count; for (; a < a_max; a++) if ((a->id >= id) && (a->id < max)) { int n = a->id - id; if (BIT32_TEST(s->visited, n)) continue; BIT32_SET(s->visited, n); if (a->undef) continue; s->eattrs = e; s->ea = a; return a; } else if (e->flags & EALF_BISECT) break; } return NULL; } static inline void ea_do_sort(ea_list *e) { unsigned n = e->count; eattr *a = e->attrs; eattr *b = alloca(n * sizeof(eattr)); unsigned s, ss; /* We need to use a stable sorting algorithm, hence mergesort */ do { s = ss = 0; while (s < n) { eattr *p, *q, *lo, *hi; p = b; ss = s; *p++ = a[s++]; while (s < n && p[-1].id <= a[s].id) *p++ = a[s++]; if (s < n) { q = p; *p++ = a[s++]; while (s < n && p[-1].id <= a[s].id) *p++ = a[s++]; lo = b; hi = q; s = ss; while (lo < q && hi < p) if (lo->id <= hi->id) a[s++] = *lo++; else a[s++] = *hi++; while (lo < q) a[s++] = *lo++; while (hi < p) a[s++] = *hi++; } } } while (ss); } /** * In place discard duplicates and undefs in sorted ea_list. We use stable sort * for this reason. **/ static inline void ea_do_prune(ea_list *e) { eattr *s, *d, *l, *s0; int i = 0; s = d = e->attrs; /* Beginning of the list. @s is source, @d is destination. */ l = e->attrs + e->count; /* End of the list */ /* Walk from begin to end. */ while (s < l) { s0 = s++; /* Find a consecutive block of the same attribute */ while (s < l && s->id == s[-1].id) s++; /* Now s0 is the most recent version, s[-1] the oldest one */ /* Drop undefs */ if (s0->undef) continue; /* Copy the newest version to destination */ *d = *s0; /* Preserve info whether it originated locally */ d->originated = s[-1].originated; /* Not fresh any more, we prefer surstroemming */ d->fresh = 0; /* Next destination */ d++; i++; } e->count = i; } /** * ea_sort - sort an attribute list * @e: list to be sorted * * This function takes a &ea_list chain and sorts the attributes * within each of its entries. * * If an attribute occurs multiple times in a single &ea_list, * ea_sort() leaves only the first (the only significant) occurrence. */ static void ea_sort(ea_list *e) { while (e) { if (!(e->flags & EALF_SORTED)) { ea_do_sort(e); ea_do_prune(e); e->flags |= EALF_SORTED; } if (e->count > 5) e->flags |= EALF_BISECT; e = e->next; } } /** * ea_scan - estimate attribute list size * @e: attribute list * * This function calculates an upper bound of the size of * a given &ea_list after merging with ea_merge(). */ static unsigned ea_scan(const ea_list *e) { unsigned cnt = 0; while (e) { cnt += e->count; e = e->next; } return sizeof(ea_list) + sizeof(eattr)*cnt; } /** * ea_merge - merge segments of an attribute list * @e: attribute list * @t: buffer to store the result to * * This function takes a possibly multi-segment attribute list * and merges all of its segments to one. * * The primary use of this function is for &ea_list normalization: * first call ea_scan() to determine how much memory will the result * take, then allocate a buffer (usually using alloca()), merge the * segments with ea_merge() and finally sort and prune the result * by calling ea_sort(). */ static void ea_merge(const ea_list *e, ea_list *t) { eattr *d = t->attrs; t->flags = 0; t->count = 0; t->next = NULL; while (e) { memcpy(d, e->attrs, sizeof(eattr)*e->count); t->count += e->count; d += e->count; e = e->next; } } ea_list * ea_normalize(const ea_list *e) { ea_list *t = tmp_alloc(ea_scan(e)); ea_merge(e, t); ea_sort(t); return t->count ? t : NULL; } /** * ea_same - compare two &ea_list's * @x: attribute list * @y: attribute list * * ea_same() compares two normalized attribute lists @x and @y and returns * 1 if they contain the same attributes, 0 otherwise. */ int ea_same(ea_list *x, ea_list *y) { int c; if (!x || !y) return x == y; ASSERT(!x->next && !y->next); if (x->count != y->count) return 0; for(c=0; ccount; c++) { eattr *a = &x->attrs[c]; eattr *b = &y->attrs[c]; if (a->id != b->id || a->flags != b->flags || a->type != b->type || a->originated != b->originated || a->fresh != b->fresh || a->undef != b->undef || ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr))) return 0; } return 1; } uint ea_list_size(ea_list *o) { unsigned i, elen; ASSERT_DIE(o); ASSERT_DIE(!o->next); elen = BIRD_CPU_ALIGN(sizeof(ea_list) + sizeof(eattr) * o->count); for(i=0; icount; i++) { eattr *a = &o->attrs[i]; if (!(a->type & EAF_EMBEDDED)) elen += ADATA_SIZE(a->u.ptr->length); } return elen; } void ea_list_copy(ea_list *n, ea_list *o, uint elen) { uint adpos = sizeof(ea_list) + sizeof(eattr) * o->count; memcpy(n, o, adpos); adpos = BIRD_CPU_ALIGN(adpos); for(uint i=0; icount; i++) { eattr *a = &n->attrs[i]; if (!(a->type & EAF_EMBEDDED)) { unsigned size = ADATA_SIZE(a->u.ptr->length); ASSERT_DIE(adpos + size <= elen); struct adata *d = ((void *) n) + adpos; memcpy(d, a->u.ptr, size); a->u.ptr = d; adpos += size; } } ASSERT_DIE(adpos == elen); } static void ea_list_ref(ea_list *l) { for(uint i=0; icount; i++) { eattr *a = &l->attrs[i]; ASSERT_DIE(a->id < ea_class_max); struct ea_class *cl = ea_class_global[a->id]; ASSERT_DIE(cl && cl->uc); cl->uc++; } } static void ea_list_unref(ea_list *l) { for(uint i=0; icount; i++) { eattr *a = &l->attrs[i]; ASSERT_DIE(a->id < ea_class_max); struct ea_class *cl = ea_class_global[a->id]; ASSERT_DIE(cl && cl->uc); if (!--cl->uc) ea_class_free(cl); } } static inline void ea_free(ea_list *o) { if (o) { ea_list_unref(o); ASSERT(!o->next); mb_free(o); } } void ea_format_bitfield(const struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max) { byte *bound = buf + bufsize - 32; u32 data = a->u.data; int i; for (i = min; i < max; i++) if ((data & (1u << i)) && names[i]) { if (buf > bound) { strcpy(buf, " ..."); return; } buf += bsprintf(buf, " %s", names[i]); data &= ~(1u << i); } if (data) bsprintf(buf, " %08x", data); return; } static inline void opaque_format(const struct adata *ad, byte *buf, uint size) { byte *bound = buf + size - 10; uint i; for(i = 0; i < ad->length; i++) { if (buf > bound) { strcpy(buf, " ..."); return; } if (i) *buf++ = ' '; buf += bsprintf(buf, "%02x", ad->data[i]); } *buf = 0; return; } static inline void ea_show_int_set(struct cli *c, const struct adata *ad, int way, byte *pos, byte *buf, byte *end) { int i = int_set_format(ad, way, 0, pos, end - pos); cli_printf(c, -1012, "\t%s", buf); while (i) { i = int_set_format(ad, way, i, buf, end - buf - 1); cli_printf(c, -1012, "\t\t%s", buf); } } static inline void ea_show_ec_set(struct cli *c, const struct adata *ad, byte *pos, byte *buf, byte *end) { int i = ec_set_format(ad, 0, pos, end - pos); cli_printf(c, -1012, "\t%s", buf); while (i) { i = ec_set_format(ad, i, buf, end - buf - 1); cli_printf(c, -1012, "\t\t%s", buf); } } static inline void ea_show_lc_set(struct cli *c, const struct adata *ad, byte *pos, byte *buf, byte *end) { int i = lc_set_format(ad, 0, pos, end - pos); cli_printf(c, -1012, "\t%s", buf); while (i) { i = lc_set_format(ad, i, buf, end - buf - 1); cli_printf(c, -1012, "\t\t%s", buf); } } /** * ea_show - print an &eattr to CLI * @c: destination CLI * @e: attribute to be printed * * This function takes an extended attribute represented by its &eattr * structure and prints it to the CLI according to the type information. * * If the protocol defining the attribute provides its own * get_attr() hook, it's consulted first. */ void ea_show(struct cli *c, const eattr *e) { const struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr; byte buf[CLI_MSG_SIZE]; byte *pos = buf, *end = buf + sizeof(buf); ASSERT_DIE(e->id < ea_class_max); struct ea_class *cls = ea_class_global[e->id]; ASSERT_DIE(cls); pos += bsprintf(pos, "%s", cls->name); *pos++ = ':'; *pos++ = ' '; if (e->undef) bsprintf(pos, "undefined (should not happen)"); else if (cls->format) cls->format(e, buf, end - buf); else switch (e->type) { case T_INT: bsprintf(pos, "%u", e->u.data); break; case T_OPAQUE: opaque_format(ad, pos, end - pos); break; case T_IP: bsprintf(pos, "%I", *(ip_addr *) ad->data); break; case T_QUAD: bsprintf(pos, "%R", e->u.data); break; case T_PATH: as_path_format(ad, pos, end - pos); break; case T_CLIST: ea_show_int_set(c, ad, 1, pos, buf, end); return; case T_ECLIST: ea_show_ec_set(c, ad, pos, buf, end); return; case T_LCLIST: ea_show_lc_set(c, ad, pos, buf, end); return; default: bsprintf(pos, "", e->type); } cli_printf(c, -1012, "\t%s", buf); } static void nexthop_dump(const struct adata *ad) { struct nexthop_adata *nhad = (struct nexthop_adata *) ad; debug(":"); NEXTHOP_WALK(nh, nhad) { if (ipa_nonzero(nh->gw)) debug(" ->%I", nh->gw); if (nh->labels) debug(" L %d", nh->label[0]); for (int i=1; ilabels; i++) debug("/%d", nh->label[i]); debug(" [%s]", nh->iface ? nh->iface->name : "???"); } } /** * ea_dump - dump an extended attribute * @e: attribute to be dumped * * ea_dump() dumps contents of the extended attribute given to * the debug output. */ void ea_dump(ea_list *e) { int i; if (!e) { debug("NONE"); return; } while (e) { debug("[%c%c%c]", (e->flags & EALF_SORTED) ? 'S' : 's', (e->flags & EALF_BISECT) ? 'B' : 'b', (e->flags & EALF_CACHED) ? 'C' : 'c'); for(i=0; icount; i++) { eattr *a = &e->attrs[i]; debug(" %04x.%02x", a->id, a->flags); debug("=%c", "?iO?IRP???S??pE?" "??L???N?????????" "?o???r??????????" [a->type]); if (a->originated) debug("o"); if (a->type & EAF_EMBEDDED) debug(":%08x", a->u.data); else if (a->id == ea_gen_nexthop.id) nexthop_dump(a->u.ptr); else { int j, len = a->u.ptr->length; debug("[%d]:", len); for(j=0; ju.ptr->data[j]); } } if (e = e->next) debug(" | "); } } /** * ea_hash - calculate an &ea_list hash key * @e: attribute list * * ea_hash() takes an extended attribute list and calculated a hopefully * uniformly distributed hash value from its contents. */ inline uint ea_hash(ea_list *e) { const u64 mul = 0x68576150f3d6847; u64 h = 0xafcef24eda8b29; int i; if (e) /* Assuming chain of length 1 */ { ASSERT_DIE(!e->next); for(i=0; icount; i++) { struct eattr *a = &e->attrs[i]; h ^= a->id; h *= mul; if (a->type & EAF_EMBEDDED) h ^= a->u.data; else { const struct adata *d = a->u.ptr; h ^= mem_hash(d->data, d->length); } h *= mul; } } return (h >> 32) ^ (h & 0xffffffff); } /** * ea_append - concatenate &ea_list's * @to: destination list (can be %NULL) * @what: list to be appended (can be %NULL) * * This function appends the &ea_list @what at the end of * &ea_list @to and returns a pointer to the resulting list. */ ea_list * ea_append(ea_list *to, ea_list *what) { ea_list *res; if (!to) return what; res = to; while (to->next) to = to->next; to->next = what; return res; } /* * rta's */ static uint rta_cache_count; static uint rta_cache_size = 32; static uint rta_cache_limit; static uint rta_cache_mask; static rta **rta_hash_table; static void rta_alloc_hash(void) { rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size); if (rta_cache_size < 32768) rta_cache_limit = rta_cache_size * 2; else rta_cache_limit = ~0; rta_cache_mask = rta_cache_size - 1; } static inline uint rta_hash(rta *a) { u64 h; mem_hash_init(&h); #define MIX(f) mem_hash_mix(&h, &(a->f), sizeof(a->f)); #define BMIX(f) mem_hash_mix_num(&h, a->f); MIX(hostentry); BMIX(dest); #undef MIX return mem_hash_value(&h) ^ ea_hash(a->eattrs); } static inline int rta_same(rta *x, rta *y) { return (x->dest == y->dest && x->hostentry == y->hostentry && ea_same(x->eattrs, y->eattrs)); } static rta * rta_copy(rta *o) { rta *r = sl_alloc(rta_slab); memcpy(r, o, rta_size(o)); r->uc = 1; if (!r->eattrs) return r; uint elen = ea_list_size(o->eattrs); r->eattrs = mb_alloc(rta_pool, elen); ea_list_copy(r->eattrs, o->eattrs, elen); ea_list_ref(r->eattrs); r->eattrs->flags |= EALF_CACHED; return r; } static inline void rta_insert(rta *r) { uint h = r->hash_key & rta_cache_mask; r->next = rta_hash_table[h]; if (r->next) r->next->pprev = &r->next; r->pprev = &rta_hash_table[h]; rta_hash_table[h] = r; } static void rta_rehash(void) { uint ohs = rta_cache_size; uint h; rta *r, *n; rta **oht = rta_hash_table; rta_cache_size = 2*rta_cache_size; DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size); rta_alloc_hash(); for(h=0; hnext; rta_insert(r); } mb_free(oht); } /** * rta_lookup - look up a &rta in attribute cache * @o: a un-cached &rta * * rta_lookup() gets an un-cached &rta structure and returns its cached * counterpart. It starts with examining the attribute cache to see whether * there exists a matching entry. If such an entry exists, it's returned and * its use count is incremented, else a new entry is created with use count * set to 1. * * The extended attribute lists attached to the &rta are automatically * converted to the normalized form. */ rta * rta_lookup(rta *o) { rta *r; uint h; ASSERT(!o->cached); if (o->eattrs) o->eattrs = ea_normalize(o->eattrs); h = rta_hash(o); for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next) if (r->hash_key == h && rta_same(r, o)) return rta_clone(r); r = rta_copy(o); r->hash_key = h; r->cached = 1; rt_lock_hostentry(r->hostentry); rta_insert(r); if (++rta_cache_count > rta_cache_limit) rta_rehash(); return r; } void rta__free(rta *a) { ASSERT(rta_cache_count && a->cached); rta_cache_count--; *a->pprev = a->next; if (a->next) a->next->pprev = a->pprev; rt_unlock_hostentry(a->hostentry); ea_free(a->eattrs); a->cached = 0; sl_free(a); } rta * rta_do_cow(rta *o, linpool *lp) { rta *r = lp_alloc(lp, rta_size(o)); memcpy(r, o, rta_size(o)); r->cached = 0; r->uc = 0; return r; } /** * rta_dump - dump route attributes * @a: attribute structure to dump * * This function takes a &rta and dumps its contents to the debug output. */ void rta_dump(rta *a) { static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" }; debug("uc=%d %s h=%04x", a->uc, rtd[a->dest], a->hash_key); if (!a->cached) debug(" !CACHED"); if (a->eattrs) { debug(" EA: "); ea_dump(a->eattrs); } } /** * rta_dump_all - dump attribute cache * * This function dumps the whole contents of route attribute cache * to the debug output. */ void rta_dump_all(void) { rta *a; uint h; debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit); for(h=0; hnext) { debug("%p ", a); rta_dump(a); debug("\n"); } debug("\n"); } void rta_show(struct cli *c, rta *a) { for(ea_list *eal = a->eattrs; eal; eal=eal->next) for(int i=0; icount; i++) ea_show(c, &eal->attrs[i]); } /** * rta_init - initialize route attribute cache * * This function is called during initialization of the routing * table module to set up the internals of the attribute cache. */ void rta_init(void) { rta_pool = rp_new(&root_pool, "Attributes"); rta_slab = sl_new(rta_pool, sizeof(rta)); rta_alloc_hash(); rte_src_init(); ea_class_init(); ea_register_init(&ea_gen_preference); ea_register_init(&ea_gen_igp_metric); ea_register_init(&ea_gen_from); ea_register_init(&ea_gen_source); ea_register_init(&ea_gen_nexthop); ea_register_init(&ea_mpls_labels); } /* * Documentation for functions declared inline in route.h */ #if 0 /** * rta_clone - clone route attributes * @r: a &rta to be cloned * * rta_clone() takes a cached &rta and returns its identical cached * copy. Currently it works by just returning the original &rta with * its use count incremented. */ static inline rta *rta_clone(rta *r) { DUMMY; } /** * rta_free - free route attributes * @r: a &rta to be freed * * If you stop using a &rta (for example when deleting a route which uses * it), you need to call rta_free() to notify the attribute cache the * attribute is no longer in use and can be freed if you were the last * user (which rta_free() tests by inspecting the use count). */ static inline void rta_free(rta *r) { DUMMY; } #endif