-
Notifications
You must be signed in to change notification settings - Fork 1
/
device_driver.cc
215 lines (184 loc) · 4.93 KB
/
device_driver.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
extern "C" {
#include <libnet.h>
}
#include "config.h"
#include "error.h"
#include "ethernet.h"
#include "raw_ethernet_packet_buffer.h"
#include "debug.h"
#define DEBUG_SEND 0
#define DEBUG_RECV 0
int MyISR(int device, int service);
EthernetConfig myconfig={0,0,MyISR};
RawEthernetPacketBuffer *input_queue, *output_queue;
void usage()
{
fprintf(stderr,"usage: device_driver input_queue_size output_queue_size\n");
}
int ReceiveNextPacket()
{
RawEthernetPacket packet;
int rc;
DEBUGPRINTF(5,"ReceiveNextPacket\n");
if (EthernetGetNextPacket(&myconfig,&packet)<0) {
fprintf(stderr,"Can't get next packet!\n");
return -1;
}
#if DEBUG_RECV
cerr << "device: received packet :" << packet<<"\n";
#endif
rc=input_queue->PushPacket(&packet);
switch (rc) {
case PACKETBUFFER_OK:
DEBUGPRINTF(5,"Pushed packet into input queue\n");
break;
case PACKETBUFFER_FULL:
DEBUGPRINTF(5,"Packet discarded - input queue is full\n");
break;
default:
DEBUGPRINTF(5,"Unknown packet_buffer error\n");
break;
}
return 0;
}
int SendNextPacket()
{
RawEthernetPacket packet;
int rc;
DEBUGPRINTF(5,"SendNextPacket\n");
rc=output_queue->PullPacket(&packet);
switch (rc) {
case PACKETBUFFER_OK:
DEBUGPRINTF(5,"Initsending next outgoing packet\n");
#if DEBUG_SEND
cerr << "device: sending packet :" << packet <<"\n";
#endif
if (EthernetInitiateSend(&myconfig,&packet)!=0) {
DEBUGPRINTF(5,"Initsend failed: packet dropped\n");
cerr << "device_driver: send failed\n";
}
break;
case PACKETBUFFER_EMPTY:
DEBUGPRINTF(5,"No outgoing packets remain\n");
break;
default:
DEBUGPRINTF(5,"Unknown packet_buffer error\n");
break;
}
return 0;
}
int MyISR(int device, int service)
{
assert(device==myconfig.device);
switch (service) {
case ETHERNET_SERVICE_PACKET_READY:
DEBUGPRINTF(5,"New packet available\n");
return ReceiveNextPacket();
break;
case ETHERNET_SERVICE_OUTPUT_BUFFER_FULL:
DEBUGPRINTF(5,"Ethernet output buffer full, packet dropped\n");
return 0;
break;
case ETHERNET_SERVICE_DMA_DONE:
DEBUGPRINTF(5,"Outgoing DMA done\n");
return SendNextPacket();
break;
case ETHERNET_SERVICE_DMA_FAIL:
DEBUGPRINTF(5,"Outgoing DMA FAILED - packet dropped\n");
return SendNextPacket();
break;
default:
DEBUGPRINTF(5,"Unknown service request\n");
return -1;
break;
}
return 0;
}
int main(int argc, char *argv[])
{
int rc;
int input_queue_size, output_queue_size;
int muxin, muxout;
fd_set input, output;
sigset_t sigset;
RawEthernetPacket packet;
if (argc!=3) {
usage();
exit(-1);
}
input_queue_size=atoi(argv[1]);
output_queue_size=atoi(argv[2]);
input_queue=new RawEthernetPacketBuffer(input_queue_size);
output_queue= new RawEthernetPacketBuffer(output_queue_size);
if (input_queue==NULL || output_queue==NULL) {
DEBUGPRINTF(5,"ISR: Can't allocate memory for queues\n");
exit(-1);
}
muxout = open(ether2mux_fifo_name,O_WRONLY);
muxin = open(mux2ether_fifo_name,O_RDONLY);
EthernetStartup(&myconfig);
cerr << "device_driver operating\n";
int maxfd=0;
while (1) {
FD_ZERO(&input);
FD_ZERO(&output);
FD_SET(muxin,&input); maxfd=muxin;
if (input_queue->Numitems()>0) {
FD_SET(muxout,&output); maxfd=MAX(maxfd,muxout);
}
rc = select(maxfd+1,&input,&output,0,0);
DEBUGPRINTF(5,"select done rc=%d\n",rc);
if (rc<0) {
if (errno==EINTR) {
continue;
} else {
DEBUGPRINTF(5,"Unexpected error in select\n");
exit(-1);
}
} else if (rc==0) {
DEBUGPRINTF(5, "Unexpected timeout in select\n");
exit(-1);
} else if (rc>0) {
DEBUGPRINTF(5,"some fds are available...\n",rc);
sigemptyset(&sigset);
sigaddset(&sigset,ETHERNET_SIGNAL);
sigprocmask(SIG_BLOCK,&sigset,0);
/* OK, now we are in critical section */
if (FD_ISSET(muxout,&output)) {
DEBUGPRINTF(5,"ISR about to output next packet to mux\n");
/* we can output the next packet now, if there is one */
if (input_queue->PullPacket(&packet)==PACKETBUFFER_OK) {
packet.Serialize(muxout);
#if DEBUG_RECV
cerr << "New input packet: "<<packet<<"\n";
#endif
} else {
DEBUGPRINTF(5,"No packets available, so ISR did not output one\n");
}
}
if (FD_ISSET(muxin,&input)) {
DEBUGPRINTF(3,"ISR about to input next packet from mux\n");
/* we can input the next packet now */
packet.Unserialize(muxin);
#if DEBUG_SEND
cerr << "New output packet: "<<packet<<"\n";
#endif
if (output_queue->PushPacket(&packet)!=PACKETBUFFER_OK) {
DEBUGPRINTF(3,"Ouput queue full, packet dropped\n");
}
/* Now see if we have to start up the packet send */
if (output_queue->Numitems()>0) {
SendNextPacket();
}
}
/* finish critical section */
sigprocmask(SIG_UNBLOCK,&sigset,0);
}
}
return 0;
}