summaryrefslogtreecommitdiffstats
path: root/src/modules/netio/netio.c
blob: 17047fffd96bacec90b7c03fb1644855c0ef3c61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/size.h"
#include "common/strutil.h"
#include "detection/netio/netio.h"
#include "modules/netio/netio.h"

#define FF_NETIO_DISPLAY_NAME "Network IO"

static int sortInfs(const FFNetIOResult* left, const FFNetIOResult* right) {
    return ffStrbufComp(&left->name, &right->name);
}

static void formatKey(const FFNetIOOptions* options, FFNetIOResult* inf, uint32_t index, FFstrbuf* key) {
    if (options->moduleArgs.key.length == 0) {
        if (!inf->name.length) {
            ffStrbufSetF(&inf->name, "unknown %u", (unsigned) index);
        }

        ffStrbufSetF(key, FF_NETIO_DISPLAY_NAME " (%s)", inf->name.chars);
    } else {
        ffStrbufClear(key);
        FF_PARSE_FORMAT_STRING_CHECKED(key, &options->moduleArgs.key, ((FFformatarg[]) {
                                                                          FF_ARG(index, "index"),
                                                                          FF_ARG(inf->name, "name"),
                                                                          FF_ARG(options->moduleArgs.keyIcon, "icon"),
                                                                      }));
    }
}

bool ffPrintNetIO(FFNetIOOptions* options) {
    FF_LIST_AUTO_DESTROY result = ffListCreate();
    const char* error = ffDetectNetIO(&result, options);

    if (error) {
        ffPrintError(FF_NETIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
        return false;
    }

    ffListSort(&result, sizeof(FFNetIOResult), (const void*) sortInfs);

    uint32_t index = 0;
    FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY buffer2 = ffStrbufCreate();

    FF_LIST_FOR_EACH (FFNetIOResult, inf, result) {
        formatKey(options, inf, result.length == 1 ? 0 : index + 1, &key);
        ffStrbufClear(&buffer);

        if (options->moduleArgs.outputFormat.length == 0) {
            ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);

            ffSizeAppendNum(inf->rxBytes, &buffer);
            if (!options->detectTotal) {
                ffStrbufAppendS(&buffer, "/s");
            }
            ffStrbufAppendS(&buffer, " (IN) - ");

            ffSizeAppendNum(inf->txBytes, &buffer);
            if (!options->detectTotal) {
                ffStrbufAppendS(&buffer, "/s");
            }
            ffStrbufAppendS(&buffer, " (OUT)");

            if (inf->defaultRoute && !options->defaultRouteOnly) {
                ffStrbufAppendS(&buffer, " *");
            }
            ffStrbufPutTo(&buffer, stdout);
        } else {
            ffStrbufClear(&buffer2);
            ffSizeAppendNum(inf->rxBytes, &buffer);
            if (!options->detectTotal) {
                ffStrbufAppendS(&buffer, "/s");
            }
            ffSizeAppendNum(inf->txBytes, &buffer2);
            if (!options->detectTotal) {
                ffStrbufAppendS(&buffer2, "/s");
            }

            FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
                                                                                                         FF_ARG(buffer, "rx-size"),
                                                                                                         FF_ARG(buffer2, "tx-size"),
                                                                                                         FF_ARG(inf->name, "ifname"),
                                                                                                         FF_ARG(inf->defaultRoute, "is-default-route"),
                                                                                                         FF_ARG(inf->txBytes, "tx-bytes"),
                                                                                                         FF_ARG(inf->rxBytes, "rx-bytes"),
                                                                                                         FF_ARG(inf->txPackets, "tx-packets"),
                                                                                                         FF_ARG(inf->rxPackets, "rx-packets"),
                                                                                                         FF_ARG(inf->rxErrors, "rx-errors"),
                                                                                                         FF_ARG(inf->txErrors, "tx-errors"),
                                                                                                         FF_ARG(inf->rxDrops, "rx-drops"),
                                                                                                         FF_ARG(inf->txDrops, "tx-drops"),
                                                                                                     }));
        }
        ++index;
    }

    FF_LIST_FOR_EACH (FFNetIOResult, inf, result) {
        ffStrbufDestroy(&inf->name);
    }

    return true;
}

