A simplification of the next-hop calculation.

Thanks to Joakim Tjernlund for the idea.
This commit is contained in:
Ondrej Zajicek 2010-12-28 01:43:07 +01:00
parent 919f5411c4
commit e7b4948cbd
4 changed files with 76 additions and 85 deletions

View file

@ -197,6 +197,13 @@ ospf_iface_down(struct ospf_iface *ifa)
ifa->cost = 0; ifa->cost = 0;
ifa->vip = IPA_NONE; ifa->vip = IPA_NONE;
} }
ifa->rt_pos_beg = 0;
ifa->rt_pos_end = 0;
#ifdef OSPFv3
ifa->px_pos_beg = 0;
ifa->px_pos_end = 0;
#endif
} }

View file

@ -195,8 +195,13 @@ struct ospf_iface
ip_addr bdrip; /* Backup DR */ ip_addr bdrip; /* Backup DR */
u32 drid; u32 drid;
u32 bdrid; u32 bdrid;
s16 rt_pos_beg; /* Position of iface in Router-LSA, begin, inclusive */
s16 rt_pos_end; /* Position of iface in Router-LSA, end, exclusive */
#ifdef OSPFv3 #ifdef OSPFv3
s16 px_pos_beg; /* Position of iface in Rt Prefix-LSA, begin, inclusive */
s16 px_pos_end; /* Position of iface in Rt Prefix-LSA, end, exclusive */
u32 dr_iface_id; /* if drid is valid, this is iface_id of DR (for connecting network) */ u32 dr_iface_id; /* if drid is valid, this is iface_id of DR (for connecting network) */
u8 instance_id; /* Used to differentiate between more OSPF u8 instance_id; /* Used to differentiate between more OSPF
instances on one interface */ instances on one interface */

View file

