MCPcopy Create free account
hub / github.com/F-Stack/f-stack / bpf_stats

Function bpf_stats

tools/netstat/bpf.c:109–171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

107}
108
109void
110bpf_stats(char *ifname)
111{
112 struct xbpf_d *d, *bd, zerostat;
113 char *pname, flagbuf[12];
114 size_t size;
115
116 if (zflag) {
117 bzero(&zerostat, sizeof(zerostat));
118 if (sysctlbyname("net.bpf.stats", NULL, NULL,
119 &zerostat, sizeof(zerostat)) < 0)
120 xo_warn("failed to zero bpf counters");
121 return;
122 }
123 if (sysctlbyname("net.bpf.stats", NULL, &size,
124 NULL, 0) < 0) {
125 xo_warn("net.bpf.stats");
126 return;
127 }
128 if (size == 0)
129 return;
130 bd = malloc(size);
131 if (bd == NULL) {
132 xo_warn("malloc failed");
133 return;
134 }
135 if (sysctlbyname("net.bpf.stats", bd, &size,
136 NULL, 0) < 0) {
137 xo_warn("net.bpf.stats");
138 free(bd);
139 return;
140 }
141 xo_emit("{T:/%5s} {T:/%6s} {T:/%7s} {T:/%9s} {T:/%9s} {T:/%9s} "
142 "{T:/%5s} {T:/%5s} {T:/%s}\n",
143 "Pid", "Netif", "Flags", "Recv", "Drop", "Match",
144 "Sblen", "Hblen", "Command");
145 xo_open_container("bpf-statistics");
146 xo_open_list("bpf-entry");
147 for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
148 if (d->bd_structsize != sizeof(*d)) {
149 xo_warnx("bpf_stats_extended: version mismatch");
150 return;
151 }
152 if (ifname && strcmp(ifname, d->bd_ifname) != 0)
153 continue;
154 xo_open_instance("bpf-entry");
155 pname = bpf_pidname(d->bd_pid);
156 xo_emit("{k:pid/%5d} {k:interface-name/%6s} ",
157 d->bd_pid, d->bd_ifname);
158 bpf_flags(d, flagbuf);
159 xo_emit("{d:flags/%7s} {:received-packets/%9ju} "
160 "{:dropped-packets/%9ju} {:filter-packets/%9ju} "
161 "{:store-buffer-length/%5d} {:hold-buffer-length/%5d} "
162 "{:process/%s}\n",
163 flagbuf, (uintmax_t)d->bd_rcount, (uintmax_t)d->bd_dcount,
164 (uintmax_t)d->bd_fcount, d->bd_slen, d->bd_hlen, pname);
165 free(pname);
166 xo_close_instance("bpf-entry");

Callers 1

main.cFile · 0.85

Calls 15

bzeroFunction · 0.85
sysctlbynameFunction · 0.85
xo_warnFunction · 0.85
mallocFunction · 0.85
xo_emitFunction · 0.85
xo_open_containerFunction · 0.85
xo_open_listFunction · 0.85
xo_warnxFunction · 0.85
strcmpFunction · 0.85
xo_open_instanceFunction · 0.85
bpf_pidnameFunction · 0.85
bpf_flagsFunction · 0.85

Tested by

no test coverage detected