Home | History | Annotate | Line # | Download | only in npf
npf.c revision 1.35
      1  1.35  christos /*	$NetBSD: npf.c,v 1.35 2018/09/12 21:58:38 christos Exp $	*/
      2   1.1     rmind 
      3   1.1     rmind /*-
      4  1.15     rmind  * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
      5   1.1     rmind  * All rights reserved.
      6   1.1     rmind  *
      7   1.1     rmind  * This material is based upon work partially supported by The
      8   1.1     rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9   1.1     rmind  *
     10   1.1     rmind  * Redistribution and use in source and binary forms, with or without
     11   1.1     rmind  * modification, are permitted provided that the following conditions
     12   1.1     rmind  * are met:
     13   1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     14   1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     15   1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     rmind  *    documentation and/or other materials provided with the distribution.
     18   1.1     rmind  *
     19   1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     rmind  */
     31   1.1     rmind 
     32   1.1     rmind /*
     33   1.1     rmind  * NPF main: dynamic load/initialisation and unload routines.
     34   1.1     rmind  */
     35   1.1     rmind 
     36  1.33  christos #ifdef _KERNEL
     37   1.1     rmind #include <sys/cdefs.h>
     38  1.35  christos __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.35 2018/09/12 21:58:38 christos Exp $");
     39   1.1     rmind 
     40   1.1     rmind #include <sys/param.h>
     41   1.1     rmind #include <sys/types.h>
     42   1.1     rmind 
     43   1.1     rmind #include <sys/conf.h>
     44   1.2     rmind #include <sys/kmem.h>
     45   1.2     rmind #include <sys/percpu.h>
     46  1.33  christos #endif
     47   1.1     rmind 
     48   1.1     rmind #include "npf_impl.h"
     49  1.20     rmind #include "npf_conn.h"
     50   1.1     rmind 
     51  1.33  christos __read_mostly static npf_t *	npf_kernel_ctx = NULL;
     52  1.23  christos 
     53  1.33  christos __dso_public int
     54  1.33  christos npf_sysinit(unsigned nworkers)
     55   1.1     rmind {
     56  1.17     rmind 	npf_bpf_sysinit();
     57   1.2     rmind 	npf_tableset_sysinit();
     58   1.1     rmind 	npf_nat_sysinit();
     59  1.35  christos 	npf_alg_sysinit();
     60  1.33  christos 	return npf_worker_sysinit(nworkers);
     61   1.1     rmind }
     62   1.1     rmind 
     63  1.33  christos __dso_public void
     64  1.33  christos npf_sysfini(void)
     65   1.1     rmind {
     66  1.33  christos 	npf_worker_sysfini();
     67  1.35  christos 	npf_alg_sysfini();
     68   1.1     rmind 	npf_nat_sysfini();
     69   1.1     rmind 	npf_tableset_sysfini();
     70  1.17     rmind 	npf_bpf_sysfini();
     71   1.1     rmind }
     72   1.1     rmind 
     73  1.33  christos __dso_public npf_t *
     74  1.33  christos npf_create(int flags, const npf_mbufops_t *mbufops, const npf_ifops_t *ifops)
     75   1.1     rmind {
     76  1.33  christos 	npf_t *npf;
     77   1.1     rmind 
     78  1.33  christos 	npf = kmem_zalloc(sizeof(npf_t), KM_SLEEP);
     79  1.33  christos 	npf->qsbr = pserialize_create();
     80  1.33  christos 	npf->stats_percpu = percpu_alloc(NPF_STATS_SIZE);
     81  1.33  christos 	npf->mbufops = mbufops;
     82  1.33  christos 
     83  1.33  christos 	npf_ifmap_init(npf, ifops);
     84  1.33  christos 	npf_conn_init(npf, flags);
     85  1.33  christos 	npf_alg_init(npf);
     86  1.33  christos 	npf_ext_init(npf);
     87  1.33  christos 
     88  1.33  christos 	/* Load an empty configuration. */
     89  1.33  christos 	npf_config_init(npf);
     90  1.33  christos 	return npf;
     91  1.33  christos }
     92  1.33  christos 
     93  1.33  christos __dso_public void
     94  1.33  christos npf_destroy(npf_t *npf)
     95  1.33  christos {
     96  1.33  christos 	/*
     97  1.33  christos 	 * Destroy the current configuration.  Note: at this point all
     98  1.33  christos 	 * handlers must be deactivated; we will drain any processing.
     99  1.33  christos 	 */
    100  1.33  christos 	npf_config_fini(npf);
    101   1.1     rmind 
    102  1.33  christos 	/* Finally, safe to destroy the subsystems. */
    103  1.33  christos 	npf_ext_fini(npf);
    104  1.33  christos 	npf_alg_fini(npf);
    105  1.33  christos 	npf_conn_fini(npf);
    106  1.33  christos 	npf_ifmap_fini(npf);
    107   1.1     rmind 
    108  1.33  christos 	pserialize_destroy(npf->qsbr);
    109  1.33  christos 	percpu_free(npf->stats_percpu, NPF_STATS_SIZE);
    110  1.33  christos 	kmem_free(npf, sizeof(npf_t));
    111   1.1     rmind }
    112   1.1     rmind 
    113  1.33  christos __dso_public int
    114  1.33  christos npf_load(npf_t *npf, void *ref, npf_error_t *err)
    115   1.1     rmind {
    116  1.33  christos 	return npfctl_load(npf, 0, ref);
    117   1.1     rmind }
    118   1.1     rmind 
    119  1.33  christos __dso_public void
    120  1.33  christos npf_gc(npf_t *npf)
    121   1.1     rmind {
    122  1.33  christos 	npf_conn_worker(npf);
    123   1.1     rmind }
    124   1.1     rmind 
    125  1.33  christos __dso_public void
    126  1.33  christos npf_thread_register(npf_t *npf)
    127   1.1     rmind {
    128  1.33  christos 	pserialize_register(npf->qsbr);
    129   1.1     rmind }
    130   1.1     rmind 
    131  1.33  christos void
    132  1.33  christos npf_setkernctx(npf_t *npf)
    133   1.1     rmind {
    134  1.33  christos 	npf_kernel_ctx = npf;
    135   1.1     rmind }
    136   1.2     rmind 
    137  1.33  christos npf_t *
    138  1.33  christos npf_getkernctx(void)
    139  1.13     rmind {
    140  1.33  christos 	return npf_kernel_ctx;
    141  1.13     rmind }
    142  1.13     rmind 
    143   1.2     rmind /*
    144   1.2     rmind  * NPF statistics interface.
    145   1.2     rmind  */
    146   1.2     rmind 
    147   1.2     rmind void
    148  1.33  christos npf_stats_inc(npf_t *npf, npf_stats_t st)
    149   1.2     rmind {
    150  1.33  christos 	uint64_t *stats = percpu_getref(npf->stats_percpu);
    151   1.2     rmind 	stats[st]++;
    152  1.33  christos 	percpu_putref(npf->stats_percpu);
    153   1.2     rmind }
    154   1.2     rmind 
    155   1.2     rmind void
    156  1.33  christos npf_stats_dec(npf_t *npf, npf_stats_t st)
    157   1.2     rmind {
    158  1.33  christos 	uint64_t *stats = percpu_getref(npf->stats_percpu);
    159   1.2     rmind 	stats[st]--;
    160  1.33  christos 	percpu_putref(npf->stats_percpu);
    161   1.2     rmind }
    162   1.2     rmind 
    163   1.2     rmind static void
    164   1.2     rmind npf_stats_collect(void *mem, void *arg, struct cpu_info *ci)
    165   1.2     rmind {
    166   1.2     rmind 	uint64_t *percpu_stats = mem, *full_stats = arg;
    167   1.2     rmind 
    168  1.33  christos 	for (unsigned i = 0; i < NPF_STATS_COUNT; i++) {
    169   1.2     rmind 		full_stats[i] += percpu_stats[i];
    170   1.2     rmind 	}
    171   1.2     rmind }
    172   1.2     rmind 
    173   1.2     rmind /*
    174  1.33  christos  * npf_stats: export collected statistics.
    175   1.2     rmind  */
    176  1.33  christos __dso_public void
    177  1.33  christos npf_stats(npf_t *npf, uint64_t *buf)
    178   1.2     rmind {
    179  1.33  christos 	memset(buf, 0, NPF_STATS_SIZE);
    180  1.33  christos 	percpu_foreach(npf->stats_percpu, npf_stats_collect, buf);
    181   1.2     rmind }
    182