sync with OpenBSD -current

This commit is contained in:
purplerain 2023-12-11 19:42:54 +00:00
parent 086d8cf2c2
commit e53750f3c3
Signed by: purplerain
GPG key ID: F42C07F07E2E35B7
87 changed files with 852 additions and 180 deletions

View file

@ -1,4 +1,4 @@
/* $OpenBSD: if_pflow.c,v 1.102 2023/12/08 23:15:44 mvs Exp $ */
/* $OpenBSD: if_pflow.c,v 1.104 2023/12/12 00:03:31 mvs Exp $ */
/*
* Copyright (c) 2011 Florian Obser <florian@narrans.de>
@ -62,8 +62,23 @@
#define DPRINTF(x)
#endif
SLIST_HEAD(, pflow_softc) pflowif_list;
struct pflowstats pflowstats;
SMR_SLIST_HEAD(, pflow_softc) pflowif_list;
enum pflowstat_counters {
pflow_flows,
pflow_packets,
pflow_onomem,
pflow_oerrors,
pflow_ncounters,
};
struct cpumem *pflow_counters;
static inline void
pflowstat_inc(enum pflowstat_counters c)
{
counters_inc(pflow_counters, c);
}
void pflowattach(int);
int pflow_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
@ -113,7 +128,8 @@ struct if_clone pflow_cloner =
void
pflowattach(int npflow)
{
SLIST_INIT(&pflowif_list);
SMR_SLIST_INIT(&pflowif_list);
pflow_counters = counters_alloc(pflow_ncounters);
if_clone_attach(&pflow_cloner);
}
@ -268,9 +284,8 @@ pflow_clone_create(struct if_clone *ifc, int unit)
task_set(&pflowif->sc_outputtask, pflow_output_process, pflowif);
/* Insert into list of pflows */
NET_LOCK();
SLIST_INSERT_HEAD(&pflowif_list, pflowif, sc_next);
NET_UNLOCK();
KERNEL_ASSERT_LOCKED();
SMR_SLIST_INSERT_HEAD_LOCKED(&pflowif_list, pflowif, sc_next);
return (0);
}
@ -284,9 +299,12 @@ pflow_clone_destroy(struct ifnet *ifp)
NET_LOCK();
sc->sc_dying = 1;
SLIST_REMOVE(&pflowif_list, sc, pflow_softc, sc_next);
NET_UNLOCK();
KERNEL_ASSERT_LOCKED();
SMR_SLIST_REMOVE_LOCKED(&pflowif_list, sc, pflow_softc, sc_next);
smr_barrier();
timeout_del(&sc->sc_tmo);
timeout_del(&sc->sc_tmo6);
timeout_del(&sc->sc_tmo_tmpl);
@ -513,7 +531,6 @@ pflowioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
if ((ifp->if_flags & IFF_UP) && sc->so != NULL) {
ifp->if_flags |= IFF_RUNNING;
mtx_enter(&sc->sc_mtx);
sc->sc_gcounter = pflowstats.pflow_flows;
/* send templates on startup */
if (sc->sc_version == PFLOW_PROTO_10)
pflow_sendout_ipfix_tmpl(sc);
@ -577,7 +594,6 @@ pflowioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
if ((ifp->if_flags & IFF_UP) && sc->so != NULL) {
ifp->if_flags |= IFF_RUNNING;
mtx_enter(&sc->sc_mtx);
sc->sc_gcounter = pflowstats.pflow_flows;
if (sc->sc_version == PFLOW_PROTO_10)
pflow_sendout_ipfix_tmpl(sc);
mtx_leave(&sc->sc_mtx);
@ -646,14 +662,14 @@ pflow_get_mbuf(struct pflow_softc *sc, u_int16_t set_id)
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
pflowstats.pflow_onomem++;
pflowstat_inc(pflow_onomem);
return (NULL);
}
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
m_free(m);
pflowstats.pflow_onomem++;
pflowstat_inc(pflow_onomem);
return (NULL);
}
@ -812,7 +828,7 @@ export_pflow(struct pf_state *st)
sk = st->key[st->direction == PF_IN ? PF_SK_WIRE : PF_SK_STACK];
SLIST_FOREACH(sc, &pflowif_list, sc_next) {
SMR_SLIST_FOREACH(sc, &pflowif_list, sc_next) {
mtx_enter(&sc->sc_mtx);
switch (sc->sc_version) {
case PFLOW_PROTO_5:
@ -898,8 +914,7 @@ copy_flow_to_m(struct pflow_flow *flow, struct pflow_softc *sc)
(sc->sc_count * sizeof(struct pflow_flow)),
sizeof(struct pflow_flow), flow, M_NOWAIT);
if (pflowstats.pflow_flows == sc->sc_gcounter)
pflowstats.pflow_flows++;
pflowstat_inc(pflow_flows);
sc->sc_gcounter++;
sc->sc_count++;
@ -928,8 +943,7 @@ copy_flow_ipfix_4_to_m(struct pflow_ipfix_flow4 *flow, struct pflow_softc *sc)
(sc->sc_count4 * sizeof(struct pflow_ipfix_flow4)),
sizeof(struct pflow_ipfix_flow4), flow, M_NOWAIT);
if (pflowstats.pflow_flows == sc->sc_gcounter)
pflowstats.pflow_flows++;
pflowstat_inc(pflow_flows);
sc->sc_gcounter++;
sc->sc_count4++;
@ -957,8 +971,7 @@ copy_flow_ipfix_6_to_m(struct pflow_ipfix_flow6 *flow, struct pflow_softc *sc)
(sc->sc_count6 * sizeof(struct pflow_ipfix_flow6)),
sizeof(struct pflow_ipfix_flow6), flow, M_NOWAIT);
if (pflowstats.pflow_flows == sc->sc_gcounter)
pflowstats.pflow_flows++;
pflowstat_inc(pflow_flows);
sc->sc_gcounter++;
sc->sc_count6++;
@ -1114,7 +1127,7 @@ pflow_sendout_v5(struct pflow_softc *sc)
return (0);
}
pflowstats.pflow_packets++;
pflowstat_inc(pflow_packets);
h = mtod(m, struct pflow_header *);
h->count = htons(sc->sc_count);
@ -1171,14 +1184,14 @@ pflow_sendout_ipfix(struct pflow_softc *sc, sa_family_t af)
return (0);
}
pflowstats.pflow_packets++;
pflowstat_inc(pflow_packets);
set_hdr = mtod(m, struct pflow_set_header *);
set_hdr->set_length = htons(set_length);
/* populate pflow_header */
M_PREPEND(m, sizeof(struct pflow_v10_header), M_DONTWAIT);
if (m == NULL) {
pflowstats.pflow_onomem++;
pflowstat_inc(pflow_onomem);
return (ENOBUFS);
}
h10 = mtod(m, struct pflow_v10_header *);
@ -1215,12 +1228,12 @@ pflow_sendout_ipfix_tmpl(struct pflow_softc *sc)
m_freem(m);
return (0);
}
pflowstats.pflow_packets++;
pflowstat_inc(pflow_packets);
/* populate pflow_header */
M_PREPEND(m, sizeof(struct pflow_v10_header), M_DONTWAIT);
if (m == NULL) {
pflowstats.pflow_onomem++;
pflowstat_inc(pflow_onomem);
return (ENOBUFS);
}
h10 = mtod(m, struct pflow_v10_header *);
@ -1260,11 +1273,23 @@ pflow_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
return (ENOTDIR);
switch (name[0]) {
case NET_PFLOW_STATS:
case NET_PFLOW_STATS: {
uint64_t counters[pflow_ncounters];
struct pflowstats pflowstats;
if (newp != NULL)
return (EPERM);
counters_read(pflow_counters, counters, pflow_ncounters, NULL);
pflowstats.pflow_flows = counters[pflow_flows];
pflowstats.pflow_packets = counters[pflow_packets];
pflowstats.pflow_onomem = counters[pflow_onomem];
pflowstats.pflow_oerrors = counters[pflow_oerrors];
return (sysctl_struct(oldp, oldlenp, newp, newlen,
&pflowstats, sizeof(pflowstats)));
}
default:
return (EOPNOTSUPP);
}