source: mainline/uspace/srv/net/ethip/ethip_nic.c@ 132ab5d1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 132ab5d1 was c1694b6b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Add str_error() in numerous places.

  • Property mode set to 100644
File size: 11.6 KB
RevLine 
[06295a9]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
[06295a9]30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
[bc38578]37#include <adt/list.h>
[06295a9]38#include <async.h>
[3e6a98c5]39#include <stdbool.h>
[06295a9]40#include <errno.h>
[c1694b6b]41#include <str_error.h>
[06295a9]42#include <fibril_synch.h>
43#include <inet/iplink_srv.h>
44#include <io/log.h>
45#include <loc.h>
[7858acbf]46#include <nic_iface.h>
[06295a9]47#include <stdlib.h>
[83781a22]48#include <mem.h>
[06295a9]49#include "ethip.h"
50#include "ethip_nic.h"
[56792a2]51#include "pdu.h"
[06295a9]52
53static int ethip_nic_open(service_id_t sid);
54static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
55
56static LIST_INITIALIZE(ethip_nic_list);
57static FIBRIL_MUTEX_INITIALIZE(ethip_discovery_lock);
58
59static int ethip_nic_check_new(void)
60{
61 bool already_known;
62 category_id_t iplink_cat;
63 service_id_t *svcs;
64 size_t count, i;
65 int rc;
66
67 fibril_mutex_lock(&ethip_discovery_lock);
68
69 rc = loc_category_get_id("nic", &iplink_cat, IPC_FLAG_BLOCKING);
70 if (rc != EOK) {
[a1a101d]71 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'nic'.");
[06295a9]72 fibril_mutex_unlock(&ethip_discovery_lock);
73 return ENOENT;
74 }
75
76 rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
77 if (rc != EOK) {
[a1a101d]78 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
[06295a9]79 fibril_mutex_unlock(&ethip_discovery_lock);
80 return EIO;
81 }
82
83 for (i = 0; i < count; i++) {
84 already_known = false;
85
[feeac0d]86 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
[06295a9]87 if (nic->svc_id == svcs[i]) {
88 already_known = true;
89 break;
90 }
91 }
92
93 if (!already_known) {
[a1a101d]94 log_msg(LOG_DEFAULT, LVL_DEBUG, "Found NIC '%lu'",
[06295a9]95 (unsigned long) svcs[i]);
96 rc = ethip_nic_open(svcs[i]);
97 if (rc != EOK)
[a1a101d]98 log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open NIC.");
[06295a9]99 }
100 }
101
102 fibril_mutex_unlock(&ethip_discovery_lock);
103 return EOK;
104}
105
106static ethip_nic_t *ethip_nic_new(void)
107{
108 ethip_nic_t *nic = calloc(1, sizeof(ethip_nic_t));
109 if (nic == NULL) {
[a1a101d]110 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC structure. "
[06295a9]111 "Out of memory.");
112 return NULL;
113 }
[83781a22]114
[65f991e]115 link_initialize(&nic->link);
[962f03b]116 list_initialize(&nic->addr_list);
[83781a22]117
[06295a9]118 return nic;
119}
120
[02a09ed]121static ethip_link_addr_t *ethip_nic_addr_new(inet_addr_t *addr)
[962f03b]122{
123 ethip_link_addr_t *laddr = calloc(1, sizeof(ethip_link_addr_t));
124 if (laddr == NULL) {
[a1a101d]125 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC address structure. "
[962f03b]126 "Out of memory.");
127 return NULL;
128 }
[a2e3ee6]129
[65f991e]130 link_initialize(&laddr->link);
[02a09ed]131 laddr->addr = *addr;
[a2e3ee6]132
[962f03b]133 return laddr;
134}
135
[06295a9]136static void ethip_nic_delete(ethip_nic_t *nic)
137{
138 if (nic->svc_name != NULL)
139 free(nic->svc_name);
[83781a22]140
[06295a9]141 free(nic);
142}
143
[962f03b]144static void ethip_link_addr_delete(ethip_link_addr_t *laddr)
145{
146 free(laddr);
147}
148
[06295a9]149static int ethip_nic_open(service_id_t sid)
150{
151 bool in_list = false;
[56792a2]152 nic_address_t nic_address;
[1038a9c]153
[a1a101d]154 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_open()");
[1038a9c]155 ethip_nic_t *nic = ethip_nic_new();
[06295a9]156 if (nic == NULL)
157 return ENOMEM;
[1038a9c]158
159 int rc = loc_service_get_name(sid, &nic->svc_name);
[06295a9]160 if (rc != EOK) {
[a1a101d]161 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
[06295a9]162 goto error;
163 }
[1038a9c]164
[f9b2cb4c]165 nic->sess = loc_service_connect(sid, INTERFACE_DDF, 0);
[06295a9]166 if (nic->sess == NULL) {
[a1a101d]167 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", nic->svc_name);
[06295a9]168 goto error;
169 }
[1038a9c]170
[06295a9]171 nic->svc_id = sid;
[1038a9c]172
[06295a9]173 rc = nic_callback_create(nic->sess, ethip_nic_cb_conn, nic);
174 if (rc != EOK) {
[a1a101d]175 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating callback connection "
[06295a9]176 "from '%s'", nic->svc_name);
177 goto error;
178 }
179
[a1a101d]180 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
[65f991e]181 list_append(&nic->link, &ethip_nic_list);
[06295a9]182 in_list = true;
183
184 rc = ethip_iplink_init(nic);
185 if (rc != EOK)
186 goto error;
187
[56792a2]188 rc = nic_get_address(nic->sess, &nic_address);
189 if (rc != EOK) {
[a1a101d]190 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting MAC address of NIC '%s'.",
[56792a2]191 nic->svc_name);
192 goto error;
193 }
[02a09ed]194
195 addr48(nic_address.address, nic->mac_addr);
[56792a2]196
[4f64a523]197 rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE);
198 if (rc != EOK) {
[a1a101d]199 log_msg(LOG_DEFAULT, LVL_ERROR, "Error activating NIC '%s'.",
[4f64a523]200 nic->svc_name);
201 goto error;
202 }
203
[695b6ff]204 rc = nic_broadcast_set_mode(nic->sess, NIC_BROADCAST_ACCEPTED);
205 if (rc != EOK) {
206 log_msg(LOG_DEFAULT, LVL_ERROR, "Error enabling "
207 "reception of broadcast frames on '%s'.", nic->svc_name);
208 goto error;
209 }
210
[02a09ed]211 log_msg(LOG_DEFAULT, LVL_DEBUG, "Initialized IP link service,");
[06295a9]212
213 return EOK;
214
215error:
216 if (in_list)
[65f991e]217 list_remove(&nic->link);
218
[06295a9]219 if (nic->sess != NULL)
220 async_hangup(nic->sess);
[65f991e]221
[06295a9]222 ethip_nic_delete(nic);
223 return rc;
224}
225
226static void ethip_nic_cat_change_cb(void)
227{
228 (void) ethip_nic_check_new();
229}
230
231static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_callid_t callid,
232 ipc_call_t *call)
233{
[1f1fa64]234 uint8_t *addr;
235 size_t size;
236 int rc;
237
238 rc = async_data_write_accept((void **)&addr, false, 0, 0, 0, &size);
239 if (rc != EOK) {
240 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
241 return;
242 }
243
244 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_changed(): "
245 "new addr=%02x:%02x:%02x:%02x:%02x:%02x",
246 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
247
[c3b25985]248 memcpy(&nic->mac_addr, addr, sizeof(nic->mac_addr));
249
250 rc = iplink_ev_change_addr(&nic->iplink, &nic->mac_addr);
251 if (rc != EOK) {
252 log_msg(LOG_DEFAULT, LVL_DEBUG, "iplink_ev_change_addr() failed");
253 return;
254 }
255
[1f1fa64]256 free(addr);
257 async_answer_0(callid, EOK);
[06295a9]258}
259
260static void ethip_nic_received(ethip_nic_t *nic, ipc_callid_t callid,
261 ipc_call_t *call)
262{
[1493811]263 int rc;
264 void *data;
265 size_t size;
266
[a1a101d]267 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
[1493811]268
269 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
270 if (rc != EOK) {
[a1a101d]271 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
[1493811]272 return;
273 }
274
[a1a101d]275 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
[df15e5f]276 size);
277
[a1a101d]278 log_msg(LOG_DEFAULT, LVL_DEBUG, "call ethip_received");
[1493811]279 rc = ethip_received(&nic->iplink, data, size);
[a1a101d]280 log_msg(LOG_DEFAULT, LVL_DEBUG, "free data");
[1493811]281 free(data);
282
[c1694b6b]283 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() done, rc=%s", str_error_name(rc));
[1493811]284 async_answer_0(callid, rc);
[06295a9]285}
286
287static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
288 ipc_call_t *call)
289{
[a1a101d]290 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_device_state()");
[06295a9]291 async_answer_0(callid, ENOTSUP);
292}
293
294static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
295{
[4f64a523]296 ethip_nic_t *nic = (ethip_nic_t *)arg;
[06295a9]297
[a1a101d]298 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethnip_nic_cb_conn()");
[06295a9]299
300 while (true) {
301 ipc_call_t call;
302 ipc_callid_t callid = async_get_call(&call);
303
304 if (!IPC_GET_IMETHOD(call)) {
305 /* TODO: Handle hangup */
306 return;
307 }
308
309 switch (IPC_GET_IMETHOD(call)) {
310 case NIC_EV_ADDR_CHANGED:
311 ethip_nic_addr_changed(nic, callid, &call);
312 break;
313 case NIC_EV_RECEIVED:
314 ethip_nic_received(nic, callid, &call);
315 break;
316 case NIC_EV_DEVICE_STATE:
317 ethip_nic_device_state(nic, callid, &call);
318 break;
319 default:
[ee95794]320 log_msg(LOG_DEFAULT, LVL_DEBUG, "unknown IPC method: %" PRIun, IPC_GET_IMETHOD(call));
[06295a9]321 async_answer_0(callid, ENOTSUP);
322 }
323 }
324}
325
326int ethip_nic_discovery_start(void)
327{
[1038a9c]328 int rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
[06295a9]329 if (rc != EOK) {
[a1a101d]330 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for NIC "
[c1694b6b]331 "discovery: %s.", str_error(rc));
[06295a9]332 return rc;
333 }
[1038a9c]334
[06295a9]335 return ethip_nic_check_new();
336}
337
[bc38578]338ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
339{
[a1a101d]340 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
[bc38578]341 (unsigned) iplink_sid);
342
[feeac0d]343 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
[a1a101d]344 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
[bc38578]345 if (nic->iplink_sid == iplink_sid) {
[a1a101d]346 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
[bc38578]347 return nic;
348 }
349 }
350
[a1a101d]351 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
[bc38578]352 return NULL;
353}
354
[1493811]355int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
356{
[fe4310f]357 int rc;
[a1a101d]358 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
[fe4310f]359 rc = nic_send_frame(nic->sess, data, size);
[c1694b6b]360 log_msg(LOG_DEFAULT, LVL_DEBUG, "nic_send_frame -> %s", str_error_name(rc));
[fe4310f]361 return rc;
[1493811]362}
363
[83781a22]364/** Setup accepted multicast addresses
365 *
366 * Currently the set of accepted multicast addresses is
367 * determined only based on IPv6 addresses.
368 *
369 */
370static int ethip_nic_setup_multicast(ethip_nic_t *nic)
371{
372 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_setup_multicast()");
373
374 /* Count the number of multicast addresses */
375
376 size_t count = 0;
377
[feeac0d]378 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
[f023251]379 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, NULL);
380 if (ver == ip_v6)
[83781a22]381 count++;
382 }
383
384 if (count == 0)
385 return nic_multicast_set_mode(nic->sess, NIC_MULTICAST_BLOCKED,
386 NULL, 0);
387
388 nic_address_t *mac_list = calloc(count, sizeof(nic_address_t));
389 if (mac_list == NULL)
390 return ENOMEM;
391
392 /* Create the multicast MAC list */
393
394 size_t i = 0;
395
[feeac0d]396 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
[83781a22]397 addr128_t v6;
[f023251]398 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, &v6);
399 if (ver != ip_v6)
[83781a22]400 continue;
401
[24bb363]402 assert(i < count);
403
[83781a22]404 addr48_t mac;
405 addr48_solicited_node(v6, mac);
406
407 /* Avoid duplicate addresses in the list */
408
409 bool found = false;
410
411 for (size_t j = 0; j < i; j++) {
412 if (addr48_compare(mac_list[j].address, mac)) {
413 found = true;
414 break;
415 }
416 }
417
418 if (!found) {
419 addr48(mac, mac_list[i].address);
420 i++;
421 } else
422 count--;
423 }
424
425 /* Setup the multicast MAC list */
426
427 int rc = nic_multicast_set_mode(nic->sess, NIC_MULTICAST_LIST,
428 mac_list, count);
429
430 free(mac_list);
431 return rc;
432}
433
[02a09ed]434int ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr)
[962f03b]435{
[a1a101d]436 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()");
[a2e3ee6]437
438 ethip_link_addr_t *laddr = ethip_nic_addr_new(addr);
[962f03b]439 if (laddr == NULL)
440 return ENOMEM;
[a2e3ee6]441
[65f991e]442 list_append(&laddr->link, &nic->addr_list);
[83781a22]443
444 return ethip_nic_setup_multicast(nic);
[962f03b]445}
446
[02a09ed]447int ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr)
[962f03b]448{
[a1a101d]449 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()");
[a2e3ee6]450
451 ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, addr);
[962f03b]452 if (laddr == NULL)
453 return ENOENT;
[a2e3ee6]454
[65f991e]455 list_remove(&laddr->link);
[962f03b]456 ethip_link_addr_delete(laddr);
[83781a22]457
458 return ethip_nic_setup_multicast(nic);
[962f03b]459}
460
461ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
[02a09ed]462 inet_addr_t *addr)
[962f03b]463{
[a1a101d]464 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()");
[257feec]465
[feeac0d]466 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
[02a09ed]467 if (inet_addr_compare(addr, &laddr->addr))
[962f03b]468 return laddr;
469 }
[257feec]470
[962f03b]471 return NULL;
472}
473
[06295a9]474/** @}
475 */
Note: See TracBrowser for help on using the repository browser.