void ffParseNetIOJsonObject(FFNetIOOptions* options, yyjson_val* module) {
    yyjson_val *key, *val;
    size_t idx, max;
    yyjson_obj_foreach (module, idx, max, key, val) {
        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
            continue;
        }

        if (unsafe_yyjson_equals_str(key, "namePrefix")) {
            ffStrbufSetJsonVal(&options->namePrefix, val);
            continue;
        }

        if (unsafe_yyjson_equals_str(key, "defaultRouteOnly")) {
            options->defaultRouteOnly = yyjson_get_bool(val);
            continue;
        }

        if (unsafe_yyjson_equals_str(key, "detectTotal")) {
            options->detectTotal = yyjson_get_bool(val);
            continue;
        }

        if (unsafe_yyjson_equals_str(key, "waitTime")) {
            options->waitTime = (uint32_t) yyjson_get_uint(val);
            continue;
        }

        ffPrintError(FF_NETIO_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
    }
}

void ffGenerateNetIOJsonConfig(FFNetIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);

    yyjson_mut_obj_add_strbuf(doc, module, "namePrefix", &options->namePrefix);

    yyjson_mut_obj_add_bool(doc, module, "defaultRouteOnly", options->defaultRouteOnly);

    yyjson_mut_obj_add_bool(doc, module, "detectTotal", options->detectTotal);

    yyjson_mut_obj_add_uint(doc, module, "waitTime", options->waitTime);
}

bool ffGenerateNetIOJsonResult(FFNetIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    FF_LIST_AUTO_DESTROY result = ffListCreate();
    const char* error = ffDetectNetIO(&result, options);

    if (error) {
        yyjson_mut_obj_add_str(doc, module, "error", error);
        return false;
    }

    yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
    FF_LIST_FOR_EACH (FFNetIOResult, counter, result) {
        yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
        yyjson_mut_obj_add_strbuf(doc, obj, "name", &counter->name);
        yyjson_mut_obj_add_bool(doc, obj, "defaultRoute", counter->defaultRoute);
        yyjson_mut_obj_add_uint(doc, obj, "txBytes", counter->txBytes);
        yyjson_mut_obj_add_uint(doc, obj, "rxBytes", counter->rxBytes);
        yyjson_mut_obj_add_uint(doc, obj, "txPackets", counter->txPackets);
        yyjson_mut_obj_add_uint(doc, obj, "rxPackets", counter->rxPackets);
        yyjson_mut_obj_add_uint(doc, obj, "rxErrors", counter->rxErrors);
        yyjson_mut_obj_add_uint(doc, obj, "txErrors", counter->txErrors);
        yyjson_mut_obj_add_uint(doc, obj, "rxDrops", counter->rxDrops);
        yyjson_mut_obj_add_uint(doc, obj, "txDrops", counter->txDrops);
    }

    FF_LIST_FOR_EACH (FFNetIOResult, inf, result) {
        ffStrbufDestroy(&inf->name);
    }

    return true;
}

void ffInitNetIOOptions(FFNetIOOptions* options) {
    ffOptionInitModuleArg(&options->moduleArgs, "󰾆");

    ffStrbufInit(&options->namePrefix);
    options->defaultRouteOnly =
#if __ANDROID__
        false
#else
        true
#endif
        ;
    options->detectTotal = false;
    options->waitTime = 1000;
}

void ffDestroyNetIOOptions(FFNetIOOptions* options) {
    ffOptionDestroyModuleArg(&options->moduleArgs);
    ffStrbufDestroy(&options->namePrefix);
}

FFModuleBaseInfo ffNetIOModuleInfo = {
    .name = FF_NETIO_MODULE_NAME,
    .description = "Print network I/O throughput",
    .initOptions = (void*) ffInitNetIOOptions,
    .destroyOptions = (void*) ffDestroyNetIOOptions,
    .parseJsonObject = (void*) ffParseNetIOJsonObject,
    .printModule = (void*) ffPrintNetIO,
    .generateJsonResult = (void*) ffGenerateNetIOJsonResult,
    .generateJsonConfig = (void*) ffGenerateNetIOJsonConfig,
    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
        { "Size of data received [per second] (formatted)", "rx-size" },
        { "Size of data sent [per second] (formatted)", "tx-size" },
        { "Interface name", "ifname" },
        { "Is default route", "is-default-route" },
        { "Size of data received [per second] (in bytes)", "rx-bytes" },
        { "Size of data sent [per second] (in bytes)", "tx-bytes" },
        { "Number of packets received [per second]", "rx-packets" },
        { "Number of packets sent [per second]", "tx-packets" },
        { "Number of errors received [per second]", "rx-errors" },
        { "Number of errors sent [per second]", "tx-errors" },
        { "Number of packets dropped when receiving [per second]", "rx-drops" },
        { "Number of packets dropped when sending [per second]", "tx-drops" },
    }))
};