Changes in uspace/srv/net/nil/nildummy/nildummy.c [14f1db0:19f857a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/nil/nildummy/nildummy.c
r14f1db0 r19f857a 28 28 29 29 /** @addtogroup nildummy 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Dummy network interface layer module implementation.35 * @see nildummy.h34 * Dummy network interface layer module implementation. 35 * @see nildummy.h 36 36 */ 37 37 … … 41 41 #include <stdio.h> 42 42 #include <str.h> 43 43 44 #include <ipc/ipc.h> 44 45 #include <ipc/services.h> 45 46 46 #include <net_err.h> 47 #include <net_messages.h> 48 #include <net_modules.h> 49 #include <net_device.h> 50 #include <netif_interface.h> 51 #include <nil_interface.h> 52 #include <il_interface.h> 53 #include <adt/measured_strings.h> 54 #include <packet/packet.h> 55 #include <packet_remote.h> 56 #include <nil_local.h> 47 #include "../../err.h" 48 #include "../../messages.h" 49 #include "../../modules.h" 50 51 #include "../../include/device.h" 52 #include "../../include/netif_interface.h" 53 #include "../../include/nil_interface.h" 54 #include "../../include/il_interface.h" 55 56 #include "../../structures/measured_strings.h" 57 #include "../../structures/packet/packet.h" 58 59 #include "../nil_module.h" 57 60 58 61 #include "nildummy.h" 59 62 60 /** The module name.61 *62 */63 #define NAME "nildummy"64 65 63 /** Default maximum transmission unit. 66 * 67 */ 68 #define NET_DEFAULT_MTU 1500 64 */ 65 #define NET_DEFAULT_MTU 1500 69 66 70 67 /** Network interface layer module global data. 71 * 72 */ 73 nildummy_globals_t nildummy_globals; 74 75 DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t); 76 77 int nil_device_state_msg_local(int nil_phone, device_id_t device_id, int state) 78 { 68 */ 69 nildummy_globals_t nildummy_globals; 70 71 /** @name Message processing functions 72 */ 73 /*@{*/ 74 75 /** Processes IPC messages from the registered device driver modules in an infinite loop. 76 * @param[in] iid The message identifier. 77 * @param[in,out] icall The message parameters. 78 */ 79 void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall); 80 81 /** Registers new device or updates the MTU of an existing one. 82 * Determines the device local hardware address. 83 * @param[in] device_id The new device identifier. 84 * @param[in] service The device driver service. 85 * @param[in] mtu The device maximum transmission unit. 86 * @returns EOK on success. 87 * @returns EEXIST if the device with the different service exists. 88 * @returns ENOMEM if there is not enough memory left. 89 * @returns Other error codes as defined for the netif_bind_service() function. 90 * @returns Other error codes as defined for the netif_get_addr_req() function. 91 */ 92 int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu); 93 94 /** Returns the device packet dimensions for sending. 95 * @param[in] device_id The device identifier. 96 * @param[out] addr_len The minimum reserved address length. 97 * @param[out] prefix The minimum reserved prefix size. 98 * @param[out] content The maximum content size. 99 * @param[out] suffix The minimum reserved suffix size. 100 * @returns EOK on success. 101 * @returns EBADMEM if either one of the parameters is NULL. 102 * @returns ENOENT if there is no such device. 103 */ 104 int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix); 105 106 /** Registers receiving module service. 107 * Passes received packets for this service. 108 * @param[in] service The module service. 109 * @param[in] phone The service phone. 110 * @returns EOK on success. 111 * @returns ENOENT if the service is not known. 112 * @returns ENOMEM if there is not enough memory left. 113 */ 114 int nildummy_register_message(services_t service, int phone); 115 116 /** Sends the packet queue. 117 * @param[in] device_id The device identifier. 118 * @param[in] packet The packet queue. 119 * @param[in] sender The sending module service. 120 * @returns EOK on success. 121 * @returns ENOENT if there no such device. 122 * @returns EINVAL if the service parameter is not known. 123 */ 124 int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender); 125 126 /** Returns the device hardware address. 127 * @param[in] device_id The device identifier. 128 * @param[out] address The device hardware address. 129 * @returns EOK on success. 130 * @returns EBADMEM if the address parameter is NULL. 131 * @returns ENOENT if there no such device. 132 */ 133 int nildummy_addr_message(device_id_t device_id, measured_string_ref * address); 134 135 /*@}*/ 136 137 DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t) 138 139 int nil_device_state_msg(int nil_phone, device_id_t device_id, int state){ 79 140 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 80 81 if (nildummy_globals.proto.phone) 82 il_device_state_msg(nildummy_globals.proto.phone, device_id, state, 83 nildummy_globals.proto.service); 84 141 if(nildummy_globals.proto.phone){ 142 il_device_state_msg(nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service); 143 } 85 144 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock); 86 87 return EOK; 88 } 89 90 int nil_initialize(int net_phone) 91 { 145 return EOK; 146 } 147 148 int nil_initialize(int net_phone){ 92 149 ERROR_DECLARE; 93 150 94 151 fibril_rwlock_initialize(&nildummy_globals.devices_lock); 95 152 fibril_rwlock_initialize(&nildummy_globals.protos_lock); 96 153 fibril_rwlock_write_lock(&nildummy_globals.devices_lock); 97 154 fibril_rwlock_write_lock(&nildummy_globals.protos_lock); 98 99 155 nildummy_globals.net_phone = net_phone; 100 156 nildummy_globals.proto.phone = 0; 101 157 ERROR_PROPAGATE(nildummy_devices_initialize(&nildummy_globals.devices)); 102 103 158 fibril_rwlock_write_unlock(&nildummy_globals.protos_lock); 104 159 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 105 106 return EOK; 107 } 108 109 /** Process IPC messages from the registered device driver modules in an infinite loop. 110 * 111 * @param[in] iid The message identifier. 112 * @param[in,out] icall The message parameters. 113 * 114 */ 115 static void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall){ 116 ERROR_DECLARE; 117 118 packet_t packet; 119 120 while(true){ 121 switch(IPC_GET_METHOD(*icall)){ 122 case NET_NIL_DEVICE_STATE: 123 ERROR_CODE = nil_device_state_msg_local(0, IPC_GET_DEVICE(icall), IPC_GET_STATE(icall)); 124 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE); 125 break; 126 case NET_NIL_RECEIVED: 127 if(! ERROR_OCCURRED(packet_translate_remote(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(icall)))){ 128 ERROR_CODE = nil_received_msg_local(0, IPC_GET_DEVICE(icall), packet, 0); 129 } 130 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE); 131 break; 132 default: 133 ipc_answer_0(iid, (ipcarg_t) ENOTSUP); 134 } 135 iid = async_get_call(icall); 136 } 137 } 138 139 /** Register new device or updates the MTU of an existing one. 140 * 141 * Determine the device local hardware address. 142 * 143 * @param[in] device_id The new device identifier. 144 * @param[in] service The device driver service. 145 * @param[in] mtu The device maximum transmission unit. 146 * 147 * @returns EOK on success. 148 * @returns EEXIST if the device with the different service exists. 149 * @returns ENOMEM if there is not enough memory left. 150 * @returns Other error codes as defined for the netif_bind_service() function. 151 * @returns Other error codes as defined for the netif_get_addr_req() function. 152 * 153 */ 154 static int nildummy_device_message(device_id_t device_id, services_t service, 155 size_t mtu) 156 { 160 return EOK; 161 } 162 163 int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu){ 157 164 ERROR_DECLARE; 158 165 … … 220 227 return index; 221 228 } 222 printf("%s: Device registered (id: %d, service: %d, mtu: %d)\n", 223 NAME, device->device_id, device->service, device->mtu); 229 printf("New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu); 224 230 } 225 231 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); … … 227 233 } 228 234 229 /** Return the device hardware address. 230 * 231 * @param[in] device_id The device identifier. 232 * @param[out] address The device hardware address. 233 * 234 * @return EOK on success. 235 * @return EBADMEM if the address parameter is NULL. 236 * @return ENOENT if there no such device. 237 * 238 */ 239 static int nildummy_addr_message(device_id_t device_id, 240 measured_string_ref *address) 241 { 235 int nildummy_addr_message(device_id_t device_id, measured_string_ref * address){ 242 236 nildummy_device_ref device; 243 237 … … 256 250 } 257 251 258 /** Return the device packet dimensions for sending. 259 * 260 * @param[in] device_id The device identifier. 261 * @param[out] addr_len The minimum reserved address length. 262 * @param[out] prefix The minimum reserved prefix size. 263 * @param[out] content The maximum content size. 264 * @param[out] suffix The minimum reserved suffix size. 265 * 266 * @return EOK on success. 267 * @return EBADMEM if either one of the parameters is NULL. 268 * @return ENOENT if there is no such device. 269 * 270 */ 271 static int nildummy_packet_space_message(device_id_t device_id, 272 size_t *addr_len, size_t *prefix, size_t *content, size_t *suffix) 273 { 252 int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix){ 274 253 nildummy_device_ref device; 275 254 … … 291 270 } 292 271 293 int nil_received_msg _local(int nil_phone, device_id_t device_id, packet_t packet, services_t target){272 int nil_received_msg(int nil_phone, device_id_t device_id, packet_t packet, services_t target){ 294 273 packet_t next; 295 274 … … 306 285 } 307 286 308 /** Register receiving module service. 309 * 310 * Pass received packets for this service. 311 * 312 * @param[in] service The module service. 313 * @param[in] phone The service phone. 314 * 315 * @return EOK on success. 316 * @return ENOENT if the service is not known. 317 * @return ENOMEM if there is not enough memory left. 318 * 319 */ 320 static int nildummy_register_message(services_t service, int phone) 321 { 287 int nildummy_register_message(services_t service, int phone){ 322 288 fibril_rwlock_write_lock(&nildummy_globals.protos_lock); 323 289 nildummy_globals.proto.service = service; 324 290 nildummy_globals.proto.phone = phone; 325 326 printf("%s: Protocol registered (service: %d, phone: %d)\n", 327 NAME, nildummy_globals.proto.service, nildummy_globals.proto.phone); 328 291 printf("New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone); 329 292 fibril_rwlock_write_unlock(&nildummy_globals.protos_lock); 330 293 return EOK; 331 294 } 332 295 333 /** Send the packet queue. 334 * 335 * @param[in] device_id The device identifier. 336 * @param[in] packet The packet queue. 337 * @param[in] sender The sending module service. 338 * 339 * @return EOK on success. 340 * @return ENOENT if there no such device. 341 * @return EINVAL if the service parameter is not known. 342 * 343 */ 344 static int nildummy_send_message(device_id_t device_id, packet_t packet, 345 services_t sender) 346 { 296 int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender){ 347 297 nildummy_device_ref device; 348 298 … … 361 311 } 362 312 363 int nil_message_standalone(const char *name, ipc_callid_t callid, ipc_call_t *call, 364 ipc_call_t *answer, int *answer_count) 365 { 313 int nil_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){ 366 314 ERROR_DECLARE; 367 315 368 316 measured_string_ref address; 369 317 packet_t packet; 370 size_t addrlen; 371 size_t prefix; 372 size_t suffix; 373 size_t content; 374 318 319 // printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NIL_FIRST); 375 320 *answer_count = 0; 376 switch (IPC_GET_METHOD(*call)){321 switch(IPC_GET_METHOD(*call)){ 377 322 case IPC_M_PHONE_HUNGUP: 378 323 return EOK; 379 324 case NET_NIL_DEVICE: 380 return nildummy_device_message(IPC_GET_DEVICE(call), 381 IPC_GET_SERVICE(call), IPC_GET_MTU(call)); 325 return nildummy_device_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), IPC_GET_MTU(call)); 382 326 case NET_NIL_SEND: 383 ERROR_PROPAGATE(packet_translate_remote(nildummy_globals.net_phone, 384 &packet, IPC_GET_PACKET(call))); 385 return nildummy_send_message(IPC_GET_DEVICE(call), packet, 386 IPC_GET_SERVICE(call)); 327 ERROR_PROPAGATE(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(call))); 328 return nildummy_send_message(IPC_GET_DEVICE(call), packet, IPC_GET_SERVICE(call)); 387 329 case NET_NIL_PACKET_SPACE: 388 ERROR_PROPAGATE(nildummy_packet_space_message(IPC_GET_DEVICE(call), 389 &addrlen, &prefix, &content, &suffix)); 390 IPC_SET_ADDR(answer, addrlen); 391 IPC_SET_PREFIX(answer, prefix); 392 IPC_SET_CONTENT(answer, content); 393 IPC_SET_SUFFIX(answer, suffix); 330 ERROR_PROPAGATE(nildummy_packet_space_message(IPC_GET_DEVICE(call), IPC_SET_ADDR(answer), IPC_SET_PREFIX(answer), IPC_SET_CONTENT(answer), IPC_SET_SUFFIX(answer))); 394 331 *answer_count = 4; 395 332 return EOK; 396 333 case NET_NIL_ADDR: 397 ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), 398 &address)); 399 return measured_strings_reply(address, 1); 400 case NET_NIL_BROADCAST_ADDR: 401 ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), 402 &address)); 334 ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), &address)); 403 335 return measured_strings_reply(address, 1); 404 336 case IPC_M_CONNECT_TO_ME: 405 return nildummy_register_message(NIL_GET_PROTO(call), 406 IPC_GET_PHONE(call)); 407 } 408 337 return nildummy_register_message(NIL_GET_PROTO(call), IPC_GET_PHONE(call)); 338 } 409 339 return ENOTSUP; 410 340 } 411 341 412 #ifndef CONFIG_NETIF_NIL_BUNDLE 413 414 /** Default thread for new connections. 415 * 416 * @param[in] iid The initial message identifier. 417 * @param[in] icall The initial message call structure. 418 * 419 */ 420 static void nil_client_connection(ipc_callid_t iid, ipc_call_t *icall) 421 { 422 /* 423 * Accept the connection 424 * - Answer the first IPC_M_CONNECT_ME_TO call. 425 */ 426 ipc_answer_0(iid, EOK); 427 428 while(true) { 429 ipc_call_t answer; 430 int answer_count; 431 432 /* Clear the answer structure */ 433 refresh_answer(&answer, &answer_count); 434 435 /* Fetch the next message */ 436 ipc_call_t call; 437 ipc_callid_t callid = async_get_call(&call); 438 439 /* Process the message */ 440 int res = nil_module_message_standalone(NAME, callid, &call, &answer, 441 &answer_count); 442 443 /* End if said to either by the message or the processing result */ 444 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP)) 445 return; 446 447 /* Answer the message */ 448 answer_call(callid, res, &answer, answer_count); 449 } 450 } 451 452 int main(int argc, char *argv[]) 453 { 342 void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall){ 454 343 ERROR_DECLARE; 455 456 /* Start the module */ 457 if (ERROR_OCCURRED(nil_module_start_standalone(nil_client_connection))) 458 return ERROR_CODE; 459 460 return EOK; 461 } 462 463 #endif /* CONFIG_NETIF_NIL_BUNDLE */ 344 345 packet_t packet; 346 347 while(true){ 348 // printf("message %d - %d\n", IPC_GET_METHOD(*icall), NET_NIL_FIRST); 349 switch(IPC_GET_METHOD(*icall)){ 350 case NET_NIL_DEVICE_STATE: 351 ERROR_CODE = nil_device_state_msg(0, IPC_GET_DEVICE(icall), IPC_GET_STATE(icall)); 352 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE); 353 break; 354 case NET_NIL_RECEIVED: 355 if(! ERROR_OCCURRED(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(icall)))){ 356 ERROR_CODE = nil_received_msg(0, IPC_GET_DEVICE(icall), packet, 0); 357 } 358 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE); 359 break; 360 default: 361 ipc_answer_0(iid, (ipcarg_t) ENOTSUP); 362 } 363 iid = async_get_call(icall); 364 } 365 } 464 366 465 367 /** @}
Note:
See TracChangeset
for help on using the changeset viewer.