Skip to content

Commit

Permalink
bfdd: implement portable code
Browse files Browse the repository at this point in the history
Allow BFDd to compile and work on *BSDs.

Signed-off-by: Rafael Zalamena <[email protected]>
  • Loading branch information
rzalamena committed Jun 1, 2018
1 parent 3de0ddd commit e0d5d62
Show file tree
Hide file tree
Showing 9 changed files with 825 additions and 264 deletions.
55 changes: 0 additions & 55 deletions bfdd/bfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,61 +109,6 @@ bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc)
return bs;
}

int ptm_bfd_fetch_ifindex(const char *ifname)
{
struct ifreq ifr;

if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name))
> sizeof(ifr.ifr_name)) {
CRITLOG("Interface name %s truncated", ifr.ifr_name);
}

if (ioctl(bglobal.bg_shop, SIOCGIFINDEX, &ifr) == -1) {
CRITLOG("Getting ifindex for %s failed: %s", ifname,
strerror(errno));
return -1;
}

return ifr.ifr_ifindex;
}

static void ptm_bfd_fetch_local_mac(const char *ifname, uint8_t *mac)
{
struct ifreq ifr;

if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name))
> sizeof(ifr.ifr_name)) {
CRITLOG("Interface name %s truncated", ifr.ifr_name);
}

if (ioctl(bglobal.bg_shop, SIOCGIFHWADDR, &ifr) == -1) {
CRITLOG("Getting mac address for %s failed: %s", ifname,
strerror(errno));
return;
}

memcpy(mac, ifr.ifr_hwaddr.sa_data, ETHERNET_ADDRESS_LENGTH);
}

/* Was _fetch_portname_from_ifindex() */
void fetch_portname_from_ifindex(int ifindex, char *ifname, size_t ifnamelen)
{
struct ifreq ifr;

ifname[0] = 0;

memset(&ifr, 0, sizeof(ifr));
ifr.ifr_ifindex = ifindex;

if (ioctl(bglobal.bg_shop, SIOCGIFNAME, &ifr) == -1) {
CRITLOG("Getting ifname for ifindex %d failed: %s", ifindex,
strerror(errno));
return;
}

strlcpy(ifname, ifr.ifr_name, ifnamelen);
}

static uint32_t ptm_bfd_gen_ID(void)
{
static uint32_t sessionID = 1;
Expand Down
37 changes: 34 additions & 3 deletions bfdd/bfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,6 @@ int bp_udp_shop(void);
int bp_udp_mhop(void);
int bp_udp6_shop(void);
int bp_udp6_mhop(void);
int ptm_bfd_echo_sock_init(void);
int ptm_bfd_vxlan_sock_init(void);
int bp_peer_socket(struct bfd_peer_cfg *bpc);
int bp_peer_socketv6(struct bfd_peer_cfg *bpc);

Expand All @@ -520,6 +518,8 @@ void ptm_bfd_echo_snd(bfd_session *bfd);

int bfd_recv_cb(struct thread *t);

uint16_t checksum(uint16_t *buf, int len);


/*
* event.c
Expand Down Expand Up @@ -561,7 +561,6 @@ void ptm_bfd_echo_stop(bfd_session *bfd, int polling);
void ptm_bfd_echo_start(bfd_session *bfd);
void ptm_bfd_xmt_TO(bfd_session *bfd, int fbit);
void ptm_bfd_start_xmt_timer(bfd_session *bfd, bool is_echo);
int ptm_bfd_fetch_ifindex(const char *ifname);
bfd_session *ptm_bfd_sess_find(bfd_pkt_t *cp, char *port_name,
struct sockaddr_any *peer,
struct sockaddr_any *local, char *vrf_name,
Expand All @@ -587,4 +586,36 @@ int bfd_echo_xmt_cb(struct thread *t);
*/
void bfdd_vty_init(void);

/*
* OS compatibility functions.
*/
typedef struct udp_psuedo_header_s {
uint32_t saddr;
uint32_t daddr;
uint8_t reserved;
uint8_t protocol;
uint16_t len;
} udp_psuedo_header_t;

#define UDP_PSUEDO_HDR_LEN sizeof(udp_psuedo_header_t)

#if defined(BFD_LINUX) || defined(BFD_BSD)
int ptm_bfd_fetch_ifindex(const char *ifname);
void ptm_bfd_fetch_local_mac(const char *ifname, uint8_t *mac);
void fetch_portname_from_ifindex(int ifindex, char *ifname, size_t ifnamelen);
int ptm_bfd_echo_sock_init(void);
int ptm_bfd_vxlan_sock_init(void);
#endif /* BFD_LINUX || BFD_BSD */

#ifdef BFD_LINUX
uint16_t udp4_checksum(struct iphdr *iph, uint8_t *buf, int len);
#endif /* BFD_LINUX */

#ifdef BFD_BSD
uint16_t udp4_checksum(struct ip *ip, uint8_t *buf, int len);
ssize_t bsd_echo_sock_read(int sd, uint8_t *buf, ssize_t *buflen,
struct sockaddr_storage *ss, socklen_t *sslen,
uint8_t *ttl, uint32_t *id);
#endif /* BFD_BSD */

#endif /* _BFD_H_ */
Loading

0 comments on commit e0d5d62

Please sign in to comment.