@ -10,7 +10,7 @@
static void add_cand(list * l, struct top_hash_entry *en, static void add_cand(list * l, struct top_hash_entry *en,
struct top_hash_entry *par, u32 dist, struct top_hash_entry *par, u32 dist,
struct ospf_area *oa, struct ospf_lsa_rt_link *rtl); struct ospf_area *oa, int i);
static void rt_sync(struct proto_ospf *po); static void rt_sync(struct proto_ospf *po);
/* In ospf_area->rtr we store paths to routers, but we use RID (and not IP address) /* In ospf_area->rtr we store paths to routers, but we use RID (and not IP address)
@ -208,48 +208,31 @@ ri_install_ext(struct proto_ospf *po, ip_addr prefix, int pxlen, orta *new)
memcpy(&old->n, new, sizeof(orta)); memcpy(&old->n, new, sizeof(orta));
} }
static inline struct ospf_iface *
#ifdef OSPFv2 rt_pos_to_ifa(struct ospf_area *oa, int pos)
static struct ospf_iface *
find_stub_src(struct ospf_area *oa, ip_addr px, int pxlen)
{ {
struct ospf_iface *iff; struct ospf_iface *ifa;
WALK_LIST(ifa, oa->po->iface_list)
WALK_LIST(iff, oa->po->iface_list) if (pos >= ifa->rt_pos_beg && pos < ifa->rt_pos_end)
if ((iff->type != OSPF_IT_VLINK) && return ifa;
(iff->oa == oa) &&
ipa_equal(iff->addr->prefix, px) &&
(iff->addr->pxlen == pxlen))
return iff;
return NULL; return NULL;
} }
#else /* OSPFv3 */ #ifdef OSPFv3
static inline struct ospf_iface *
static struct ospf_iface * px_pos_to_ifa(struct ospf_area *oa, int pos)
find_stub_src(struct ospf_area *oa, ip_addr px, int pxlen)
{ {
struct ospf_iface *iff; struct ospf_iface *ifa;
struct ifa *a; WALK_LIST(ifa, oa->po->iface_list)
if (pos >= ifa->px_pos_beg && pos < ifa->px_pos_end)
WALK_LIST(iff, oa->po->iface_list) return ifa;
if ((iff->type != OSPF_IT_VLINK) &&
(iff->oa == oa))
WALK_LIST(a, iff->iface->addrs)
if (ipa_equal(a->prefix, px) &&
(a->pxlen == pxlen) &&
!(a->flags & IA_SECONDARY))
return iff;
return NULL; return NULL;
} }
#endif #endif
static void static void
add_network(struct ospf_area *oa, ip_addr px, int pxlen, int metric, struct top_hash_entry *en) add_network(struct ospf_area *oa, ip_addr px, int pxlen, int metric, struct top_hash_entry *en, int pos)
{ {
orta nf = { orta nf = {
.type = RTS_OSPF, .type = RTS_OSPF,
@ -272,7 +255,13 @@ add_network(struct ospf_area *oa, ip_addr px, int pxlen, int metric, struct top_
* be removed in rt_sync(). * be removed in rt_sync().
*/ */
struct ospf_iface *ifa = find_stub_src(oa, px, pxlen); struct ospf_iface *ifa;
#ifdef OSPFv2
ifa = rt_pos_to_ifa(oa, pos);
#else /* OSPFv3 */
ifa = px_pos_to_ifa(oa, pos);
#endif
nf.nhs = ifa ? new_nexthop(oa->po, IPA_NONE, ifa->iface, ifa->ecmp_weight) : NULL; nf.nhs = ifa ? new_nexthop(oa->po, IPA_NONE, ifa->iface, ifa->ecmp_weight) : NULL;
} }
@ -335,7 +324,7 @@ process_prefixes(struct ospf_area *oa)
if ((pxopts & OPT_PX_LA) && ipa_zero(src->lb)) if ((pxopts & OPT_PX_LA) && ipa_zero(src->lb))
src->lb = pxa; src->lb = pxa;
add_network(oa, pxa, pxlen, src->dist + metric, src); add_network(oa, pxa, pxlen, src->dist + metric, src, i);
} }
} }
} }
@ -347,7 +336,7 @@ ospf_rt_spfa_rtlinks(struct ospf_area *oa, struct top_hash_entry *act, struct to
{ {
// struct proto *p = &oa->po->proto; // struct proto *p = &oa->po->proto;
struct proto_ospf *po = oa->po; struct proto_ospf *po = oa->po;
u32 i; int i;
struct ospf_lsa_rt *rt = en->lsa_body; struct ospf_lsa_rt *rt = en->lsa_body;
struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1); struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1);
@ -370,7 +359,7 @@ ospf_rt_spfa_rtlinks(struct ospf_area *oa, struct top_hash_entry *act, struct to
*/ */
add_network(oa, ipa_from_u32(rtl->id), add_network(oa, ipa_from_u32(rtl->id),
ipa_mklen(ipa_from_u32(rtl->data)), ipa_mklen(ipa_from_u32(rtl->data)),
act->dist + rtl->metric, act); act->dist + rtl->metric, act, i);
break; break;
#endif #endif
@ -396,7 +385,7 @@ ospf_rt_spfa_rtlinks(struct ospf_area *oa, struct top_hash_entry *act, struct to
if (tmp) if (tmp)
DBG("Going to add cand, Mydist: %u, Req: %u\n", DBG("Going to add cand, Mydist: %u, Req: %u\n",
tmp->dist, act->dist + rtl->metric); tmp->dist, act->dist + rtl->metric);
add_cand(&oa->cand, tmp, act, act->dist + rtl->metric, oa, rtl); add_cand(&oa->cand, tmp, act, act->dist + rtl->metric, oa, i);
} }
} }
@ -482,7 +471,7 @@ ospf_rt_spfa(struct ospf_area *oa)
#ifdef OSPFv2 #ifdef OSPFv2
add_network(oa, ipa_and(ipa_from_u32(act->lsa.id), ln->netmask), add_network(oa, ipa_and(ipa_from_u32(act->lsa.id), ln->netmask),
ipa_mklen(ln->netmask), act->dist, act); ipa_mklen(ln->netmask), act->dist, act, -1);
#endif #endif
rts = (u32 *) (ln + 1); rts = (u32 *) (ln + 1);
@ -494,7 +483,7 @@ ospf_rt_spfa(struct ospf_area *oa)
DBG("Found :-)\n"); DBG("Found :-)\n");
else else
DBG("Not found!\n"); DBG("Not found!\n");
add_cand(&oa->cand, tmp, act, act->dist, oa, NULL); add_cand(&oa->cand, tmp, act, act->dist, oa, -1);
} }
break; break;
} }
@ -1326,31 +1315,6 @@ ospf_rt_spf(struct proto_ospf *po)
} }
static inline int
match_dr(struct ospf_iface *ifa, struct top_hash_entry *en)
{
#ifdef OSPFv2
return (ifa->drid == en->lsa.rt) && (ipa_to_u32(ifa->drip) == en->lsa.id);
#else /* OSPFv3 */
return (ifa->drid == en->lsa.rt) && (ifa->dr_iface_id == en->lsa.id);
#endif
}
static inline int
match_rtlink(struct ospf_iface *ifa, struct ospf_lsa_rt_link *rtl)
{
#ifdef OSPFv2
return ((ifa->type == OSPF_IT_PTP) || (ifa->type == OSPF_IT_PTMP)) &&
(ifa->cost == rtl->metric) &&
(((ifa->addr->flags & IA_UNNUMBERED) ? ifa->iface->index :
ipa_to_u32(ifa->addr->ip)) == rtl->data);
#else /* OSPFv3 */
return ((ifa->type == OSPF_IT_PTP) || (ifa->type == OSPF_IT_PTMP)) &&
(ifa->cost == rtl->metric) && (ifa->iface->index == rtl->lif);
#endif
}
static inline int static inline int
inherit_nexthops(struct mpnh *pn) inherit_nexthops(struct mpnh *pn)
{ {
@ -1360,7 +1324,7 @@ inherit_nexthops(struct mpnh *pn)
static struct mpnh * static struct mpnh *
calc_next_hop(struct ospf_area *oa, struct top_hash_entry *en, calc_next_hop(struct ospf_area *oa, struct top_hash_entry *en,
struct top_hash_entry *par, struct ospf_lsa_rt_link *rtl) struct top_hash_entry *par, int pos)
{ {
// struct proto *p = &oa->po->proto; // struct proto *p = &oa->po->proto;
struct proto_ospf *po = oa->po; struct proto_ospf *po = oa->po;
@ -1386,28 +1350,28 @@ calc_next_hop(struct ospf_area *oa, struct top_hash_entry *en,
/* The first case - local network */ /* The first case - local network */
if ((en->lsa.type == LSA_T_NET) && (par == oa->rt)) if ((en->lsa.type == LSA_T_NET) && (par == oa->rt))
{ {
WALK_LIST(ifa, po->iface_list) ifa = rt_pos_to_ifa(oa, pos);
if (match_dr(ifa, en)) if (!ifa)
return new_nexthop(po, IPA_NONE, ifa->iface, ifa->ecmp_weight); return NULL;
return NULL; return new_nexthop(po, IPA_NONE, ifa->iface, ifa->ecmp_weight);
} }
/* The second case - ptp or ptmp neighbor */ /* The second case - ptp or ptmp neighbor */
if ((en->lsa.type == LSA_T_RT) && (par == oa->rt)) if ((en->lsa.type == LSA_T_RT) && (par == oa->rt))
{ {
if (rtl->type == LSART_VLNK) ifa = rt_pos_to_ifa(oa, pos);
if (!ifa)
return NULL;
if (ifa->type == OSPF_IT_VLINK)
return new_nexthop(po, IPA_NONE, NULL, 0); return new_nexthop(po, IPA_NONE, NULL, 0);
WALK_LIST(ifa, po->iface_list) struct ospf_neighbor *m = find_neigh(ifa, rid);
if (match_rtlink(ifa, rtl)) if (!m || (m->state != NEIGHBOR_FULL))
{ return NULL;
struct ospf_neighbor *m = find_neigh(ifa, rid);
if (m && (m->state == NEIGHBOR_FULL)) return new_nexthop(po, m->ip, ifa->iface, ifa->ecmp_weight);
return new_nexthop(po, m->ip, ifa->iface, ifa->ecmp_weight);
}
return NULL;
} }
/* The third case - bcast or nbma neighbor */ /* The third case - bcast or nbma neighbor */
@ -1532,7 +1496,7 @@ merge_nexthops(struct proto_ospf *po, struct top_hash_entry *en,
/* Add LSA into list of candidates in Dijkstra's algorithm */ /* Add LSA into list of candidates in Dijkstra's algorithm */
static void static void
add_cand(list * l, struct top_hash_entry *en, struct top_hash_entry *par, add_cand(list * l, struct top_hash_entry *en, struct top_hash_entry *par,
u32 dist, struct ospf_area *oa, struct ospf_lsa_rt_link *rtl) u32 dist, struct ospf_area *oa, int pos)
{ {
struct proto_ospf *po = oa->po; struct proto_ospf *po = oa->po;
node *prev, *n; node *prev, *n;
@ -1566,7 +1530,7 @@ add_cand(list * l, struct top_hash_entry *en, struct top_hash_entry *par,
if (!link_back(oa, en, par)) if (!link_back(oa, en, par))
return; return;
struct mpnh *nhs = calc_next_hop(oa, en, par, rtl); struct mpnh *nhs = calc_next_hop(oa, en, par, pos);
if (!nhs) if (!nhs)
{ {
log(L_WARN "Cannot find next hop for LSA (Type: %04x, Id: %R, Rt: %R)", log(L_WARN "Cannot find next hop for LSA (Type: %04x, Id: %R, Rt: %R)",

View file

@ -241,6 +241,8 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN)) if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN))
continue; continue;
ifa->rt_pos_beg = i;
/* RFC2328 - 12.4.1.1-4 */ /* RFC2328 - 12.4.1.1-4 */
switch (ifa->type) switch (ifa->type)
{ {
@ -294,6 +296,8 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
break; break;
} }
ifa->rt_pos_end = i;
/* Now we will originate stub area if there is no primary */ /* Now we will originate stub area if there is no primary */
if (net_lsa || if (net_lsa ||
(ifa->type == OSPF_IT_VLINK) || (ifa->type == OSPF_IT_VLINK) ||
@ -321,6 +325,8 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
ln->padding = 0; ln->padding = 0;
} }
i++; i++;
ifa->rt_pos_end = i;
} }
struct ospf_stubnet_config *sn; struct ospf_stubnet_config *sn;
@ -367,6 +373,7 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
struct proto_ospf *po = oa->po; struct proto_ospf *po = oa->po;
struct ospf_iface *ifa; struct ospf_iface *ifa;
int bitv = 0; int bitv = 0;
int i = 0;
struct ospf_lsa_rt *rt; struct ospf_lsa_rt *rt;
struct ospf_neighbor *neigh; struct ospf_neighbor *neigh;
@ -396,6 +403,8 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN)) if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN))
continue; continue;
ifa->rt_pos_beg = i;
/* RFC5340 - 4.4.3.2 */ /* RFC5340 - 4.4.3.2 */
switch (ifa->type) switch (ifa->type)
{ {
@ -403,25 +412,27 @@ originate_rt_lsa_body(struct ospf_area *oa, u16 *length)
case OSPF_IT_PTMP: case OSPF_IT_PTMP:
WALK_LIST(neigh, ifa->neigh_list) WALK_LIST(neigh, ifa->neigh_list)
if (neigh->state == NEIGHBOR_FULL) if (neigh->state == NEIGHBOR_FULL)
add_lsa_rt_link(po, ifa, LSART_PTP, neigh->iface_id, neigh->rid); add_lsa_rt_link(po, ifa, LSART_PTP, neigh->iface_id, neigh->rid), i++;
break; break;
case OSPF_IT_BCAST: case OSPF_IT_BCAST:
case OSPF_IT_NBMA: case OSPF_IT_NBMA:
if (bcast_net_active(ifa)) if (bcast_net_active(ifa))
add_lsa_rt_link(po, ifa, LSART_NET, ifa->dr_iface_id, ifa->drid); add_lsa_rt_link(po, ifa, LSART_NET, ifa->dr_iface_id, ifa->drid), i++;
break; break;
case OSPF_IT_VLINK: case OSPF_IT_VLINK:
neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list); neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
if ((!EMPTY_LIST(ifa->neigh_list)) && (neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff)) if ((!EMPTY_LIST(ifa->neigh_list)) && (neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
add_lsa_rt_link(po, ifa, LSART_VLNK, neigh->iface_id, neigh->rid); add_lsa_rt_link(po, ifa, LSART_VLNK, neigh->iface_id, neigh->rid), i++;
break; break;
default: default:
log("Unknown interface type %s", ifa->iface->name); log("Unknown interface type %s", ifa->iface->name);
break; break;
} }
ifa->rt_pos_end = i;
} }
if (bitv) if (bitv)
@ -1184,6 +1195,8 @@ originate_prefix_rt_lsa_body(struct ospf_area *oa, u16 *length)
if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN)) if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN))
continue; continue;
ifa->px_pos_beg = i;
if ((ifa->type == OSPF_IT_BCAST) || if ((ifa->type == OSPF_IT_BCAST) ||
(ifa->type == OSPF_IT_NBMA)) (ifa->type == OSPF_IT_NBMA))
net_lsa = bcast_net_active(ifa); net_lsa = bcast_net_active(ifa);
@ -1215,6 +1228,8 @@ originate_prefix_rt_lsa_body(struct ospf_area *oa, u16 *length)
(a->pxlen == MAX_PREFIX_LENGTH)) (a->pxlen == MAX_PREFIX_LENGTH))
host_addr = 1; host_addr = 1;
} }
ifa->px_pos_end = i;
} }
/* If there are some configured vlinks, add some global address, /* If there are some configured vlinks, add some global address,