Print. Add printing for qids, cnams, and chans
[akaros.git] / kern / src / net / netlog.c
index 6126de9..ee33e24 100644 (file)
@@ -14,7 +14,7 @@
 #include <ip.h>
 
 enum {
-       Nlog            = 4*1024,
+       Nlog = 4 * 1024,
 };
 
 /*
@@ -22,49 +22,47 @@ enum {
  */
 struct Netlog {
        spinlock_t lock;
-       int     opens;
-       char*   buf;
-       char    *end;
-       char    *rptr;
-       int     len;
+       int opens;
+       char *buf;
+       char *end;
+       char *rptr;
+       int len;
 
-       int     logmask;                        /* mask of things to debug */
-       uint8_t iponly[IPaddrlen];              /* ip address to print debugging for */
-       int     iponlyset;
+       int logmask;                            /* mask of things to debug */
+       uint8_t iponly[IPaddrlen];      /* ip address to print debugging for */
+       int iponlyset;
 
        qlock_t qlock;
        struct rendez r;
 };
 
 typedef struct Netlogflag {
-       char*   name;
-       int     mask;
+       char *name;
+       int mask;
 } Netlogflag;
 
-static Netlogflag flags[] =
-{
-       { "ppp",        Logppp, },
-       { "ip",         Logip, },
-       { "fs",         Logfs, },
-       { "tcp",        Logtcp, },
-       { "il",         Logil, },
-       { "icmp",       Logicmp, },
-       { "udp",        Logudp, },
-       { "compress",   Logcompress, },
-       { "ilmsg",      Logil|Logilmsg, },
-       { "gre",        Loggre, },
-       { "tcpwin",     Logtcp|Logtcpwin, },
-       { "tcprxmt",    Logtcp|Logtcprxmt, },
-       { "udpmsg",     Logudp|Logudpmsg, },
-       { "ipmsg",      Logip|Logipmsg, },
-       { "esp",        Logesp, },
-       { NULL,         0, },
+static Netlogflag flags[] = {
+       {"ppp", Logppp,},
+       {"ip", Logip,},
+       {"fs", Logfs,},
+       {"tcp", Logtcp,},
+       {"il", Logil,},
+       {"icmp", Logicmp,},
+       {"udp", Logudp,},
+       {"compress", Logcompress,},
+       {"ilmsg", Logil | Logilmsg,},
+       {"gre", Loggre,},
+       {"tcpwin", Logtcp | Logtcpwin,},
+       {"tcprxmt", Logtcp | Logtcprxmt,},
+       {"udpmsg", Logudp | Logudpmsg,},
+       {"ipmsg", Logip | Logipmsg,},
+       {"esp", Logesp,},
+       {NULL, 0,},
 };
 
 char Ebadnetctl[] = "too few arguments for netlog control message";
 
-enum
-{
+enum {
        CMset,
        CMclear,
        CMonly,
@@ -72,28 +70,29 @@ enum
 
 static
 struct cmdtab routecmd[] = {
-       {CMset,         "set",          0},
-       {CMclear,       "clear",        0},
-       {CMonly,                "only",         0},
+       {CMset, "set", 0},
+       {CMclear, "clear", 0},
+       {CMonly, "only", 0},
 };
 
-void
-netloginit(struct Fs *f)
+void netloginit(struct Fs *f)
 {
        f->alog = kzmalloc(sizeof(struct Netlog), 0);
+       spinlock_init(&f->alog->lock);
+       qlock_init(&f->alog->qlock);
+       rendez_init(&f->alog->r);
 }
 
-void
-netlogopen(struct Fs *f)
+void netlogopen(struct Fs *f)
 {
-       ERRSTACK(2);
+       ERRSTACK(1);
        spin_lock(&f->alog->lock);
-       if(waserror()){
+       if (waserror()) {
                spin_unlock(&f->alog->lock);
                nexterror();
        }
-       if(f->alog->opens == 0){
-               if(f->alog->buf == NULL)
+       if (f->alog->opens == 0) {
+               if (f->alog->buf == NULL)
                        f->alog->buf = kzmalloc(Nlog, 0);
                f->alog->rptr = f->alog->buf;
                f->alog->end = f->alog->buf + Nlog;
@@ -103,17 +102,16 @@ netlogopen(struct Fs *f)
        poperror();
 }
 
-void
-netlogclose(struct Fs *f)
+void netlogclose(struct Fs *f)
 {
-       ERRSTACK(2);
+       ERRSTACK(1);
        spin_lock(&f->alog->lock);
-       if(waserror()){
+       if (waserror()) {
                spin_unlock(&f->alog->lock);
                nexterror();
        }
        f->alog->opens--;
-       if(f->alog->opens == 0){
+       if (f->alog->opens == 0) {
                kfree(f->alog->buf);
                f->alog->buf = NULL;
        }
@@ -121,49 +119,46 @@ netlogclose(struct Fs *f)
        poperror();
 }
 
-static int
-netlogready(void *a)
+static int netlogready(void *a)
 {
        struct Fs *f = a;
 
        return f->alog->len;
 }
 
-long
-netlogread(struct Fs *f, void *a, uint32_t unused, long n)
+long netlogread(struct Fs *f, void *a, uint32_t unused, long n)
 {
-       ERRSTACK(2);
+       ERRSTACK(1);
        int i, d;
        char *p, *rptr;
 
        qlock(&f->alog->qlock);
-       if(waserror()){
+       if (waserror()) {
                qunlock(&f->alog->qlock);
                nexterror();
        }
 
-       for(;;){
+       for (;;) {
                spin_lock(&f->alog->lock);
-               if(f->alog->len){
-                       if(n > f->alog->len)
+               if (f->alog->len) {
+                       if (n > f->alog->len)
                                n = f->alog->len;
                        d = 0;
                        rptr = f->alog->rptr;
                        f->alog->rptr += n;
-                       if(f->alog->rptr >= f->alog->end){
+                       if (f->alog->rptr >= f->alog->end) {
                                d = f->alog->rptr - f->alog->end;
                                f->alog->rptr = f->alog->buf + d;
                        }
                        f->alog->len -= n;
                        spin_unlock(&f->alog->lock);
 
-                       i = n-d;
+                       i = n - d;
                        p = a;
                        memmove(p, rptr, i);
-                       memmove(p+i, f->alog->buf, d);
+                       memmove(p + i, f->alog->buf, d);
                        break;
-               }
-               else
+               } else
                        spin_unlock(&f->alog->lock);
 
                rendez_sleep(&f->alog->r, netlogready, f);
@@ -175,55 +170,55 @@ netlogread(struct Fs *f, void *a, uint32_t unused, long n)
        return n;
 }
 
-void
-netlogctl(struct Fs *f, char* s, int n)
+void netlogctl(struct Fs *f, char *s, int n)
 {
-       ERRSTACK(2);
+       ERRSTACK(1);
        int i, set = 0;
        Netlogflag *fp;
        struct cmdbuf *cb;
        struct cmdtab *ct;
 
        cb = parsecmd(s, n);
-       if(waserror()){
+       if (waserror()) {
                kfree(cb);
                nexterror();
        }
 
-       if(cb->nf < 2)
+       if (cb->nf < 2)
                error(Ebadnetctl);
 
        ct = lookupcmd(cb, routecmd, ARRAY_SIZE(routecmd));
 
-       switch(ct->index){
-       case CMset:
-               set = 1;
-               break;
-
-       case CMclear:
-               set = 0;
-               break;
+       switch (ct->index) {
+               case CMset:
+                       set = 1;
+                       break;
 
-       case CMonly:
-               parseip(f->alog->iponly, cb->f[1]);
-               if(ipcmp(f->alog->iponly, IPnoaddr) == 0)
-                       f->alog->iponlyset = 0;
-               else
-                       f->alog->iponlyset = 1;
-               kfree(cb);
-               return;
+               case CMclear:
+                       set = 0;
+                       break;
 
-       default:
-               cmderror(cb, "unknown ip control message");
+               case CMonly:
+                       parseip(f->alog->iponly, cb->f[1]);
+                       if (ipcmp(f->alog->iponly, IPnoaddr) == 0)
+                               f->alog->iponlyset = 0;
+                       else
+                               f->alog->iponlyset = 1;
+                       kfree(cb);
+                       poperror();
+                       return;
+
+               default:
+                       cmderror(cb, "unknown ip control message");
        }
 
-       for(i = 1; i < cb->nf; i++){
-               for(fp = flags; fp->name; fp++)
-                       if(strcmp(fp->name, cb->f[i]) == 0)
+       for (i = 1; i < cb->nf; i++) {
+               for (fp = flags; fp->name; fp++)
+                       if (strcmp(fp->name, cb->f[i]) == 0)
                                break;
-               if(fp->name == NULL)
+               if (fp->name == NULL)
                        continue;
-               if(set)
+               if (set)
                        f->alog->logmask |= fp->mask;
                else
                        f->alog->logmask &= ~fp->mask;
@@ -233,17 +228,16 @@ netlogctl(struct Fs *f, char* s, int n)
        poperror();
 }
 
-void
-netlog(struct Fs *f, int mask, char *fmt, ...)
+void netlog(struct Fs *f, int mask, char *fmt, ...)
 {
        char buf[128], *t, *fp;
        int i, n;
        va_list arg;
 
-       if(!(f->alog->logmask & mask))
+       if (!(f->alog->logmask & mask))
                return;
 
-       if(f->alog->opens == 0)
+       if (f->alog->opens == 0)
                return;
 
        va_start(arg, fmt);
@@ -252,17 +246,17 @@ netlog(struct Fs *f, int mask, char *fmt, ...)
 
        spin_lock(&f->alog->lock);
        i = f->alog->len + n - Nlog;
-       if(i > 0){
+       if (i > 0) {
                f->alog->len -= i;
                f->alog->rptr += i;
-               if(f->alog->rptr >= f->alog->end)
+               if (f->alog->rptr >= f->alog->end)
                        f->alog->rptr = f->alog->buf + (f->alog->rptr - f->alog->end);
        }
        t = f->alog->rptr + f->alog->len;
        fp = buf;
        f->alog->len += n;
-       while(n-- > 0){
-               if(t >= f->alog->end)
+       while (n-- > 0) {
+               if (t >= f->alog->end)
                        t = f->alog->buf + (t - f->alog->end);
                *t++ = *fp++;
        }