target: DCC / target message backoff algorithm
[openocd.git] / src / server / server.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "server.h"
31 #include <target/target.h>
32 #include <target/target_request.h>
33 #include "openocd.h"
34 #include "tcl_server.h"
35 #include "telnet_server.h"
36
37 #include <signal.h>
38
39 #ifndef _WIN32
40 #include <netinet/tcp.h>
41 #endif
42
43
44 static struct service *services = NULL;
45
46 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
47 static int shutdown_openocd = 0;
48
49 static int add_connection(struct service *service, struct command_context *cmd_ctx)
50 {
51 socklen_t address_size;
52 struct connection *c, **p;
53 int retval;
54 int flag = 1;
55
56 c = malloc(sizeof(struct connection));
57 c->fd = -1;
58 c->fd_out = -1;
59 memset(&c->sin, 0, sizeof(c->sin));
60 c->cmd_ctx = copy_command_context(cmd_ctx);
61 c->service = service;
62 c->input_pending = 0;
63 c->priv = NULL;
64 c->next = NULL;
65
66 if (service->type == CONNECTION_TCP)
67 {
68 address_size = sizeof(c->sin);
69
70 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
71 c->fd_out = c->fd;
72
73 /* This increases performance dramatically for e.g. GDB load which
74 * does not have a sliding window protocol. */
75 retval = setsockopt(c->fd, /* socket affected */
76 IPPROTO_TCP, /* set option at TCP level */
77 TCP_NODELAY, /* name of option */
78 (char *)&flag, /* the cast is historical cruft */
79 sizeof(int)); /* length of option value */
80
81 LOG_INFO("accepting '%s' connection from %s", service->name, service->port);
82 if ((retval = service->new_connection(c)) != ERROR_OK)
83 {
84 close_socket(c->fd);
85 LOG_ERROR("attempted '%s' connection rejected", service->name);
86 free(c);
87 return retval;
88 }
89 } else if (service->type == CONNECTION_STDINOUT)
90 {
91 c->fd = service->fd;
92 c->fd_out = fileno(stdout);
93
94 #ifdef _WIN32
95 /* we are using stdin/out so ignore ctrl-c under windoze */
96 SetConsoleCtrlHandler(NULL, TRUE);
97 #endif
98
99 /* do not check for new connections again on stdin */
100 service->fd = -1;
101
102 LOG_INFO("accepting '%s' connection from pipe", service->name);
103 if ((retval = service->new_connection(c)) != ERROR_OK)
104 {
105 LOG_ERROR("attempted '%s' connection rejected", service->name);
106 free(c);
107 return retval;
108 }
109 } else if (service->type == CONNECTION_PIPE)
110 {
111 c->fd = service->fd;
112 /* do not check for new connections again on stdin */
113 service->fd = -1;
114
115 char * out_file = alloc_printf("%so", service->port);
116 c->fd_out = open(out_file, O_WRONLY);
117 free(out_file);
118 if (c->fd_out == -1)
119 {
120 LOG_ERROR("could not open %s", service->port);
121 exit(1);
122 }
123
124 LOG_INFO("accepting '%s' connection from pipe %s", service->name, service->port);
125 if ((retval = service->new_connection(c)) != ERROR_OK)
126 {
127 LOG_ERROR("attempted '%s' connection rejected", service->name);
128 free(c);
129 return retval;
130 }
131 }
132
133 /* add to the end of linked list */
134 for (p = &service->connections; *p; p = &(*p)->next);
135 *p = c;
136
137 service->max_connections--;
138
139 return ERROR_OK;
140 }
141
142 static int remove_connection(struct service *service, struct connection *connection)
143 {
144 struct connection **p = &service->connections;
145 struct connection *c;
146
147 /* find connection */
148 while ((c = *p))
149 {
150 if (c->fd == connection->fd)
151 {
152 service->connection_closed(c);
153 if (service->type == CONNECTION_TCP)
154 {
155 close_socket(c->fd);
156 } else if (service->type == CONNECTION_PIPE)
157 {
158 /* The service will listen to the pipe again */
159 c->service->fd = c->fd;
160 }
161
162 command_done(c->cmd_ctx);
163
164 /* delete connection */
165 *p = c->next;
166 free(c);
167
168 service->max_connections++;
169 break;
170 }
171
172 /* redirect p to next list pointer */
173 p = &(*p)->next;
174 }
175
176 return ERROR_OK;
177 }
178
179 /* FIX! make service return error instead of invoking exit() */
180 int add_service(char *name, const char *port, int max_connections, new_connection_handler_t new_connection_handler, input_handler_t input_handler, connection_closed_handler_t connection_closed_handler, void *priv)
181 {
182 struct service *c, **p;
183 int so_reuseaddr_option = 1;
184
185 c = malloc(sizeof(struct service));
186
187 c->name = strdup(name);
188 c->port = strdup(port);
189 c->max_connections = 1; /* Only TCP/IP ports can support more than one connection */
190 c->fd = -1;
191 c->connections = NULL;
192 c->new_connection = new_connection_handler;
193 c->input = input_handler;
194 c->connection_closed = connection_closed_handler;
195 c->priv = priv;
196 c->next = NULL;
197 long portnumber;
198 if (strcmp(c->port, "pipe") == 0)
199 {
200 c->type = CONNECTION_STDINOUT;
201 } else
202 {
203 char *end;
204 portnumber = strtol(c->port, &end, 0);
205 if (!*end && (parse_long(c->port, &portnumber) == ERROR_OK))
206 {
207 c->portnumber = portnumber;
208 c->type = CONNECTION_TCP;
209 } else
210 {
211 c->type = CONNECTION_PIPE;
212 }
213 }
214
215 if (c->type == CONNECTION_TCP)
216 {
217 c->max_connections = max_connections;
218
219 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
220 {
221 LOG_ERROR("error creating socket: %s", strerror(errno));
222 exit(-1);
223 }
224
225 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
226
227 socket_nonblock(c->fd);
228
229 memset(&c->sin, 0, sizeof(c->sin));
230 c->sin.sin_family = AF_INET;
231 c->sin.sin_addr.s_addr = INADDR_ANY;
232 c->sin.sin_port = htons(c->portnumber);
233
234 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
235 {
236 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
237 exit(-1);
238 }
239
240 #ifndef _WIN32
241 int segsize = 65536;
242 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
243 #endif
244 int window_size = 128 * 1024;
245
246 /* These setsockopt()s must happen before the listen() */
247
248 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
249 (char *)&window_size, sizeof(window_size));
250 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
251 (char *)&window_size, sizeof(window_size));
252
253 if (listen(c->fd, 1) == -1)
254 {
255 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
256 exit(-1);
257 }
258 }
259 else if (c->type == CONNECTION_STDINOUT)
260 {
261 c->fd = fileno(stdin);
262
263 #ifdef _WIN32
264 /* for win32 set stdin/stdout to binary mode */
265 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
266 LOG_WARNING("cannot change stdout mode to binary");
267 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
268 LOG_WARNING("cannot change stdin mode to binary");
269 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
270 LOG_WARNING("cannot change stderr mode to binary");
271 #else
272 socket_nonblock(c->fd);
273 #endif
274 }
275 else if (c->type == CONNECTION_PIPE)
276 {
277 #ifdef _WIN32
278 /* we currenty do not support named pipes under win32
279 * so exit openocd for now */
280 LOG_ERROR("Named pipes currently not supported under this os");
281 exit(1);
282 #else
283 /* Pipe we're reading from */
284 c->fd = open(c->port, O_RDONLY | O_NONBLOCK);
285 if (c->fd == -1)
286 {
287 LOG_ERROR("could not open %s", c->port);
288 exit(1);
289 }
290 #endif
291 }
292
293 /* add to the end of linked list */
294 for (p = &services; *p; p = &(*p)->next);
295 *p = c;
296
297 return ERROR_OK;
298 }
299
300 static int remove_services(void)
301 {
302 struct service *c = services;
303
304 /* loop service */
305 while (c)
306 {
307 struct service *next = c->next;
308
309 if (c->name)
310 free((void *)c->name);
311
312 if (c->type == CONNECTION_PIPE)
313 {
314 if (c->fd != -1)
315 close(c->fd);
316 }
317 if (c->port)
318 free((void *)c->port);
319
320 if (c->priv)
321 free(c->priv);
322
323 /* delete service */
324 free(c);
325
326 /* remember the last service for unlinking */
327 c = next;
328 }
329
330 services = NULL;
331
332 return ERROR_OK;
333 }
334
335 int server_loop(struct command_context *command_context)
336 {
337 struct service *service;
338
339 bool poll_ok = true;
340
341 /* used in select() */
342 fd_set read_fds;
343 int fd_max;
344
345 /* used in accept() */
346 int retval;
347
348 #ifndef _WIN32
349 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
350 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
351 #endif
352
353 while (!shutdown_openocd)
354 {
355 /* monitor sockets for activity */
356 fd_max = 0;
357 FD_ZERO(&read_fds);
358
359 /* add service and connection fds to read_fds */
360 for (service = services; service; service = service->next)
361 {
362 if (service->fd != -1)
363 {
364 /* listen for new connections */
365 FD_SET(service->fd, &read_fds);
366
367 if (service->fd > fd_max)
368 fd_max = service->fd;
369 }
370
371 if (service->connections)
372 {
373 struct connection *c;
374
375 for (c = service->connections; c; c = c->next)
376 {
377 /* check for activity on the connection */
378 FD_SET(c->fd, &read_fds);
379 if (c->fd > fd_max)
380 fd_max = c->fd;
381 }
382 }
383 }
384
385 struct timeval tv;
386 tv.tv_sec = 0;
387 if (poll_ok)
388 {
389 /* we're just polling this iteration, this is faster on embedded
390 * hosts */
391 tv.tv_usec = 0;
392 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
393 } else
394 {
395 /* Every 100ms */
396 tv.tv_usec = 100000;
397 /* Only while we're sleeping we'll let others run */
398 openocd_sleep_prelude();
399 kept_alive();
400 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
401 openocd_sleep_postlude();
402 }
403
404 if (retval == -1)
405 {
406 #ifdef _WIN32
407
408 errno = WSAGetLastError();
409
410 if (errno == WSAEINTR)
411 FD_ZERO(&read_fds);
412 else
413 {
414 LOG_ERROR("error during select: %s", strerror(errno));
415 exit(-1);
416 }
417 #else
418
419 if (errno == EINTR)
420 {
421 FD_ZERO(&read_fds);
422 }
423 else
424 {
425 LOG_ERROR("error during select: %s", strerror(errno));
426 exit(-1);
427 }
428 #endif
429 }
430
431 if (retval == 0)
432 {
433 /* We only execute these callbacks when there was nothing to do or we timed out */
434 target_call_timer_callbacks();
435 process_jim_events(command_context);
436
437 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
438
439 /* We timed out/there was nothing to do, timeout rather than poll next time */
440 poll_ok = false;
441 } else
442 {
443 /* There was something to do, next time we'll just poll */
444 poll_ok = true;
445 }
446
447 /* This is a simple back-off algorithm where we immediately
448 * re-poll if we did something this time around.
449 *
450 * This greatly improves performance of DCC.
451 */
452 poll_ok = poll_ok || target_got_message();
453
454 for (service = services; service; service = service->next)
455 {
456 /* handle new connections on listeners */
457 if ((service->fd != -1)
458 && (FD_ISSET(service->fd, &read_fds)))
459 {
460 if (service->max_connections > 0)
461 {
462 add_connection(service, command_context);
463 }
464 else
465 {
466 if (service->type == CONNECTION_TCP)
467 {
468 struct sockaddr_in sin;
469 socklen_t address_size = sizeof(sin);
470 int tmp_fd;
471 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
472 close_socket(tmp_fd);
473 }
474 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
475 }
476 }
477
478 /* handle activity on connections */
479 if (service->connections)
480 {
481 struct connection *c;
482
483 for (c = service->connections; c;)
484 {
485 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
486 {
487 if ((retval = service->input(c)) != ERROR_OK)
488 {
489 struct connection *next = c->next;
490 if (service->type == CONNECTION_PIPE)
491 {
492 /* if connection uses a pipe then shutdown openocd on error */
493 shutdown_openocd = 1;
494 }
495 remove_connection(service, c);
496 LOG_INFO("dropped '%s' connection", service->name);
497 c = next;
498 continue;
499 }
500 }
501 c = c->next;
502 }
503 }
504 }
505
506 #ifdef _WIN32
507 MSG msg;
508 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
509 {
510 if (msg.message == WM_QUIT)
511 shutdown_openocd = 1;
512 }
513 #endif
514 }
515
516 return ERROR_OK;
517 }
518
519 #ifdef _WIN32
520 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
521 {
522 shutdown_openocd = 1;
523 return TRUE;
524 }
525
526 void sig_handler(int sig) {
527 shutdown_openocd = 1;
528 }
529 #endif
530
531 int server_preinit(void)
532 {
533 /* this currently only calls WSAStartup on native win32 systems
534 * before any socket operations are performed.
535 * This is an issue if you call init in your config script */
536
537 #ifdef _WIN32
538 WORD wVersionRequested;
539 WSADATA wsaData;
540
541 wVersionRequested = MAKEWORD(2, 2);
542
543 if (WSAStartup(wVersionRequested, &wsaData) != 0)
544 {
545 LOG_ERROR("Failed to Open Winsock");
546 exit(-1);
547 }
548
549 /* register ctrl-c handler */
550 SetConsoleCtrlHandler(ControlHandler, TRUE);
551
552 signal(SIGINT, sig_handler);
553 signal(SIGTERM, sig_handler);
554 signal(SIGBREAK, sig_handler);
555 signal(SIGABRT, sig_handler);
556 #endif
557
558 return ERROR_OK;
559 }
560
561 int server_init(struct command_context *cmd_ctx)
562 {
563 int ret = tcl_init();
564 if (ERROR_OK != ret)
565 return ret;
566
567 return telnet_init("Open On-Chip Debugger");
568 }
569
570 int server_quit(void)
571 {
572 remove_services();
573
574 #ifdef _WIN32
575 WSACleanup();
576 SetConsoleCtrlHandler(ControlHandler, FALSE);
577 #endif
578
579 return ERROR_OK;
580 }
581
582 int connection_write(struct connection *connection, const void *data, int len)
583 {
584 if (len == 0)
585 {
586 /* successful no-op. Sockets and pipes behave differently here... */
587 return 0;
588 }
589 if (connection->service->type == CONNECTION_TCP)
590 {
591 return write_socket(connection->fd_out, data, len);
592 } else
593 {
594 return write(connection->fd_out, data, len);
595 }
596 }
597
598 int connection_read(struct connection *connection, void *data, int len)
599 {
600 if (connection->service->type == CONNECTION_TCP)
601 {
602 return read_socket(connection->fd, data, len);
603 } else
604 {
605 return read(connection->fd, data, len);
606 }
607 }
608
609 /* tell the server we want to shut down */
610 COMMAND_HANDLER(handle_shutdown_command)
611 {
612 LOG_USER("shutdown command invoked");
613
614 shutdown_openocd = 1;
615
616 return ERROR_OK;
617 }
618
619 static const struct command_registration server_command_handlers[] = {
620 {
621 .name = "shutdown",
622 .handler = &handle_shutdown_command,
623 .mode = COMMAND_ANY,
624 .help = "shut the server down",
625 },
626 COMMAND_REGISTRATION_DONE
627 };
628
629 int server_register_commands(struct command_context *cmd_ctx)
630 {
631 int retval = telnet_register_commands(cmd_ctx);
632 if (ERROR_OK != retval)
633 return retval;
634
635 retval = tcl_register_commands(cmd_ctx);
636 if (ERROR_OK != retval)
637 return retval;
638
639 return register_commands(cmd_ctx, NULL, server_command_handlers);
640 }
641
642 SERVER_PORT_COMMAND()
643 {
644 switch (CMD_ARGC) {
645 case 0:
646 command_print(CMD_CTX, "%d", *out);
647 break;
648 case 1:
649 {
650 uint16_t port;
651 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
652 *out = port;
653 break;
654 }
655 default:
656 return ERROR_INVALID_ARGUMENTS;
657 }
658 return ERROR_OK;
659 }
660
661 SERVER_PIPE_COMMAND()
662 {
663 switch (CMD_ARGC) {
664 case 0:
665 command_print(CMD_CTX, "%s", *out);
666 break;
667 case 1:
668 {
669 const char * t = strdup(CMD_ARGV[0]);
670 free((void *)*out);
671 *out = t;
672 break;
673 }
674 default:
675 return ERROR_INVALID_ARGUMENTS;
676 }
677 return ERROR_OK;
678 }
679

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)