source: mainline/uspace/srv/ethip/ethip_nic.c@ df15e5f

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

Fix bug in MAC address decoding.

  • Property mode set to 100644
File size: 7.3 KB
Line 
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
29/** @addtogroup ethip
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <adt/list.h>
38#include <async.h>
39#include <bool.h>
40#include <errno.h>
41#include <fibril_synch.h>
42#include <inet/iplink_srv.h>
43#include <io/log.h>
44#include <loc.h>
45#include <device/nic.h>
46#include <stdlib.h>
47
48#include "ethip.h"
49#include "ethip_nic.h"
50
51static int ethip_nic_open(service_id_t sid);
52static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
53
54static LIST_INITIALIZE(ethip_nic_list);
55static FIBRIL_MUTEX_INITIALIZE(ethip_discovery_lock);
56
57static int ethip_nic_check_new(void)
58{
59 bool already_known;
60 category_id_t iplink_cat;
61 service_id_t *svcs;
62 size_t count, i;
63 int rc;
64
65 fibril_mutex_lock(&ethip_discovery_lock);
66
67 rc = loc_category_get_id("nic", &iplink_cat, IPC_FLAG_BLOCKING);
68 if (rc != EOK) {
69 log_msg(LVL_ERROR, "Failed resolving category 'nic'.");
70 fibril_mutex_unlock(&ethip_discovery_lock);
71 return ENOENT;
72 }
73
74 rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
75 if (rc != EOK) {
76 log_msg(LVL_ERROR, "Failed getting list of IP links.");
77 fibril_mutex_unlock(&ethip_discovery_lock);
78 return EIO;
79 }
80
81 for (i = 0; i < count; i++) {
82 already_known = false;
83
84 list_foreach(ethip_nic_list, nic_link) {
85 ethip_nic_t *nic = list_get_instance(nic_link,
86 ethip_nic_t, nic_list);
87 if (nic->svc_id == svcs[i]) {
88 already_known = true;
89 break;
90 }
91 }
92
93 if (!already_known) {
94 log_msg(LVL_DEBUG, "Found NIC '%lu'",
95 (unsigned long) svcs[i]);
96 rc = ethip_nic_open(svcs[i]);
97 if (rc != EOK)
98 log_msg(LVL_ERROR, "Could not open NIC.");
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
110 if (nic == NULL) {
111 log_msg(LVL_ERROR, "Failed allocating NIC structure. "
112 "Out of memory.");
113 return NULL;
114 }
115
116 link_initialize(&nic->nic_list);
117
118 return nic;
119}
120
121static void ethip_nic_delete(ethip_nic_t *nic)
122{
123 if (nic->svc_name != NULL)
124 free(nic->svc_name);
125 free(nic);
126}
127
128static int ethip_nic_open(service_id_t sid)
129{
130 ethip_nic_t *nic;
131 int rc;
132 bool in_list = false;
133
134 log_msg(LVL_DEBUG, "ethip_nic_open()");
135 nic = ethip_nic_new();
136 if (nic == NULL)
137 return ENOMEM;
138
139 rc = loc_service_get_name(sid, &nic->svc_name);
140 if (rc != EOK) {
141 log_msg(LVL_ERROR, "Failed getting service name.");
142 goto error;
143 }
144
145 nic->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
146 if (nic->sess == NULL) {
147 log_msg(LVL_ERROR, "Failed connecting '%s'", nic->svc_name);
148 goto error;
149 }
150
151 nic->svc_id = sid;
152
153 rc = nic_callback_create(nic->sess, ethip_nic_cb_conn, nic);
154 if (rc != EOK) {
155 log_msg(LVL_ERROR, "Failed creating callback connection "
156 "from '%s'", nic->svc_name);
157 goto error;
158 }
159
160 log_msg(LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
161 list_append(&nic->nic_list, &ethip_nic_list);
162 in_list = true;
163
164 rc = ethip_iplink_init(nic);
165 if (rc != EOK)
166 goto error;
167
168 rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE);
169 if (rc != EOK) {
170 log_msg(LVL_ERROR, "Failed activating NIC '%s'.",
171 nic->svc_name);
172 goto error;
173 }
174
175 log_msg(LVL_DEBUG, "Initialized IP link service.");
176
177 return EOK;
178
179error:
180 if (in_list)
181 list_remove(&nic->nic_list);
182 if (nic->sess != NULL)
183 async_hangup(nic->sess);
184 ethip_nic_delete(nic);
185 return rc;
186}
187
188static void ethip_nic_cat_change_cb(void)
189{
190 (void) ethip_nic_check_new();
191}
192
193static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_callid_t callid,
194 ipc_call_t *call)
195{
196 log_msg(LVL_DEBUG, "ethip_nic_addr_changed()");
197 async_answer_0(callid, ENOTSUP);
198}
199
200#include <stdio.h>
201static void ethip_nic_received(ethip_nic_t *nic, ipc_callid_t callid,
202 ipc_call_t *call)
203{
204 int rc;
205 void *data;
206 size_t size;
207
208 log_msg(LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
209
210 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
211 if (rc != EOK) {
212 log_msg(LVL_DEBUG, "data_write_accept() failed");
213 return;
214 }
215
216 log_msg(LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
217 size);
218 size_t i;
219 for (i = 0; i < size; i++)
220 printf("%02x ", ((uint8_t *)data)[i]);
221 printf("\n");
222
223 log_msg(LVL_DEBUG, "call ethip_received");
224 rc = ethip_received(&nic->iplink, data, size);
225 log_msg(LVL_DEBUG, "free data");
226 free(data);
227
228 async_answer_0(callid, rc);
229}
230
231static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
232 ipc_call_t *call)
233{
234 log_msg(LVL_DEBUG, "ethip_nic_device_state()");
235 async_answer_0(callid, ENOTSUP);
236}
237
238static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
239{
240 ethip_nic_t *nic = (ethip_nic_t *)arg;
241
242 log_msg(LVL_DEBUG, "ethnip_nic_cb_conn()");
243
244 while (true) {
245 ipc_call_t call;
246 ipc_callid_t callid = async_get_call(&call);
247
248 if (!IPC_GET_IMETHOD(call)) {
249 /* TODO: Handle hangup */
250 return;
251 }
252
253 switch (IPC_GET_IMETHOD(call)) {
254 case NIC_EV_ADDR_CHANGED:
255 ethip_nic_addr_changed(nic, callid, &call);
256 break;
257 case NIC_EV_RECEIVED:
258 ethip_nic_received(nic, callid, &call);
259 break;
260 case NIC_EV_DEVICE_STATE:
261 ethip_nic_device_state(nic, callid, &call);
262 break;
263 default:
264 async_answer_0(callid, ENOTSUP);
265 }
266 }
267}
268
269int ethip_nic_discovery_start(void)
270{
271 int rc;
272
273 rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
274 if (rc != EOK) {
275 log_msg(LVL_ERROR, "Failed registering callback for NIC "
276 "discovery (%d).", rc);
277 return rc;
278 }
279
280 return ethip_nic_check_new();
281}
282
283ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
284{
285 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
286 (unsigned) iplink_sid);
287
288 list_foreach(ethip_nic_list, link) {
289 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
290 ethip_nic_t *nic = list_get_instance(link, ethip_nic_t,
291 nic_list);
292
293 if (nic->iplink_sid == iplink_sid) {
294 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
295 return nic;
296 }
297 }
298
299 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
300 return NULL;
301}
302
303int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
304{
305 return nic_send_frame(nic->sess, data, size);
306}
307
308/** @}
309 */
Note: See TracBrowser for help on using the repository browser.