#include <sys/cdefs.h>
#include "opt_ddb.h"
#include "opt_device_polling.h"
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/interrupt.h>
#include <sys/lock.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/rmlock.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#ifdef DDB
#include <ddb/ddb.h>
#endif
#define _WANT_NETISR_INTERNAL
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_private.h>
#include <net/netisr.h>
#include <net/netisr_internal.h>
#include <net/vnet.h>
static struct rmlock netisr_rmlock;
#define NETISR_LOCK_INIT() rm_init_flags(&netisr_rmlock, "netisr", \
RM_NOWITNESS)
#define NETISR_LOCK_ASSERT()
#define NETISR_RLOCK(tracker) rm_rlock(&netisr_rmlock, (tracker))
#define NETISR_RUNLOCK(tracker) rm_runlock(&netisr_rmlock, (tracker))
#define NETISR_WLOCK() rm_wlock(&netisr_rmlock)
#define NETISR_WUNLOCK() rm_wunlock(&netisr_rmlock)
static SYSCTL_NODE(_net, OID_AUTO, isr, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"netisr");
#define NETISR_DISPATCH_POLICY_DEFAULT NETISR_DISPATCH_DIRECT
#define NETISR_DISPATCH_POLICY_MAXSTR 20
static u_int netisr_dispatch_policy = NETISR_DISPATCH_POLICY_DEFAULT;
static int sysctl_netisr_dispatch_policy(SYSCTL_HANDLER_ARGS);
SYSCTL_PROC(_net_isr, OID_AUTO, dispatch,
CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
0, 0, sysctl_netisr_dispatch_policy, "A",
"netisr dispatch policy");
static int netisr_maxthreads = 1;
SYSCTL_INT(_net_isr, OID_AUTO, maxthreads, CTLFLAG_RDTUN,
&netisr_maxthreads, 0,
"Use at most this many CPUs for netisr processing");
static int netisr_bindthreads = 0;
SYSCTL_INT(_net_isr, OID_AUTO, bindthreads, CTLFLAG_RDTUN,
&netisr_bindthreads, 0, "Bind netisr threads to CPUs.");
#define NETISR_DEFAULT_MAXQLIMIT 10240
static u_int netisr_maxqlimit = NETISR_DEFAULT_MAXQLIMIT;
SYSCTL_UINT(_net_isr, OID_AUTO, maxqlimit, CTLFLAG_RDTUN,
&netisr_maxqlimit, 0,
"Maximum netisr per-protocol, per-CPU queue depth.");
#define NETISR_DEFAULT_DEFAULTQLIMIT 256
static u_int netisr_defaultqlimit = NETISR_DEFAULT_DEFAULTQLIMIT;
SYSCTL_UINT(_net_isr, OID_AUTO, defaultqlimit, CTLFLAG_RDTUN,
&netisr_defaultqlimit, 0,
"Default netisr per-protocol, per-CPU queue limit if not set by protocol");
static u_int netisr_maxprot = NETISR_MAXPROT;
SYSCTL_UINT(_net_isr, OID_AUTO, maxprot, CTLFLAG_RD,
&netisr_maxprot, 0,
"Compile-time limit on the number of protocols supported by netisr.");
static struct netisr_proto netisr_proto[NETISR_MAXPROT];
#ifdef VIMAGE
VNET_DEFINE_STATIC(u_int, netisr_enable[NETISR_MAXPROT]);
#define V_netisr_enable VNET(netisr_enable)
#endif
DPCPU_DEFINE(struct netisr_workstream, nws);
static u_int nws_array[MAXCPU];
static u_int nws_count;
SYSCTL_UINT(_net_isr, OID_AUTO, numthreads, CTLFLAG_RD,
&nws_count, 0, "Number of extant netisr threads.");
#define NWS_LOCK(s) mtx_lock(&(s)->nws_mtx)
#define NWS_LOCK_ASSERT(s) mtx_assert(&(s)->nws_mtx, MA_OWNED)
#define NWS_UNLOCK(s) mtx_unlock(&(s)->nws_mtx)
#define NWS_SIGNAL(s) swi_sched((s)->nws_swi_cookie, 0)
u_int
netisr_get_cpucount(void)
{
return (nws_count);
}
u_int
netisr_get_cpuid(u_int cpunumber)
{
return (nws_array[cpunumber % nws_count]);
}
u_int
netisr_default_flow2cpu(u_int flowid)
{
return (nws_array[flowid % nws_count]);
}
struct netisr_dispatch_table_entry {
u_int ndte_policy;
const char *ndte_policy_str;
};
static const struct netisr_dispatch_table_entry netisr_dispatch_table[] = {
{ NETISR_DISPATCH_DEFAULT, "default" },
{ NETISR_DISPATCH_DEFERRED, "deferred" },
{ NETISR_DISPATCH_HYBRID, "hybrid" },
{ NETISR_DISPATCH_DIRECT, "direct" },
};
static void
netisr_dispatch_policy_to_str(u_int dispatch_policy, char *buffer,
u_int buflen)
{
const struct netisr_dispatch_table_entry *ndtep;
const char *str;
u_int i;
str = "unknown";
for (i = 0; i < nitems(netisr_dispatch_table); i++) {
ndtep = &netisr_dispatch_table[i];
if (ndtep->ndte_policy == dispatch_policy) {
str = ndtep->ndte_policy_str;
break;
}
}
snprintf(buffer, buflen, "%s", str);
}
static int
netisr_dispatch_policy_from_str(const char *str, u_int *dispatch_policyp)
{
const struct netisr_dispatch_table_entry *ndtep;
u_int i;
for (i = 0; i < nitems(netisr_dispatch_table); i++) {
ndtep = &netisr_dispatch_table[i];
if (strcmp(ndtep->ndte_policy_str, str) == 0) {
*dispatch_policyp = ndtep->ndte_policy;
return (0);
}
}
return (EINVAL);
}
static int
sysctl_netisr_dispatch_policy(SYSCTL_HANDLER_ARGS)
{
char tmp[NETISR_DISPATCH_POLICY_MAXSTR];
size_t len;
u_int dispatch_policy;
int error;
netisr_dispatch_policy_to_str(netisr_dispatch_policy, tmp,
sizeof(tmp));
if (req->newptr != NULL) {
len = req->newlen - req->newidx;
if (len >= NETISR_DISPATCH_POLICY_MAXSTR)
return (EINVAL);
error = SYSCTL_IN(req, tmp, len);
if (error == 0) {
tmp[len] = '\0';
error = netisr_dispatch_policy_from_str(tmp,
&dispatch_policy);
if (error == 0 &&
dispatch_policy == NETISR_DISPATCH_DEFAULT)
error = EINVAL;
if (error == 0)
netisr_dispatch_policy = dispatch_policy;
}
} else {
error = sysctl_handle_string(oidp, tmp, sizeof(tmp), req);
}
return (error);
}
void
netisr_register(const struct netisr_handler *nhp)
{
VNET_ITERATOR_DECL(vnet_iter);
struct netisr_work *npwp;
const char *name;
u_int i, proto;
proto = nhp->nh_proto;
name = nhp->nh_name;
CURVNET_ASSERT_SET();
MPASS(IS_DEFAULT_VNET(curvnet));
KASSERT(nhp->nh_name != NULL,
("%s: nh_name NULL for %u", __func__, proto));
KASSERT(nhp->nh_handler != NULL,
("%s: nh_handler NULL for %s", __func__, name));
KASSERT(nhp->nh_policy == NETISR_POLICY_SOURCE ||
nhp->nh_policy == NETISR_POLICY_FLOW ||
nhp->nh_policy == NETISR_POLICY_CPU,
("%s: unsupported nh_policy %u for %s", __func__,
nhp->nh_policy, name));
KASSERT(nhp->nh_policy == NETISR_POLICY_FLOW ||
nhp->nh_m2flow == NULL,
("%s: nh_policy != FLOW but m2flow defined for %s", __func__,
name));
KASSERT(nhp->nh_policy == NETISR_POLICY_CPU || nhp->nh_m2cpuid == NULL,
("%s: nh_policy != CPU but m2cpuid defined for %s", __func__,
name));
KASSERT(nhp->nh_policy != NETISR_POLICY_CPU || nhp->nh_m2cpuid != NULL,
("%s: nh_policy == CPU but m2cpuid not defined for %s", __func__,
name));
KASSERT(nhp->nh_dispatch == NETISR_DISPATCH_DEFAULT ||
nhp->nh_dispatch == NETISR_DISPATCH_DEFERRED ||
nhp->nh_dispatch == NETISR_DISPATCH_HYBRID ||
nhp->nh_dispatch == NETISR_DISPATCH_DIRECT,
("%s: invalid nh_dispatch (%u)", __func__, nhp->nh_dispatch));
KASSERT(proto < NETISR_MAXPROT,
("%s(%u, %s): protocol too big", __func__, proto, name));
NETISR_WLOCK();
KASSERT(netisr_proto[proto].np_name == NULL,
("%s(%u, %s): name present", __func__, proto, name));
KASSERT(netisr_proto[proto].np_handler == NULL,
("%s(%u, %s): handler present", __func__, proto, name));
netisr_proto[proto].np_name = name;
netisr_proto[proto].np_handler = nhp->nh_handler;
netisr_proto[proto].np_m2flow = nhp->nh_m2flow;
netisr_proto[proto].np_m2cpuid = nhp->nh_m2cpuid;
netisr_proto[proto].np_drainedcpu = nhp->nh_drainedcpu;
if (nhp->nh_qlimit == 0)
netisr_proto[proto].np_qlimit = netisr_defaultqlimit;
else if (nhp->nh_qlimit > netisr_maxqlimit) {
printf("%s: %s requested queue limit %u capped to "
"net.isr.maxqlimit %u\n", __func__, name, nhp->nh_qlimit,
netisr_maxqlimit);
netisr_proto[proto].np_qlimit = netisr_maxqlimit;
} else
netisr_proto[proto].np_qlimit = nhp->nh_qlimit;
netisr_proto[proto].np_policy = nhp->nh_policy;
netisr_proto[proto].np_dispatch = nhp->nh_dispatch;
CPU_FOREACH(i) {
npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
bzero(npwp, sizeof(*npwp));
npwp->nw_qlimit = netisr_proto[proto].np_qlimit;
}
#ifdef VIMAGE
V_netisr_enable[proto] = 1;
VNET_LIST_RLOCK_NOSLEEP();
VNET_FOREACH(vnet_iter) {
if (vnet_iter == curvnet)
continue;
CURVNET_SET(vnet_iter);
V_netisr_enable[proto] = 1;
CURVNET_RESTORE();
}
VNET_LIST_RUNLOCK_NOSLEEP();
#endif
NETISR_WUNLOCK();
}
void
netisr_clearqdrops(const struct netisr_handler *nhp)
{
struct netisr_work *npwp;
#ifdef INVARIANTS
const char *name;
#endif
u_int i, proto;
proto = nhp->nh_proto;
#ifdef INVARIANTS
name = nhp->nh_name;
#endif
KASSERT(proto < NETISR_MAXPROT,
("%s(%u): protocol too big for %s", __func__, proto, name));
NETISR_WLOCK();
KASSERT(netisr_proto[proto].np_handler != NULL,
("%s(%u): protocol not registered for %s", __func__, proto,
name));
CPU_FOREACH(i) {
npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
npwp->nw_qdrops = 0;
}
NETISR_WUNLOCK();
}
void
netisr_getqdrops(const struct netisr_handler *nhp, u_int64_t *qdropp)
{
struct netisr_work *npwp;
struct rm_priotracker tracker;
#ifdef INVARIANTS
const char *name;
#endif
u_int i, proto;
*qdropp = 0;
proto = nhp->nh_proto;
#ifdef INVARIANTS
name = nhp->nh_name;
#endif
KASSERT(proto < NETISR_MAXPROT,
("%s(%u): protocol too big for %s", __func__, proto, name));
NETISR_RLOCK(&tracker);
KASSERT(netisr_proto[proto].np_handler != NULL,
("%s(%u): protocol not registered for %s", __func__, proto,
name));
CPU_FOREACH(i) {
npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
*qdropp += npwp->nw_qdrops;
}
NETISR_RUNLOCK(&tracker);
}
void
netisr_getqlimit(const struct netisr_handler *nhp, u_int *qlimitp)
{
struct rm_priotracker tracker;
#ifdef INVARIANTS
const char *name;
#endif
u_int proto;
proto = nhp->nh_proto;
#ifdef INVARIANTS
name = nhp->nh_name;
#endif
KASSERT(proto < NETISR_MAXPROT,
("%s(%u): protocol too big for %s", __func__, proto, name));
NETISR_RLOCK(&tracker);
KASSERT(netisr_proto[proto].np_handler != NULL,
("%s(%u): protocol not registered for %s", __func__, proto,
name));
*qlimitp = netisr_proto[proto].np_qlimit;
NETISR_RUNLOCK(&tracker);
}
int
netisr_setqlimit(const struct netisr_handler *nhp, u_int qlimit)
{
struct netisr_work *npwp;
#ifdef INVARIANTS
const char *name;
#endif
u_int i, proto;
if (qlimit > netisr_maxqlimit)
return (EINVAL);
proto = nhp->nh_proto;
#ifdef INVARIANTS
name = nhp->nh_name;
#endif
KASSERT(proto < NETISR_MAXPROT,
("%s(%u): protocol too big for %s", __func__, proto, name));
NETISR_WLOCK();
KASSERT(netisr_proto[proto].np_handler != NULL,
("%s(%u): protocol not registered for %s", __func__, proto,
name));
netisr_proto[proto].np_qlimit = qlimit;
CPU_FOREACH(i) {
npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
npwp->nw_qlimit = qlimit;
}
NETISR_WUNLOCK();
return (0);
}
static void
netisr_drain_proto(struct netisr_work *npwp)
{
struct mbuf *m;
while ((m = npwp->nw_head) != NULL) {
npwp->nw_head = m->m_nextpkt;
m->m_nextpkt = NULL;
if (npwp->nw_head == NULL)
npwp->nw_tail = NULL;
npwp->nw_len--;
m_freem(m);
}
KASSERT(npwp->nw_tail == NULL, ("%s: tail", __func__));
KASSERT(npwp->nw_len == 0, ("%s: len", __func__));
}
void
netisr_unregister(const struct netisr_handler *nhp)
{
VNET_ITERATOR_DECL(vnet_iter);
struct netisr_work *npwp;
#ifdef INVARIANTS
const char *name;
#endif
u_int i, proto;
proto = nhp->nh_proto;
#ifdef INVARIANTS
name = nhp->nh_name;
#endif
KASSERT(proto < NETISR_MAXPROT,
("%s(%u): protocol too big for %s", __func__, proto, name));
NETISR_WLOCK();
KASSERT(netisr_proto[proto].np_handler != NULL,
("%s(%u): protocol not registered for %s", __func__, proto,
name));
#ifdef VIMAGE
VNET_LIST_RLOCK_NOSLEEP();
VNET_FOREACH(vnet_iter) {
CURVNET_SET(vnet_iter);
V_netisr_enable[proto] = 0;
CURVNET_RESTORE();
}
VNET_LIST_RUNLOCK_NOSLEEP();
#endif
netisr_proto[proto].np_name = NULL;
netisr_proto[proto].np_handler = NULL;
netisr_proto[proto].np_m2flow = NULL;
netisr_proto[proto].np_m2cpuid = NULL;
netisr_proto[proto].np_qlimit = 0;
netisr_proto[proto].np_policy = 0;
CPU_FOREACH(i) {
npwp = &(DPCPU_ID_PTR(i, nws))->nws_work[proto];
netisr_drain_proto(npwp);
bzero(npwp, sizeof(*npwp));
}
NETISR_WUNLOCK();
}
#ifdef VIMAGE
void
netisr_register_vnet(const struct netisr_handler *nhp)
{
u_int proto;
proto = nhp->nh_proto;
KASSERT(curvnet != NULL, ("%s: curvnet is NULL", __func__));
KASSERT(proto < NETISR_MAXPROT,
("%s(%u): protocol too big for %s", __func__, proto, nhp->nh_name));
NETISR_WLOCK();
KASSERT(netisr_proto[proto].np_handler != NULL,
("%s(%u): protocol not registered for %s", __func__, proto,
nhp->nh_name));
V_netisr_enable[proto] = 1;
NETISR_WUNLOCK();
}
static void
netisr_drain_proto_vnet(struct vnet *vnet, u_int proto)
{
struct epoch_tracker et;
struct netisr_workstream *nwsp;
struct netisr_work *npwp;
struct mbuf *m, *mp, *n, *ne;
struct ifnet *ifp;
u_int i;
KASSERT(vnet != NULL, ("%s: vnet is NULL", __func__));
NETISR_LOCK_ASSERT();
CPU_FOREACH(i) {
nwsp = DPCPU_ID_PTR(i, nws);
if (nwsp->nws_intr_event == NULL)
continue;
npwp = &nwsp->nws_work[proto];
NWS_LOCK(nwsp);
m = npwp->nw_head;
n = ne = NULL;
NET_EPOCH_ENTER(et);
while (m != NULL) {
mp = m;
m = m->m_nextpkt;
mp->m_nextpkt = NULL;
if ((ifp = ifnet_byindexgen(mp->m_pkthdr.rcvidx,
mp->m_pkthdr.rcvgen)) != NULL &&
ifp->if_vnet != vnet) {
if (n == NULL) {
n = ne = mp;
} else {
ne->m_nextpkt = mp;
ne = mp;
}
continue;
}
npwp->nw_len--;
m_freem(mp);
}
NET_EPOCH_EXIT(et);
npwp->nw_head = n;
npwp->nw_tail = ne;
NWS_UNLOCK(nwsp);
}
}
void
netisr_unregister_vnet(const struct netisr_handler *nhp)
{
u_int proto;
proto = nhp->nh_proto;
KASSERT(curvnet != NULL, ("%s: curvnet is NULL", __func__));
KASSERT(proto < NETISR_MAXPROT,
("%s(%u): protocol too big for %s", __func__, proto, nhp->nh_name));
NETISR_WLOCK();
KASSERT(netisr_proto[proto].np_handler != NULL,
("%s(%u): protocol not registered for %s", __func__, proto,
nhp->nh_name));
V_netisr_enable[proto] = 0;
netisr_drain_proto_vnet(curvnet, proto);
NETISR_WUNLOCK();
}
#endif
static u_int
netisr_get_dispatch(struct netisr_proto *npp)
{
if (npp->np_dispatch != NETISR_DISPATCH_DEFAULT)
return (npp->np_dispatch);
return (netisr_dispatch_policy);
}
static struct mbuf *
netisr_select_cpuid(struct netisr_proto *npp, u_int dispatch_policy,
uintptr_t source, struct mbuf *m, u_int *cpuidp)
{
struct ifnet *ifp;
u_int policy;
NETISR_LOCK_ASSERT();
if (nws_count == 1) {
*cpuidp = nws_array[0];
return (m);
}
policy = npp->np_policy;
if (policy == NETISR_POLICY_CPU) {
m = npp->np_m2cpuid(m, source, cpuidp);
if (m == NULL)
return (NULL);
if (*cpuidp != NETISR_CPUID_NONE) {
*cpuidp = netisr_get_cpuid(*cpuidp);
return (m);
}
if (dispatch_policy == NETISR_DISPATCH_HYBRID) {
*cpuidp = netisr_get_cpuid(curcpu);
return (m);
}
policy = NETISR_POLICY_SOURCE;
}
if (policy == NETISR_POLICY_FLOW) {
if (M_HASHTYPE_GET(m) == M_HASHTYPE_NONE &&
npp->np_m2flow != NULL) {
m = npp->np_m2flow(m, source);
if (m == NULL)
return (NULL);
}
if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
*cpuidp =
netisr_default_flow2cpu(m->m_pkthdr.flowid);
return (m);
}
policy = NETISR_POLICY_SOURCE;
}
KASSERT(policy == NETISR_POLICY_SOURCE,
("%s: invalid policy %u for %s", __func__, npp->np_policy,
npp->np_name));
MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
ifp = m->m_pkthdr.rcvif;
if (ifp != NULL)
*cpuidp = nws_array[(ifp->if_index + source) % nws_count];
else
*cpuidp = nws_array[source % nws_count];
return (m);
}
static u_int
netisr_process_workstream_proto(struct netisr_workstream *nwsp, u_int proto)
{
struct netisr_work local_npw, *npwp;
u_int handled;
struct mbuf *m;
NETISR_LOCK_ASSERT();
NWS_LOCK_ASSERT(nwsp);
KASSERT(nwsp->nws_flags & NWS_RUNNING,
("%s(%u): not running", __func__, proto));
KASSERT(proto >= 0 && proto < NETISR_MAXPROT,
("%s(%u): invalid proto\n", __func__, proto));
npwp = &nwsp->nws_work[proto];
if (npwp->nw_len == 0)
return (0);
handled = npwp->nw_len;
local_npw = *npwp;
npwp->nw_head = NULL;
npwp->nw_tail = NULL;
npwp->nw_len = 0;
nwsp->nws_pendingbits &= ~(1 << proto);
NWS_UNLOCK(nwsp);
while ((m = local_npw.nw_head) != NULL) {
local_npw.nw_head = m->m_nextpkt;
m->m_nextpkt = NULL;
if (local_npw.nw_head == NULL)
local_npw.nw_tail = NULL;
local_npw.nw_len--;
if (__predict_false(m_rcvif_restore(m) == NULL)) {
m_freem(m);
continue;
}
CURVNET_SET(m->m_pkthdr.rcvif->if_vnet);
netisr_proto[proto].np_handler(m);
CURVNET_RESTORE();
}
KASSERT(local_npw.nw_len == 0,
("%s(%u): len %u", __func__, proto, local_npw.nw_len));
if (netisr_proto[proto].np_drainedcpu)
netisr_proto[proto].np_drainedcpu(nwsp->nws_cpu);
NWS_LOCK(nwsp);
npwp->nw_handled += handled;
return (handled);
}
static void
swi_net(void *arg)
{
#ifdef NETISR_LOCKING
struct rm_priotracker tracker;
#endif
struct netisr_workstream *nwsp;
u_int bits, prot;
nwsp = arg;
#ifdef DEVICE_POLLING
KASSERT(nws_count == 1,
("%s: device_polling but nws_count != 1", __func__));
netisr_poll();
#endif
#ifdef NETISR_LOCKING
NETISR_RLOCK(&tracker);
#endif
NWS_LOCK(nwsp);
KASSERT(!(nwsp->nws_flags & NWS_RUNNING), ("swi_net: running"));
if (nwsp->nws_flags & NWS_DISPATCHING)
goto out;
nwsp->nws_flags |= NWS_RUNNING;
nwsp->nws_flags &= ~NWS_SCHEDULED;
while ((bits = nwsp->nws_pendingbits) != 0) {
while (bits != 0) {
prot = ffs(bits) - 1;
bits &= ~(1 << prot);
(void)netisr_process_workstream_proto(nwsp, prot);
}
}
nwsp->nws_flags &= ~NWS_RUNNING;
out:
NWS_UNLOCK(nwsp);
#ifdef NETISR_LOCKING
NETISR_RUNLOCK(&tracker);
#endif
#ifdef DEVICE_POLLING
netisr_pollmore();
#endif
}
static int
netisr_queue_workstream(struct netisr_workstream *nwsp, u_int proto,
struct netisr_work *npwp, struct mbuf *m, int *dosignalp)
{
NWS_LOCK_ASSERT(nwsp);
*dosignalp = 0;
if (npwp->nw_len < npwp->nw_qlimit) {
m_rcvif_serialize(m);
m->m_nextpkt = NULL;
if (npwp->nw_head == NULL) {
npwp->nw_head = m;
npwp->nw_tail = m;
} else {
npwp->nw_tail->m_nextpkt = m;
npwp->nw_tail = m;
}
npwp->nw_len++;
if (npwp->nw_len > npwp->nw_watermark)
npwp->nw_watermark = npwp->nw_len;
nwsp->nws_pendingbits |= (1 << proto);
if (!(nwsp->nws_flags &
(NWS_RUNNING | NWS_DISPATCHING | NWS_SCHEDULED))) {
nwsp->nws_flags |= NWS_SCHEDULED;
*dosignalp = 1;
}
npwp->nw_queued++;
return (0);
} else {
m_freem(m);
npwp->nw_qdrops++;
return (ENOBUFS);
}
}
static int
netisr_queue_internal(u_int proto, struct mbuf *m, u_int cpuid)
{
struct netisr_workstream *nwsp;
struct netisr_work *npwp;
int dosignal, error;
#ifdef NETISR_LOCKING
NETISR_LOCK_ASSERT();
#endif
KASSERT(cpuid <= mp_maxid, ("%s: cpuid too big (%u, %u)", __func__,
cpuid, mp_maxid));
KASSERT(!CPU_ABSENT(cpuid), ("%s: CPU %u absent", __func__, cpuid));
dosignal = 0;
error = 0;
nwsp = DPCPU_ID_PTR(cpuid, nws);
npwp = &nwsp->nws_work[proto];
NWS_LOCK(nwsp);
error = netisr_queue_workstream(nwsp, proto, npwp, m, &dosignal);
NWS_UNLOCK(nwsp);
if (dosignal)
NWS_SIGNAL(nwsp);
return (error);
}
int
netisr_queue_src(u_int proto, uintptr_t source, struct mbuf *m)
{
#ifdef NETISR_LOCKING
struct rm_priotracker tracker;
#endif
u_int cpuid;
int error;
KASSERT(proto < NETISR_MAXPROT,
("%s: invalid proto %u", __func__, proto));
#ifdef NETISR_LOCKING
NETISR_RLOCK(&tracker);
#endif
KASSERT(netisr_proto[proto].np_handler != NULL,
("%s: invalid proto %u", __func__, proto));
#ifdef VIMAGE
if (V_netisr_enable[proto] == 0) {
m_freem(m);
return (ENOPROTOOPT);
}
#endif
m = netisr_select_cpuid(&netisr_proto[proto], NETISR_DISPATCH_DEFERRED,
source, m, &cpuid);
if (m != NULL) {
KASSERT(!CPU_ABSENT(cpuid), ("%s: CPU %u absent", __func__,
cpuid));
VNET_ASSERT(m->m_pkthdr.rcvif != NULL,
("%s:%d rcvif == NULL: m=%p", __func__, __LINE__, m));
error = netisr_queue_internal(proto, m, cpuid);
} else
error = ENOBUFS;
#ifdef NETISR_LOCKING
NETISR_RUNLOCK(&tracker);
#endif
return (error);
}
int
netisr_queue(u_int proto, struct mbuf *m)
{
return (netisr_queue_src(proto, 0, m));
}
int
netisr_dispatch_src(u_int proto, uintptr_t source, struct mbuf *m)
{
#ifdef NETISR_LOCKING
struct rm_priotracker tracker;
#endif
struct netisr_workstream *nwsp;
struct netisr_proto *npp;
struct netisr_work *npwp;
int dosignal, error;
u_int cpuid, dispatch_policy;
NET_EPOCH_ASSERT();
KASSERT(proto < NETISR_MAXPROT,
("%s: invalid proto %u", __func__, proto));
#ifdef NETISR_LOCKING
NETISR_RLOCK(&tracker);
#endif
npp = &netisr_proto[proto];
KASSERT(npp->np_handler != NULL, ("%s: invalid proto %u", __func__,
proto));
#ifdef VIMAGE
if (V_netisr_enable[proto] == 0) {
m_freem(m);
return (ENOPROTOOPT);
}
#endif
dispatch_policy = netisr_get_dispatch(npp);
if (dispatch_policy == NETISR_DISPATCH_DEFERRED)
return (netisr_queue_src(proto, source, m));
if (dispatch_policy == NETISR_DISPATCH_DIRECT) {
nwsp = DPCPU_PTR(nws);
npwp = &nwsp->nws_work[proto];
npwp->nw_dispatched++;
npwp->nw_handled++;
netisr_proto[proto].np_handler(m);
error = 0;
goto out_unlock;
}
KASSERT(dispatch_policy == NETISR_DISPATCH_HYBRID,
("%s: unknown dispatch policy (%u)", __func__, dispatch_policy));
sched_pin();
m = netisr_select_cpuid(&netisr_proto[proto], NETISR_DISPATCH_HYBRID,
source, m, &cpuid);
if (m == NULL) {
error = ENOBUFS;
goto out_unpin;
}
KASSERT(!CPU_ABSENT(cpuid), ("%s: CPU %u absent", __func__, cpuid));
if (cpuid != curcpu)
goto queue_fallback;
nwsp = DPCPU_PTR(nws);
npwp = &nwsp->nws_work[proto];
NWS_LOCK(nwsp);
if (nwsp->nws_flags & (NWS_RUNNING | NWS_DISPATCHING | NWS_SCHEDULED)) {
error = netisr_queue_workstream(nwsp, proto, npwp, m,
&dosignal);
NWS_UNLOCK(nwsp);
if (dosignal)
NWS_SIGNAL(nwsp);
goto out_unpin;
}
nwsp->nws_flags |= NWS_DISPATCHING;
NWS_UNLOCK(nwsp);
netisr_proto[proto].np_handler(m);
NWS_LOCK(nwsp);
nwsp->nws_flags &= ~NWS_DISPATCHING;
npwp->nw_handled++;
npwp->nw_hybrid_dispatched++;
if (nwsp->nws_pendingbits != 0) {
nwsp->nws_flags |= NWS_SCHEDULED;
dosignal = 1;
} else
dosignal = 0;
NWS_UNLOCK(nwsp);
if (dosignal)
NWS_SIGNAL(nwsp);
error = 0;
goto out_unpin;
queue_fallback:
error = netisr_queue_internal(proto, m, cpuid);
out_unpin:
sched_unpin();
out_unlock:
#ifdef NETISR_LOCKING
NETISR_RUNLOCK(&tracker);
#endif
return (error);
}
int
netisr_dispatch(u_int proto, struct mbuf *m)
{
return (netisr_dispatch_src(proto, 0, m));
}
#ifdef DEVICE_POLLING
void
netisr_sched_poll(void)
{
struct netisr_workstream *nwsp;
nwsp = DPCPU_ID_PTR(nws_array[0], nws);
NWS_SIGNAL(nwsp);
}
#endif
static void
netisr_start_swi(u_int cpuid, struct pcpu *pc)
{
char swiname[12];
struct netisr_workstream *nwsp;
int error;
KASSERT(!CPU_ABSENT(cpuid), ("%s: CPU %u absent", __func__, cpuid));
nwsp = DPCPU_ID_PTR(cpuid, nws);
mtx_init(&nwsp->nws_mtx, "netisr_mtx", NULL, MTX_DEF);
nwsp->nws_cpu = cpuid;
snprintf(swiname, sizeof(swiname), "netisr %u", cpuid);
error = swi_add(&nwsp->nws_intr_event, swiname, swi_net, nwsp,
SWI_NET, INTR_TYPE_NET | INTR_MPSAFE, &nwsp->nws_swi_cookie);
if (error)
panic("%s: swi_add %d", __func__, error);
pc->pc_netisr = nwsp->nws_intr_event;
if (netisr_bindthreads) {
error = intr_event_bind(nwsp->nws_intr_event, cpuid);
if (error != 0)
printf("%s: cpu %u: intr_event_bind: %d", __func__,
cpuid, error);
}
NETISR_WLOCK();
nws_array[nws_count] = nwsp->nws_cpu;
nws_count++;
NETISR_WUNLOCK();
}
static void
netisr_init(void *arg)
{
struct pcpu *pc;
NETISR_LOCK_INIT();
if (netisr_maxthreads == 0 || netisr_maxthreads < -1 )
netisr_maxthreads = 1;
else if (netisr_maxthreads == -1)
netisr_maxthreads = mp_ncpus;
if (netisr_maxthreads > mp_ncpus) {
printf("netisr_init: forcing maxthreads from %d to %d\n",
netisr_maxthreads, mp_ncpus);
netisr_maxthreads = mp_ncpus;
}
if (netisr_defaultqlimit > netisr_maxqlimit) {
printf("netisr_init: forcing defaultqlimit from %d to %d\n",
netisr_defaultqlimit, netisr_maxqlimit);
netisr_defaultqlimit = netisr_maxqlimit;
}
#ifdef DEVICE_POLLING
if (netisr_maxthreads != 1 || netisr_bindthreads != 0) {
printf("netisr_init: forcing maxthreads to 1 and "
"bindthreads to 0 for device polling\n");
netisr_maxthreads = 1;
netisr_bindthreads = 0;
}
#endif
#ifdef EARLY_AP_STARTUP
STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
if (nws_count >= netisr_maxthreads)
break;
netisr_start_swi(pc->pc_cpuid, pc);
}
#else
pc = get_pcpu();
netisr_start_swi(pc->pc_cpuid, pc);
#endif
}
SYSINIT(netisr_init, SI_SUB_SOFTINTR, SI_ORDER_FIRST, netisr_init, NULL);
#ifndef EARLY_AP_STARTUP
static void
netisr_start(void *arg)
{
struct pcpu *pc;
STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
if (nws_count >= netisr_maxthreads)
break;
if (pc->pc_netisr != NULL)
continue;
netisr_start_swi(pc->pc_cpuid, pc);
}
}
SYSINIT(netisr_start, SI_SUB_SMP, SI_ORDER_MIDDLE, netisr_start, NULL);
#endif
static int
sysctl_netisr_proto(SYSCTL_HANDLER_ARGS)
{
struct rm_priotracker tracker;
struct sysctl_netisr_proto *snpp, *snp_array;
struct netisr_proto *npp;
u_int counter, proto;
int error;
if (req->newptr != NULL)
return (EINVAL);
snp_array = malloc(sizeof(*snp_array) * NETISR_MAXPROT, M_TEMP,
M_ZERO | M_WAITOK);
counter = 0;
NETISR_RLOCK(&tracker);
for (proto = 0; proto < NETISR_MAXPROT; proto++) {
npp = &netisr_proto[proto];
if (npp->np_name == NULL)
continue;
snpp = &snp_array[counter];
snpp->snp_version = sizeof(*snpp);
strlcpy(snpp->snp_name, npp->np_name, NETISR_NAMEMAXLEN);
snpp->snp_proto = proto;
snpp->snp_qlimit = npp->np_qlimit;
snpp->snp_policy = npp->np_policy;
snpp->snp_dispatch = npp->np_dispatch;
if (npp->np_m2flow != NULL)
snpp->snp_flags |= NETISR_SNP_FLAGS_M2FLOW;
if (npp->np_m2cpuid != NULL)
snpp->snp_flags |= NETISR_SNP_FLAGS_M2CPUID;
if (npp->np_drainedcpu != NULL)
snpp->snp_flags |= NETISR_SNP_FLAGS_DRAINEDCPU;
counter++;
}
NETISR_RUNLOCK(&tracker);
KASSERT(counter <= NETISR_MAXPROT,
("sysctl_netisr_proto: counter too big (%d)", counter));
error = SYSCTL_OUT(req, snp_array, sizeof(*snp_array) * counter);
free(snp_array, M_TEMP);
return (error);
}
SYSCTL_PROC(_net_isr, OID_AUTO, proto,
CTLFLAG_RD|CTLTYPE_STRUCT|CTLFLAG_MPSAFE, 0, 0, sysctl_netisr_proto,
"S,sysctl_netisr_proto",
"Return list of protocols registered with netisr");
static int
sysctl_netisr_workstream(SYSCTL_HANDLER_ARGS)
{
struct rm_priotracker tracker;
struct sysctl_netisr_workstream *snwsp, *snws_array;
struct netisr_workstream *nwsp;
u_int counter, cpuid;
int error;
if (req->newptr != NULL)
return (EINVAL);
snws_array = malloc(sizeof(*snws_array) * MAXCPU, M_TEMP,
M_ZERO | M_WAITOK);
counter = 0;
NETISR_RLOCK(&tracker);
CPU_FOREACH(cpuid) {
nwsp = DPCPU_ID_PTR(cpuid, nws);
if (nwsp->nws_intr_event == NULL)
continue;
NWS_LOCK(nwsp);
snwsp = &snws_array[counter];
snwsp->snws_version = sizeof(*snwsp);
snwsp->snws_wsid = cpuid;
snwsp->snws_cpu = cpuid;
if (nwsp->nws_intr_event != NULL)
snwsp->snws_flags |= NETISR_SNWS_FLAGS_INTR;
NWS_UNLOCK(nwsp);
counter++;
}
NETISR_RUNLOCK(&tracker);
KASSERT(counter <= MAXCPU,
("sysctl_netisr_workstream: counter too big (%d)", counter));
error = SYSCTL_OUT(req, snws_array, sizeof(*snws_array) * counter);
free(snws_array, M_TEMP);
return (error);
}
SYSCTL_PROC(_net_isr, OID_AUTO, workstream,
CTLFLAG_RD|CTLTYPE_STRUCT|CTLFLAG_MPSAFE, 0, 0, sysctl_netisr_workstream,
"S,sysctl_netisr_workstream",
"Return list of workstreams implemented by netisr");
static int
sysctl_netisr_work(SYSCTL_HANDLER_ARGS)
{
struct rm_priotracker tracker;
struct sysctl_netisr_work *snwp, *snw_array;
struct netisr_workstream *nwsp;
struct netisr_proto *npp;
struct netisr_work *nwp;
u_int counter, cpuid, proto;
int error;
if (req->newptr != NULL)
return (EINVAL);
snw_array = malloc(sizeof(*snw_array) * MAXCPU * NETISR_MAXPROT,
M_TEMP, M_ZERO | M_WAITOK);
counter = 0;
NETISR_RLOCK(&tracker);
CPU_FOREACH(cpuid) {
nwsp = DPCPU_ID_PTR(cpuid, nws);
if (nwsp->nws_intr_event == NULL)
continue;
NWS_LOCK(nwsp);
for (proto = 0; proto < NETISR_MAXPROT; proto++) {
npp = &netisr_proto[proto];
if (npp->np_name == NULL)
continue;
nwp = &nwsp->nws_work[proto];
snwp = &snw_array[counter];
snwp->snw_version = sizeof(*snwp);
snwp->snw_wsid = cpuid;
snwp->snw_proto = proto;
snwp->snw_len = nwp->nw_len;
snwp->snw_watermark = nwp->nw_watermark;
snwp->snw_dispatched = nwp->nw_dispatched;
snwp->snw_hybrid_dispatched =
nwp->nw_hybrid_dispatched;
snwp->snw_qdrops = nwp->nw_qdrops;
snwp->snw_queued = nwp->nw_queued;
snwp->snw_handled = nwp->nw_handled;
counter++;
}
NWS_UNLOCK(nwsp);
}
KASSERT(counter <= MAXCPU * NETISR_MAXPROT,
("sysctl_netisr_work: counter too big (%d)", counter));
NETISR_RUNLOCK(&tracker);
error = SYSCTL_OUT(req, snw_array, sizeof(*snw_array) * counter);
free(snw_array, M_TEMP);
return (error);
}
SYSCTL_PROC(_net_isr, OID_AUTO, work,
CTLFLAG_RD|CTLTYPE_STRUCT|CTLFLAG_MPSAFE, 0, 0, sysctl_netisr_work,
"S,sysctl_netisr_work",
"Return list of per-workstream, per-protocol work in netisr");
#ifdef DDB
DB_SHOW_COMMAND(netisr, db_show_netisr)
{
struct netisr_workstream *nwsp;
struct netisr_work *nwp;
int first, proto;
u_int cpuid;
db_printf("%3s %6s %5s %5s %5s %8s %8s %8s %8s\n", "CPU", "Proto",
"Len", "WMark", "Max", "Disp", "HDisp", "Drop", "Queue");
CPU_FOREACH(cpuid) {
nwsp = DPCPU_ID_PTR(cpuid, nws);
if (nwsp->nws_intr_event == NULL)
continue;
first = 1;
for (proto = 0; proto < NETISR_MAXPROT; proto++) {
if (netisr_proto[proto].np_handler == NULL)
continue;
nwp = &nwsp->nws_work[proto];
if (first) {
db_printf("%3d ", cpuid);
first = 0;
} else
db_printf("%3s ", "");
db_printf(
"%6s %5d %5d %5d %8ju %8ju %8ju %8ju\n",
netisr_proto[proto].np_name, nwp->nw_len,
nwp->nw_watermark, nwp->nw_qlimit,
nwp->nw_dispatched, nwp->nw_hybrid_dispatched,
nwp->nw_qdrops, nwp->nw_queued);
}
}
}
#endif