]> Joshua Wise's Git repositories - netwatch.git/blob - net/net.c
Move elfload into tools/
[netwatch.git] / net / net.c
1 /* net.c
2  * Top-level network glue code
3  * NetWatch system management mode administration console
4  *
5  * Copyright (c) 2008 Jacob Potter and Joshua Wise.  All rights reserved.
6  * This program is free software; you can redistribute and/or modify it under
7  * the terms found in the file LICENSE in the root of this source tree.
8  *
9  */
10
11 #include <pci.h>
12 #include <smram.h>
13 #include <pci-bother.h>
14 #include <output.h>
15 #include <minilib.h>
16 #include <tables.h>
17 #include <lwip/init.h>
18 #include "net.h"
19
20 #include "lwip/opt.h"
21 #include "lwip/def.h"
22 #include "lwip/mem.h"
23 #include "lwip/pbuf.h"
24 #include "lwip/sys.h"
25 #include <lwip/stats.h>
26 #include <lwip/snmp.h>
27 #include <lwip/dhcp.h>
28 #include <lwip/tcp.h>
29 #include "netif/etharp.h"
30 #include "netif/ppp_oe.h"
31
32 static struct nic *_nic = 0x0;
33 static struct netif _netif;
34
35 extern struct pci_driver a3c90x_driver;
36
37 void eth_recv(struct nic *nic, struct pbuf *p)
38 {
39         struct eth_hdr *ethhdr;
40         
41         LINK_STATS_INC(link.recv);
42         
43         ethhdr = p->payload;
44         
45         switch (htons(ethhdr->type)) {
46         case ETHTYPE_IP:
47         case ETHTYPE_ARP:
48                 if (_netif.input(p, &_netif) != ERR_OK)
49                 {
50                         LWIP_DEBUGF(NETIF_DEBUG, ("netdev_input: IP input error\n"));
51                         pbuf_free(p);
52                 }
53                 break;
54                 
55         default:
56                 outputf("Unhandled packet type %04x input", ethhdr->type);
57                 pbuf_free(p);
58                 break;
59         }
60 }
61
62 void eth_poll()
63 {
64         static int ticks = 0;
65         int i = 15;     /* Don't process more than 15 packets at a time; we don't want the host to get TOO badly slowed down... */
66         
67         if (!_nic)
68                 return;
69         
70         smram_tseg_set_state(SMRAM_TSEG_OPEN);
71         
72         if ((ticks % 1000) == 0)        /* About a minute */
73                 dhcp_coarse_tmr();
74         if ((ticks % 8) == 0)   /* About 500msec*/
75                 dhcp_fine_tmr();
76         if ((ticks % 4) == 0)   /* About 250msec*/
77                 tcp_tmr();
78         ticks++;
79
80         while (i > 0)
81         {
82                 int n = _nic->recv(_nic);
83                 i -= n;
84                 if (n == 0)
85                         break;
86         }
87 }
88
89 static err_t _transmit(struct netif *netif, struct pbuf *p)
90 {
91         struct nic *nic = netif->state;
92
93 //      outputf("NIC: Transmit packet");
94
95         nic->transmit(nic, p);
96
97         LINK_STATS_INC(link.xmit);
98
99         return ERR_OK;
100 }
101
102 static err_t _init(struct netif *netif)
103 {
104         struct nic *nic = netif->state;
105         
106         LWIP_ASSERT("netif != NULL", (netif != NULL));
107                 
108 #if LWIP_NETIF_HOSTNAME
109         netif->hostname = "netwatch";
110 #endif
111         
112         NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
113
114         netif->name[0] = 'e';
115         netif->name[1] = 'n';
116         netif->output = etharp_output;
117         netif->linkoutput = _transmit;
118         
119         memcpy(netif->hwaddr, nic->hwaddr, 6);
120         netif->mtu = 1500;
121         netif->hwaddr_len = ETHARP_HWADDR_LEN;
122         netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
123         
124         return ERR_OK;
125 }
126
127 int eth_register(struct nic *nic)
128 {
129         static struct ip_addr ipa = { 0 } , netmask = { 0 } , gw = { 0 };
130         
131         if (_nic)
132                 return -1;
133         netif_add(&_netif, &ipa, &netmask, &gw, (void*)nic, _init, ethernet_input);
134         netif_set_default(&_netif);
135         netif_set_up(&_netif);
136         dhcp_start(&_netif);
137         _nic = nic;
138         return 0;
139 }
140
141 typedef void(*thunk_t)();
142
143 TABLE(thunk_t, protocols);
144
145 void eth_init()
146 {
147         int i;
148         
149         /* Required for DMA to work. :( */
150         smram_tseg_set_state(SMRAM_TSEG_OPEN);
151
152         lwip_init();
153
154         for (i = 0; i < TABLE_LENGTH(protocols); i++)
155                 protocols_table[i]();
156 }
This page took 0.029342 seconds and 4 git commands to generate.