ECOMMUNITY_SIZE);
}
+/* Helper function to convert IEEE-754 Floating Point to uint32 */
+static uint32_t ieee_float_uint32_to_uint32(uint32_t u)
+{
+ union {
+ float r;
+ uint32_t d;
+ } f = {.d = u};
+
+ return (uint32_t)f.r;
+}
+
static int ecommunity_lb_str(char *buf, size_t bufsz, const uint8_t *pnt)
{
int len = 0;
as_t as;
- uint32_t bw;
+ uint32_t bw_tmp, bw;
char bps_buf[20] = {0};
#define ONE_GBPS_BYTES (1000 * 1000 * 1000 / 8)
as = (*pnt++ << 8);
as |= (*pnt++);
- (void)ptr_get_be32(pnt, &bw);
+ (void)ptr_get_be32(pnt, &bw_tmp);
+
+ bw = ieee_float_uint32_to_uint32(bw_tmp);
+
if (bw >= ONE_GBPS_BYTES)
snprintf(bps_buf, sizeof(bps_buf), "%.3f Gbps",
(float)(bw / ONE_GBPS_BYTES));
pnt = ptr_get_be32(pnt, &bwval);
(void)pnt; /* consume value */
if (bw)
- *bw = bwval;
+ *bw = ieee_float_uint32_to_uint32(bwval);
return eval;
}
}
eval->val[7] = val & 0xff;
}
+/* Helper function to convert uint32 to IEEE-754 Floating Point */
+static uint32_t uint32_to_ieee_float_uint32(uint32_t u)
+{
+ union {
+ float r;
+ uint32_t d;
+ } f = {.r = (float)u};
+
+ return f.d;
+}
+
/*
* Encode BGP Link Bandwidth extended community
* bandwidth (bw) is in bytes-per-sec
static inline void encode_lb_extcomm(as_t as, uint32_t bw, bool non_trans,
struct ecommunity_val *eval)
{
+ uint32_t bandwidth = uint32_to_ieee_float_uint32(bw);
+
memset(eval, 0, sizeof(*eval));
eval->val[0] = ECOMMUNITY_ENCODE_AS;
if (non_trans)
eval->val[1] = ECOMMUNITY_LINK_BANDWIDTH;
eval->val[2] = (as >> 8) & 0xff;
eval->val[3] = as & 0xff;
- eval->val[4] = (bw >> 24) & 0xff;
- eval->val[5] = (bw >> 16) & 0xff;
- eval->val[6] = (bw >> 8) & 0xff;
- eval->val[7] = bw & 0xff;
+ eval->val[4] = (bandwidth >> 24) & 0xff;
+ eval->val[5] = (bandwidth >> 16) & 0xff;
+ eval->val[6] = (bandwidth >> 8) & 0xff;
+ eval->val[7] = bandwidth & 0xff;
}
extern void ecommunity_init(void);