Skip to content

Commit

Permalink
rstats: backport fixes from 386_36400
Browse files Browse the repository at this point in the history
  • Loading branch information
RMerl committed Mar 9, 2020
1 parent a815746 commit 67a2522
Showing 1 changed file with 62 additions and 22 deletions.
84 changes: 62 additions & 22 deletions release/src/router/rstats/rstats.c
Expand Up @@ -55,10 +55,15 @@
#define SDAY (60 * 60 * 24)

#define INTERVAL 30
#if defined(RTCONFIG_WANPORT2)
#define MAX_BW 2000
#if defined(RTCONFIG_SWITCH_QCA8075_QCA8337_PHY_AQR107_AR8035_QCA8033)
#define MAX_BW (2 * 10 * 1024 + 4 * 1024) /* need NIC driver provide 64-bits TX/RX bytes */
#elif defined(RTCONFIG_SWITCH_RTL8370M_PHY_QCA8033_X2) || \
defined(RTCONFIG_SWITCH_RTL8370MB_PHY_QCA8033_X2)
#define MAX_BW (4 * 1024) /* need NIC driver provide 64-bits TX/RX bytes */
#elif defined(RTCONFIG_WANPORT2)
#define MAX_BW 2048 /* need NIC driver provide 64-bits TX/RX bytes */
#else
#define MAX_BW 1000
#define MAX_BW 1024
#endif

#ifdef RTCONFIG_ISP_METER
Expand Down Expand Up @@ -130,6 +135,8 @@ enum if_id {
IFID_WIRELESS3_4, /* WIRELESS3.4 */
IFID_WIRELESS3_5, /* WIRELESS3.6 */
IFID_WIRELESS3_6, /* WIRELESS3.5 */
IFID_LACP1, /* LACP1, first slave interface of bonding interface */
IFID_LACP2, /* LACP2, second slave interface of bonding interface */

IFID_MAX
};
Expand Down Expand Up @@ -619,7 +626,7 @@ static void save_speedjs(long next)
FILE *f;
uint64_t total;
uint64_t tmax;
unsigned long n;
unsigned long long n;
char c;

if ((f = fopen("/var/tmp/rstats-speed.js", "w")) == NULL) return;
Expand All @@ -638,7 +645,7 @@ static void save_speedjs(long next)
for (k = 0; k < MAX_NSPEED; ++k) {
p = (p + 1) % MAX_NSPEED;
n = sp->speed[p][j];
fprintf(f, "%s%lu", k ? "," : "", n);
fprintf(f, "%s%llu", k ? "," : "", n);
total += n;
if (n > tmax) tmax = n;
}
Expand Down Expand Up @@ -771,6 +778,10 @@ static enum if_id desc_to_id(char *desc)
else if (*d == '.' && *s >= '0' && *s <= '6' && *(s + 1) == '\0')
id = IFID_WIRELESS3 + *s - '0' + 1;
}
else if (!strcmp(desc, "LACP1"))
id = IFID_LACP1;
else if (!strcmp(desc, "LACP2"))
id = IFID_LACP2;

