Home | History | Annotate | Line # | Download | only in npf
npf_rproc.c revision 1.6
      1  1.6    rmind /*	$NetBSD: npf_rproc.c,v 1.6 2013/02/09 03:35:32 rmind Exp $	*/
      2  1.1    rmind 
      3  1.1    rmind /*-
      4  1.6    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.3    rmind  * NPF extension and rule procedure interface.
     34  1.1    rmind  */
     35  1.1    rmind 
     36  1.1    rmind #include <sys/cdefs.h>
     37  1.1    rmind __KERNEL_RCSID(0, "$NetBSD");
     38  1.1    rmind 
     39  1.1    rmind #include <sys/param.h>
     40  1.2    rmind #include <sys/types.h>
     41  1.1    rmind 
     42  1.1    rmind #include <sys/atomic.h>
     43  1.1    rmind #include <sys/kmem.h>
     44  1.3    rmind #include <sys/mutex.h>
     45  1.1    rmind 
     46  1.1    rmind #include "npf_impl.h"
     47  1.1    rmind 
     48  1.3    rmind #define	EXT_NAME_LEN		32
     49  1.3    rmind 
     50  1.3    rmind typedef struct npf_ext {
     51  1.3    rmind 	char			ext_callname[EXT_NAME_LEN];
     52  1.3    rmind 	LIST_ENTRY(npf_ext)	ext_entry;
     53  1.3    rmind 	const npf_ext_ops_t *	ext_ops;
     54  1.3    rmind 	unsigned		ext_refcnt;
     55  1.3    rmind } npf_ext_t;
     56  1.3    rmind 
     57  1.6    rmind struct npf_rprocset {
     58  1.6    rmind 	LIST_HEAD(, npf_rproc)	rps_list;
     59  1.6    rmind };
     60  1.6    rmind 
     61  1.3    rmind #define	RPROC_NAME_LEN		32
     62  1.3    rmind #define	RPROC_EXT_COUNT		16
     63  1.1    rmind 
     64  1.1    rmind struct npf_rproc {
     65  1.6    rmind 	/* Flags and reference count. */
     66  1.6    rmind 	uint32_t		rp_flags;
     67  1.1    rmind 	u_int			rp_refcnt;
     68  1.6    rmind 
     69  1.3    rmind 	/* Associated extensions and their metadata . */
     70  1.3    rmind 	unsigned		rp_ext_count;
     71  1.3    rmind 	npf_ext_t *		rp_ext[RPROC_EXT_COUNT];
     72  1.3    rmind 	void *			rp_ext_meta[RPROC_EXT_COUNT];
     73  1.6    rmind 
     74  1.6    rmind 	/* Name of the procedure and list entry. */
     75  1.6    rmind 	char			rp_name[RPROC_NAME_LEN];
     76  1.6    rmind 	LIST_ENTRY(npf_rproc)	rp_entry;
     77  1.1    rmind };
     78  1.1    rmind 
     79  1.3    rmind static LIST_HEAD(, npf_ext)	ext_list	__cacheline_aligned;
     80  1.3    rmind static kmutex_t			ext_lock	__cacheline_aligned;
     81  1.3    rmind 
     82  1.3    rmind void
     83  1.3    rmind npf_ext_sysinit(void)
     84  1.3    rmind {
     85  1.3    rmind 	mutex_init(&ext_lock, MUTEX_DEFAULT, IPL_NONE);
     86  1.3    rmind 	LIST_INIT(&ext_list);
     87  1.3    rmind }
     88  1.3    rmind 
     89  1.3    rmind void
     90  1.3    rmind npf_ext_sysfini(void)
     91  1.3    rmind {
     92  1.3    rmind 	KASSERT(LIST_EMPTY(&ext_list));
     93  1.3    rmind 	mutex_destroy(&ext_lock);
     94  1.3    rmind }
     95  1.3    rmind 
     96  1.3    rmind /*
     97  1.3    rmind  * NPF extension management for the rule procedures.
     98  1.3    rmind  */
     99  1.3    rmind 
    100  1.3    rmind static npf_ext_t *
    101  1.3    rmind npf_ext_lookup(const char *name)
    102  1.3    rmind {
    103  1.3    rmind 	npf_ext_t *ext = NULL;
    104  1.3    rmind 
    105  1.3    rmind 	KASSERT(mutex_owned(&ext_lock));
    106  1.3    rmind 
    107  1.3    rmind 	LIST_FOREACH(ext, &ext_list, ext_entry)
    108  1.3    rmind 		if (strcmp(ext->ext_callname, name) == 0)
    109  1.3    rmind 			break;
    110  1.3    rmind 	return ext;
    111  1.3    rmind }
    112  1.3    rmind 
    113  1.3    rmind void *
    114  1.3    rmind npf_ext_register(const char *name, const npf_ext_ops_t *ops)
    115  1.3    rmind {
    116  1.3    rmind 	npf_ext_t *ext;
    117  1.3    rmind 
    118  1.3    rmind 	ext = kmem_zalloc(sizeof(npf_ext_t), KM_SLEEP);
    119  1.3    rmind 	strlcpy(ext->ext_callname, name, EXT_NAME_LEN);
    120  1.3    rmind 	ext->ext_ops = ops;
    121  1.3    rmind 
    122  1.3    rmind 	mutex_enter(&ext_lock);
    123  1.3    rmind 	if (npf_ext_lookup(name)) {
    124  1.3    rmind 		mutex_exit(&ext_lock);
    125  1.3    rmind 		kmem_free(ext, sizeof(npf_ext_t));
    126  1.3    rmind 		return NULL;
    127  1.3    rmind 	}
    128  1.3    rmind 	LIST_INSERT_HEAD(&ext_list, ext, ext_entry);
    129  1.3    rmind 	mutex_exit(&ext_lock);
    130  1.3    rmind 
    131  1.3    rmind 	return (void *)ext;
    132  1.3    rmind }
    133  1.3    rmind 
    134  1.3    rmind int
    135  1.3    rmind npf_ext_unregister(void *extid)
    136  1.3    rmind {
    137  1.3    rmind 	npf_ext_t *ext = extid;
    138  1.3    rmind 
    139  1.3    rmind 	/*
    140  1.3    rmind 	 * Check if in-use first (re-check with the lock held).
    141  1.3    rmind 	 */
    142  1.3    rmind 	if (ext->ext_refcnt) {
    143  1.3    rmind 		return EBUSY;
    144  1.3    rmind 	}
    145  1.3    rmind 
    146  1.3    rmind 	mutex_enter(&ext_lock);
    147  1.3    rmind 	if (ext->ext_refcnt) {
    148  1.3    rmind 		mutex_exit(&ext_lock);
    149  1.3    rmind 		return EBUSY;
    150  1.3    rmind 	}
    151  1.3    rmind 	KASSERT(npf_ext_lookup(ext->ext_callname));
    152  1.3    rmind 	LIST_REMOVE(ext, ext_entry);
    153  1.3    rmind 	mutex_exit(&ext_lock);
    154  1.3    rmind 
    155  1.3    rmind 	kmem_free(ext, sizeof(npf_ext_t));
    156  1.3    rmind 	return 0;
    157  1.3    rmind }
    158  1.3    rmind 
    159  1.3    rmind int
    160  1.3    rmind npf_ext_construct(const char *name, npf_rproc_t *rp, prop_dictionary_t params)
    161  1.3    rmind {
    162  1.3    rmind 	const npf_ext_ops_t *extops;
    163  1.3    rmind 	npf_ext_t *ext;
    164  1.3    rmind 	unsigned i;
    165  1.3    rmind 	int error;
    166  1.3    rmind 
    167  1.3    rmind 	if (rp->rp_ext_count >= RPROC_EXT_COUNT) {
    168  1.3    rmind 		return ENOSPC;
    169  1.3    rmind 	}
    170  1.3    rmind 
    171  1.3    rmind 	mutex_enter(&ext_lock);
    172  1.3    rmind 	ext = npf_ext_lookup(name);
    173  1.3    rmind 	if (ext) {
    174  1.3    rmind 		atomic_inc_uint(&ext->ext_refcnt);
    175  1.3    rmind 	}
    176  1.3    rmind 	mutex_exit(&ext_lock);
    177  1.4  mlelstv 
    178  1.3    rmind 	if (!ext) {
    179  1.3    rmind 		return ENOENT;
    180  1.3    rmind 	}
    181  1.3    rmind 
    182  1.4  mlelstv 	extops = ext->ext_ops;
    183  1.4  mlelstv 	KASSERT(extops != NULL);
    184  1.4  mlelstv 
    185  1.3    rmind 	error = extops->ctor(rp, params);
    186  1.3    rmind 	if (error) {
    187  1.3    rmind 		atomic_dec_uint(&ext->ext_refcnt);
    188  1.3    rmind 		return error;
    189  1.3    rmind 	}
    190  1.3    rmind 	i = rp->rp_ext_count++;
    191  1.3    rmind 	rp->rp_ext[i] = ext;
    192  1.3    rmind 	return 0;
    193  1.3    rmind }
    194  1.3    rmind 
    195  1.3    rmind /*
    196  1.3    rmind  * Rule procedure management.
    197  1.3    rmind  */
    198  1.3    rmind 
    199  1.6    rmind npf_rprocset_t *
    200  1.6    rmind npf_rprocset_create(void)
    201  1.6    rmind {
    202  1.6    rmind 	npf_rprocset_t *rpset;
    203  1.6    rmind 
    204  1.6    rmind 	rpset = kmem_zalloc(sizeof(npf_rprocset_t), KM_SLEEP);
    205  1.6    rmind 	LIST_INIT(&rpset->rps_list);
    206  1.6    rmind 	return rpset;
    207  1.6    rmind }
    208  1.6    rmind 
    209  1.6    rmind void
    210  1.6    rmind npf_rprocset_destroy(npf_rprocset_t *rpset)
    211  1.6    rmind {
    212  1.6    rmind 	npf_rproc_t *rp;
    213  1.6    rmind 
    214  1.6    rmind 	while ((rp = LIST_FIRST(&rpset->rps_list)) != NULL) {
    215  1.6    rmind 		LIST_REMOVE(rp, rp_entry);
    216  1.6    rmind 		npf_rproc_release(rp);
    217  1.6    rmind 	}
    218  1.6    rmind 	kmem_free(rpset, sizeof(npf_rprocset_t));
    219  1.6    rmind }
    220  1.6    rmind 
    221  1.6    rmind /*
    222  1.6    rmind  * npf_rproc_lookup: find a rule procedure by the name.
    223  1.6    rmind  */
    224  1.6    rmind npf_rproc_t *
    225  1.6    rmind npf_rprocset_lookup(npf_rprocset_t *rpset, const char *name)
    226  1.6    rmind {
    227  1.6    rmind 	npf_rproc_t *rp;
    228  1.6    rmind 
    229  1.6    rmind 	LIST_FOREACH(rp, &rpset->rps_list, rp_entry) {
    230  1.6    rmind 		if (strncmp(rp->rp_name, name, RPROC_NAME_LEN) == 0)
    231  1.6    rmind 			break;
    232  1.6    rmind 	}
    233  1.6    rmind 	return rp;
    234  1.6    rmind }
    235  1.6    rmind 
    236  1.6    rmind /*
    237  1.6    rmind  * npf_rproc_insert: insert a new rule procedure into the set.
    238  1.6    rmind  */
    239  1.6    rmind void
    240  1.6    rmind npf_rprocset_insert(npf_rprocset_t *rpset, npf_rproc_t *rp)
    241  1.6    rmind {
    242  1.6    rmind 	LIST_INSERT_HEAD(&rpset->rps_list, rp, rp_entry);
    243  1.6    rmind }
    244  1.6    rmind 
    245  1.3    rmind /*
    246  1.3    rmind  * npf_rproc_create: construct a new rule procedure, lookup and associate
    247  1.3    rmind  * the extension calls with it.
    248  1.3    rmind  */
    249  1.1    rmind npf_rproc_t *
    250  1.1    rmind npf_rproc_create(prop_dictionary_t rpdict)
    251  1.1    rmind {
    252  1.3    rmind 	const char *name;
    253  1.1    rmind 	npf_rproc_t *rp;
    254  1.3    rmind 
    255  1.3    rmind 	if (!prop_dictionary_get_cstring_nocopy(rpdict, "name", &name)) {
    256  1.3    rmind 		return NULL;
    257  1.3    rmind 	}
    258  1.1    rmind 
    259  1.2    rmind 	rp = kmem_intr_zalloc(sizeof(npf_rproc_t), KM_SLEEP);
    260  1.1    rmind 	rp->rp_refcnt = 1;
    261  1.1    rmind 
    262  1.3    rmind 	strlcpy(rp->rp_name, name, RPROC_NAME_LEN);
    263  1.1    rmind 	prop_dictionary_get_uint32(rpdict, "flags", &rp->rp_flags);
    264  1.1    rmind 	return rp;
    265  1.1    rmind }
    266  1.1    rmind 
    267  1.3    rmind /*
    268  1.3    rmind  * npf_rproc_acquire: acquire the reference on the rule procedure.
    269  1.3    rmind  */
    270  1.1    rmind void
    271  1.1    rmind npf_rproc_acquire(npf_rproc_t *rp)
    272  1.1    rmind {
    273  1.1    rmind 	atomic_inc_uint(&rp->rp_refcnt);
    274  1.1    rmind }
    275  1.1    rmind 
    276  1.3    rmind /*
    277  1.3    rmind  * npf_rproc_release: drop the reference count and destroy the rule
    278  1.3    rmind  * procedure on the last reference.
    279  1.3    rmind  */
    280  1.1    rmind void
    281  1.1    rmind npf_rproc_release(npf_rproc_t *rp)
    282  1.1    rmind {
    283  1.1    rmind 
    284  1.1    rmind 	KASSERT(rp->rp_refcnt > 0);
    285  1.1    rmind 	if (atomic_dec_uint_nv(&rp->rp_refcnt) != 0) {
    286  1.1    rmind 		return;
    287  1.1    rmind 	}
    288  1.3    rmind 	/* XXXintr */
    289  1.3    rmind 	for (unsigned i = 0; i < rp->rp_ext_count; i++) {
    290  1.3    rmind 		npf_ext_t *ext = rp->rp_ext[i];
    291  1.3    rmind 		const npf_ext_ops_t *extops = ext->ext_ops;
    292  1.3    rmind 
    293  1.3    rmind 		extops->dtor(rp, rp->rp_ext_meta[i]);
    294  1.3    rmind 		atomic_dec_uint(&ext->ext_refcnt);
    295  1.3    rmind 	}
    296  1.2    rmind 	kmem_intr_free(rp, sizeof(npf_rproc_t));
    297  1.1    rmind }
    298  1.1    rmind 
    299  1.1    rmind void
    300  1.3    rmind npf_rproc_assign(npf_rproc_t *rp, void *params)
    301  1.1    rmind {
    302  1.3    rmind 	unsigned i = rp->rp_ext_count;
    303  1.3    rmind 
    304  1.3    rmind 	/* Note: params may be NULL. */
    305  1.3    rmind 	KASSERT(i < RPROC_EXT_COUNT);
    306  1.3    rmind 	rp->rp_ext_meta[i] = params;
    307  1.3    rmind }
    308  1.3    rmind 
    309  1.3    rmind /*
    310  1.3    rmind  * npf_rproc_run: run the rule procedure by executing each extension call.
    311  1.3    rmind  *
    312  1.3    rmind  * => Reference on the rule procedure must be held.
    313  1.3    rmind  */
    314  1.3    rmind void
    315  1.3    rmind npf_rproc_run(npf_cache_t *npc, nbuf_t *nbuf, npf_rproc_t *rp, int *decision)
    316  1.3    rmind {
    317  1.3    rmind 	const unsigned extcount = rp->rp_ext_count;
    318  1.1    rmind 
    319  1.5    rmind 	KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
    320  1.1    rmind 	KASSERT(rp->rp_refcnt > 0);
    321  1.1    rmind 
    322  1.3    rmind 	for (unsigned i = 0; i < extcount; i++) {
    323  1.3    rmind 		const npf_ext_t *ext = rp->rp_ext[i];
    324  1.3    rmind 		const npf_ext_ops_t *extops = ext->ext_ops;
    325  1.1    rmind 
    326  1.3    rmind 		KASSERT(ext->ext_refcnt > 0);
    327  1.3    rmind 		extops->proc(npc, nbuf, rp->rp_ext_meta[i], decision);
    328  1.5    rmind 
    329  1.5    rmind 		if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
    330  1.5    rmind 			npf_recache(npc, nbuf);
    331  1.5    rmind 		}
    332  1.1    rmind 	}
    333  1.1    rmind }
    334