akaros/kern/src/net/eipconv.c
<<
>>
Prefs
   1/* Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
   2 * Portions Copyright © 1997-1999 Vita Nuova Limited
   3 * Portions Copyright © 2000-2007 Vita Nuova Holdings Limited
   4 *                                (www.vitanuova.com)
   5 * Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
   6 *
   7 * Modified for the Akaros operating system:
   8 * Copyright (c) 2013-2014 The Regents of the University of California
   9 * Copyright (c) 2013-2015 Google Inc.
  10 *
  11 * Permission is hereby granted, free of charge, to any person obtaining a copy
  12 * of this software and associated documentation files (the "Software"), to deal
  13 * in the Software without restriction, including without limitation the rights
  14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15 * copies of the Software, and to permit persons to whom the Software is
  16 * furnished to do so, subject to the following conditions:
  17 *
  18 * The above copyright notice and this permission notice shall be included in
  19 * all copies or substantial portions of the Software.
  20 *
  21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27 * SOFTWARE. */
  28
  29#include <slab.h>
  30#include <kmalloc.h>
  31#include <kref.h>
  32#include <string.h>
  33#include <stdio.h>
  34#include <assert.h>
  35#include <error.h>
  36#include <cpio.h>
  37#include <pmap.h>
  38#include <smp.h>
  39#include <net/ip.h>
  40
  41enum {
  42        Isprefix = 16,
  43};
  44
  45uint8_t prefixvals[256] = {
  46        [0x00] 0 | Isprefix,
  47        [0x80] 1 | Isprefix,
  48        [0xC0] 2 | Isprefix,
  49        [0xE0] 3 | Isprefix,
  50        [0xF0] 4 | Isprefix,
  51        [0xF8] 5 | Isprefix,
  52        [0xFC] 6 | Isprefix,
  53        [0xFE] 7 | Isprefix,
  54        [0xFF] 8 | Isprefix,
  55};
  56
  57static char *efmt = "%02x:%02x:%02x:%02x:%02x:%02x";
  58static char *ifmt = "%d.%d.%d.%d";
  59
  60void printemac(void (*putch) (int, void **), void **putdat, uint8_t * mac)
  61{
  62        printfmt(putch, putdat, efmt, mac[0], mac[1], mac[2], mac[3], mac[4],
  63                         mac[5]);
  64}
  65
  66void printip(void (*putch) (int, void **), void **putdat, uint8_t * ip)
  67{
  68        int i, j, eln, eli;
  69        uint16_t s;
  70        if (memcmp(ip, v4prefix, 12) == 0)
  71                printfmt(putch, putdat, ifmt, ip[12], ip[13], ip[14], ip[15]);
  72        else {
  73                /* find longest elision */
  74                eln = eli = -1;
  75                for (i = 0; i < 16; i += 2) {
  76                        for (j = i; j < 16; j += 2)
  77                                if (ip[j] != 0 || ip[j + 1] != 0)
  78                                        break;
  79                        if (j > i && j - i > eln) {
  80                                eli = i;
  81                                eln = j - i;
  82                        }
  83                }
  84
  85                /* print with possible elision */
  86                for (i = 0; i < 16; i += 2) {
  87                        if (i == eli) {
  88                                /* not sure what to do ... we don't get
  89                                 * the number of bytes back from printing.
  90                                 */
  91                                printfmt(putch, putdat, "::");
  92                                i += eln;
  93                                if (i >= 16)
  94                                        break;
  95                        } else if (i != 0)
  96                                printfmt(putch, putdat, ":");
  97
  98                        s = (ip[i] << 8) + ip[i + 1];
  99                        printfmt(putch, putdat, "0x%x", s);
 100                }
 101        }
 102}
 103
 104void printipv4(void (*putch) (int, void **), void **putdat, uint8_t * p)
 105{
 106        printfmt(putch, putdat, ifmt, p[0], p[1], p[2], p[3]);
 107}
 108
 109void printipmask(void (*putch) (int, void **), void **putdat, uint8_t * ip)
 110{
 111        int i, j, n;
 112        /* look for a prefix mask */
 113        for (i = 0; i < 16; i++)
 114                if (ip[i] != 0xff)
 115                        break;
 116        if (i < 16) {
 117                if ((prefixvals[ip[i]] & Isprefix) == 0) {
 118                        printip(putch, putdat, ip);
 119                        return;
 120                }
 121                for (j = i + 1; j < 16; j++)
 122                        if (ip[j] != 0) {
 123                                printip(putch, putdat, ip);
 124                                return;
 125                        }
 126                n = 8 * i + (prefixvals[ip[i]] & ~Isprefix);
 127        } else
 128                n = 8 * 16;
 129
 130        /* got one, use /xx format */
 131        printfmt(putch, putdat, "/%d", n);
 132}
 133
 134void printqid(void (*putch) (int, void **), void **putdat, struct qid *q)
 135{
 136        printfmt(putch, putdat, "{path:%p,type:%02x,vers:%p}",
 137                 q->path, q->type, q->vers);
 138
 139}
 140
 141void printcname(void (*putch) (int, void **), void **putdat, struct cname *c)
 142{
 143        if (c)
 144                printfmt(putch, putdat, "{ref %d, alen %d, len %d, s %s}",
 145                        kref_refcnt(&c->ref), c->alen, c->len, c->s);
 146}
 147
 148void printchan(void (*putch) (int, void **), void **putdat, struct chan *c)
 149{
 150        if (!c)
 151                return;
 152        printfmt(putch, putdat, "(%p): ", c);
 153        printfmt(putch, putdat, "%slocked ", spin_locked(&c->lock) ? "":"un");
 154        printfmt(putch, putdat, "refs %p ", kref_refcnt(&c->ref));
 155//      printfmt(putch, putdat, "%p ", struct chan *next,
 156//      printfmt(putch, putdat, "%p ", struct chan *link,
 157        printfmt(putch, putdat, "off %p ", c->offset);
 158        printfmt(putch, putdat, "type %p ", c->type);
 159        if (c->type != -1)
 160                printfmt(putch, putdat, "(#%s) ", devtab[c->type].name);
 161        printfmt(putch, putdat, "dev %p ", c->dev);
 162        printfmt(putch, putdat, "mode %p ", c->mode);
 163        printfmt(putch, putdat, "flag %p ", c->flag);
 164        printfmt(putch, putdat, "qid");
 165        printqid(putch, putdat, &c->qid);
 166        printfmt(putch, putdat, " fid %p ", c->fid);
 167        printfmt(putch, putdat, "iounit %p ", c->iounit);
 168        printfmt(putch, putdat, "umh %p ", c->umh);
 169        printfmt(putch, putdat, "umc %p ", c->umc);
 170//      printfmt(putch, putdat, "%p ", qlock_t umqlock,
 171        printfmt(putch, putdat, "uri %p ", c->uri);
 172        printfmt(putch, putdat, "dri %p ", c->dri);
 173        printfmt(putch, putdat, "mountid %p ", c->mountid);
 174        printfmt(putch, putdat, "mntcache %p ", c->mcp);
 175        printfmt(putch, putdat, "mux %p ", c->mux);
 176        if (c->mux && c->mux->c) 
 177                printfmt(putch, putdat, "mux->c %p ", c->mux->c);
 178        printfmt(putch, putdat, "aux %p ", c->aux);
 179        printfmt(putch, putdat, "mchan %p ", c->mchan);
 180        printfmt(putch, putdat, "mqid %p ");
 181        printqid(putch, putdat, &c->mqid);
 182        printfmt(putch, putdat, " cname ");
 183        printcname(putch, putdat, c->name);
 184        printfmt(putch, putdat, " ateof %p ", c->ateof);
 185        printfmt(putch, putdat, "buf %p ", c->buf);
 186        printfmt(putch, putdat, "bufused %p ", c->bufused);
 187}
 188
 189static uint8_t testvec[11][16] = {
 190        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 1, 3, 4, 5,},
 191        {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 192         0xff, 0xff, 0xff, 0xff,},
 193        {0xff, 0xff, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
 194        {0xff, 0xff, 0xff, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
 195        {0xff, 0xff, 0xff, 0xff, 0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
 196        {0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
 197        {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
 198        {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 199         0xff, 0xff, 0xff, 0xff,},
 200        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
 201        {0, 0, 0, 0, 0, 0x11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
 202        {0, 0, 0, 0x11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12,},
 203};
 204
 205/* handy dandy test function. When in doubt, you can call this from the monitor.
 206 * I doubt we want this long term.
 207 * Google 'remove before flight'.
 208 */
 209void testeip(void)
 210{
 211        int i;
 212        for (i = 0; i < 11; i++)
 213                printk("%I\n", &testvec[i]);
 214
 215}
 216