//if (id < 0 || id == IFID_MAX)
//_dprintf("%s: Unknown desc [%s]\n", __func__, desc);
Expand Down Expand Up @@ -837,12 +848,13 @@ static void calc(void)
time_t now;
time_t mon;
struct tm *tms;
uint32_t c;
uint32_t sc;
unsigned long long c;
unsigned long long sc;
unsigned long long diff;
long tick;
int n;
char *exclude;
char buf_lan_ifname[2 * IFNAMSIZ], buf_lan_ifnames[20 * IFNAMSIZ], buf_exclude[2 * IFNAMSIZ];
char *exclude = NULL;
enum if_id id;
struct tmp_speed_s {
char desc[20];
Expand All @@ -854,8 +866,8 @@ static void calc(void)
#ifdef RTCONFIG_ISP_METER
char traffic[64];
#endif
char *nv_lan_ifname;
char *nv_lan_ifnames;
char *nv_lan_ifname = NULL;
char *nv_lan_ifnames = NULL;

#ifdef RTCONFIG_QTN
qcsapi_unsigned_int l_counter_value;
Expand All @@ -870,14 +882,32 @@ static void calc(void)
rx2 = 0;
tx2 = 0;
now = time(0);
exclude = nvram_safe_get("rstats_exclude");
nv_lan_ifname = nvram_safe_get("lan_ifname");
nv_lan_ifnames = nvram_safe_get("lan_ifnames");
if (strlen(nvram_safe_get("rstats_exclude")) >= sizeof(buf_exclude))
exclude = strdup(nvram_safe_get("rstats_exclude"));
if (!exclude) {
strlcpy(buf_exclude, nvram_safe_get("rstats_exclude"), sizeof(buf_exclude));
exclude = buf_exclude;
}

if (strlen(nvram_safe_get("lan_ifname")) >= sizeof(buf_lan_ifname))
nv_lan_ifname = strdup(nvram_safe_get("lan_ifname"));
if (!nv_lan_ifname) {
strlcpy(buf_lan_ifname, nvram_safe_get("lan_ifname"), sizeof(buf_lan_ifname));
nv_lan_ifname = buf_lan_ifname;
}

if (strlen(nvram_safe_get("lan_ifnames")) >= sizeof(buf_lan_ifnames))
nv_lan_ifnames = strdup(nvram_safe_get("lan_ifnames"));
if (!nv_lan_ifnames) {
strlcpy(buf_lan_ifnames, nvram_safe_get("lan_ifnames"), sizeof(buf_lan_ifnames));
nv_lan_ifnames = buf_lan_ifnames;
}

#ifdef RTCONFIG_LANTIQ
if ((nvram_get_int("switch_stb_x") == 0 || nvram_get_int("switch_stb_x") > 6) && ppa_support(WAN_UNIT_FIRST)) {
if(nvram_get_int("wave_ready") == 0 ||
nvram_get_int("wave_action") != 0 ) return;
nvram_get_int("wave_action") != 0 )
goto exit_calc;
memset(tmp_speed, 0, sizeof(tmp_speed));
doSystem("ppacmd getwan > %s", RS_PPACMD_WAN_PATH);
doSystem("ppacmd getlan > %s", RS_PPACMD_LAN_PATH);
Expand All @@ -888,7 +918,9 @@ static void calc(void)
#endif
f = fopen("/proc/net/dev", "r");

if (!f) return;
if (!f)
goto exit_calc;

#ifdef RTCONFIG_LANTIQ
if ((nvram_get_int("switch_stb_x") > 0 && nvram_get_int("switch_stb_x") <= 6) || !ppa_support(WAN_UNIT_FIRST))
#endif
Expand Down Expand Up @@ -935,27 +967,27 @@ static void calc(void)
vlan_tx += counter[1];
}
if(strncmp(ifname, "eth0", 4)==0){
if(counter[0]>vlan_rx) {
if(counter[0]>=vlan_rx) {
counter[0] -= vlan_rx;
} else {
counter[0] = counter[0] + 0xffffffff - vlan_rx;
counter[0] = counter[0] + (~0ULL - vlan_rx + 1);
}
if(counter[1]>vlan_tx) {
if(counter[1]>=vlan_tx) {
counter[1] -= vlan_tx;
} else {
counter[1] = counter[1] + 0xffffffff - vlan_tx;
counter[1] = counter[1] + (~0ULL - vlan_tx + 1);
}
}
}
#endif
/* retrieve vlan-if counters again for bcm5301x case */
#if defined(RTCONFIG_BCM5301X_TRAFFIC_MONITOR)
if(strncmp(ifname, "vlan", 4)==0){
traffic_wanlan(ifname, &counter[0], &counter[1]);
traffic_wanlan(ifname, (uint32_t*) &counter[0], (uint32_t*) &counter[1]);
}
#endif

if (!netdev_calc(ifname, ifname_desc, (unsigned long*) &counter[0], (unsigned long*) &counter[1], ifname_desc2, (unsigned long*) &rx2, (unsigned long*) &tx2, nv_lan_ifname, nv_lan_ifnames))
if (!netdev_calc(ifname, ifname_desc, &counter[0], &counter[1], ifname_desc2, &rx2, &tx2, nv_lan_ifname, nv_lan_ifnames))
continue;
#ifdef RTCONFIG_QTN
if (!strcmp(ifname, nvram_safe_get("wl_ifname")))
Expand Down Expand Up @@ -1078,7 +1110,7 @@ static void calc(void)
c = tmp->counter[i];
sc = sp->last[i];
if (c < sc) {
diff = (0xFFFFFFFF - sc + 1) + c;
diff = ((~0ULL) - sc + 1) + c;
if (diff > MAX_ROLLOVER) diff = 0;
}
else {
Expand Down Expand Up @@ -1159,6 +1191,14 @@ _dprintf("CUR MONTH Tx= %lu = %lu + %llu - %lu\n",month_tx,last_month_tx,(histor
save_utime = current_uptime + get_stime();
//_dprintf("%s: uptime = %dm, save_utime = %dm\n", __FUNCTION__, current_uptime / 60, save_utime / 60);
}

exit_calc:
if (exclude != buf_exclude)
free(exclude);
if (nv_lan_ifname != buf_lan_ifname)
free(nv_lan_ifname);
if (nv_lan_ifnames != buf_lan_ifnames)
free(nv_lan_ifnames);
}

static void sig_handler(int sig)
Expand Down

0 comments on commit 67a2522

Please sign in to comment.