netif.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. typedef struct Etherpkt Etherpkt;
  10. typedef struct Netaddr Netaddr;
  11. typedef struct Netfile Netfile;
  12. typedef struct Netif Netif;
  13. enum
  14. {
  15. Nmaxaddr= 64,
  16. Nmhash= 31,
  17. Ncloneqid= 1,
  18. Naddrqid,
  19. N2ndqid,
  20. N3rdqid,
  21. Ndataqid,
  22. Nctlqid,
  23. Nstatqid,
  24. Ntypeqid,
  25. Nifstatqid,
  26. Nmtuqid,
  27. };
  28. /*
  29. * Macros to manage Qid's used for multiplexed devices
  30. */
  31. #define NETTYPE(x) (((uint32_t)x)&0x1f)
  32. #define NETID(x) ((((uint32_t)x))>>5)
  33. #define NETQID(i,t) ((((uint32_t)i)<<5)|(t))
  34. /*
  35. * one per multiplexed connection
  36. */
  37. struct Netfile
  38. {
  39. QLock q;
  40. int inuse;
  41. uint32_t mode;
  42. char owner[KNAMELEN];
  43. int type; /* multiplexor type */
  44. int prom; /* promiscuous mode */
  45. int scan; /* base station scanning interval */
  46. int bridge; /* bridge mode */
  47. int headersonly; /* headers only - no data */
  48. uint8_t maddr[8]; /* bitmask of multicast addresses requested */
  49. int nmaddr; /* number of multicast addresses */
  50. Queue* iq; /* input */
  51. };
  52. /*
  53. * a network address
  54. */
  55. struct Netaddr
  56. {
  57. Netaddr *next; /* allocation chain */
  58. Netaddr *hnext;
  59. uint8_t addr[Nmaxaddr];
  60. int ref;
  61. };
  62. /*
  63. * a network interface
  64. */
  65. struct Netif
  66. {
  67. QLock q;
  68. /* multiplexing */
  69. char name[KNAMELEN]; /* for top level directory */
  70. int nfile; /* max number of Netfiles */
  71. Netfile **f;
  72. /* about net */
  73. int limit; /* flow control */
  74. int alen; /* address length */
  75. int mbps; /* megabits per sec */
  76. int link; /* link status */
  77. int minmtu;
  78. int maxmtu;
  79. int mtu;
  80. uint8_t addr[Nmaxaddr];
  81. uint8_t bcast[Nmaxaddr];
  82. Netaddr *maddr; /* known multicast addresses */
  83. int nmaddr; /* number of known multicast addresses */
  84. Netaddr *mhash[Nmhash]; /* hash table of multicast addresses */
  85. int prom; /* number of promiscuous opens */
  86. int _scan; /* number of base station scanners */
  87. int all; /* number of -1 multiplexors */
  88. /* statistics */
  89. uint64_t misses;
  90. uint64_t inpackets;
  91. uint64_t outpackets;
  92. uint64_t crcs; /* input crc errors */
  93. uint64_t oerrs; /* output errors */
  94. uint64_t frames; /* framing errors */
  95. uint64_t overflows; /* packet overflows */
  96. uint64_t buffs; /* buffering errors */
  97. uint64_t soverflows; /* software overflow */
  98. /* routines for touching the hardware */
  99. void *arg;
  100. void (*promiscuous)(void*, int);
  101. void (*multicast)(void*, uint8_t*, int);
  102. int (*hwmtu)(void*, int); /* get/set mtu */
  103. void (*scanbs)(void*, uint); /* scan for base stations */
  104. };
  105. void netifinit(Netif*, char*, int, uint32_t);
  106. Walkqid* netifwalk(Netif*, Chan*, Chan*, char **, int);
  107. Chan* netifopen(Netif*, Chan*, int);
  108. void netifclose(Netif*, Chan*);
  109. int32_t netifread(Netif*, Chan*, void*, int32_t, int64_t);
  110. Block* netifbread(Netif*, Chan*, int32_t, int64_t);
  111. int32_t netifwrite(Netif*, Chan*, void*, int32_t);
  112. int32_t netifwstat(Netif*, Chan*, unsigned char*, int32_t);
  113. int32_t netifstat(Netif*, Chan*, unsigned char*, int32_t);
  114. int activemulti(Netif*, unsigned char*, int);
  115. /*
  116. * Ethernet specific
  117. */
  118. enum
  119. {
  120. Eaddrlen= 6,
  121. ETHERMINTU = 60, /* minimum transmit size */
  122. ETHERMAXTU = 1514, /* maximum transmit size */
  123. ETHERHDRSIZE = 14, /* size of an ethernet header */
  124. /* ethernet packet types */
  125. ETARP = 0x0806,
  126. ETIP4 = 0x0800,
  127. ETIP6 = 0x86DD,
  128. };
  129. struct Etherpkt
  130. {
  131. uint8_t d[Eaddrlen];
  132. uint8_t s[Eaddrlen];
  133. uint8_t type[2];
  134. uint8_t data[1500];
  135. };