diff --git a/src/confd/src/if-qos.c b/src/confd/src/if-qos.c index 8acbfad1d..04a153ff0 100644 --- a/src/confd/src/if-qos.c +++ b/src/confd/src/if-qos.c @@ -15,6 +15,7 @@ #define MAX_TC 8 #define FRAME_OVERHEAD 22 /* Ethernet header, VLAN tag and FCS beyond the MTU */ #define MAX_GATE_ENTRIES 1024 /* where the port names no limit of its own */ +#define MAX_MBPS 1000000 /* 1 Tbps, bounds a speed read from the system */ #define NSEC_PER_SEC 1000000000ULL /* @@ -575,6 +576,15 @@ static long qos_max_speed(const char *ifname) return max; } +/* Mbps to bits per second, a gigabit for a speed no port links at. */ +static uint64_t qos_bps(long mbps) +{ + if (mbps <= 0 || mbps > MAX_MBPS) + mbps = 1000; + + return (uint64_t)mbps * 1000000; +} + /* The port's highest rate in bits per second, a gigabit when it is virtual. */ static uint64_t qos_port_rate(const char *ifname) { @@ -583,7 +593,7 @@ static uint64_t qos_port_rate(const char *ifname) if (!speed) speed = qos_sysfs_long(ifname, "speed", 1000); - return (uint64_t)speed * 1000000; + return qos_bps(speed); } /* Link rate in bits per second, the port's highest while the link is down. */ @@ -591,7 +601,7 @@ static uint64_t qos_link_rate(const char *ifname) { long speed = qos_sysfs_long(ifname, "speed", 0); - return speed ? (uint64_t)speed * 1000000 : qos_port_rate(ifname); + return speed ? qos_bps(speed) : qos_port_rate(ifname); } /* @@ -1133,7 +1143,8 @@ static void gen_cbs(FILE *fp, const char *ifname, struct qos_egress *eg, int han uint64_t cycle = qos_gate_cycle(&eg->gates); uint64_t open = qos_gate_open(&eg->gates, tc); - bps = bps / open * cycle + bps % open * cycle / open; + if (open) + bps = bps / open * cycle + bps % open * cycle / open; } idle = (bps + 999) / 1000; band = by_queue ? tc + 1 : eg->num_tc - tc;