Skip to content

Commit

Permalink
*: change interface structure, from vrf_id to vrf
Browse files Browse the repository at this point in the history
Field vrf_id is replaced by the pointer of the struct vrf *.
For that all other code referencing to (interface)->vrf_id is replaced.
This work should not change the behaviour.
It is just a continuation work toward having an interface API handling
vrf pointer only.

some new generic functions are created in vrf:
vrf_to_id, vrf_to_name,

a zebra function is also created:
zvrf_info_lookup

an ospf function is also created:
ospf_lookup_by_vrf

it is to be noted that now that interface has a vrf pointer, some more
optimisations could be thought through all the rest of the code. as
example, many structure store the vrf_id. those structures could get
the exact vrf structure if inherited from an interface vrf context.

Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
  • Loading branch information
pguibert6WIND committed Jun 11, 2019
1 parent 23de0ec commit bb677b3
Show file tree
Hide file tree
Showing 36 changed files with 229 additions and 177 deletions.
4 changes: 2 additions & 2 deletions bfdd/bfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int bfd_session_enable(struct bfd_session *bs)

if (bs->key.ifname[0]) {
if (vrf)
ifp = if_lookup_by_name(bs->key.ifname, vrf->vrf_id);
ifp = if_lookup_by_name(bs->key.ifname, vrf);
else
ifp = if_lookup_by_name_all_vrf(bs->key.ifname);
if (ifp == NULL) {
Expand All @@ -148,7 +148,7 @@ int bfd_session_enable(struct bfd_session *bs)
return 0;
}
if (bs->key.ifname[0] && !vrf) {
vrf = vrf_lookup_by_id(ifp->vrf_id);
vrf = ifp->vrf;
if (vrf == NULL) {
log_error(
"session-enable: specified VRF doesn't exists.");
Expand Down
8 changes: 4 additions & 4 deletions bfdd/ptm_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,10 @@ static void bfdd_sessions_enable_interface(struct interface *ifp)
{
struct bfd_session_observer *bso;
struct bfd_session *bs;
struct vrf *vrf;
struct vrf *vrf = ifp->vrf;

if (!vrf)
return;

TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) {
bs = bso->bso_bs;
Expand All @@ -581,9 +584,6 @@ static void bfdd_sessions_enable_interface(struct interface *ifp)
/* Interface name mismatch. */
if (strcmp(ifp->name, bs->key.ifname))
continue;
vrf = vrf_lookup_by_id(ifp->vrf_id);
if (!vrf)
continue;
if (bs->key.vrfname[0] &&
strcmp(vrf->name, bs->key.vrfname))
continue;
Expand Down
2 changes: 1 addition & 1 deletion bgpd/bgp_zebra.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ bool bgp_zebra_nexthop_set(union sockunion *local, union sockunion *remote,
peer->bgp->vrf_id));
} else if (peer->update_if)
ifp = if_lookup_by_name(peer->update_if,
vrf_lookup_by_id(peer->bgp->vrf_id));
vrf_lookup_by_id(peer->bgp->vrf_id));
else
ifp = if_lookup_by_ipv6_exact(&local->sin6.sin6_addr,
local->sin6.sin6_scope_id,
Expand Down
3 changes: 1 addition & 2 deletions isisd/isis_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ DEFPY(no_router_isis, no_router_isis_cmd, "no router isis WORD$tag",
/* add callbacks to delete each of the circuits listed
*/
const char *vrf_name =
vrf_lookup_by_id(circuit->interface->vrf_id)
->name;
circuit->interface->vrf->name;
snprintf(
temp_xpath, XPATH_MAXLEN,
"/frr-interface:lib/interface[name='%s'][vrf='%s']/frr-isisd:isis",
Expand Down
25 changes: 13 additions & 12 deletions lib/if.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ struct interface *if_create(const char *name, struct vrf *vrf)

assert(name);
strlcpy(ifp->name, name, sizeof(ifp->name));
ifp->vrf_id = vrf->vrf_id;
ifp->vrf = vrf;
IFNAME_RB_INSERT(vrf, ifp);
ifp->connected = list_new();
ifp->connected->del = (void (*)(void *))connected_free;
Expand Down Expand Up @@ -170,14 +170,14 @@ void if_update_to_new_vrf(struct interface *ifp, struct vrf *vrf)
return;
}
/* remove interface from old master vrf list */
old_vrf = vrf_lookup_by_id(ifp->vrf_id);
old_vrf = ifp->vrf;
if (old_vrf) {
IFNAME_RB_REMOVE(old_vrf, ifp);
if (ifp->ifindex != IFINDEX_INTERNAL)
IFINDEX_RB_REMOVE(old_vrf, ifp);
}

ifp->vrf_id = vrf->vrf_id;
ifp->vrf = vrf;

IFNAME_RB_INSERT(vrf, ifp);
if (ifp->ifindex != IFINDEX_INTERNAL)
Expand Down Expand Up @@ -225,9 +225,8 @@ void if_delete_retain(struct interface *ifp)
/* Delete and free interface structure. */
void if_delete(struct interface *ifp)
{
struct vrf *vrf;
struct vrf *vrf = ifp->vrf;

vrf = vrf_lookup_by_id(ifp->vrf_id);
assert(vrf);

IFNAME_RB_REMOVE(vrf, ifp);
Expand Down Expand Up @@ -440,7 +439,7 @@ struct interface *if_get_by_name(const char *name, struct vrf *vrf)
case VRF_BACKEND_VRF_LITE:
ifp = if_lookup_by_name_all_vrf(name);
if (ifp) {
if (ifp->vrf_id == vrf->vrf_id)
if (ifp->vrf == vrf)
return ifp;
/* If it came from the kernel or by way of zclient,
* believe it and update the ifp accordingly.
Expand All @@ -458,7 +457,7 @@ void if_set_index(struct interface *ifp, ifindex_t ifindex)
{
struct vrf *vrf;

vrf = vrf_lookup_by_id(ifp->vrf_id);
vrf = ifp->vrf;
assert(vrf);

if (ifp->ifindex == ifindex)
Expand Down Expand Up @@ -602,7 +601,8 @@ static void if_dump(const struct interface *ifp)
zlog_info(
"Interface %s vrf %u index %d metric %d mtu %d "
"mtu6 %d %s",
ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric,
ifp->name, vrf_to_id(ifp->vrf),
ifp->ifindex, ifp->metric,
ifp->mtu, ifp->mtu6, if_flag_dump(ifp->flags));
}

Expand Down Expand Up @@ -784,7 +784,8 @@ connected_log(struct connected *connected, char *str)
p = connected->address;

snprintf(logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ", str,
ifp->name, ifp->vrf_id, prefix_family_str(p),
ifp->name, vrf_to_id(ifp->vrf),
prefix_family_str(p),
inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);

p = connected->destination;
Expand Down Expand Up @@ -1159,7 +1160,7 @@ DEFPY_NOSH (interface,
ifp = if_lookup_by_name(ifname, vrf);
else
ifp = if_lookup_by_name_all_vrf(ifname);
if (ifp && ifp->vrf_id != vrf->vrf_id) {
if (ifp && ifp->vrf != vrf) {
/*
* Special case 1: a VRF name was specified, but the found
* interface is associated to different VRF. Reject the command.
Expand All @@ -1175,7 +1176,7 @@ DEFPY_NOSH (interface,
* interface is associated to a VRF other than the default one.
* Update vrf_id and vrfname to account for that.
*/
vrf = vrf_lookup_by_id(ifp->vrf_id);
vrf = ifp->vrf;
assert(vrf);
vrfname = vrf->name;
}
Expand Down Expand Up @@ -1340,7 +1341,7 @@ static int lib_interface_create(enum nb_event event,
*/
if (vrf_get_backend() == VRF_BACKEND_VRF_LITE) {
ifp = if_lookup_by_name_all_vrf(ifname);
if (ifp && ifp->vrf_id != vrf->vrf_id) {
if (ifp && ifp->vrf != vrf) {
zlog_warn(
"%s: interface %s already exists in another VRF",
__func__, ifp->name);
Expand Down
30 changes: 17 additions & 13 deletions lib/if.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ struct interface {
#endif /* HAVE_NET_RT_IFLIST */

struct route_node *node;
vrf_id_t vrf_id;
struct vrf *vrf;

QOBJ_FIELDS
};
Expand All @@ -305,33 +305,37 @@ RB_HEAD(if_index_head, interface);
RB_PROTOTYPE(if_index_head, interface, index_entry, if_cmp_func)
DECLARE_QOBJ_TYPE(interface)

#define IFNAME_RB_INSERT(vrf, ifp) \
if (RB_INSERT(if_name_head, &vrf->ifaces_by_name, (ifp))) \
#define IFNAME_RB_INSERT(_vrf, _ifp) \
if (RB_INSERT(if_name_head, &(_vrf)->ifaces_by_name, (_ifp))) \
flog_err(EC_LIB_INTERFACE, \
"%s(%s): corruption detected -- interface with this " \
"name exists already in VRF %u!", \
__func__, (ifp)->name, (ifp)->vrf_id);
__func__, (_ifp)->name, (_ifp)->vrf ? \
(_ifp)->vrf->vrf_id : VRF_UNKNOWN);

#define IFNAME_RB_REMOVE(vrf, ifp) \
if (RB_REMOVE(if_name_head, &vrf->ifaces_by_name, (ifp)) == NULL) \
#define IFNAME_RB_REMOVE(_vrf, _ifp) \
if (RB_REMOVE(if_name_head, &(_vrf)->ifaces_by_name, (_ifp)) == NULL) \
flog_err(EC_LIB_INTERFACE, \
"%s(%s): corruption detected -- interface with this " \
"name doesn't exist in VRF %u!", \
__func__, (ifp)->name, (ifp)->vrf_id);
__func__, (_ifp)->name, (_ifp)->vrf ? \
(_ifp)->vrf->vrf_id : VRF_UNKNOWN);

#define IFINDEX_RB_INSERT(vrf, ifp) \
if (RB_INSERT(if_index_head, &vrf->ifaces_by_index, (ifp))) \
#define IFINDEX_RB_INSERT(_vrf, _ifp) \
if (RB_INSERT(if_index_head, &(_vrf)->ifaces_by_index, (_ifp))) \
flog_err(EC_LIB_INTERFACE, \
"%s(%u): corruption detected -- interface with this " \
"ifindex exists already in VRF %u!", \
__func__, (ifp)->ifindex, (ifp)->vrf_id);
__func__, (_ifp)->ifindex, (_ifp)->vrf ? \
(_ifp)->vrf->vrf_id : VRF_UNKNOWN);

#define IFINDEX_RB_REMOVE(vrf, ifp) \
if (RB_REMOVE(if_index_head, &vrf->ifaces_by_index, (ifp)) == NULL) \
#define IFINDEX_RB_REMOVE(_vrf, _ifp) \
if (RB_REMOVE(if_index_head, &(_vrf)->ifaces_by_index, (_ifp)) == NULL)\
flog_err(EC_LIB_INTERFACE, \
"%s(%u): corruption detected -- interface with this " \
"ifindex doesn't exist in VRF %u!", \
__func__, (ifp)->ifindex, (ifp)->vrf_id);
__func__, (_ifp)->ifindex, (_ifp)->vrf ? \
(_ifp)->vrf->vrf_id : VRF_UNKNOWN);

#define FOR_ALL_INTERFACES(vrf, ifp) \
if (vrf) \
Expand Down
15 changes: 13 additions & 2 deletions lib/vrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,14 +923,15 @@ int vrf_bind(vrf_id_t vrf_id, int fd, const char *name)
{
int ret = 0;
struct interface *ifp;
struct vrf *vrf = vrf_lookup_by_id(vrf_id);

if (fd < 0 || name == NULL)
if (fd < 0 || name == NULL || !vrf)
return fd;
/* the device should exist
* otherwise we should return
* case ifname = vrf in netns mode => return
*/
ifp = if_lookup_by_name(name, vrf_id);
ifp = if_lookup_by_name(name, vrf);
if (!ifp)
return fd;
#ifdef SO_BINDTODEVICE
Expand Down Expand Up @@ -1017,3 +1018,13 @@ vrf_id_t vrf_generate_id(void)

return ++vrf_id_local;
}

vrf_id_t vrf_to_id(struct vrf *vrf)
{
return vrf ? vrf->vrf_id : VRF_UNKNOWN;
}

const char *vrf_to_name(struct vrf *vrf)
{
return vrf ? vrf->name : "NIL";
}
2 changes: 2 additions & 0 deletions lib/vrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ extern struct vrf *vrf_lookup_by_name(const char *);
extern struct vrf *vrf_get(vrf_id_t, const char *);
extern const char *vrf_id_to_name(vrf_id_t vrf_id);
extern vrf_id_t vrf_name_to_id(const char *);
extern vrf_id_t vrf_to_id(struct vrf *vrf);
extern const char *vrf_to_name(struct vrf *vrf);

/* vrf context is searched and created
*/
Expand Down
12 changes: 9 additions & 3 deletions lib/zclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,11 @@ struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
/* Lookup/create interface by name. */
ifp = if_get_by_name(ifname_tmp, vrf);

/* update vrf_id of interface */
if (ifp->vrf->vrf_id == VRF_UNKNOWN &&
vrf->vrf_id != VRF_UNKNOWN)
ifp->vrf = vrf;

zebra_interface_if_set_value(s, ifp);

return ifp;
Expand Down Expand Up @@ -2844,11 +2849,12 @@ void zclient_interface_set_master(struct zclient *client,
s = client->obuf;
stream_reset(s);

zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id);
zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER,
vrf_to_id(master->vrf));

stream_putl(s, master->vrf_id);
stream_putl(s, vrf_to_id(master->vrf));
stream_putl(s, master->ifindex);
stream_putl(s, slave->vrf_id);
stream_putl(s, vrf_to_id(slave->vrf));
stream_putl(s, slave->ifindex);

stream_putw_at(s, 0, stream_get_endp(s));
Expand Down
4 changes: 2 additions & 2 deletions ospfd/ospf_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ void ospf_if_free(struct ospf_interface *oi)
if (IS_DEBUG_OSPF_EVENT)
zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
__PRETTY_FUNCTION__, oi->ifp->name,
ospf_vrf_id_to_name(oi->ifp->vrf_id),
oi->ifp->vrf_id);
vrf_to_name(oi->ifp->vrf),
vrf_to_id(oi->ifp->vrf));

ospf_delete_from_if(oi->ifp, oi);

Expand Down
14 changes: 7 additions & 7 deletions ospfd/ospf_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -7279,7 +7279,7 @@ static int ospf_vty_dead_interval_set(struct vty *vty, const char *interval_str,
if (nbr_str) {
struct ospf *ospf = NULL;

ospf = ospf_lookup_by_vrf_id(ifp->vrf_id);
ospf = ospf_lookup_by_vrf(ifp->vrf);
if (ospf) {
oi = ospf_if_lookup_by_local_addr(ospf, ifp, addr);
if (oi)
Expand Down Expand Up @@ -7396,7 +7396,7 @@ DEFUN (no_ip_ospf_dead_interval,
if (argc == 1) {
struct ospf *ospf = NULL;

ospf = ospf_lookup_by_vrf_id(ifp->vrf_id);
ospf = ospf_lookup_by_vrf(ifp->vrf);
if (ospf) {
oi = ospf_if_lookup_by_local_addr(ospf, ifp, addr);
if (oi)
Expand Down Expand Up @@ -7995,8 +7995,8 @@ DEFUN (ip_ospf_area,
argv_find(argv, argc, "area", &idx);
areaid = argv[idx + 1]->arg;

if (ifp->vrf_id && !instance)
ospf = ospf_lookup_by_vrf_id(ifp->vrf_id);
if (ifp->vrf && ifp->vrf->vrf_id && !instance)
ospf = ospf_lookup_by_vrf(ifp->vrf);
else
ospf = ospf_lookup_instance(instance);

Expand Down Expand Up @@ -8093,8 +8093,8 @@ DEFUN (no_ip_ospf_area,
if (argv_find(argv, argc, "(1-65535)", &idx))
instance = strtol(argv[idx]->arg, NULL, 10);

if (ifp->vrf_id && !instance)
ospf = ospf_lookup_by_vrf_id(ifp->vrf_id);
if (ifp->vrf && ifp->vrf->vrf_id && !instance)
ospf = ospf_lookup_by_vrf(ifp->vrf);
else
ospf = ospf_lookup_instance(instance);

Expand Down Expand Up @@ -9720,7 +9720,7 @@ static int config_write_interface_one(struct vty *vty, struct vrf *vrf)
continue;

vty_frame(vty, "!\n");
if (ifp->vrf_id == VRF_DEFAULT)
if (ifp->vrf->vrf_id == VRF_DEFAULT)
vty_frame(vty, "interface %s\n", ifp->name);
else
vty_frame(vty, "interface %s vrf %s\n", ifp->name,
Expand Down
8 changes: 4 additions & 4 deletions ospfd/ospf_zebra.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ static int ospf_interface_add(ZAPI_CALLBACK_ARGS)
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
zlog_debug(
"Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u",
ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
ifp->vrf_id, ifp->ifindex,
ifp->name, vrf_to_name(ifp->vrf),
vrf_to_id(ifp->vrf), ifp->ifindex,
(unsigned long long)ifp->flags, ifp->metric, ifp->mtu,
ifp->speed);

Expand Down Expand Up @@ -152,8 +152,8 @@ static int ospf_interface_delete(ZAPI_CALLBACK_ARGS)
if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
zlog_debug(
"Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
ifp->vrf_id, ifp->ifindex,
ifp->name, vrf_to_name(ifp->vrf),
vrf_to_id(ifp->vrf), ifp->ifindex,
(unsigned long long)ifp->flags, ifp->metric, ifp->mtu);

hook_call(ospf_if_delete, ifp);
Expand Down
9 changes: 7 additions & 2 deletions ospfd/ospfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ struct ospf *ospf_lookup_by_vrf_id(vrf_id_t vrf_id)
vrf = vrf_lookup_by_id(vrf_id);
if (!vrf)
return NULL;
return ospf_lookup_by_vrf(vrf);
}

struct ospf *ospf_lookup_by_vrf(struct vrf *vrf)
{
return (vrf->info) ? (struct ospf *)vrf->info : NULL;
}

Expand Down Expand Up @@ -1347,8 +1352,8 @@ void ospf_if_update(struct ospf *ospf, struct interface *ifp)

if (IS_DEBUG_OSPF_EVENT)
zlog_debug(
"%s: interface %s ifp->vrf_id %u ospf vrf %s vrf_id %u router_id %s",
__PRETTY_FUNCTION__, ifp->name, ifp->vrf_id,
"%s: interface %s ifp->vrf->vrf_id %u ospf vrf %s vrf_id %u router_id %s",
__PRETTY_FUNCTION__, ifp->name, vrf_to_id(ifp->vrf),
ospf_vrf_id_to_name(ospf->vrf_id), ospf->vrf_id,
inet_ntoa(ospf->router_id));

Expand Down
Loading

0 comments on commit bb677b3

Please sign in to comment.