diff options
| author | Carmine Scarpitta <cscarpit@cisco.com> | 2024-02-13 11:40:49 +0100 | 
|---|---|---|
| committer | Carmine Scarpitta <cscarpit@cisco.com> | 2024-02-18 18:31:13 +0100 | 
| commit | b1f1cb9c23fd49f81bfda8876610c3d7dda6d88c (patch) | |
| tree | abb3d6be53bce3ba37e6439d5ca2a0752190bdc0 /fpm | |
| parent | 4ec8ef5098c0d8f9fc1c098269afeb4a740adb24 (diff) | |
fpm: Add functions to encode nexthop in protobuf
Add two helper functions to encode/decode nexthops in protobuf.
Specifically,
* `fpm_nexthop_create`: encode a `struct nexthop` in a protobuf nexthop
structure
* `fpm_nexthop_get`: decode a nexthop protobuf structure into a `struct
nexthop`
This is a preliminary commit to support sending SRv6 Local SIDs and VPN
SIDs via protobuf.
Signed-off-by: Carmine Scarpitta <cscarpit@cisco.com>
Diffstat (limited to 'fpm')
| -rw-r--r-- | fpm/fpm_pb.h | 92 | 
1 files changed, 92 insertions, 0 deletions
diff --git a/fpm/fpm_pb.h b/fpm/fpm_pb.h index 7e39054d19..427948d1ff 100644 --- a/fpm/fpm_pb.h +++ b/fpm/fpm_pb.h @@ -15,6 +15,7 @@  #define _FPM_PB_H  #include "lib/route_types.h" +#include "lib/vrf.h"  #include "qpb/qpb.h"  #include "fpm/fpm.pb-c.h" @@ -42,4 +43,95 @@ static inline Fpm__RouteKey *fpm__route_key__create(qpb_allocator_t *allocator,  	return key;  } +/* + * fpm__nexthop__create + */ +#define fpm_nexthop_create fpm__nexthop__create +static inline Fpm__Nexthop * +fpm__nexthop__create(qpb_allocator_t *allocator, struct nexthop *nh) +{ +	Fpm__Nexthop *nexthop; +	uint8_t family; + +	nexthop = QPB_ALLOC(allocator, typeof(*nexthop)); +	if (!nexthop) +		return NULL; + +	fpm__nexthop__init(nexthop); + +	if (nh->type == NEXTHOP_TYPE_IPV4 || +	    nh->type == NEXTHOP_TYPE_IPV4_IFINDEX) +		family = AF_INET; +	else if (nh->type == NEXTHOP_TYPE_IPV6 || +		 nh->type == NEXTHOP_TYPE_IPV6_IFINDEX) +		family = AF_INET6; +	else +		return NULL; + +	nexthop->if_id = qpb__if_identifier__create(allocator, nh->ifindex); +	if (!nexthop->if_id) +		return NULL; + +	nexthop->address = qpb__l3_address__create(allocator, &nh->gate, family); +	if (!nexthop->address) +		return NULL; + + +	return nexthop; +} + +/* + * fpm__nexthop__get + * + * Read out information from a protobuf nexthop structure. + */ +#define fpm_nexthop_get fpm__nexthop__get +static inline int fpm__nexthop__get(const Fpm__Nexthop *nh, +				    struct nexthop *nexthop) +{ +	struct in_addr ipv4; +	struct in6_addr ipv6; +	uint32_t ifindex; +	char *ifname; + +	if (!nh) +		return 0; + +	if (!qpb_if_identifier_get(nh->if_id, &ifindex, &ifname)) +		return 0; + +	if (nh->address) { +		if (nh->address->v4) { +			memset(&ipv4, 0, sizeof(ipv4)); +			if (!qpb__ipv4_address__get(nh->address->v4, &ipv4)) +				return 0; + +			nexthop->vrf_id = VRF_DEFAULT; +			nexthop->type = NEXTHOP_TYPE_IPV4; +			nexthop->gate.ipv4 = ipv4; +			if (ifindex) { +				nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX; +				nexthop->ifindex = ifindex; +			} +			return 1; +		} + +		if (nh->address->v6) { +			memset(&ipv6, 0, sizeof(ipv6)); +			if (!qpb__ipv6_address__get(nh->address->v6, &ipv6)) +				return 0; +			nexthop->vrf_id = VRF_DEFAULT; +			nexthop->type = NEXTHOP_TYPE_IPV6; +			nexthop->gate.ipv6 = ipv6; +			if (ifindex) { +				nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX; +				nexthop->ifindex = ifindex; +			} +			return 1; +		} +	} + +	return 0; +} +  #endif  | 
