source: mainline/uspace/srv/net/loopip/loopip.c@ ca48672

Last change on this file since ca48672 was ca48672, checked in by Jiri Svoboda <jiri@…>, 9 days ago

loc_service_register() needs to take a port ID argument.

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[606c369]1/*
[ca48672]2 * Copyright (c) 2025 Jiri Svoboda
[606c369]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 loopip
30 * @{
31 */
32/**
33 * @file
34 * @brief Loopback IP link provider
35 */
36
37#include <adt/prodcons.h>
38#include <async.h>
39#include <errno.h>
[c1694b6b]40#include <str_error.h>
[606c369]41#include <inet/iplink_srv.h>
[02a09ed]42#include <inet/addr.h>
[b4edc96]43#include <inet/eth_addr.h>
[606c369]44#include <io/log.h>
45#include <loc.h>
46#include <stdio.h>
47#include <stdlib.h>
[1d6dd2a]48#include <str.h>
[1c635d6]49#include <task.h>
[606c369]50
[257feec]51#define NAME "loopip"
[606c369]52
[b7fd2a0]53static errno_t loopip_open(iplink_srv_t *srv);
54static errno_t loopip_close(iplink_srv_t *srv);
55static errno_t loopip_send(iplink_srv_t *srv, iplink_sdu_t *sdu);
56static errno_t loopip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu);
57static errno_t loopip_get_mtu(iplink_srv_t *srv, size_t *mtu);
[b4edc96]58static errno_t loopip_get_mac48(iplink_srv_t *srv, eth_addr_t *mac);
[b7fd2a0]59static errno_t loopip_addr_add(iplink_srv_t *srv, inet_addr_t *addr);
60static errno_t loopip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr);
[606c369]61
[984a9ba]62static void loopip_client_conn(ipc_call_t *icall, void *arg);
[606c369]63
64static iplink_ops_t loopip_iplink_ops = {
65 .open = loopip_open,
66 .close = loopip_close,
67 .send = loopip_send,
[a17356fd]68 .send6 = loopip_send6,
[606c369]69 .get_mtu = loopip_get_mtu,
[a17356fd]70 .get_mac48 = loopip_get_mac48,
[606c369]71 .addr_add = loopip_addr_add,
72 .addr_remove = loopip_addr_remove
73};
74
75static iplink_srv_t loopip_iplink;
76static prodcons_t loopip_rcv_queue;
77
78typedef struct {
79 link_t link;
[a35b458]80
[417a2ba1]81 /* XXX Version should be part of SDU */
82 ip_ver_t ver;
[02a09ed]83 iplink_recv_sdu_t sdu;
[606c369]84} rqueue_entry_t;
85
[b7fd2a0]86static errno_t loopip_recv_fibril(void *arg)
[606c369]87{
88 while (true) {
[a1a101d]89 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_recv_fibril(): Wait for one item");
[606c369]90 link_t *link = prodcons_consume(&loopip_rcv_queue);
[02a09ed]91 rqueue_entry_t *rqe =
92 list_get_instance(link, rqueue_entry_t, link);
[a35b458]93
[417a2ba1]94 (void) iplink_ev_recv(&loopip_iplink, &rqe->sdu, rqe->ver);
[a35b458]95
[02a09ed]96 free(rqe->sdu.data);
97 free(rqe);
[606c369]98 }
[a35b458]99
[606c369]100 return 0;
101}
102
[b7fd2a0]103static errno_t loopip_init(void)
[606c369]104{
[4c6fd56]105 loc_srv_t *srv;
106
[b688fd8]107 async_set_fallback_port_handler(loopip_client_conn, NULL);
[a35b458]108
[4c6fd56]109 errno_t rc = loc_server_register(NAME, &srv);
[606c369]110 if (rc != EOK) {
[a1a101d]111 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
[606c369]112 return rc;
113 }
[a35b458]114
[606c369]115 iplink_srv_init(&loopip_iplink);
116 loopip_iplink.ops = &loopip_iplink_ops;
117 loopip_iplink.arg = NULL;
[a35b458]118
[606c369]119 prodcons_initialize(&loopip_rcv_queue);
[a35b458]120
[02a09ed]121 const char *svc_name = "net/loopback";
122 service_id_t sid;
[ca48672]123 rc = loc_service_register(srv, svc_name, fallback_port_id, &sid);
[606c369]124 if (rc != EOK) {
[4c6fd56]125 loc_server_unregister(srv);
[02a09ed]126 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.",
127 svc_name);
[606c369]128 return rc;
129 }
[a35b458]130
[02a09ed]131 category_id_t iplink_cat;
[606c369]132 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
133 if (rc != EOK) {
[4c6fd56]134 loc_service_unregister(srv, sid);
135 loc_server_unregister(srv);
[a1a101d]136 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
[606c369]137 return rc;
138 }
[a35b458]139
[4c6fd56]140 rc = loc_service_add_to_cat(srv, sid, iplink_cat);
[606c369]141 if (rc != EOK) {
[4c6fd56]142 loc_service_unregister(srv, sid);
143 loc_server_unregister(srv);
[02a09ed]144 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.",
145 svc_name);
[606c369]146 return rc;
147 }
[a35b458]148
[606c369]149 fid_t fid = fibril_create(loopip_recv_fibril, NULL);
[4c6fd56]150 if (fid == 0) {
151 loc_service_unregister(srv, sid);
152 loc_server_unregister(srv);
[606c369]153 return ENOMEM;
[4c6fd56]154 }
[a35b458]155
[606c369]156 fibril_add_ready(fid);
[a35b458]157
[606c369]158 return EOK;
159}
160
[984a9ba]161static void loopip_client_conn(ipc_call_t *icall, void *arg)
[606c369]162{
[a1a101d]163 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_client_conn()");
[984a9ba]164 iplink_conn(icall, &loopip_iplink);
[606c369]165}
166
[b7fd2a0]167static errno_t loopip_open(iplink_srv_t *srv)
[606c369]168{
[a1a101d]169 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_open()");
[606c369]170 return EOK;
171}
172
[b7fd2a0]173static errno_t loopip_close(iplink_srv_t *srv)
[606c369]174{
[a1a101d]175 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_close()");
[606c369]176 return EOK;
177}
178
[b7fd2a0]179static errno_t loopip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
[606c369]180{
[a1a101d]181 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_send()");
[a35b458]182
[a17356fd]183 rqueue_entry_t *rqe = calloc(1, sizeof(rqueue_entry_t));
184 if (rqe == NULL)
185 return ENOMEM;
[a35b458]186
[a17356fd]187 /*
188 * Clone SDU
189 */
[417a2ba1]190 rqe->ver = ip_v4;
[a17356fd]191 rqe->sdu.data = malloc(sdu->size);
192 if (rqe->sdu.data == NULL) {
193 free(rqe);
194 return ENOMEM;
195 }
[a35b458]196
[a17356fd]197 memcpy(rqe->sdu.data, sdu->data, sdu->size);
198 rqe->sdu.size = sdu->size;
[a35b458]199
[a17356fd]200 /*
201 * Insert to receive queue
202 */
203 prodcons_produce(&loopip_rcv_queue, &rqe->link);
[a35b458]204
[a17356fd]205 return EOK;
206}
207
[b7fd2a0]208static errno_t loopip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu)
[a17356fd]209{
210 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip6_send()");
[a35b458]211
[02a09ed]212 rqueue_entry_t *rqe = calloc(1, sizeof(rqueue_entry_t));
[606c369]213 if (rqe == NULL)
214 return ENOMEM;
[a35b458]215
[606c369]216 /*
217 * Clone SDU
218 */
[417a2ba1]219 rqe->ver = ip_v6;
[606c369]220 rqe->sdu.data = malloc(sdu->size);
221 if (rqe->sdu.data == NULL) {
222 free(rqe);
223 return ENOMEM;
224 }
[a35b458]225
[606c369]226 memcpy(rqe->sdu.data, sdu->data, sdu->size);
227 rqe->sdu.size = sdu->size;
[a35b458]228
[606c369]229 /*
230 * Insert to receive queue
231 */
232 prodcons_produce(&loopip_rcv_queue, &rqe->link);
[a35b458]233
[606c369]234 return EOK;
235}
236
[b7fd2a0]237static errno_t loopip_get_mtu(iplink_srv_t *srv, size_t *mtu)
[606c369]238{
[a1a101d]239 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_get_mtu()");
[606c369]240 *mtu = 1500;
241 return EOK;
242}
243
[b4edc96]244static errno_t loopip_get_mac48(iplink_srv_t *src, eth_addr_t *mac)
[a17356fd]245{
246 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_get_mac48()");
247 return ENOTSUP;
248}
249
[b7fd2a0]250static errno_t loopip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
[606c369]251{
252 return EOK;
253}
254
[b7fd2a0]255static errno_t loopip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
[606c369]256{
257 return EOK;
258}
259
260int main(int argc, char *argv[])
261{
[02a09ed]262 printf("%s: HelenOS loopback IP link provider\n", NAME);
[a35b458]263
[b7fd2a0]264 errno_t rc = log_init(NAME);
[02a09ed]265 if (rc != EOK) {
[c1694b6b]266 printf("%s: Failed to initialize logging: %s.\n", NAME, str_error(rc));
[02a09ed]267 return rc;
[606c369]268 }
[a35b458]269
[606c369]270 rc = loopip_init();
[c1694b6b]271 if (rc != EOK) {
272 printf("%s: Failed to initialize loopip: %s.\n", NAME, str_error(rc));
[02a09ed]273 return rc;
[c1694b6b]274 }
[a35b458]275
[02a09ed]276 printf("%s: Accepting connections.\n", NAME);
[606c369]277 task_retval(0);
278 async_manager();
[a35b458]279
[606c369]280 /* Not reached */
281 return 0;
282}
283
284/** @}
285 */
Note: See TracBrowser for help on using the repository browser.