Home | History | Annotate | Line # | Download | only in npf
npf.c revision 1.31.2.4
      1  1.31.2.4  pgoyette /*	$NetBSD: npf.c,v 1.31.2.4 2017/01/07 08:56:50 pgoyette 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.31.2.4  pgoyette #ifdef _KERNEL
     37       1.1     rmind #include <sys/cdefs.h>
     38  1.31.2.4  pgoyette __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.31.2.4 2017/01/07 08:56:50 pgoyette 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.31.2.1  pgoyette #include <sys/localcount.h>
     47  1.31.2.4  pgoyette #endif
     48       1.1     rmind 
     49       1.1     rmind #include "npf_impl.h"
     50      1.20     rmind #include "npf_conn.h"
     51       1.1     rmind 
     52  1.31.2.4  pgoyette __read_mostly static npf_t *	npf_kernel_ctx = NULL;
     53      1.23  christos 
     54  1.31.2.4  pgoyette __dso_public int
     55  1.31.2.4  pgoyette npf_sysinit(unsigned nworkers)
     56       1.1     rmind {
     57      1.17     rmind 	npf_bpf_sysinit();
     58       1.2     rmind 	npf_tableset_sysinit();
     59       1.1     rmind 	npf_nat_sysinit();
     60  1.31.2.4  pgoyette 	return npf_worker_sysinit(nworkers);
     61       1.1     rmind }
     62       1.1     rmind 
     63  1.31.2.4  pgoyette __dso_public void
     64  1.31.2.4  pgoyette npf_sysfini(void)
     65       1.1     rmind {
     66  1.31.2.4  pgoyette 	npf_worker_sysfini();
     67       1.1     rmind 	npf_nat_sysfini();
     68       1.1     rmind 	npf_tableset_sysfini();
     69      1.17     rmind 	npf_bpf_sysfini();
     70       1.1     rmind }
     71       1.1     rmind 
     72  1.31.2.4  pgoyette __dso_public npf_t *
     73  1.31.2.4  pgoyette npf_create(int flags, const npf_mbufops_t *mbufops, const npf_ifops_t *ifops)
     74       1.1     rmind {
     75  1.31.2.4  pgoyette 	npf_t *npf;
     76       1.1     rmind 
     77  1.31.2.4  pgoyette 	npf = kmem_zalloc(sizeof(npf_t), KM_SLEEP);
     78  1.31.2.4  pgoyette 	npf->qsbr = pserialize_create();
     79  1.31.2.4  pgoyette 	if (!npf->qsbr) {
     80  1.31.2.4  pgoyette 		kmem_free(npf, sizeof(npf_t));
     81  1.31.2.4  pgoyette 		return NULL;
     82  1.31.2.4  pgoyette 	}
     83  1.31.2.4  pgoyette 	npf->stats_percpu = percpu_alloc(NPF_STATS_SIZE);
     84  1.31.2.4  pgoyette 	npf->mbufops = mbufops;
     85  1.31.2.4  pgoyette 
     86  1.31.2.4  pgoyette 	npf_ifmap_init(npf, ifops);
     87  1.31.2.4  pgoyette 	npf_conn_init(npf, flags);
     88  1.31.2.4  pgoyette 	npf_alg_init(npf);
     89  1.31.2.4  pgoyette 	npf_ext_init(npf);
     90  1.31.2.4  pgoyette 
     91  1.31.2.4  pgoyette 	/* Load an empty configuration. */
     92  1.31.2.4  pgoyette 	npf_config_init(npf);
     93  1.31.2.4  pgoyette 	return npf;
     94  1.31.2.4  pgoyette }
     95  1.31.2.4  pgoyette 
     96  1.31.2.4  pgoyette __dso_public void
     97  1.31.2.4  pgoyette npf_destroy(npf_t *npf)
     98  1.31.2.4  pgoyette {
     99  1.31.2.4  pgoyette 	/*
    100  1.31.2.4  pgoyette 	 * Destroy the current configuration.  Note: at this point all
    101  1.31.2.4  pgoyette 	 * handlers must be deactivated; we will drain any processing.
    102  1.31.2.4  pgoyette 	 */
    103  1.31.2.4  pgoyette 	npf_config_fini(npf);
    104       1.1     rmind 
    105  1.31.2.4  pgoyette 	/* Finally, safe to destroy the subsystems. */
    106  1.31.2.4  pgoyette 	npf_ext_fini(npf);
    107  1.31.2.4  pgoyette 	npf_alg_fini(npf);
    108  1.31.2.4  pgoyette 	npf_conn_fini(npf);
    109  1.31.2.4  pgoyette 	npf_ifmap_fini(npf);
    110       1.1     rmind 
    111  1.31.2.4  pgoyette 	pserialize_destroy(npf->qsbr);
    112  1.31.2.4  pgoyette 	percpu_free(npf->stats_percpu, NPF_STATS_SIZE);
    113  1.31.2.4  pgoyette 	kmem_free(npf, sizeof(npf_t));
    114       1.1     rmind }
    115       1.1     rmind 
    116  1.31.2.4  pgoyette __dso_public int
    117  1.31.2.4  pgoyette npf_load(npf_t *npf, void *ref, npf_error_t *err)
    118       1.1     rmind {
    119  1.31.2.4  pgoyette 	return npfctl_load(npf, 0, ref);
    120       1.1     rmind }
    121       1.1     rmind 
    122  1.31.2.4  pgoyette __dso_public void
    123  1.31.2.4  pgoyette npf_gc(npf_t *npf)
    124       1.1     rmind {
    125  1.31.2.4  pgoyette 	npf_conn_worker(npf);
    126       1.1     rmind }
    127       1.1     rmind 
    128  1.31.2.4  pgoyette __dso_public void
    129  1.31.2.4  pgoyette npf_thread_register(npf_t *npf)
    130       1.1     rmind {
    131  1.31.2.4  pgoyette 	pserialize_register(npf->qsbr);
    132       1.1     rmind }
    133       1.1     rmind 
    134  1.31.2.4  pgoyette void
    135  1.31.2.4  pgoyette npf_setkernctx(npf_t *npf)
    136       1.1     rmind {
    137  1.31.2.4  pgoyette 	npf_kernel_ctx = npf;
    138       1.1     rmind }
    139       1.2     rmind 
    140  1.31.2.4  pgoyette npf_t *
    141  1.31.2.4  pgoyette npf_getkernctx(void)
    142      1.13     rmind {
    143  1.31.2.4  pgoyette 	return npf_kernel_ctx;
    144      1.13     rmind }
    145      1.13     rmind 
    146       1.2     rmind /*
    147       1.2     rmind  * NPF statistics interface.
    148       1.2     rmind  */
    149       1.2     rmind 
    150       1.2     rmind void
    151  1.31.2.4  pgoyette npf_stats_inc(npf_t *npf, npf_stats_t st)
    152       1.2     rmind {
    153  1.31.2.4  pgoyette 	uint64_t *stats = percpu_getref(npf->stats_percpu);
    154       1.2     rmind 	stats[st]++;
    155  1.31.2.4  pgoyette 	percpu_putref(npf->stats_percpu);
    156       1.2     rmind }
    157       1.2     rmind 
    158       1.2     rmind void
    159  1.31.2.4  pgoyette npf_stats_dec(npf_t *npf, npf_stats_t st)
    160       1.2     rmind {
    161  1.31.2.4  pgoyette 	uint64_t *stats = percpu_getref(npf->stats_percpu);
    162       1.2     rmind 	stats[st]--;
    163  1.31.2.4  pgoyette 	percpu_putref(npf->stats_percpu);
    164       1.2     rmind }
    165       1.2     rmind 
    166       1.2     rmind static void
    167       1.2     rmind npf_stats_collect(void *mem, void *arg, struct cpu_info *ci)
    168       1.2     rmind {
    169       1.2     rmind 	uint64_t *percpu_stats = mem, *full_stats = arg;
    170       1.2     rmind 
    171  1.31.2.4  pgoyette 	for (unsigned i = 0; i < NPF_STATS_COUNT; i++) {
    172       1.2     rmind 		full_stats[i] += percpu_stats[i];
    173       1.2     rmind 	}
    174       1.2     rmind }
    175       1.2     rmind 
    176       1.2     rmind /*
    177  1.31.2.4  pgoyette  * npf_stats: export collected statistics.
    178       1.2     rmind  */
    179  1.31.2.4  pgoyette __dso_public void
    180  1.31.2.4  pgoyette npf_stats(npf_t *npf, uint64_t *buf)
    181       1.2     rmind {
    182  1.31.2.4  pgoyette 	memset(buf, 0, NPF_STATS_SIZE);
    183  1.31.2.4  pgoyette 	percpu_foreach(npf->stats_percpu, npf_stats_collect, buf);
    184       1.2     rmind }
    185