source: mainline/uspace/srv/net/ethip/ethip.c@ dc12262

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dc12262 was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 8.1 KB
RevLine 
[e2e56e67]1/*
2 * Copyright (c) 2012 Jiri Svoboda
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
[1493811]29/** @addtogroup ethip
[e2e56e67]30 * @{
31 */
32/**
33 * @file
34 * @brief IP link provider for Ethernet
[1493811]35 *
36 * Based on the IETF RFC 894 standard.
[e2e56e67]37 */
38
39#include <async.h>
40#include <errno.h>
41#include <inet/iplink_srv.h>
42#include <io/log.h>
43#include <loc.h>
44#include <stdio.h>
[06295a9]45#include <stdlib.h>
[1c635d6]46#include <task.h>
[87e5658c]47#include "arp.h"
[06295a9]48#include "ethip.h"
49#include "ethip_nic.h"
[1493811]50#include "pdu.h"
51#include "std.h"
[e2e56e67]52
[06a1d077]53#define NAME "ethip"
[e2e56e67]54
[4f64a523]55static int ethip_open(iplink_srv_t *srv);
56static int ethip_close(iplink_srv_t *srv);
[02a09ed]57static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu);
[a17356fd]58static int ethip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu);
[4f64a523]59static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu);
[a17356fd]60static int ethip_get_mac48(iplink_srv_t *srv, addr48_t *mac);
[c3b25985]61static int ethip_set_mac48(iplink_srv_t *srv, addr48_t *mac);
[02a09ed]62static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr);
63static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr);
[e2e56e67]64
65static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
66
67static iplink_ops_t ethip_iplink_ops = {
68 .open = ethip_open,
69 .close = ethip_close,
70 .send = ethip_send,
[a17356fd]71 .send6 = ethip_send6,
[962f03b]72 .get_mtu = ethip_get_mtu,
[a17356fd]73 .get_mac48 = ethip_get_mac48,
[c3b25985]74 .set_mac48 = ethip_set_mac48,
[962f03b]75 .addr_add = ethip_addr_add,
76 .addr_remove = ethip_addr_remove
[e2e56e67]77};
78
79static int ethip_init(void)
80{
[b688fd8]81 async_set_fallback_port_handler(ethip_client_conn, NULL);
[1038a9c]82
83 int rc = loc_server_register(NAME);
[e2e56e67]84 if (rc != EOK) {
[a1a101d]85 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
[e2e56e67]86 return rc;
87 }
[1038a9c]88
[06295a9]89 rc = ethip_nic_discovery_start();
90 if (rc != EOK)
[e2e56e67]91 return rc;
[1038a9c]92
[06295a9]93 return EOK;
94}
95
96int ethip_iplink_init(ethip_nic_t *nic)
97{
98 int rc;
99 service_id_t sid;
100 category_id_t iplink_cat;
101 static unsigned link_num = 0;
102 char *svc_name = NULL;
103
[a1a101d]104 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_iplink_init()");
[06295a9]105
[4f64a523]106 iplink_srv_init(&nic->iplink);
[06295a9]107 nic->iplink.ops = &ethip_iplink_ops;
108 nic->iplink.arg = nic;
109
110 rc = asprintf(&svc_name, "net/eth%u", ++link_num);
111 if (rc < 0) {
[a1a101d]112 log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
[06295a9]113 goto error;
114 }
115
116 rc = loc_service_register(svc_name, &sid);
117 if (rc != EOK) {
[a1a101d]118 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", svc_name);
[06295a9]119 goto error;
[e2e56e67]120 }
121
[bc38578]122 nic->iplink_sid = sid;
123
[e2e56e67]124 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
125 if (rc != EOK) {
[a1a101d]126 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
[06295a9]127 goto error;
[e2e56e67]128 }
129
130 rc = loc_service_add_to_cat(sid, iplink_cat);
131 if (rc != EOK) {
[a1a101d]132 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", svc_name);
[06295a9]133 goto error;
[e2e56e67]134 }
135
136 return EOK;
[06295a9]137
138error:
139 if (svc_name != NULL)
140 free(svc_name);
141 return rc;
[e2e56e67]142}
143
144static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
145{
[bc38578]146 ethip_nic_t *nic;
147 service_id_t sid;
148
[f9b2cb4c]149 sid = (service_id_t) IPC_GET_ARG2(*icall);
[a1a101d]150 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_client_conn(%u)", (unsigned)sid);
[bc38578]151 nic = ethip_nic_find_by_iplink_sid(sid);
152 if (nic == NULL) {
[a1a101d]153 log_msg(LOG_DEFAULT, LVL_WARN, "Uknown service ID.");
[bc38578]154 return;
155 }
156
157 iplink_conn(iid, icall, &nic->iplink);
[e2e56e67]158}
159
[4f64a523]160static int ethip_open(iplink_srv_t *srv)
[e2e56e67]161{
[a1a101d]162 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_open()");
[e2e56e67]163 return EOK;
164}
165
[4f64a523]166static int ethip_close(iplink_srv_t *srv)
[e2e56e67]167{
[a1a101d]168 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_close()");
[e2e56e67]169 return EOK;
170}
171
[02a09ed]172static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
[e2e56e67]173{
[02a09ed]174 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send()");
175
176 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
[a17356fd]177 eth_frame_t frame;
[02a09ed]178
[a17356fd]179 int rc = arp_translate(nic, sdu->src, sdu->dest, frame.dest);
180 if (rc != EOK) {
181 log_msg(LOG_DEFAULT, LVL_WARN, "Failed to look up IPv4 address 0x%"
182 PRIx32, sdu->dest);
183 return rc;
184 }
[02a09ed]185
[a17356fd]186 addr48(nic->mac_addr, frame.src);
187 frame.etype_len = ETYPE_IP;
188 frame.data = sdu->data;
189 frame.size = sdu->size;
[02a09ed]190
[a17356fd]191 void *data;
192 size_t size;
193 rc = eth_pdu_encode(&frame, &data, &size);
194 if (rc != EOK)
195 return rc;
[02a09ed]196
[a17356fd]197 rc = ethip_nic_send(nic, data, size);
198 free(data);
199
200 return rc;
201}
202
203static int ethip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu)
204{
205 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send6()");
206
207 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
[1493811]208 eth_frame_t frame;
[02a09ed]209
[a17356fd]210 addr48(sdu->dest, frame.dest);
211 addr48(nic->mac_addr, frame.src);
212 frame.etype_len = ETYPE_IPV6;
213 frame.data = sdu->data;
214 frame.size = sdu->size;
[02a09ed]215
[1493811]216 void *data;
217 size_t size;
[a17356fd]218 int rc = eth_pdu_encode(&frame, &data, &size);
[1493811]219 if (rc != EOK)
220 return rc;
[257feec]221
[1493811]222 rc = ethip_nic_send(nic, data, size);
223 free(data);
[257feec]224
[1493811]225 return rc;
226}
227
228int ethip_received(iplink_srv_t *srv, void *data, size_t size)
229{
[a1a101d]230 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received(): srv=%p", srv);
[02a09ed]231 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
232
[a1a101d]233 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode");
[02a09ed]234
235 eth_frame_t frame;
236 int rc = eth_pdu_decode(data, size, &frame);
[4f64a523]237 if (rc != EOK) {
[a1a101d]238 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode failed");
[1493811]239 return rc;
[4f64a523]240 }
[02a09ed]241
242 iplink_recv_sdu_t sdu;
243
[87e5658c]244 switch (frame.etype_len) {
245 case ETYPE_ARP:
246 arp_received(nic, &frame);
247 break;
248 case ETYPE_IP:
[a1a101d]249 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU");
[87e5658c]250 sdu.data = frame.data;
251 sdu.size = frame.size;
[a1a101d]252 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
[417a2ba1]253 rc = iplink_ev_recv(&nic->iplink, &sdu, ip_v4);
[87e5658c]254 break;
[1d24ad3]255 case ETYPE_IPV6:
256 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU IPv6");
257 sdu.data = frame.data;
258 sdu.size = frame.size;
259 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv");
[417a2ba1]260 rc = iplink_ev_recv(&nic->iplink, &sdu, ip_v6);
[1d24ad3]261 break;
[87e5658c]262 default:
[a1a101d]263 log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ethertype 0x%" PRIx16,
[87e5658c]264 frame.etype_len);
265 }
[257feec]266
[1493811]267 free(frame.data);
268 return rc;
[e2e56e67]269}
270
[4f64a523]271static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu)
[e2e56e67]272{
[a1a101d]273 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_get_mtu()");
[e2e56e67]274 *mtu = 1500;
275 return EOK;
276}
277
[a17356fd]278static int ethip_get_mac48(iplink_srv_t *srv, addr48_t *mac)
279{
280 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_get_mac48()");
281
282 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
283 addr48(nic->mac_addr, *mac);
284
285 return EOK;
286}
287
[c3b25985]288static int ethip_set_mac48(iplink_srv_t *srv, addr48_t *mac)
289{
290 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_set_mac48()");
291
292 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
293 addr48(*mac, nic->mac_addr);
294
295 return EOK;
296}
297
[02a09ed]298static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
[962f03b]299{
[a2e3ee6]300 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
301
[962f03b]302 return ethip_nic_addr_add(nic, addr);
303}
304
[02a09ed]305static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
[962f03b]306{
[02a09ed]307 ethip_nic_t *nic = (ethip_nic_t *) srv->arg;
[a2e3ee6]308
[02a09ed]309 return ethip_nic_addr_remove(nic, addr);
[962f03b]310}
311
[e2e56e67]312int main(int argc, char *argv[])
313{
314 int rc;
315
316 printf(NAME ": HelenOS IP over Ethernet service\n");
317
[267f235]318 if (log_init(NAME) != EOK) {
[e2e56e67]319 printf(NAME ": Failed to initialize logging.\n");
320 return 1;
321 }
322
323 rc = ethip_init();
324 if (rc != EOK)
325 return 1;
326
327 printf(NAME ": Accepting connections.\n");
328 task_retval(0);
329 async_manager();
330
331 /* Not reached */
332 return 0;
333}
334
335/** @}
336 */
Note: See TracBrowser for help on using the repository browser.