Changes in uspace/srv/net/nil/nildummy/nildummy.c [ffa2c8ef:49bd793b] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/nil/nildummy/nildummy.c
rffa2c8ef r49bd793b 1 1 /* 2 2 * Copyright (c) 2009 Lukas Mejdrech 3 * Copyright (c) 2011 Radim Vansa 3 4 * All rights reserved. 4 5 * … … 36 37 */ 37 38 39 #include <assert.h> 38 40 #include <async.h> 39 41 #include <malloc.h> … … 44 46 #include <ipc/net.h> 45 47 #include <ipc/services.h> 46 47 48 #include <net/modules.h> 48 49 #include <net/device.h> … … 51 52 #include <net/packet.h> 52 53 #include <packet_remote.h> 53 #include <netif_remote.h> 54 #include <packet_client.h> 55 #include <devman.h> 56 #include <device/nic.h> 54 57 #include <nil_skel.h> 55 56 58 #include "nildummy.h" 57 59 … … 67 69 DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t); 68 70 69 int nil_device_state_msg_local( int nil_phone, device_id_t device_id, int state)71 int nil_device_state_msg_local(nic_device_id_t device_id, sysarg_t state) 70 72 { 71 73 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 72 if (nildummy_globals.proto. phone)73 il_device_state_msg(nildummy_globals.proto. phone, device_id,74 if (nildummy_globals.proto.sess) 75 il_device_state_msg(nildummy_globals.proto.sess, device_id, 74 76 state, nildummy_globals.proto.service); 75 77 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock); … … 78 80 } 79 81 80 int nil_initialize( int net_phone)82 int nil_initialize(async_sess_t *sess) 81 83 { 82 84 fibril_rwlock_initialize(&nildummy_globals.devices_lock); … … 85 87 fibril_rwlock_write_lock(&nildummy_globals.protos_lock); 86 88 87 nildummy_globals.net_ phone = net_phone;88 nildummy_globals.proto. phone = 0;89 nildummy_globals.net_sess = sess; 90 nildummy_globals.proto.sess = NULL; 89 91 int rc = nildummy_devices_initialize(&nildummy_globals.devices); 90 92 … … 93 95 94 96 return rc; 95 }96 97 /** Process IPC messages from the registered device driver modules98 *99 * @param[in] iid Message identifier.100 * @param[in,out] icall Message parameters.101 *102 */103 static void nildummy_receiver(ipc_callid_t iid, ipc_call_t *icall)104 {105 packet_t *packet;106 int rc;107 108 while (true) {109 switch (IPC_GET_IMETHOD(*icall)) {110 case NET_NIL_DEVICE_STATE:111 rc = nil_device_state_msg_local(0,112 IPC_GET_DEVICE(*icall), IPC_GET_STATE(*icall));113 async_answer_0(iid, (sysarg_t) rc);114 break;115 116 case NET_NIL_RECEIVED:117 rc = packet_translate_remote(nildummy_globals.net_phone,118 &packet, IPC_GET_PACKET(*icall));119 if (rc == EOK)120 rc = nil_received_msg_local(0,121 IPC_GET_DEVICE(*icall), packet, 0);122 123 async_answer_0(iid, (sysarg_t) rc);124 break;125 126 default:127 async_answer_0(iid, (sysarg_t) ENOTSUP);128 }129 130 iid = async_get_call(icall);131 }132 97 } 133 98 … … 149 114 * 150 115 */ 151 static int nildummy_device_message( device_id_t device_id, services_t service,152 size_t mtu)116 static int nildummy_device_message(nic_device_id_t device_id, 117 devman_handle_t handle, size_t mtu) 153 118 { 154 119 fibril_rwlock_write_lock(&nildummy_globals.devices_lock); … … 158 123 nildummy_devices_find(&nildummy_globals.devices, device_id); 159 124 if (device) { 160 if (device-> service != service) {161 printf("Device %d already exists\n", device->device_id);162 fibril_rwlock_write_unlock(163 125 if (device->handle != handle) { 126 printf("Device %d exists, handles do not match\n", 127 device->device_id); 128 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 164 129 return EEXIST; 165 130 } … … 171 136 device->mtu = NET_DEFAULT_MTU; 172 137 173 printf("Device %d already exists :\tMTU\t= %zu\n",174 device-> device_id, device->mtu);138 printf("Device %d already exists (mtu: %zu)\n", device->device_id, 139 device->mtu); 175 140 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 176 141 177 142 /* Notify the upper layer module */ 178 143 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 179 if (nildummy_globals.proto. phone) {180 il_mtu_changed_msg(nildummy_globals.proto. phone,144 if (nildummy_globals.proto.sess) { 145 il_mtu_changed_msg(nildummy_globals.proto.sess, 181 146 device->device_id, device->mtu, 182 147 nildummy_globals.proto.service); … … 193 158 194 159 device->device_id = device_id; 195 device-> service = service;160 device->handle = handle; 196 161 if (mtu > 0) 197 162 device->mtu = mtu; 198 163 else 199 164 device->mtu = NET_DEFAULT_MTU; 200 165 201 166 /* Bind the device driver */ 202 device-> phone = netif_bind_service(device->service, device->device_id,203 SERVICE_ETHERNET, nildummy_receiver);204 if (device-> phone < 0) {167 device->sess = devman_device_connect(EXCHANGE_SERIALIZE, handle, 168 IPC_FLAG_BLOCKING); 169 if (device->sess == NULL) { 205 170 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 206 171 free(device); 207 return device->phone; 208 } 172 return ENOENT; 173 } 174 175 nic_connect_to_nil(device->sess, SERVICE_NILDUMMY, device_id); 209 176 210 177 /* Get hardware address */ 211 int rc = netif_get_addr_req(device->phone, device->device_id, 212 &device->addr, &device->addr_data); 178 int rc = nic_get_address(device->sess, &device->addr); 213 179 if (rc != EOK) { 214 180 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); … … 216 182 return rc; 217 183 } 184 185 device->addr_len = ETH_ADDR; 218 186 219 187 /* Add to the cache */ … … 222 190 if (index < 0) { 223 191 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 224 free(device->addr);225 free(device->addr_data);226 192 free(device); 227 193 return index; 228 194 } 229 195 230 printf("%s: Device registered (id: %d, service: %d, mtu: %zu)\n",231 NAME, device->device_id, device->service, device->mtu);196 printf("%s: Device registered (id: %d, mtu: %zu)\n", NAME, 197 device->device_id, device->mtu); 232 198 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 233 199 return EOK; … … 244 210 * 245 211 */ 246 static int nildummy_addr_message(device_id_t device_id, 247 measured_string_t **address) 248 { 249 if (!address) 212 static int nildummy_addr_message(nic_device_id_t device_id, size_t *addrlen) 213 { 214 if (!addrlen) 250 215 return EBADMEM; 251 216 … … 259 224 } 260 225 261 *address = device->addr; 226 ipc_callid_t callid; 227 size_t max_len; 228 if (!async_data_read_receive(&callid, &max_len)) { 229 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 230 return EREFUSED; 231 } 232 233 if (max_len < device->addr_len) { 234 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 235 async_data_read_finalize(callid, NULL, 0); 236 return ELIMIT; 237 } 238 239 int rc = async_data_read_finalize(callid, 240 (uint8_t *) &device->addr.address, device->addr_len); 241 if (rc != EOK) { 242 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 243 return rc; 244 } 245 246 *addrlen = device->addr_len; 262 247 263 248 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 264 265 return (*address) ? EOK : ENOENT; 249 return EOK; 266 250 } 267 251 … … 279 263 * 280 264 */ 281 static int nildummy_packet_space_message( device_id_t device_id, size_t *addr_len,282 size_t * prefix, size_t *content, size_t *suffix)265 static int nildummy_packet_space_message(nic_device_id_t device_id, 266 size_t *addr_len, size_t *prefix, size_t *content, size_t *suffix) 283 267 { 284 268 if ((!addr_len) || (!prefix) || (!content) || (!suffix)) … … 304 288 } 305 289 306 int nil_received_msg_local(int nil_phone, device_id_t device_id, 307 packet_t *packet, services_t target) 290 int nil_received_msg_local(nic_device_id_t device_id, packet_t *packet) 308 291 { 309 292 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 310 293 311 if (nildummy_globals.proto. phone) {294 if (nildummy_globals.proto.sess) { 312 295 do { 313 296 packet_t *next = pq_detach(packet); 314 il_received_msg(nildummy_globals.proto. phone, device_id,297 il_received_msg(nildummy_globals.proto.sess, device_id, 315 298 packet, nildummy_globals.proto.service); 316 299 packet = next; … … 328 311 * 329 312 * @param[in] service Module service. 330 * @param[in] phone Service phone.313 * @param[in] sess Service session. 331 314 * 332 315 * @return EOK on success. … … 335 318 * 336 319 */ 337 static int nildummy_register_message(services_t service, int phone)320 static int nildummy_register_message(services_t service, async_sess_t *sess) 338 321 { 339 322 fibril_rwlock_write_lock(&nildummy_globals.protos_lock); 340 323 nildummy_globals.proto.service = service; 341 nildummy_globals.proto. phone = phone;342 343 printf("%s: Protocol registered (service: % d, phone: %d)\n",344 NAME, nildummy_globals.proto.service , nildummy_globals.proto.phone);324 nildummy_globals.proto.sess = sess; 325 326 printf("%s: Protocol registered (service: %#x)\n", 327 NAME, nildummy_globals.proto.service); 345 328 346 329 fibril_rwlock_write_unlock(&nildummy_globals.protos_lock); … … 359 342 * 360 343 */ 361 static int nildummy_send_message( device_id_t device_id, packet_t *packet,344 static int nildummy_send_message(nic_device_id_t device_id, packet_t *packet, 362 345 services_t sender) 363 346 { … … 373 356 /* Send packet queue */ 374 357 if (packet) 375 netif_send_msg(device->phone, device_id, packet, 376 SERVICE_NILDUMMY); 358 nic_send_message(device->sess, packet_get_id(packet)); 377 359 378 360 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); … … 384 366 ipc_call_t *answer, size_t *answer_count) 385 367 { 386 measured_string_t *address;387 368 packet_t *packet; 388 369 size_t addrlen; … … 393 374 394 375 *answer_count = 0; 376 377 if (!IPC_GET_IMETHOD(*call)) 378 return EOK; 379 380 async_sess_t *callback = 381 async_callback_receive_start(EXCHANGE_SERIALIZE, call); 382 if (callback) 383 return nildummy_register_message(NIL_GET_PROTO(*call), callback); 384 395 385 switch (IPC_GET_IMETHOD(*call)) { 396 case IPC_M_PHONE_HUNGUP:397 return EOK;398 399 386 case NET_NIL_DEVICE: 400 387 return nildummy_device_message(IPC_GET_DEVICE(*call), 401 IPC_GET_ SERVICE(*call), IPC_GET_MTU(*call));388 IPC_GET_DEVICE_HANDLE(*call), IPC_GET_MTU(*call)); 402 389 403 390 case NET_NIL_SEND: 404 rc = packet_translate_remote(nildummy_globals.net_ phone,391 rc = packet_translate_remote(nildummy_globals.net_sess, 405 392 &packet, IPC_GET_PACKET(*call)); 406 393 if (rc != EOK) … … 422 409 423 410 case NET_NIL_ADDR: 424 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address); 411 case NET_NIL_BROADCAST_ADDR: 412 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &addrlen); 425 413 if (rc != EOK) 426 414 return rc; 427 return measured_strings_reply(address, 1); 428 429 case NET_NIL_BROADCAST_ADDR: 430 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address); 431 if (rc != EOK) 432 return rc; 433 return measured_strings_reply(address, 1); 434 435 case IPC_M_CONNECT_TO_ME: 436 return nildummy_register_message(NIL_GET_PROTO(*call), 437 IPC_GET_PHONE(*call)); 415 416 IPC_SET_ADDR(*answer, addrlen); 417 *answer_count = 1; 418 return rc; 419 case NET_NIL_DEVICE_STATE: 420 rc = nil_device_state_msg_local(IPC_GET_DEVICE(*call), 421 IPC_GET_STATE(*call)); 422 async_answer_0(callid, (sysarg_t) rc); 423 return rc; 424 425 case NET_NIL_RECEIVED: 426 rc = packet_translate_remote(nildummy_globals.net_sess, &packet, 427 IPC_GET_ARG2(*call)); 428 if (rc == EOK) 429 rc = nil_received_msg_local(IPC_GET_ARG1(*call), packet); 430 431 async_answer_0(callid, (sysarg_t) rc); 432 return rc; 438 433 } 439 434
Note:
See TracChangeset
for help on using the changeset viewer.