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

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

Distinguish between datagram and packet. Deliver received datagrams to
clients based on protocol number.

  • Property mode set to 100644
File size: 7.8 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 log_msg(LVL_DEBUG, "ethip_nic_received() done, rc=%d", rc);
229 async_answer_0(callid, rc);
230}
231
232static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
233 ipc_call_t *call)
234{
235 log_msg(LVL_DEBUG, "ethip_nic_device_state()");
236 async_answer_0(callid, ENOTSUP);
237}
238
239static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
240{
241 ethip_nic_t *nic = (ethip_nic_t *)arg;
242
243 log_msg(LVL_DEBUG, "ethnip_nic_cb_conn()");
244
245 while (true) {
246 ipc_call_t call;
247 ipc_callid_t callid = async_get_call(&call);
248
249 if (!IPC_GET_IMETHOD(call)) {
250 /* TODO: Handle hangup */
251 return;
252 }
253
254 switch (IPC_GET_IMETHOD(call)) {
255 case NIC_EV_ADDR_CHANGED:
256 ethip_nic_addr_changed(nic, callid, &call);
257 break;
258 case NIC_EV_RECEIVED:
259 ethip_nic_received(nic, callid, &call);
260 break;
261 case NIC_EV_DEVICE_STATE:
262 ethip_nic_device_state(nic, callid, &call);
263 break;
264 default:
265 async_answer_0(callid, ENOTSUP);
266 }
267 }
268}
269
270int ethip_nic_discovery_start(void)
271{
272 int rc;
273
274 rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
275 if (rc != EOK) {
276 log_msg(LVL_ERROR, "Failed registering callback for NIC "
277 "discovery (%d).", rc);
278 return rc;
279 }
280
281 return ethip_nic_check_new();
282}
283
284ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
285{
286 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
287 (unsigned) iplink_sid);
288
289 list_foreach(ethip_nic_list, link) {
290 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
291 ethip_nic_t *nic = list_get_instance(link, ethip_nic_t,
292 nic_list);
293
294 if (nic->iplink_sid == iplink_sid) {
295 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
296 return nic;
297 }
298 }
299
300 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
301 return NULL;
302}
303
304int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
305{
306 int rc;
307 log_msg(LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
308 rc = nic_send_frame(nic->sess, data, size);
309 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
310 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
311 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
312 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
313 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
314 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
315 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
316 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
317 return rc;
318}
319
320/** @}
321 */
Note: See TracBrowser for help on using the repository browser.