-
Notifications
You must be signed in to change notification settings - Fork 0
/
boardctrld.cpp
286 lines (248 loc) · 6.95 KB
/
boardctrld.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// SPDX-License-Identifier: BSD-2-Clause
// Copyright (c) Matthew Naylor
// Board Control Daemon
// ====================
//
// Control FPGAs, and communicate with each FPGA JTAG UART, via a TCP socket.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <poll.h>
#include <pwd.h>
#include <errno.h>
#include <config.h>
#include "jtag/UARTBuffer.h"
#include "jtag/UART.h"
#include "PowerLink.h"
#include "BoardCtrl.h"
#include "SocketUtils.h"
#include "DebugLinkFormat.h"
// Constants
// ---------
// Default TCP port to listen on
#define TCP_PORT 10101
// Email address to use to report overheating
#define EMAIL_ADDR "[email protected]"
// SMTP server for email
#define SMTP_SERVER "ppsw.cam.ac.uk"
// Functions
// ---------
// Try to reduce privilege
void reducePrivilege()
{
if (getuid() == 0) {
passwd* entry = getpwnam("daemon");
if (entry) {
if (setgid(entry->pw_gid) != 0)
fprintf(stderr, "Unable to drop group privilege\n");
if (setuid(entry->pw_uid) != 0)
fprintf(stderr, "Unable to drop user privilege\n");
}
}
}
// Create listening socket
int createListener()
{
// Create socket
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("boardctrld: socket");
exit(EXIT_FAILURE);
}
// Set reuse-address socket option
int reuseAddr = 1;
int ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
&reuseAddr, sizeof(reuseAddr));
if (ret < 0) {
perror("setsockopt[SO_REUSEADDR]");
exit(EXIT_FAILURE);
}
// Bind socket
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockaddr_in));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockAddr.sin_port = htons(TCP_PORT);
ret = bind(sock, (const struct sockaddr *) &sockAddr,
sizeof(struct sockaddr_in));
if (ret == -1) {
perror("boardctrld: bind");
exit(EXIT_FAILURE);
}
// Listen for connections
ret = listen(sock, 1);
if (ret == -1) {
perror("boardctrld: listen");
exit(EXIT_FAILURE);
}
return sock;
}
// Send an email reporting overheating
bool reportOverheat(int linkId)
{
// Get host name
char hostname[256];
hostname[0] = '\0';
gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname)-1] = '\0';
// Send email
FILE* fp = popen("msmtp -f " EMAIL_ADDR
" --host=" SMTP_SERVER
" " EMAIL_ADDR, "w");
if (fp == NULL) return false;
fprintf(fp, "Subject: FPGA %i overheating on %s\n", linkId, hostname);
fclose(fp);
return true;
}
void server(int conn, int numBoards, UARTBuffer* uartLinks)
{
// Open each UART
#ifdef SIMULATE
// Worker boards
int count = 0;
for (int y = 0; y < TinselMeshYLenWithinBox; y++)
for (int x = 0; x < TinselMeshXLenWithinBox; x++) {
int boardId = (y<<TinselMeshXBitsWithinBox) + x;
uartLinks[count++].uart->open(boardId);
}
// Host board
uartLinks[count++].uart->open(-1);
#else
for (int i = 0; i < numBoards; i++) uartLinks[i].uart->open(i+1);
#endif
// Packet buffer for sending and receiving
BoardCtrlPkt pkt;
// Send initial packet to indicate that all boards are up
pkt.linkId = 0;
pkt.payload[0] = DEBUGLINK_READY;
while (1) {
int n = socketPut(conn, (char*) &pkt, sizeof(BoardCtrlPkt));
if (n < 0) return;
if (n > 0) break;
}
// Serve UARTs every 'serveCount' iterations of event loop
int serveMax = 32;
int serveCount = 0;
// Event loop
while (1) {
bool didPut = false;
bool didGet = false;
// Can we write a DebugLink packet to all UART buffers?
bool allCanPut = true;
for (int i = 0; i < numBoards; i++)
allCanPut = allCanPut && uartLinks[i].canPut(DEBUGLINK_MAX_PKT_BYTES);
// If so, try to receive a network packet and forward to UART
if (allCanPut) {
int ok = socketGet(conn, (char*) &pkt, sizeof(BoardCtrlPkt));
if (ok < 0) return;
if (ok > 0) {
int numBytes = toDebugLinkSize(pkt.payload[0]);
for (int i = 0; i < numBytes; i++)
uartLinks[pkt.linkId].put(pkt.payload[i]);
didPut = true;
}
}
// Try to read from each UART, and forward to network
for (int i = 0; i < numBoards; i++) {
if (uartLinks[i].canGet(1)) {
pkt.linkId = i;
uint8_t cmd = uartLinks[i].peek();
if (cmd == DEBUGLINK_OVERHEAT) {
// Report overheating issue via email
reportOverheat(i);
// return; // Emergency shutdown
uartLinks[i].get(); didGet = true; // Carry on
}
else {
uint8_t numBytes = fromDebugLinkSize(cmd);
if (uartLinks[i].canGet(numBytes)) {
for (int j = 0; j < numBytes; j++)
pkt.payload[j] = uartLinks[i].peekAt(j);
int ok = socketPut(conn, (char*) &pkt, sizeof(BoardCtrlPkt));
if (ok < 0) return;
if (ok > 0) {
for (int j = 0; j < numBytes; j++) uartLinks[i].get();
didGet = true;
}
}
}
}
}
// Sleep for a while if no progress made
bool didSleep = false;
if (!didPut && !didGet) {
usleep(100);
didSleep = true;
}
// Make progress on each UART buffer
serveCount++;
if (didSleep || serveCount >= serveMax) {
//if (!socketAlive(conn)) return;
for (int i = 0; i < numBoards; i++) uartLinks[i].serve();
serveCount = 0;
}
}
}
int main(int argc, char* argv[])
{
// Drop root privileges if necessary
reducePrivilege();
// Ignore SIGPIPE
signal(SIGPIPE, SIG_IGN);
// JTAG UARTs
UARTBuffer* uartLinks;
// Determine number of boards
int numBoards = TinselMeshXLenWithinBox * TinselMeshYLenWithinBox + 1;
while (1) {
// Listen on TCP port
int sock = createListener();
// Accept connection
int conn = accept(sock, NULL, NULL);
if (conn == -1) {
perror("boardctrld: accept");
exit(EXIT_FAILURE);
}
// Prevent new connections while busy
close(sock);
// Power up worker boards
#ifndef SIMULATE
powerEnable(1);
sleep(1);
waitForFPGAs(numBoards);
sleep(1);
#endif
// Fork a process to handle connection
// (This is only needed to avoid a bug in jtagatlantic in which
// UARTs cannot be reopened by the same process.)
int pid = fork();
if (pid == 0) {
// Create a UART link to each board
uartLinks = new UARTBuffer [numBoards];
// Invoke server to handle connection
server(conn, numBoards, uartLinks);
// Close UARTs
delete [] uartLinks;
return 0;
}
waitpid(pid, NULL, 0);
close(conn);
// Power down worker boards
#ifndef SIMULATE
powerEnable(0);
usleep(1500000);
#endif
}
return 0;
}