source: mainline/uspace/srv/net/slip/slip.c@ 195b7b3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 195b7b3 was f2d88f3, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Add forgotten changes to enable non-blocking chardev read

  • Property mode set to 100644
File size: 10.3 KB
RevLine 
[2093fbe]1/*
2 * Copyright (c) 2013 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup slip
30 * @{
31 */
32/**
33 * @file
34 * @brief IP over serial line IP link provider.
35 */
36
37#include <stdio.h>
[7b64cf0]38#include <stdint.h>
[c1694b6b]39#include <stdlib.h>
[2093fbe]40#include <loc.h>
[02a09ed]41#include <inet/addr.h>
[2093fbe]42#include <inet/iplink_srv.h>
[74017ce]43#include <io/chardev.h>
[2093fbe]44#include <io/log.h>
45#include <errno.h>
[c1694b6b]46#include <str_error.h>
[1c635d6]47#include <task.h>
[2093fbe]48
49#define NAME "slip"
50#define CAT_IPLINK "iplink"
51
52#define SLIP_MTU 1006 /* as per RFC 1055 */
53
[7b64cf0]54#define SLIP_END 0300
55#define SLIP_ESC 0333
[02a09ed]56#define SLIP_ESC_END 0334
[7b64cf0]57#define SLIP_ESC_ESC 0335
58
[b7fd2a0]59static errno_t slip_open(iplink_srv_t *);
60static errno_t slip_close(iplink_srv_t *);
61static errno_t slip_send(iplink_srv_t *, iplink_sdu_t *);
62static errno_t slip_send6(iplink_srv_t *, iplink_sdu6_t *);
63static errno_t slip_get_mtu(iplink_srv_t *, size_t *);
64static errno_t slip_get_mac48(iplink_srv_t *, addr48_t *);
65static errno_t slip_addr_add(iplink_srv_t *, inet_addr_t *);
66static errno_t slip_addr_remove(iplink_srv_t *, inet_addr_t *);
[2093fbe]67
68static iplink_srv_t slip_iplink;
69
70static iplink_ops_t slip_iplink_ops = {
71 .open = slip_open,
72 .close = slip_close,
73 .send = slip_send,
[a17356fd]74 .send6 = slip_send6,
[2093fbe]75 .get_mtu = slip_get_mtu,
[a17356fd]76 .get_mac48 = slip_get_mac48,
[2093fbe]77 .addr_add = slip_addr_add,
78 .addr_remove = slip_addr_remove
79};
80
[7b64cf0]81static uint8_t slip_send_buf[SLIP_MTU + 2];
82static size_t slip_send_pending;
83
84static uint8_t slip_recv_buf[SLIP_MTU + 2];
85static size_t slip_recv_pending;
86static size_t slip_recv_read;
87
[b7fd2a0]88errno_t slip_open(iplink_srv_t *srv)
[2093fbe]89{
90 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_open()");
91 return EOK;
92}
93
[b7fd2a0]94errno_t slip_close(iplink_srv_t *srv)
[2093fbe]95{
96 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_close()");
97 return EOK;
98}
99
[74017ce]100static void write_flush(chardev_t *chardev)
[7b64cf0]101{
102 size_t written = 0;
103
104 while (slip_send_pending > 0) {
[b7fd2a0]105 errno_t rc;
[74017ce]106 size_t nwr;
[7b64cf0]107
[74017ce]108 rc = chardev_write(chardev, &slip_send_buf[written],
109 slip_send_pending, &nwr);
110 if (rc != EOK) {
[7b64cf0]111 log_msg(LOG_DEFAULT, LVL_ERROR,
[c1694b6b]112 "chardev_write() -> %s", str_error_name(rc));
[7b64cf0]113 slip_send_pending = 0;
114 break;
115 }
[74017ce]116 written += nwr;
117 slip_send_pending -= nwr;
[7b64cf0]118 }
119}
120
[74017ce]121static void write_buffered(chardev_t *chardev, uint8_t ch)
[7b64cf0]122{
123 if (slip_send_pending == sizeof(slip_send_buf))
[74017ce]124 write_flush(chardev);
[7b64cf0]125 slip_send_buf[slip_send_pending++] = ch;
126}
127
[b7fd2a0]128errno_t slip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
[2093fbe]129{
[a17356fd]130 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_send()");
[a35b458]131
[74017ce]132 chardev_t *chardev = (chardev_t *) srv->arg;
[7b64cf0]133 uint8_t *data = sdu->data;
[a35b458]134
[7b64cf0]135 /*
[a17356fd]136 * Strictly speaking, this is not prescribed by the RFC, but the RFC
137 * suggests to start with sending a SLIP_END byte as a synchronization
138 * measure for dealing with previous possible noise on the line.
139 */
[74017ce]140 write_buffered(chardev, SLIP_END);
[a35b458]141
[a17356fd]142 for (size_t i = 0; i < sdu->size; i++) {
[7b64cf0]143 switch (data[i]) {
144 case SLIP_END:
[74017ce]145 write_buffered(chardev, SLIP_ESC);
146 write_buffered(chardev, SLIP_ESC_END);
[7b64cf0]147 break;
148 case SLIP_ESC:
[74017ce]149 write_buffered(chardev, SLIP_ESC);
150 write_buffered(chardev, SLIP_ESC_ESC);
[7b64cf0]151 break;
152 default:
[74017ce]153 write_buffered(chardev, data[i]);
[7b64cf0]154 break;
155 }
156 }
[a35b458]157
[74017ce]158 write_buffered(chardev, SLIP_END);
159 write_flush(chardev);
[a35b458]160
[7b64cf0]161 return EOK;
[2093fbe]162}
163
[b7fd2a0]164errno_t slip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu)
[a17356fd]165{
166 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_send6()");
[a35b458]167
[a17356fd]168 return ENOTSUP;
169}
170
[b7fd2a0]171errno_t slip_get_mtu(iplink_srv_t *srv, size_t *mtu)
[2093fbe]172{
173 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_get_mtu()");
174 *mtu = SLIP_MTU;
175 return EOK;
176}
177
[b7fd2a0]178errno_t slip_get_mac48(iplink_srv_t *src, addr48_t *mac)
[a17356fd]179{
180 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_get_mac48()");
181 return ENOTSUP;
182}
183
[b7fd2a0]184errno_t slip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
[2093fbe]185{
186 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_add()");
187 return EOK;
188}
189
[b7fd2a0]190errno_t slip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
[2093fbe]191{
192 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_remove()");
193 return EOK;
194}
195
196static void usage(void)
197{
198 printf("Usage: " NAME " <service-name> <link-name>\n");
199}
200
[984a9ba]201static void slip_client_conn(ipc_call_t *icall, void *arg)
[2093fbe]202{
203 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_client_conn()");
[984a9ba]204 iplink_conn(icall, &slip_iplink);
[2093fbe]205}
206
[74017ce]207static uint8_t read_buffered(chardev_t *chardev)
[7b64cf0]208{
209 while (slip_recv_pending == 0) {
[b7fd2a0]210 errno_t rc;
[74017ce]211 size_t nread;
[7b64cf0]212
[74017ce]213 rc = chardev_read(chardev, slip_recv_buf,
[f2d88f3]214 sizeof(slip_recv_buf), &nread, chardev_f_none);
[74017ce]215 if (rc != EOK) {
[7b64cf0]216 log_msg(LOG_DEFAULT, LVL_ERROR,
[c1694b6b]217 "char_dev_read() -> %s", str_error_name(rc));
[7b64cf0]218 }
[74017ce]219
220 if (nread == 0)
221 return SLIP_END;
222
223 slip_recv_pending = nread;
[7b64cf0]224 slip_recv_read = 0;
225 }
226 slip_recv_pending--;
227 return slip_recv_buf[slip_recv_read++];
228}
229
[b7fd2a0]230static errno_t slip_recv_fibril(void *arg)
[7b64cf0]231{
[74017ce]232 chardev_t *chardev = (chardev_t *) arg;
[7b64cf0]233 static uint8_t recv_final[SLIP_MTU];
[02a09ed]234 iplink_recv_sdu_t sdu;
[7b64cf0]235 uint8_t ch;
[b7fd2a0]236 errno_t rc;
[7b64cf0]237
238 sdu.data = recv_final;
239
240 while (true) {
[84239b1]241 sdu.size = 0;
242 while (sdu.size < sizeof(recv_final)) {
[74017ce]243 ch = read_buffered(chardev);
[7b64cf0]244 switch (ch) {
245 case SLIP_END:
246 if (sdu.size == 0) {
247 /*
[02a09ed]248 * Discard the empty SLIP datagram.
249 */
[7b64cf0]250 break;
251 }
252 goto pass;
253
254 case SLIP_ESC:
[74017ce]255 ch = read_buffered(chardev);
[7b64cf0]256 if (ch == SLIP_ESC_END) {
257 recv_final[sdu.size++] = SLIP_END;
258 break;
[74017ce]259 } else if (ch == SLIP_ESC_ESC) {
[7b64cf0]260 recv_final[sdu.size++] = SLIP_ESC;
261 break;
262 }
263
264 /*
[e141281]265 * The RFC suggests to simply insert the wrongly
266 * escaped character into the packet so we fall
267 * through.
268 */
[dc12262]269 /* Fallthrough */
[e141281]270
[7b64cf0]271 default:
272 recv_final[sdu.size++] = ch;
273 break;
274 }
[a35b458]275
[7b64cf0]276 }
277
278 /*
[ae7d03c]279 * We have reached the limit of our MTU. Regardless of whether
280 * the datagram is properly ended with SLIP_END, pass it along.
281 * If the next character is really SLIP_END, nothing
282 * catastrophic happens. The algorithm will just see an
283 * artificially empty SLIP datagram and life will go on.
284 */
[7b64cf0]285
[3bacee1]286 pass:
[417a2ba1]287 rc = iplink_ev_recv(&slip_iplink, &sdu, ip_v4);
[7b64cf0]288 if (rc != EOK) {
289 log_msg(LOG_DEFAULT, LVL_ERROR,
[c1694b6b]290 "iplink_ev_recv() -> %s", str_error_name(rc));
[7b64cf0]291 }
292 }
293
294 return 0;
295}
296
[b7fd2a0]297static errno_t slip_init(const char *svcstr, const char *linkstr)
[2093fbe]298{
299 service_id_t svcid;
300 service_id_t linksid;
301 category_id_t iplinkcid;
[3abf0760]302 async_sess_t *sess_in = NULL;
303 async_sess_t *sess_out = NULL;
[74017ce]304 chardev_t *chardev_in = NULL;
305 chardev_t *chardev_out = NULL;
[7b64cf0]306 fid_t fid;
[b7fd2a0]307 errno_t rc;
[2093fbe]308
309 iplink_srv_init(&slip_iplink);
310 slip_iplink.ops = &slip_iplink_ops;
311
[b688fd8]312 async_set_fallback_port_handler(slip_client_conn, NULL);
[2093fbe]313
314 rc = loc_server_register(NAME);
315 if (rc != EOK) {
316 log_msg(LOG_DEFAULT, LVL_ERROR,
317 "Failed registering server.");
318 return rc;
319 }
320
321 rc = loc_service_get_id(svcstr, &svcid, 0);
322 if (rc != EOK) {
323 log_msg(LOG_DEFAULT, LVL_ERROR,
324 "Failed getting ID for service %s", svcstr);
325 return rc;
326 }
327
328 rc = loc_category_get_id(CAT_IPLINK, &iplinkcid, 0);
329 if (rc != EOK) {
330 log_msg(LOG_DEFAULT, LVL_ERROR,
331 "Failed to get category ID for %s",
332 CAT_IPLINK);
333 return rc;
334 }
335
[7b64cf0]336 /*
[3abf0760]337 * Create two sessions to allow to both read and write from the
338 * char_dev at the same time.
[7b64cf0]339 */
[f9b2cb4c]340 sess_out = loc_service_connect(svcid, INTERFACE_DDF, 0);
[3abf0760]341 if (!sess_out) {
[2093fbe]342 log_msg(LOG_DEFAULT, LVL_ERROR,
343 "Failed to connect to service %s (ID=%d)",
344 svcstr, (int) svcid);
[b11f6fb]345 return ENOENT;
[2093fbe]346 }
[74017ce]347
348 rc = chardev_open(sess_out, &chardev_out);
349 if (rc != EOK) {
350 log_msg(LOG_DEFAULT, LVL_ERROR,
351 "Failed opening character device.");
352 return ENOENT;
353 }
354
355 slip_iplink.arg = chardev_out;
[3abf0760]356
[f9b2cb4c]357 sess_in = loc_service_connect(svcid, INTERFACE_DDF, 0);
[3abf0760]358 if (!sess_in) {
359 log_msg(LOG_DEFAULT, LVL_ERROR,
360 "Failed to connect to service %s (ID=%d)",
361 svcstr, (int) svcid);
[b11f6fb]362 rc = ENOENT;
[3abf0760]363 goto fail;
364 }
[2093fbe]365
[74017ce]366 rc = chardev_open(sess_in, &chardev_in);
367 if (rc != EOK) {
368 log_msg(LOG_DEFAULT, LVL_ERROR,
369 "Failed opening character device.");
370 return ENOENT;
371 }
372
[2093fbe]373 rc = loc_service_register(linkstr, &linksid);
374 if (rc != EOK) {
375 log_msg(LOG_DEFAULT, LVL_ERROR,
[3bacee1]376 "Failed to register service %s",
377 linkstr);
[2093fbe]378 goto fail;
379 }
380
381 rc = loc_service_add_to_cat(linksid, iplinkcid);
382 if (rc != EOK) {
383 log_msg(LOG_DEFAULT, LVL_ERROR,
[7b64cf0]384 "Failed to add service %d (%s) to category %d (%s).",
[2093fbe]385 (int) linksid, linkstr, (int) iplinkcid, CAT_IPLINK);
386 goto fail;
387 }
388
[74017ce]389 fid = fibril_create(slip_recv_fibril, chardev_in);
[7b64cf0]390 if (!fid) {
391 log_msg(LOG_DEFAULT, LVL_ERROR,
392 "Failed to create receive fibril.");
[b11f6fb]393 rc = ENOENT;
[7b64cf0]394 goto fail;
395 }
396 fibril_add_ready(fid);
397
[2093fbe]398 return EOK;
399
400fail:
[74017ce]401 chardev_close(chardev_out);
[3abf0760]402 if (sess_out)
403 async_hangup(sess_out);
[74017ce]404 chardev_close(chardev_in);
[3abf0760]405 if (sess_in)
406 async_hangup(sess_in);
[2093fbe]407
408 /*
[ae7d03c]409 * We assume that our registration at the location service will be
410 * cleaned up automatically as the service (i.e. this task) terminates.
411 */
[2093fbe]412
413 return rc;
414}
415
416int main(int argc, char *argv[])
417{
[b7fd2a0]418 errno_t rc;
[2093fbe]419
420 printf(NAME ": IP over serial line service\n");
421
422 if (argc != 3) {
423 usage();
424 return EINVAL;
425 }
426
427 rc = log_init(NAME);
428 if (rc != EOK) {
429 printf(NAME ": failed to initialize log\n");
430 return rc;
431 }
432
433 rc = slip_init(argv[1], argv[2]);
434 if (rc != EOK)
435 return 1;
436
437 printf(NAME ": Accepting connections.\n");
438 task_retval(0);
439 async_manager();
440
441 /* not reached */
442 return 0;
443}
444
445/** @}
446 */
Note: See TracBrowser for help on using the repository browser.