inet_common.c revision aa92d85b088543197e9fb4594eb30d5215fca2c1
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * CDDL HEADER START
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The contents of this file are subject to the terms of the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Common Development and Distribution License (the "License").
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * You may not use this file except in compliance with the License.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * or http://www.opensolaris.org/os/licensing.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * See the License for the specific language governing permissions
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and limitations under the License.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * When distributing Covered Code, include this CDDL HEADER in each
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * If applicable, add the following below this CDDL HEADER, with the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * CDDL HEADER END
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Use is subject to license terms.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#pragma ident "%Z%%M% %I% %E% SMI"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Minor number allocation for various protocol modules.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/types.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/kmem.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/mutex.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/ddi.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/types.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/mkdev.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/param.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <inet/common.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowetypedef struct inet_arena {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe vmem_t *ineta_arena; /* Minor number arena */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe minor_t ineta_maxminor; /* max minor number in the arena */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe} inet_arena_t;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinet_minor_create(char *name, dev_t min_dev, dev_t max_dev, int kmflags)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe inet_arena_t *arena = kmem_alloc(sizeof (inet_arena_t), kmflags);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (arena != NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe arena->ineta_maxminor = max_dev;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe arena->ineta_arena = vmem_create(name,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void *)min_dev, arena->ineta_maxminor - min_dev + 1,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe 1, NULL, NULL, NULL, 1, kmflags | VMC_IDENTIFIER);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (arena->ineta_arena == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe kmem_free(arena, sizeof (inet_arena_t));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe arena = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (arena);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinet_minor_destroy(void *a)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe inet_arena_t *arena = (inet_arena_t *)a;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (arena != NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe vmem_destroy(arena->ineta_arena);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe kmem_free(arena, sizeof (inet_arena_t));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowedev_t
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinet_minor_alloc(void *arena)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return ((dev_t)vmem_alloc(((inet_arena_t *)arena)->ineta_arena,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe 1, VM_NOSLEEP));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinet_minor_free(void *arena, dev_t dev)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ASSERT((dev != OPENFAIL) && (dev != 0) && (dev <= MAXMIN));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe vmem_free(((inet_arena_t *)arena)->ineta_arena, (void *)dev, 1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * This function is used to free a message that has gone through
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * mi_copyin processing which modifies the M_IOCTL mblk's b_next
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and b_prev pointers. We use this function to set b_next/b_prev
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * to NULL and free them.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinet_freemsg(mblk_t *mp)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe mblk_t *bp = mp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (; bp != NULL; bp = bp->b_cont) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe bp->b_prev = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe bp->b_next = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe freemsg(mp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe