00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include <l4/ore/ore.h>
00062 #include <l4/log/l4log.h>
00063 #include <l4/util/util.h>
00064 #include <l4/sys/ipc.h>
00065
00066 #include <fcntl.h>
00067 #include <stdlib.h>
00068 #include <stdio.h>
00069 #include <string.h>
00070 #include <unistd.h>
00071 #include <sys/types.h>
00072 #include <sys/uio.h>
00073 #include <sys/socket.h>
00074
00075 #include "lwip/debug.h"
00076 #include "lwip/opt.h"
00077 #include "lwip/def.h"
00078 #include "lwip/ip.h"
00079 #include "lwip/mem.h"
00080 #include "lwip/pbuf.h"
00081 #include "lwip/sys.h"
00082 #include "netif/etharp.h"
00083 #include "netif/oreif.h"
00084
00085
00086 #define BPS 512000
00087 #define QUEUELEN 6
00088
00089 struct oreif {
00090 struct eth_addr *ethaddr;
00091 int fd;
00092 l4ore_config conf;
00093 l4_timeout_t timeout;
00094 };
00095
00096 #define CONFIG_OREDEV_TIMEOUT 500000
00097
00098 static void oreif_input(struct netif *netif);
00099 static void oreif_thread(void *data);
00100
00101
00102
00103 static void low_level_init(struct netif *netif)
00104 {
00105 struct oreif *oreif;
00106 char buf[100];
00107 unsigned char mac[6];
00108
00109 oreif = netif->state;
00110
00111
00112 oreif->conf = L4ORE_DEFAULT_CONFIG;
00113 oreif->conf.rw_broadcast = 1;
00114 oreif->fd = l4ore_open("eth0", mac, &oreif->conf);
00115 if (oreif->fd < 0) {
00116 LOG("Could not open connection to ORe: %d", oreif->fd);
00117 exit(1);
00118 }
00119
00120 LOG_MAC(1, mac);
00121
00122
00123 oreif->timeout = l4_timeout(L4_IPC_TIMEOUT_NEVER,
00124 l4util_micros2l4to(CONFIG_OREDEV_TIMEOUT));
00125
00126
00127 memcpy(&oreif->ethaddr->addr[0], &mac[0], 6);
00128
00129 LWIP_DEBUGF(OREIF_DEBUG, ("oreif_init: system(\"%s\");\n", buf));
00130 system(buf);
00131 sys_thread_new(oreif_thread, netif, DEFAULT_THREAD_PRIO);
00132 }
00133
00134 static err_t arp_out(struct netif *netif, struct pbuf *p,
00135 struct ip_addr *ipaddr)
00136 {
00137 return etharp_output(netif, ipaddr, p);
00138 }
00139
00140 static err_t low_level_output(struct netif *netif, struct pbuf *p)
00141 {
00142 struct pbuf *q;
00143 char buf[1514];
00144 char *bufptr;
00145 struct oreif *oreif;
00146
00147 oreif = netif->state;
00148
00149
00150 bufptr = &buf[0];
00151
00152 for(q = p; q != NULL; q = q->next) {
00153
00154
00155
00156
00157 memcpy(bufptr, q->payload, q->len);
00158 bufptr += q->len;
00159 }
00160
00161
00162 if(l4ore_send(oreif->fd, buf, p->tot_len) == -1) {
00163 perror("oreif: write");
00164 }
00165 return ERR_OK;
00166 }
00167
00168 static struct pbuf *low_level_input(struct oreif *oreif)
00169 {
00170 struct pbuf *p, *q;
00171 u16_t len;
00172 char buf[1514];
00173 char *bufptr;
00174 l4_size_t buf_size = sizeof(buf);
00175
00176
00177
00178 len = l4ore_recv_blocking(oreif->fd, (char**)&buf, &buf_size,
00179 oreif->timeout);
00180
00181
00182 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
00183
00184 if(p != NULL) {
00185
00186
00187 bufptr = &buf[0];
00188 for(q = p; q != NULL; q = q->next) {
00189
00190
00191
00192
00193 memcpy(q->payload, bufptr, q->len);
00194 bufptr += q->len;
00195 }
00196
00197 } else {
00198
00199 }
00200
00201 return p;
00202 }
00203
00204
00205 static void oreif_thread(void *arg)
00206 {
00207 struct netif *netif;
00208 struct oreif *oreif;
00209
00210 netif = arg;
00211 oreif = netif->state;
00212
00213 while(1) {
00214
00215 oreif_input(netif);
00216 }
00217 }
00218
00219
00220 static void oreif_input(struct netif *netif)
00221 {
00222 struct oreif *oreif;
00223 struct eth_hdr *ethhdr;
00224 struct pbuf *p;
00225
00226
00227 oreif = netif->state;
00228
00229 p = low_level_input(oreif);
00230
00231 if(p == NULL) {
00232 LWIP_DEBUGF(OREIF_DEBUG, ("oreif_input: low_level_input returned NULL\n"));
00233 return;
00234 }
00235 ethhdr = p->payload;
00236
00237 switch(htons(ethhdr->type)) {
00238 case ETHTYPE_IP:
00239 LWIP_DEBUGF(OREIF_DEBUG, ("oreif_input: IP packet\n"));
00240 #if 0
00241
00242
00243 etharp_ip_input(netif, p);
00244 #endif
00245 pbuf_header(p, -14);
00246 netif->input(p, netif);
00247 break;
00248 case ETHTYPE_ARP:
00249 LWIP_DEBUGF(OREIF_DEBUG, ("oreif_input: ARP packet\n"));
00250 etharp_arp_input(netif, oreif->ethaddr, p);
00251 break;
00252 default:
00253 pbuf_free(p);
00254 break;
00255 }
00256 }
00257
00258
00259 static void arp_timer(void *arg)
00260 {
00261 etharp_tmr();
00262 sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);
00263 }
00264
00265
00266
00267 err_t oreif_init(struct netif *netif)
00268 {
00269 struct oreif *oreif = mem_malloc(sizeof(struct oreif));
00270
00271 if (!oreif)
00272 return ERR_MEM;
00273
00274 netif->state = oreif;
00275 netif->name[0] = 'o';
00276 netif->name[1] = 'r';
00277 netif->output = arp_out;
00278 netif->linkoutput = low_level_output;
00279 netif->mtu = 1500;
00280
00281 netif->hwaddr_len = 6;
00282
00283 oreif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
00284 low_level_init(netif);
00285 etharp_init();
00286
00287 sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);
00288
00289 return ERR_OK;
00290 }
00291