netlog.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "../ip/ip.h"
  16. enum {
  17. Nlog = 16*1024,
  18. };
  19. /*
  20. * action log
  21. */
  22. struct Netlog {
  23. Lock _lock;
  24. int opens;
  25. char* buf;
  26. char *end;
  27. char *rptr;
  28. int len;
  29. int logmask; /* mask of things to debug */
  30. uint8_t iponly[IPaddrlen]; /* ip address to print debugging for */
  31. int iponlyset;
  32. QLock ql;
  33. Rendez Rendez;
  34. };
  35. typedef struct Netlogflag {
  36. char* name;
  37. int mask;
  38. } Netlogflag;
  39. static Netlogflag flags[] =
  40. {
  41. { "ppp", Logppp, },
  42. { "ip", Logip, },
  43. { "fs", Logfs, },
  44. { "tcp", Logtcp, },
  45. { "icmp", Logicmp, },
  46. { "udp", Logudp, },
  47. { "compress", Logcompress, },
  48. { "gre", Loggre, },
  49. { "tcpwin", Logtcp|Logtcpwin, },
  50. { "tcprxmt", Logtcp|Logtcprxmt, },
  51. { "udpmsg", Logudp|Logudpmsg, },
  52. { "ipmsg", Logip|Logipmsg, },
  53. { "esp", Logesp, },
  54. { nil, 0, },
  55. };
  56. char Ebadnetctl[] = "too few arguments for netlog control message";
  57. enum
  58. {
  59. CMset,
  60. CMclear,
  61. CMonly,
  62. };
  63. static
  64. Cmdtab routecmd[] = {
  65. {CMset, "set", 0},
  66. {CMclear, "clear", 0},
  67. {CMonly, "only", 0},
  68. };
  69. void
  70. netloginit(Fs *f)
  71. {
  72. f->alog = smalloc(sizeof(Netlog));
  73. }
  74. void
  75. netlogopen(Fs *f)
  76. {
  77. Proc *up = externup();
  78. lock(&f->alog->_lock);
  79. if(waserror()){
  80. unlock(&f->alog->_lock);
  81. nexterror();
  82. }
  83. if(f->alog->opens == 0){
  84. if(f->alog->buf == nil)
  85. f->alog->buf = malloc(Nlog);
  86. if(f->alog->buf == nil)
  87. error(Enomem);
  88. f->alog->rptr = f->alog->buf;
  89. f->alog->end = f->alog->buf + Nlog;
  90. }
  91. f->alog->opens++;
  92. unlock(&f->alog->Rendez.l);
  93. poperror();
  94. }
  95. void
  96. netlogclose(Fs *f)
  97. {
  98. Proc *up = externup();
  99. lock(&f->alog->Rendez.l);
  100. if(waserror()){
  101. unlock(&f->alog->Rendez.l);
  102. nexterror();
  103. }
  104. f->alog->opens--;
  105. if(f->alog->opens == 0){
  106. free(f->alog->buf);
  107. f->alog->buf = nil;
  108. }
  109. unlock(&f->alog->Rendez.l);
  110. poperror();
  111. }
  112. static int
  113. netlogready(void *a)
  114. {
  115. Fs *f = a;
  116. return f->alog->len;
  117. }
  118. int32_t
  119. netlogread(Fs *f, void *a, uint32_t u, int32_t n)
  120. {
  121. Proc *up = externup();
  122. int i, d;
  123. char *p, *rptr;
  124. qlock(&f->alog->ql);
  125. if(waserror()){
  126. qunlock(&f->alog->ql);
  127. nexterror();
  128. }
  129. for(;;){
  130. lock(&f->alog->_lock);
  131. if(f->alog->len){
  132. if(n > f->alog->len)
  133. n = f->alog->len;
  134. d = 0;
  135. rptr = f->alog->rptr;
  136. f->alog->rptr += n;
  137. if(f->alog->rptr >= f->alog->end){
  138. d = f->alog->rptr - f->alog->end;
  139. f->alog->rptr = f->alog->buf + d;
  140. }
  141. f->alog->len -= n;
  142. unlock(&f->alog->_lock);
  143. i = n-d;
  144. p = a;
  145. memmove(p, rptr, i);
  146. memmove(p+i, f->alog->buf, d);
  147. break;
  148. }
  149. else
  150. unlock(&f->alog->_lock);
  151. sleep(&f->alog->Rendez, netlogready, f);
  152. }
  153. qunlock(&f->alog->ql);
  154. poperror();
  155. return n;
  156. }
  157. void
  158. netlogctl(Fs *f, char* s, int n)
  159. {
  160. Proc *up = externup();
  161. int i, set;
  162. Netlogflag *fp;
  163. Cmdbuf *cb;
  164. Cmdtab *ct;
  165. cb = parsecmd(s, n);
  166. if(waserror()){
  167. free(cb);
  168. nexterror();
  169. }
  170. if(cb->nf < 2)
  171. error(Ebadnetctl);
  172. ct = lookupcmd(cb, routecmd, nelem(routecmd));
  173. SET(set);
  174. switch(ct->index){
  175. case CMset:
  176. set = 1;
  177. break;
  178. case CMclear:
  179. set = 0;
  180. break;
  181. case CMonly:
  182. parseip(f->alog->iponly, cb->f[1]);
  183. if(ipcmp(f->alog->iponly, IPnoaddr) == 0)
  184. f->alog->iponlyset = 0;
  185. else
  186. f->alog->iponlyset = 1;
  187. free(cb);
  188. poperror();
  189. return;
  190. default:
  191. cmderror(cb, "unknown netlog control message");
  192. }
  193. for(i = 1; i < cb->nf; i++){
  194. for(fp = flags; fp->name; fp++)
  195. if(strcmp(fp->name, cb->f[i]) == 0)
  196. break;
  197. if(fp->name == nil)
  198. continue;
  199. if(set)
  200. f->alog->logmask |= fp->mask;
  201. else
  202. f->alog->logmask &= ~fp->mask;
  203. }
  204. free(cb);
  205. poperror();
  206. }
  207. void
  208. netlog(Fs *f, int mask, char *fmt, ...)
  209. {
  210. char buf[256], *t, *fp;
  211. int i, n;
  212. va_list arg;
  213. if(!(f->alog->logmask & mask))
  214. return;
  215. if(f->alog->opens == 0)
  216. return;
  217. va_start(arg, fmt);
  218. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  219. va_end(arg);
  220. lock(&f->alog->_lock);
  221. i = f->alog->len + n - Nlog;
  222. if(i > 0){
  223. f->alog->len -= i;
  224. f->alog->rptr += i;
  225. if(f->alog->rptr >= f->alog->end)
  226. f->alog->rptr = f->alog->buf + (f->alog->rptr - f->alog->end);
  227. }
  228. t = f->alog->rptr + f->alog->len;
  229. fp = buf;
  230. f->alog->len += n;
  231. while(n-- > 0){
  232. if(t >= f->alog->end)
  233. t = f->alog->buf + (t - f->alog->end);
  234. *t++ = *fp++;
  235. }
  236. unlock(&f->alog->_lock);
  237. wakeup(&f->alog->Rendez);
  238. }