Skip to content

Commit

Permalink
Add tool: netstat.
Browse files Browse the repository at this point in the history
Including libraries: libutil,libmemstat,libxo.
  • Loading branch information
logwang committed Aug 31, 2017
1 parent eec9e77 commit 1eaf0ac
Show file tree
Hide file tree
Showing 542 changed files with 116,526 additions and 13 deletions.
11 changes: 11 additions & 0 deletions freebsd/net/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
VNET_DEFINE(struct rtstat, rtstat);
#define V_rtstat VNET(rtstat)

#ifdef FSTACK
static int
sysctl_rtstat(SYSCTL_HANDLER_ARGS)
{
return (SYSCTL_OUT(req, &V_rtstat, sizeof(struct rtstat)));
}

SYSCTL_PROC(_net, OID_AUTO, rtstat, CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_RD,
0, 0, sysctl_rtstat, "S,rtstat", "Routing statistics.");
#endif

VNET_DEFINE(struct rib_head *, rt_tables);
#define V_rt_tables VNET(rt_tables)

Expand Down
1 change: 1 addition & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ NETINET_SRCS+= \
ip_icmp.c \
ip_id.c \
ip_input.c \
ip_mroute.c \
ip_options.c \
ip_output.c \
raw_ip.c \
Expand Down
38 changes: 38 additions & 0 deletions lib/ff_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
volatile int ticks;
int cpu_disable_deep_sleep;

static int sysctl_kern_smp_active(SYSCTL_HANDLER_ARGS);

/* This is used in modules that need to work in both SMP and UP. */
cpuset_t all_cpus;

Expand All @@ -114,6 +116,31 @@ int mp_maxcpus = MAXCPU;
volatile int smp_started;
u_int mp_maxid;

static SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD|CTLFLAG_CAPRD, NULL,
"Kernel SMP");

SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD|CTLFLAG_CAPRD, &mp_maxid, 0,
"Max CPU ID.");

SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD|CTLFLAG_CAPRD, &mp_maxcpus,
0, "Max number of CPUs that the system was compiled for.");

SYSCTL_PROC(_kern_smp, OID_AUTO, active, CTLFLAG_RD | CTLTYPE_INT, NULL, 0,
sysctl_kern_smp_active, "I", "Indicates system is running in SMP mode");

int smp_disabled = 0; /* has smp been disabled? */
SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN|CTLFLAG_CAPRD,
&smp_disabled, 0, "SMP has been disabled from the loader");

int smp_cpus = 1; /* how many cpu's running */
SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD|CTLFLAG_CAPRD, &smp_cpus, 0,
"Number of CPUs online");

int smp_topology = 0; /* Which topology we're using. */
SYSCTL_INT(_kern_smp, OID_AUTO, topology, CTLFLAG_RDTUN, &smp_topology, 0,
"Topology override setting; 0 is default provided by hardware.");


long first_page = 0;

struct vmmeter vm_cnt;
Expand All @@ -139,6 +166,17 @@ int cpu_disable_c3_sleep = 0; /* Timer dies in C3. */

static void timevalfix(struct timeval *);

/* Extra care is taken with this sysctl because the data type is volatile */
static int
sysctl_kern_smp_active(SYSCTL_HANDLER_ARGS)
{
int error, active;

active = smp_started;
error = SYSCTL_OUT(req, &active, sizeof(active));
return (error);
}

