bgpd: evpn use string handling functions from mac manipulation

EVPN code adaptation to replace old mac string internal utility with the
new one available in lib folder.

Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
This commit is contained in:
Philippe Guibert 2017-02-01 18:52:33 +01:00
parent b2bc8e2331
commit 0bee00f915
3 changed files with 7 additions and 98 deletions

View File

@ -42,7 +42,7 @@ void bgp_add_routermac_ecom(struct attr *attr, char *routermac)
memset(&routermac_ecom, 0, sizeof(struct ecommunity_val));
routermac_ecom.val[0] = ECOMMUNITY_ENCODE_EVPN;
routermac_ecom.val[1] = ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC;
memcpy(&routermac_ecom.val[2], routermac, MAC_LEN);
memcpy(&routermac_ecom.val[2], routermac, ETHER_ADDR_LEN);
if (!attr->extra->ecommunity)
attr->extra->ecommunity = ecommunity_new();
ecommunity_add_val(attr->extra->ecommunity, &routermac_ecom);
@ -72,79 +72,6 @@ static uint8_t convertchartohexa(uint8_t * hexa, int *error)
return 0;
}
/* converts to internal representation of mac address
* returns 1 on success, 0 otherwise
* format accepted: AA:BB:CC:DD:EE:FF
* if mac parameter is null, then check only
*/
int str2mac(const char *str, char *mac)
{
unsigned int k = 0, i, j;
uint8_t *ptr, *ptr2;
size_t len;
uint8_t car;
if (!str)
return 0;
if (str[0] == ':' && str[1] == '\0')
return 1;
i = 0;
ptr = (uint8_t *) str;
while (i < 6) {
uint8_t temp[5];
int error = 0;
ptr2 = (uint8_t *) strchr((const char *)ptr, ':');
if (ptr2 == NULL) {
/* if last occurence return ok */
if (i != 5) {
zlog_err("[%s]: format non recognized", mac);
return 0;
}
len = strlen((char *)ptr);
} else {
len = ptr2 - ptr;
}
if (len > 5) {
zlog_err("[%s]: format non recognized", mac);
return 0;
}
memcpy(temp, ptr, len);
for (j = 0; j < len; j++) {
if (k >= MAC_LEN)
return 0;
if (mac)
mac[k] = 0;
car = convertchartohexa(&temp[j], &error);
if (error)
return 0;
if (mac)
mac[k] = car << 4;
j++;
if (j == len)
return 0;
car = convertchartohexa(&temp[j], &error) & 0xf;
if (error)
return 0;
if (mac)
mac[k] |= car & 0xf;
k++;
i++;
}
ptr = ptr2;
if (ptr == NULL)
break;
ptr++;
}
if (mac && 0) {
zlog_err("leave correct : %02x:%02x:%02x:%02x:%02x:%02x",
mac[0] & 0xff, mac[1] & 0xff, mac[2] & 0xff,
mac[3] & 0xff, mac[4] & 0xff, mac[5] & 0xff);
}
return 1;
}
/* converts to an esi
* returns 1 on success, 0 otherwise
* format accepted: AA:BB:CC:DD:EE:FF:GG:HH:II:JJ
@ -238,30 +165,13 @@ char *esi2str(struct eth_segment_id *id)
return ptr;
}
char *mac2str(char *mac)
{
char *ptr;
if (!mac)
return NULL;
ptr = (char *)malloc((MAC_LEN * 2 + MAC_LEN - 1 + 1) * sizeof(char));
snprintf(ptr, (MAC_LEN * 2 + MAC_LEN - 1 + 1),
"%02x:%02x:%02x:%02x:%02x:%02x", (uint8_t) mac[0],
(uint8_t) mac[1], (uint8_t) mac[2], (uint8_t) mac[3],
(uint8_t) mac[4], (uint8_t) mac[5]);
return ptr;
}
char *ecom_mac2str(char *ecom_mac)
{
char *en;
en = ecom_mac;
en += 2;
return mac2str(en);
return mac2str(en, NULL, 0);
}
/* dst prefix must be AF_INET or AF_INET6 prefix, to forge EVPN prefix */

View File

@ -39,8 +39,6 @@ struct eth_segment_id {
u_char val[ESI_LEN];
};
#define MAC_LEN 6
union gw_addr {
struct in_addr ipv4;
struct in6_addr ipv6;
@ -54,7 +52,6 @@ struct bgp_route_evpn {
extern int str2esi(const char *str, struct eth_segment_id *id);
extern int str2mac(const char *str, char *mac);
extern char *esi2str(struct eth_segment_id *id);
extern char *mac2str(char *mac);
extern char *ecom_mac2str(char *ecom_mac);
extern void bgp_add_routermac_ecom(struct attr *attr, char *routermac);

View File

@ -4567,7 +4567,7 @@ bgp_static_set_safi (safi_t safi, struct vty *vty, const char *ip_str,
}
if( routermac)
{
bgp_static->router_mac = XCALLOC (MTYPE_ATTR, MAC_LEN+1);
bgp_static->router_mac = XCALLOC (MTYPE_ATTR, ETHER_ADDR_LEN+1);
str2mac (routermac, bgp_static->router_mac);
}
if (gwip)
@ -6633,7 +6633,7 @@ route_vty_out_overlay (struct vty *vty, struct prefix *p,
if(mac)
{
vty_out (vty, "/%s",(char *)mac);
free(mac);
XFREE(MTYPE_TMP, mac);
}
}
}
@ -10531,7 +10531,7 @@ bgp_config_write_network_evpn (struct vty *vty, struct bgp *bgp,
char *esi = NULL;
if(bgp_static->router_mac)
macrouter = mac2str(bgp_static->router_mac);
macrouter = mac2str(bgp_static->router_mac, NULL, 0);
if(bgp_static->eth_s_id)
esi = esi2str(bgp_static->eth_s_id);
p = &rn->p;
@ -10550,6 +10550,8 @@ bgp_config_write_network_evpn (struct vty *vty, struct bgp *bgp,
buf, rdbuf, p->u.prefix_evpn.eth_tag,
decode_label (bgp_static->tag), esi, buf2 , macrouter);
vty_out (vty, "%s", VTY_NEWLINE);
if (macrouter)
XFREE (MTYPE_TMP, macrouter);
}
return 0;
}