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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b99f6e2 was ee95794, checked in by Agnieszka Tabaka <nufcia@…>, 11 years ago

Fix build errors on occuring on various architectures.

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