void
procinit()
{
Expand Down
2 changes: 1 addition & 1 deletion lib/ff_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct ff_msg {
/* Result of msg processing */
int result;
/* Length of segment buffer. */
uint16_t buf_len;
size_t buf_len;
/* Address of segment buffer. */
char *buf_addr;

Expand Down
2 changes: 1 addition & 1 deletion tools/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SUBDIRS=compat sysctl ifconfig route top
SUBDIRS=compat libutil libmemstat libxo sysctl ifconfig route top netstat

all:
for d in $(SUBDIRS); do ( cd $$d; $(MAKE) all ) ; done
Expand Down
28 changes: 28 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,34 @@ Examples:
| 99.79%| 0.00%| 0.21%| 8147676|
```

# netstat
Usage:
```
netstat -P <f-stack proc_id> [-46AaLnRSTWx] [-f protocol_family | -p protocol]
netstat -P <f-stack proc_id> -i | -I interface [-46abdhnW] [-f address_family]
netstat -P <f-stack proc_id> -w wait [-I interface] [-46d] [-q howmany]
netstat -P <f-stack proc_id> -s [-46sz] [-f protocol_family | -p protocol]
netstat -P <f-stack proc_id> -i | -I interface -s [-46s]
[-f protocol_family | -p protocol]
netstat -P <f-stack proc_id> -B [-z] [-I interface]
netstat -P <f-stack proc_id> -r [-46AnW] [-F fibnum] [-f address_family]
netstat -P <f-stack proc_id> -rs [-s]
netstat -P <f-stack proc_id> -g [-46W] [-f address_family]
netstat -P <f-stack proc_id> -gs [-46s] [-f address_family]
netstat -P <f-stack proc_id> -Q
```

Unsupported commands or features:
```
-M
-N
-m
ipv6
netgraph
ipsec
```

For more details, see [Manual page](https://www.freebsd.org/cgi/man.cgi?netstat).

# how to implement a custom tool for communicating with F-Stack process

Expand Down
10 changes: 10 additions & 0 deletions tools/compat/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#define __dead __dead2
#endif

#ifndef __unused
#define __unused __attribute__((__unused__))
#endif

#ifndef nitems
#define nitems(x) (sizeof((x)) / sizeof((x)[0]))
#endif
Expand All @@ -45,6 +49,10 @@
#define __FBSDID(s) /* nothing */
#endif

#ifndef _PATH_ETC
#define _PATH_ETC "/etc"
#endif

void *reallocf(void *ptr, size_t size);

int feature_present(const char *feature);
Expand All @@ -57,4 +65,6 @@ size_t strlcpy(char * __restrict dst, const char * __restrict src,
long long strtonum(const char *numstr, long long minval,
long long maxval, const char **errstrp);

const char *getprogname(void);

#endif
195 changes: 195 additions & 0 deletions tools/compat/getifmaddrs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* Copyright (c) 2003 Bruce M. Simpson.
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <sys/cdefs.h>

#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/route.h>

#include <errno.h>
#include <ifaddrs.h>
#include <stdlib.h>
#include <string.h>

#define SALIGN (sizeof(long) - 1)
#define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : \
(SALIGN + 1))
#define MAX_SYSCTL_TRY 5
#define RTA_MASKS (RTA_GATEWAY | RTA_IFP | RTA_IFA)

int
getifmaddrs(struct ifmaddrs **pif)
{
int icnt = 1;
int dcnt = 0;
int ntry = 0;
size_t len;
size_t needed;
int mib[6];
int i;
char *buf;
char *data;
char *next;
char *p;
struct ifma_msghdr *ifmam;
struct ifmaddrs *ifa, *ift;
struct rt_msghdr *rtm;
struct sockaddr *sa;

mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0; /* protocol */
mib[3] = 0; /* wildcard address family */
mib[4] = NET_RT_IFMALIST;
mib[5] = 0; /* no flags */
do {
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
return (-1);
if ((buf = malloc(needed)) == NULL)
return (-1);
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
free(buf);
return (-1);
}
free(buf);
buf = NULL;
}
} while (buf == NULL);

for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)(void *)next;
if (rtm->rtm_version != RTM_VERSION)
continue;
switch (rtm->rtm_type) {
case RTM_NEWMADDR:
ifmam = (struct ifma_msghdr *)(void *)rtm;
if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
break;
icnt++;
p = (char *)(ifmam + 1);
for (i = 0; i < RTAX_MAX; i++) {
if ((RTA_MASKS & ifmam->ifmam_addrs &
(1 << i)) == 0)
continue;
sa = (struct sockaddr *)(void *)p;
len = SA_RLEN(sa);
dcnt += len;
p += len;
}
break;
}
}

data = malloc(sizeof(struct ifmaddrs) * icnt + dcnt);
if (data == NULL) {
free(buf);
return (-1);
}

ifa = (struct ifmaddrs *)(void *)data;
data += sizeof(struct ifmaddrs) * icnt;

memset(ifa, 0, sizeof(struct ifmaddrs) * icnt);
ift = ifa;

for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)(void *)next;
if (rtm->rtm_version != RTM_VERSION)
continue;

switch (rtm->rtm_type) {
case RTM_NEWMADDR:
ifmam = (struct ifma_msghdr *)(void *)rtm;
if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
break;

p = (char *)(ifmam + 1);
for (i = 0; i < RTAX_MAX; i++) {
if ((RTA_MASKS & ifmam->ifmam_addrs &
(1 << i)) == 0)
continue;
sa = (struct sockaddr *)(void *)p;
len = SA_RLEN(sa);
switch (i) {
case RTAX_GATEWAY:
ift->ifma_lladdr =
(struct sockaddr *)(void *)data;
memcpy(data, p, len);
data += len;
break;

case RTAX_IFP:
ift->ifma_name =
(struct sockaddr *)(void *)data;
memcpy(data, p, len);
data += len;
break;

case RTAX_IFA:
ift->ifma_addr =
(struct sockaddr *)(void *)data;
memcpy(data, p, len);
data += len;
break;

default:
data += len;
break;
}
p += len;
}
ift->ifma_next = ift + 1;
ift = ift->ifma_next;
break;
}
}

free(buf);

if (ift > ifa) {
ift--;
ift->ifma_next = NULL;
*pif = ifa;
} else {
*pif = NULL;
free(ifa);
}
return (0);
}

void
freeifmaddrs(struct ifmaddrs *ifmp)
{

free(ifmp);
}

7 changes: 5 additions & 2 deletions tools/compat/getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ __FBSDID("$FreeBSD$");
#include "un-namespace.h"

#include "libc_private.h"
#else
#include "compat.h"
#endif

int opterr = 1, /* if error message should be printed */
Expand Down Expand Up @@ -103,7 +105,7 @@ getopt(int nargc, char * const nargv[], const char *ostr)
#ifndef FSTACK
"%s: illegal option -- %c\n", _getprogname(),
#else
"illegal option -- %c\n",
"%s: illegal option -- %c\n", getprogname(),
#endif
optopt);
return (BADCH);
Expand Down Expand Up @@ -139,7 +141,8 @@ getopt(int nargc, char * const nargv[], const char *ostr)
"%s: option requires an argument -- %c\n",
_getprogname(), optopt);
#else
"option requires an argument -- %c\n", optopt);
"%s: option requires an argument -- %c\n",
getprogname(), optopt);
#endif
return (BADCH);
}
Expand Down
10 changes: 10 additions & 0 deletions tools/compat/getprogname.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#define _GNU_SOURCE
#include <errno.h>
#include "compat.h"

const char *
getprogname(void)
{
return program_invocation_name;
}

Loading

0 comments on commit 1eaf0ac

Please sign in to comment.