Changeset b0f00a9 in mainline for uspace/srv/net
- Timestamp:
- 2011-11-06T22:21:05Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 898e847
- Parents:
- 2bdf8313 (diff), 7b5f4c9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- uspace/srv/net
- Files:
-
- 3 deleted
- 17 edited
- 4 moved
-
cfg/lo.nic (moved) (moved from uspace/srv/net/cfg/lo ) (1 diff)
-
cfg/ne2k.nic (moved) (moved from uspace/srv/net/cfg/ne2k ) (2 diffs)
-
documentation.txt (deleted)
-
il/arp/arp.c (modified) (26 diffs)
-
il/arp/arp.h (modified) (3 diffs)
-
il/ip/ip.c (modified) (67 diffs)
-
il/ip/ip.h (modified) (5 diffs)
-
net/Makefile (modified) (2 diffs)
-
net/net.c (modified) (18 diffs)
-
net/net.h (modified) (9 diffs)
-
net/net_standalone.c (deleted)
-
net/packet_server.c (moved) (moved from uspace/lib/packet/generic/packet_server.c ) (18 diffs)
-
net/packet_server.h (moved) (moved from uspace/lib/packet/include/packet_server.h ) (2 diffs)
-
netif/lo/lo.c (deleted)
-
nil/eth/eth.c (modified) (40 diffs)
-
nil/eth/eth.h (modified) (6 diffs)
-
nil/nildummy/nildummy.c (modified) (25 diffs)
-
nil/nildummy/nildummy.h (modified) (5 diffs)
-
tl/icmp/icmp.c (modified) (16 diffs)
-
tl/tcp/tcp.c (modified) (71 diffs)
-
tl/tcp/tcp.h (modified) (3 diffs)
-
tl/tcp/tcp_header.h (modified) (2 diffs)
-
tl/udp/udp.c (modified) (29 diffs)
-
tl/udp/udp.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/cfg/lo.nic
r2bdf8313 rb0f00a9 3 3 NAME=lo 4 4 5 NETIF=lo 5 HWPATH=/virt/lo/port0 6 6 NIL=nildummy 7 7 IL=ip -
uspace/srv/net/cfg/ne2k.nic
r2bdf8313 rb0f00a9 1 # DP8390 (NE2k)configuration1 # NE2000 configuration 2 2 3 3 NAME=ne2k 4 4 5 NETIF=ne20005 HWPATH=/hw/pci0/00:01.0/ne2k/port0 6 6 NIL=eth 7 7 IL=ip 8 9 IRQ=510 IO=30011 8 12 9 # 8023_2_LSAP, 8023_2_SNAP … … 17 14 IP_ADDR=10.0.2.15 18 15 IP_ROUTING=yes 19 IP_NETMASK=255.255.255. 016 IP_NETMASK=255.255.255.240 20 17 IP_BROADCAST=10.0.2.255 21 18 IP_GATEWAY=10.0.2.2 -
uspace/srv/net/il/arp/arp.c
r2bdf8313 rb0f00a9 164 164 } 165 165 166 static int arp_clean_cache_req( int arp_phone)166 static int arp_clean_cache_req(void) 167 167 { 168 168 int count; … … 174 174 count); 175 175 176 if (device) {176 if (device) 177 177 arp_clear_device(device); 178 if (device->addr_data)179 free(device->addr_data);180 181 if (device->broadcast_data)182 free(device->broadcast_data);183 }184 178 } 185 179 … … 190 184 } 191 185 192 static int arp_clear_address_req( int arp_phone,device_id_t device_id,186 static int arp_clear_address_req(nic_device_id_t device_id, 193 187 services_t protocol, measured_string_t *address) 194 188 { … … 218 212 } 219 213 220 static int arp_clear_device_req( int arp_phone,device_id_t device_id)214 static int arp_clear_device_req(nic_device_id_t device_id) 221 215 { 222 216 fibril_mutex_lock(&arp_globals.lock); … … 289 283 * 290 284 */ 291 static int arp_receive_message( device_id_t device_id, packet_t *packet)285 static int arp_receive_message(nic_device_id_t device_id, packet_t *packet) 292 286 { 293 287 int rc; … … 365 359 memcpy(src_proto, proto->addr->value, 366 360 header->protocol_length); 367 memcpy(src_hw, device->addr ->value,361 memcpy(src_hw, device->addr, 368 362 device->packet_dimension.addr_len); 369 363 memcpy(des_hw, trans->hw_addr->value, … … 375 369 return rc; 376 370 377 nil_send_msg(device-> phone, device_id, packet,371 nil_send_msg(device->sess, device_id, packet, 378 372 SERVICE_ARP); 379 373 return 1; … … 393 387 * 394 388 */ 395 static int arp_mtu_changed_message( device_id_t device_id, size_t mtu)389 static int arp_mtu_changed_message(nic_device_id_t device_id, size_t mtu) 396 390 { 397 391 fibril_mutex_lock(&arp_globals.lock); … … 412 406 } 413 407 408 static int arp_addr_changed_message(nic_device_id_t device_id) 409 { 410 uint8_t addr_buffer[NIC_MAX_ADDRESS_LENGTH]; 411 size_t length; 412 ipc_callid_t data_callid; 413 if (!async_data_write_receive(&data_callid, &length)) { 414 async_answer_0(data_callid, EINVAL); 415 return EINVAL; 416 } 417 if (length > NIC_MAX_ADDRESS_LENGTH) { 418 async_answer_0(data_callid, ELIMIT); 419 return ELIMIT; 420 } 421 if (async_data_write_finalize(data_callid, addr_buffer, length) != EOK) { 422 return EINVAL; 423 } 424 425 fibril_mutex_lock(&arp_globals.lock); 426 427 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id); 428 if (!device) { 429 fibril_mutex_unlock(&arp_globals.lock); 430 return ENOENT; 431 } 432 433 memcpy(device->addr, addr_buffer, length); 434 device->addr_len = length; 435 436 fibril_mutex_unlock(&arp_globals.lock); 437 return EOK; 438 } 439 414 440 /** Process IPC messages from the registered device driver modules 415 441 * 416 442 * @param[in] iid Message identifier. 417 443 * @param[in,out] icall Message parameters. 418 * 419 */ 420 static void arp_receiver(ipc_callid_t iid, ipc_call_t *icall) 444 * @param[in] arg Local argument. 445 * 446 */ 447 static void arp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg) 421 448 { 422 449 packet_t *packet; … … 431 458 432 459 case NET_IL_RECEIVED: 433 rc = packet_translate_remote(arp_globals.net_ phone, &packet,460 rc = packet_translate_remote(arp_globals.net_sess, &packet, 434 461 IPC_GET_PACKET(*icall)); 435 462 if (rc == EOK) { … … 439 466 rc = arp_receive_message(IPC_GET_DEVICE(*icall), packet); 440 467 if (rc != 1) { 441 pq_release_remote(arp_globals.net_ phone,468 pq_release_remote(arp_globals.net_sess, 442 469 packet_get_id(packet)); 443 470 } … … 455 482 async_answer_0(iid, (sysarg_t) rc); 456 483 break; 484 case NET_IL_ADDR_CHANGED: 485 rc = arp_addr_changed_message(IPC_GET_DEVICE(*icall)); 486 async_answer_0(iid, (sysarg_t) rc); 457 487 458 488 default: … … 482 512 * 483 513 */ 484 static int arp_device_message( device_id_t device_id, services_t service,514 static int arp_device_message(nic_device_id_t device_id, services_t service, 485 515 services_t protocol, measured_string_t *address) 486 516 { … … 564 594 565 595 /* Bind */ 566 device-> phone= nil_bind_service(device->service,596 device->sess = nil_bind_service(device->service, 567 597 (sysarg_t) device->device_id, SERVICE_ARP, 568 598 arp_receiver); 569 if (device-> phone < 0) {599 if (device->sess == NULL) { 570 600 fibril_mutex_unlock(&arp_globals.lock); 571 601 arp_protos_destroy(&device->protos, free); … … 575 605 576 606 /* Get packet dimensions */ 577 rc = nil_packet_size_req(device-> phone, device_id,607 rc = nil_packet_size_req(device->sess, device_id, 578 608 &device->packet_dimension); 579 609 if (rc != EOK) { … … 585 615 586 616 /* Get hardware address */ 587 rc = nil_get_addr_req(device->phone, device_id, &device->addr, 588 &device->addr_data); 617 int len = nil_get_addr_req(device->sess, device_id, device->addr, 618 NIC_MAX_ADDRESS_LENGTH); 619 if (len < 0) { 620 fibril_mutex_unlock(&arp_globals.lock); 621 arp_protos_destroy(&device->protos, free); 622 free(device); 623 return len; 624 } 625 626 device->addr_len = len; 627 628 /* Get broadcast address */ 629 len = nil_get_broadcast_addr_req(device->sess, device_id, 630 device->broadcast_addr, NIC_MAX_ADDRESS_LENGTH); 631 if (len < 0) { 632 fibril_mutex_unlock(&arp_globals.lock); 633 arp_protos_destroy(&device->protos, free); 634 free(device); 635 return len; 636 } 637 638 device->broadcast_addr_len = len; 639 640 rc = arp_cache_add(&arp_globals.cache, device->device_id, 641 device); 589 642 if (rc != EOK) { 590 643 fibril_mutex_unlock(&arp_globals.lock); … … 593 646 return rc; 594 647 } 595 596 /* Get broadcast address */597 rc = nil_get_broadcast_addr_req(device->phone, device_id,598 &device->broadcast_addr, &device->broadcast_data);599 if (rc != EOK) {600 fibril_mutex_unlock(&arp_globals.lock);601 free(device->addr);602 free(device->addr_data);603 arp_protos_destroy(&device->protos, free);604 free(device);605 return rc;606 }607 608 rc = arp_cache_add(&arp_globals.cache, device->device_id,609 device);610 if (rc != EOK) {611 fibril_mutex_unlock(&arp_globals.lock);612 free(device->addr);613 free(device->addr_data);614 free(device->broadcast_addr);615 free(device->broadcast_data);616 arp_protos_destroy(&device->protos, free);617 free(device);618 return rc;619 }620 648 printf("%s: Device registered (id: %d, type: 0x%x, service: %d," 621 649 " proto: %d)\n", NAME, device->device_id, device->hardware, … … 627 655 } 628 656 629 int il_initialize( int net_phone)657 int il_initialize(async_sess_t *net_sess) 630 658 { 631 659 fibril_mutex_initialize(&arp_globals.lock); 632 660 633 661 fibril_mutex_lock(&arp_globals.lock); 634 arp_globals.net_ phone = net_phone;662 arp_globals.net_sess = net_sess; 635 663 int rc = arp_cache_initialize(&arp_globals.cache); 636 664 fibril_mutex_unlock(&arp_globals.lock); … … 639 667 } 640 668 641 static int arp_send_request( device_id_t device_id, services_t protocol,669 static int arp_send_request(nic_device_id_t device_id, services_t protocol, 642 670 measured_string_t *target, arp_device_t *device, arp_proto_t *proto) 643 671 { 644 672 /* ARP packet content size = header + (address + translation) * 2 */ 645 size_t length = 8 + 2 * (proto->addr->length + device->addr ->length);673 size_t length = 8 + 2 * (proto->addr->length + device->addr_len); 646 674 if (length > device->packet_dimension.content) 647 675 return ELIMIT; 648 676 649 packet_t *packet = packet_get_4_remote(arp_globals.net_ phone,677 packet_t *packet = packet_get_4_remote(arp_globals.net_sess, 650 678 device->packet_dimension.addr_len, device->packet_dimension.prefix, 651 679 length, device->packet_dimension.suffix); … … 655 683 arp_header_t *header = (arp_header_t *) packet_suffix(packet, length); 656 684 if (!header) { 657 pq_release_remote(arp_globals.net_ phone, packet_get_id(packet));685 pq_release_remote(arp_globals.net_sess, packet_get_id(packet)); 658 686 return ENOMEM; 659 687 } 660 688 661 689 header->hardware = htons(device->hardware); 662 header->hardware_length = (uint8_t) device->addr ->length;690 header->hardware_length = (uint8_t) device->addr_len; 663 691 header->protocol = htons(protocol_map(device->service, protocol)); 664 692 header->protocol_length = (uint8_t) proto->addr->length; … … 666 694 667 695 length = sizeof(arp_header_t); 668 669 memcpy(((uint8_t *) header) + length, device->addr->value, 670 device->addr->length); 671 length += device->addr->length; 696 memcpy(((uint8_t *) header) + length, device->addr, 697 device->addr_len); 698 length += device->addr_len; 672 699 memcpy(((uint8_t *) header) + length, proto->addr->value, 673 700 proto->addr->length); 674 701 length += proto->addr->length; 675 bzero(((uint8_t *) header) + length, device->addr ->length);676 length += device->addr ->length;702 bzero(((uint8_t *) header) + length, device->addr_len); 703 length += device->addr_len; 677 704 memcpy(((uint8_t *) header) + length, target->value, target->length); 678 679 int rc = packet_set_addr(packet, (uint8_t *) device->addr->value,680 (uint8_t *) device->broadcast_addr->value, device->addr->length);705 706 int rc = packet_set_addr(packet, device->addr, device->broadcast_addr, 707 device->addr_len); 681 708 if (rc != EOK) { 682 pq_release_remote(arp_globals.net_ phone, packet_get_id(packet));709 pq_release_remote(arp_globals.net_sess, packet_get_id(packet)); 683 710 return rc; 684 711 } 685 712 686 nil_send_msg(device-> phone, device_id, packet, SERVICE_ARP);713 nil_send_msg(device->sess, device_id, packet, SERVICE_ARP); 687 714 return EOK; 688 715 } … … 703 730 * 704 731 */ 705 static int arp_translate_message( device_id_t device_id, services_t protocol,732 static int arp_translate_message(nic_device_id_t device_id, services_t protocol, 706 733 measured_string_t *target, measured_string_t **translation) 707 734 { … … 845 872 846 873 *count = 0; 874 875 if (!IPC_GET_IMETHOD(*call)) 876 return EOK; 877 847 878 switch (IPC_GET_IMETHOD(*call)) { 848 case IPC_M_PHONE_HUNGUP:849 return EOK;850 851 879 case NET_ARP_DEVICE: 852 880 rc = measured_strings_receive(&address, &data, 1); … … 889 917 890 918 case NET_ARP_CLEAR_DEVICE: 891 return arp_clear_device_req( 0,IPC_GET_DEVICE(*call));919 return arp_clear_device_req(IPC_GET_DEVICE(*call)); 892 920 893 921 case NET_ARP_CLEAR_ADDRESS: … … 896 924 return rc; 897 925 898 arp_clear_address_req( 0,IPC_GET_DEVICE(*call),926 arp_clear_address_req(IPC_GET_DEVICE(*call), 899 927 IPC_GET_SERVICE(*call), address); 900 928 free(address); … … 903 931 904 932 case NET_ARP_CLEAN_CACHE: 905 return arp_clean_cache_req( 0);933 return arp_clean_cache_req(); 906 934 } 907 935 -
uspace/srv/net/il/arp/arp.h
r2bdf8313 rb0f00a9 38 38 #define NET_ARP_H_ 39 39 40 #include <async.h> 40 41 #include <fibril_synch.h> 41 42 #include <ipc/services.h> … … 91 92 struct arp_device { 92 93 /** Actual device hardware address. */ 93 measured_string_t *addr;94 /** Actual device hardware address data. */95 uint8_t *addr_data;94 uint8_t addr[NIC_MAX_ADDRESS_LENGTH]; 95 /** Actual device hardware address length. */ 96 size_t addr_len; 96 97 /** Broadcast device hardware address. */ 97 measured_string_t *broadcast_addr;98 /** Broadcast device hardware address data. */99 uint8_t *broadcast_data;98 uint8_t broadcast_addr[NIC_MAX_ADDRESS_LENGTH]; 99 /** Broadcast device hardware address length. */ 100 size_t broadcast_addr_len; 100 101 /** Device identifier. */ 101 device_id_t device_id;102 nic_device_id_t device_id; 102 103 /** Hardware type. */ 103 104 hw_type_t hardware; 104 105 /** Packet dimension. */ 105 106 packet_dimension_t packet_dimension; 106 /** Device module phone. */107 int phone;107 /** Device module session. */ 108 async_sess_t *sess; 108 109 109 110 /** … … 122 123 arp_cache_t cache; 123 124 124 /** Networking module phone. */ 125 int net_phone; 125 /** Networking module session. */ 126 async_sess_t *net_sess; 127 126 128 /** Safety lock. */ 127 129 fibril_mutex_t lock; -
uspace/srv/net/il/ip/ip.c
r2bdf8313 rb0f00a9 120 120 GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t); 121 121 122 static void ip_receiver(ipc_callid_t, ipc_call_t *); 123 124 /** Releases the packet and returns the result. 125 * 126 * @param[in] packet The packet queue to be released. 127 * @param[in] result The result to be returned. 128 * @return The result parameter. 122 static void ip_receiver(ipc_callid_t, ipc_call_t *, void *); 123 124 /** Release the packet and returns the result. 125 * 126 * @param[in] packet Packet queue to be released. 127 * @param[in] result Result to be returned. 128 * 129 * @return Result parameter. 130 * 129 131 */ 130 132 static int ip_release_and_return(packet_t *packet, int result) 131 133 { 132 pq_release_remote(ip_globals.net_ phone, packet_get_id(packet));134 pq_release_remote(ip_globals.net_sess, packet_get_id(packet)); 133 135 return result; 134 136 } 135 137 136 /** Returns the ICMP phone. 137 * 138 * Searches the registered protocols. 139 * 140 * @return The found ICMP phone. 141 * @return ENOENT if the ICMP is not registered. 142 */ 143 static int ip_get_icmp_phone(void) 144 { 145 ip_proto_t *proto; 146 int phone; 147 138 /** Return the ICMP session. 139 * 140 * Search the registered protocols. 141 * 142 * @return Found ICMP session. 143 * @return NULL if the ICMP is not registered. 144 * 145 */ 146 static async_sess_t *ip_get_icmp_session(void) 147 { 148 148 fibril_rwlock_read_lock(&ip_globals.protos_lock); 149 proto = ip_protos_find(&ip_globals.protos, IPPROTO_ICMP);150 phone = proto ? proto->phone : ENOENT;149 ip_proto_t *proto = ip_protos_find(&ip_globals.protos, IPPROTO_ICMP); 150 async_sess_t *sess = proto ? proto->sess : NULL; 151 151 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 152 return phone; 152 153 return sess; 153 154 } 154 155 … … 179 180 next = pq_detach(packet); 180 181 if (next) 181 pq_release_remote(ip_globals.net_ phone, packet_get_id(next));182 pq_release_remote(ip_globals.net_sess, packet_get_id(next)); 182 183 183 184 if (!header) { … … 201 202 202 203 /* Set the destination address */ 203 switch ( header->version) {204 switch (GET_IP_HEADER_VERSION(header)) { 204 205 case IPVERSION: 205 206 addrlen = sizeof(dest_in); … … 218 219 } 219 220 220 /** Prepare sthe ICMP notification packet.221 * 222 * Release s additional packets and keepsonly the first one.221 /** Prepare the ICMP notification packet. 222 * 223 * Release additional packets and keep only the first one. 223 224 * All packets are released on error. 224 225 * 225 * @param[in] error The packet error service. 226 * @param[in] packet The packet or the packet queue to be reported as faulty. 227 * @param[in] header The first packet IP header. May be NULL. 228 * @return The found ICMP phone. 229 * @return EINVAL if the error parameter is set. 230 * @return EINVAL if the ICMP phone is not found. 231 * @return EINVAL if the ip_prepare_icmp() fails. 232 */ 233 static int 234 ip_prepare_icmp_and_get_phone(services_t error, packet_t *packet, 235 ip_header_t *header) 236 { 237 int phone; 238 239 phone = ip_get_icmp_phone(); 240 if (error || (phone < 0) || ip_prepare_icmp(packet, header)) 241 return ip_release_and_return(packet, EINVAL); 242 return phone; 243 } 244 245 int il_initialize(int net_phone) 226 * @param[in] error Packet error service. 227 * @param[in] packet Packet or the packet queue to be reported as faulty. 228 * @param[in] header First packet IP header. May be NULL. 229 * 230 * @return Found ICMP session. 231 * @return NULL if the error parameter is set. 232 * @return NULL if the ICMP session is not found. 233 * @return NULL if the ip_prepare_icmp() fails. 234 * 235 */ 236 static async_sess_t *ip_prepare_icmp_and_get_session(services_t error, 237 packet_t *packet, ip_header_t *header) 238 { 239 async_sess_t *sess = ip_get_icmp_session(); 240 241 if ((error) || (!sess) || (ip_prepare_icmp(packet, header))) { 242 pq_release_remote(ip_globals.net_sess, packet_get_id(packet)); 243 return NULL; 244 } 245 246 return sess; 247 } 248 249 int il_initialize(async_sess_t *net_sess) 246 250 { 247 251 fibril_rwlock_initialize(&ip_globals.lock); … … 250 254 fibril_rwlock_initialize(&ip_globals.netifs_lock); 251 255 252 ip_globals.net_ phone = net_phone;256 ip_globals.net_sess = net_sess; 253 257 ip_globals.packet_counter = 0; 254 258 ip_globals.gateway.address.s_addr = 0; … … 350 354 ip_netif->routing = NET_DEFAULT_IP_ROUTING; 351 355 configuration = &names[0]; 352 356 353 357 /* Get configuration */ 354 rc = net_get_device_conf_req(ip_globals.net_ phone, ip_netif->device_id,358 rc = net_get_device_conf_req(ip_globals.net_sess, ip_netif->device_id, 355 359 &configuration, count, &data); 356 360 if (rc != EOK) … … 402 406 return ENOTSUP; 403 407 } 404 408 405 409 if (configuration[6].value) { 406 410 ip_netif->arp = get_running_module(&ip_globals.modules, … … 413 417 } 414 418 } 419 415 420 if (configuration[7].value) 416 421 ip_netif->routing = (configuration[7].value[0] == 'y'); 417 422 418 423 net_free_settings(configuration, data); 419 424 } 420 425 421 426 /* Bind netif service which also initializes the device */ 422 ip_netif-> phone= nil_bind_service(ip_netif->service,427 ip_netif->sess = nil_bind_service(ip_netif->service, 423 428 (sysarg_t) ip_netif->device_id, SERVICE_IP, 424 429 ip_receiver); 425 if (ip_netif-> phone < 0) {430 if (ip_netif->sess == NULL) { 426 431 printf("Failed to contact the nil service %d\n", 427 432 ip_netif->service); 428 return ip_netif->phone;429 } 430 433 return ENOENT; 434 } 435 431 436 /* Has to be after the device netif module initialization */ 432 437 if (ip_netif->arp) { … … 435 440 address.length = sizeof(in_addr_t); 436 441 437 rc = arp_device_req(ip_netif->arp-> phone,442 rc = arp_device_req(ip_netif->arp->sess, 438 443 ip_netif->device_id, SERVICE_IP, ip_netif->service, 439 444 &address); … … 444 449 } 445 450 } 446 451 447 452 /* Get packet dimensions */ 448 rc = nil_packet_size_req(ip_netif-> phone, ip_netif->device_id,453 rc = nil_packet_size_req(ip_netif->sess, ip_netif->device_id, 449 454 &ip_netif->packet_dimension); 450 455 if (rc != EOK) … … 457 462 ip_netif->packet_dimension.content = IP_MIN_CONTENT; 458 463 } 459 464 460 465 index = ip_netifs_add(&ip_globals.netifs, ip_netif->device_id, ip_netif); 461 466 if (index < 0) … … 474 479 printf("%s: Default gateway (%s)\n", NAME, defgateway); 475 480 } 476 481 477 482 return EOK; 478 483 } 479 484 480 static int ip_device_req_local(int il_phone, device_id_t device_id, 481 services_t netif) 485 static int ip_device_req_local(nic_device_id_t device_id, services_t netif) 482 486 { 483 487 ip_netif_t *ip_netif; … … 495 499 return rc; 496 500 } 497 501 498 502 ip_netif->device_id = device_id; 499 503 ip_netif->service = netif; 500 ip_netif->state = N ETIF_STOPPED;501 504 ip_netif->state = NIC_STATE_STOPPED; 505 502 506 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 503 507 … … 513 517 514 518 /* Print the settings */ 515 printf("%s: Device registered (id: %d, phone: %d,ipv: %d, conf: %s)\n",516 NAME, ip_netif->device_id, ip_netif-> phone, ip_netif->ipv,519 printf("%s: Device registered (id: %d, ipv: %d, conf: %s)\n", 520 NAME, ip_netif->device_id, ip_netif->ipv, 517 521 ip_netif->dhcp ? "dhcp" : "static"); 518 522 … … 591 595 while (index >= 0) { 592 596 netif = ip_netifs_get_index(&ip_globals.netifs, index); 593 if (netif && (netif->state == N ETIF_ACTIVE)) {597 if (netif && (netif->state == NIC_STATE_ACTIVE)) { 594 598 route = ip_netif_find_route(netif, destination); 595 599 if (route) … … 635 639 636 640 /* Process all IP options */ 637 while (next < first->header_length) {641 while (next < GET_IP_HEADER_LENGTH(first)) { 638 642 option = (ip_option_t *) (((uint8_t *) first) + next); 639 643 /* Skip end or noop */ … … 656 660 if (length % 4) { 657 661 bzero(((uint8_t *) last) + length, 4 - (length % 4)); 658 last->header_length = length / 4 + 1;662 SET_IP_HEADER_LENGTH(last, (length / 4 + 1)); 659 663 } else { 660 last->header_length = length / 4;664 SET_IP_HEADER_LENGTH(last, (length / 4)); 661 665 } 662 666 … … 706 710 return rc; 707 711 708 header->version = IPV4;709 header->fragment_offset_high = 0;712 SET_IP_HEADER_VERSION(header, IPV4); 713 SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(header, 0); 710 714 header->fragment_offset_low = 0; 711 715 header->header_checksum = 0; … … 735 739 memcpy(middle_header, last_header, 736 740 IP_HEADER_LENGTH(last_header)); 737 header->flags |= IPFLAG_MORE_FRAGMENTS; 741 SET_IP_HEADER_FLAGS(header, 742 (GET_IP_HEADER_FLAGS(header) | IPFLAG_MORE_FRAGMENTS)); 738 743 middle_header->total_length = 739 744 htons(packet_get_data_length(next)); 740 middle_header->fragment_offset_high =741 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length) ;745 SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(middle_header, 746 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length)); 742 747 middle_header->fragment_offset_low = 743 748 IP_COMPUTE_FRAGMENT_OFFSET_LOW(length); … … 768 773 middle_header->total_length = 769 774 htons(packet_get_data_length(next)); 770 middle_header->fragment_offset_high =771 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length) ;775 SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(middle_header, 776 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length)); 772 777 middle_header->fragment_offset_low = 773 778 IP_COMPUTE_FRAGMENT_OFFSET_LOW(length); … … 785 790 length += packet_get_data_length(next); 786 791 free(last_header); 787 header->flags |= IPFLAG_MORE_FRAGMENTS; 792 SET_IP_HEADER_FLAGS(header, 793 (GET_IP_HEADER_FLAGS(header) | IPFLAG_MORE_FRAGMENTS)); 788 794 } 789 795 … … 834 840 new_header->total_length = htons(IP_HEADER_LENGTH(new_header) + length); 835 841 offset = IP_FRAGMENT_OFFSET(header) + IP_HEADER_DATA_LENGTH(header); 836 new_header->fragment_offset_high =837 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(offset) ;842 SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(new_header, 843 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(offset)); 838 844 new_header->fragment_offset_low = 839 845 IP_COMPUTE_FRAGMENT_OFFSET_LOW(offset); … … 865 871 return NULL; 866 872 memcpy(middle, last, IP_HEADER_LENGTH(last)); 867 middle->flags |= IPFLAG_MORE_FRAGMENTS; 873 SET_IP_HEADER_FLAGS(middle, 874 (GET_IP_HEADER_FLAGS(middle) | IPFLAG_MORE_FRAGMENTS)); 868 875 return middle; 869 876 } … … 922 929 923 930 /* Fragmentation forbidden? */ 924 if( header->flags& IPFLAG_DONT_FRAGMENT)931 if(GET_IP_HEADER_FLAGS(header) & IPFLAG_DONT_FRAGMENT) 925 932 return EPERM; 926 933 927 934 /* Create the last fragment */ 928 new_packet = packet_get_4_remote(ip_globals.net_ phone, prefix, length,935 new_packet = packet_get_4_remote(ip_globals.net_sess, prefix, length, 929 936 suffix, ((addrlen > addr_len) ? addrlen : addr_len)); 930 937 if (!new_packet) … … 958 965 959 966 /* Mark the first as fragmented */ 960 header->flags |= IPFLAG_MORE_FRAGMENTS; 967 SET_IP_HEADER_FLAGS(header, 968 (GET_IP_HEADER_FLAGS(header) | IPFLAG_MORE_FRAGMENTS)); 961 969 962 970 /* Create middle fragments */ 963 971 while (IP_TOTAL_LENGTH(header) > length) { 964 new_packet = packet_get_4_remote(ip_globals.net_ phone, prefix,972 new_packet = packet_get_4_remote(ip_globals.net_sess, prefix, 965 973 length, suffix, 966 974 ((addrlen >= addr_len) ? addrlen : addr_len)); … … 987 995 } 988 996 989 /** Check sthe packet queue lengths and fragments the packets if needed.997 /** Check the packet queue lengths and fragments the packets if needed. 990 998 * 991 999 * The ICMP_FRAG_NEEDED error notification may be sent if the packet needs to 992 1000 * be fragmented and the fragmentation is not allowed. 993 1001 * 994 * @param[in,out] packet The packet or the packet queue to be checked. 995 * @param[in] prefix The minimum prefix size. 996 * @param[in] content The maximum content size. 997 * @param[in] suffix The minimum suffix size. 998 * @param[in] addr_len The minimum address length. 999 * @param[in] error The error module service. 1000 * @return The packet or the packet queue of the allowed length. 1001 * @return NULL if there are no packets left. 1002 */ 1003 static packet_t * 1004 ip_split_packet(packet_t *packet, size_t prefix, size_t content, size_t suffix, 1005 socklen_t addr_len, services_t error) 1002 * @param[in,out] packet Packet or the packet queue to be checked. 1003 * @param[in] prefix Minimum prefix size. 1004 * @param[in] content Maximum content size. 1005 * @param[in] suffix Minimum suffix size. 1006 * @param[in] addr_len Minimum address length. 1007 * @param[in] error Error module service. 1008 * 1009 * @return The packet or the packet queue of the allowed length. 1010 * @return NULL if there are no packets left. 1011 * 1012 */ 1013 static packet_t *ip_split_packet(packet_t *packet, size_t prefix, size_t content, 1014 size_t suffix, socklen_t addr_len, services_t error) 1006 1015 { 1007 1016 size_t length; … … 1009 1018 packet_t *new_packet; 1010 1019 int result; 1011 int phone;1012 1020 async_sess_t *sess; 1021 1013 1022 next = packet; 1014 1023 /* Check all packets */ … … 1032 1041 /* Fragmentation needed? */ 1033 1042 if (result == EPERM) { 1034 phone = ip_prepare_icmp_and_get_phone( 1035 error, next, NULL); 1036 if (phone >= 0) { 1043 sess = ip_prepare_icmp_and_get_session(error, next, NULL); 1044 if (sess) { 1037 1045 /* Fragmentation necessary ICMP */ 1038 icmp_destination_unreachable_msg( phone,1046 icmp_destination_unreachable_msg(sess, 1039 1047 ICMP_FRAG_NEEDED, content, next); 1040 1048 } 1041 1049 } else { 1042 pq_release_remote(ip_globals.net_ phone,1050 pq_release_remote(ip_globals.net_sess, 1043 1051 packet_get_id(next)); 1044 1052 } … … 1054 1062 } 1055 1063 1056 /** Send sthe packet or the packet queue via the specified route.1064 /** Send the packet or the packet queue via the specified route. 1057 1065 * 1058 1066 * The ICMP_HOST_UNREACH error notification may be sent if route hardware 1059 1067 * destination address is found. 1060 1068 * 1061 * @param[in,out] packet The packet to be sent.1062 * @param[in] netif The target network interface.1063 * @param[in] route The target route.1064 * @param[in] src The source address.1065 * @param[in] dest The destination address.1066 * @param[in] error The error module service.1067 * @return EOK on success.1068 * @return Other error codes as defined for the arp_translate_req()1069 * function.1070 * @return Other error codes as defined for the ip_prepare_packet()1071 * function.1069 * @param[in,out] packet Packet to be sent. 1070 * @param[in] netif Target network interface. 1071 * @param[in] route Target route. 1072 * @param[in] src Source address. 1073 * @param[in] dest Destination address. 1074 * @param[in] error Error module service. 1075 * 1076 * @return EOK on success. 1077 * @return Other error codes as defined for arp_translate_req(). 1078 * @return Other error codes as defined for ip_prepare_packet(). 1079 * 1072 1080 */ 1073 1081 static int ip_send_route(packet_t *packet, ip_netif_t *netif, … … 1077 1085 measured_string_t *translation; 1078 1086 uint8_t *data; 1079 int phone;1087 async_sess_t *sess; 1080 1088 int rc; 1081 1089 … … 1086 1094 destination.length = sizeof(dest.s_addr); 1087 1095 1088 rc = arp_translate_req(netif->arp-> phone, netif->device_id,1096 rc = arp_translate_req(netif->arp->sess, netif->device_id, 1089 1097 SERVICE_IP, &destination, &translation, &data); 1090 1098 if (rc != EOK) { 1091 pq_release_remote(ip_globals.net_ phone,1099 pq_release_remote(ip_globals.net_sess, 1092 1100 packet_get_id(packet)); 1093 1101 return rc; … … 1099 1107 free(data); 1100 1108 } 1101 phone = ip_prepare_icmp_and_get_phone(error, packet,1109 sess = ip_prepare_icmp_and_get_session(error, packet, 1102 1110 NULL); 1103 if ( phone >= 0) {1111 if (sess) { 1104 1112 /* Unreachable ICMP if no routing */ 1105 icmp_destination_unreachable_msg( phone,1113 icmp_destination_unreachable_msg(sess, 1106 1114 ICMP_HOST_UNREACH, 0, packet); 1107 1115 } … … 1115 1123 rc = ip_prepare_packet(src, dest, packet, translation); 1116 1124 if (rc != EOK) { 1117 pq_release_remote(ip_globals.net_ phone, packet_get_id(packet));1125 pq_release_remote(ip_globals.net_sess, packet_get_id(packet)); 1118 1126 } else { 1119 1127 packet = ip_split_packet(packet, netif->packet_dimension.prefix, … … 1122 1130 netif->packet_dimension.addr_len, error); 1123 1131 if (packet) { 1124 nil_send_msg(netif-> phone, netif->device_id, packet,1132 nil_send_msg(netif->sess, netif->device_id, packet, 1125 1133 SERVICE_IP); 1126 1134 } … … 1135 1143 } 1136 1144 1137 static int ip_send_msg_local( int il_phone, device_id_t device_id,1138 packet_t *packet,services_t sender, services_t error)1145 static int ip_send_msg_local(nic_device_id_t device_id, packet_t *packet, 1146 services_t sender, services_t error) 1139 1147 { 1140 1148 int addrlen; … … 1145 1153 in_addr_t *dest; 1146 1154 in_addr_t *src; 1147 int phone;1155 async_sess_t *sess; 1148 1156 int rc; 1149 1157 … … 1190 1198 if (!netif || !route) { 1191 1199 fibril_rwlock_read_unlock(&ip_globals.netifs_lock); 1192 phone = ip_prepare_icmp_and_get_phone(error, packet, NULL);1193 if ( phone >= 0) {1200 sess = ip_prepare_icmp_and_get_session(error, packet, NULL); 1201 if (sess) { 1194 1202 /* Unreachable ICMP if no routing */ 1195 icmp_destination_unreachable_msg( phone,1203 icmp_destination_unreachable_msg(sess, 1196 1204 ICMP_NET_UNREACH, 0, packet); 1197 1205 } … … 1221 1229 if (!netif || !route) { 1222 1230 fibril_rwlock_read_unlock(&ip_globals.netifs_lock); 1223 phone = ip_prepare_icmp_and_get_phone(error, packet,1231 sess = ip_prepare_icmp_and_get_session(error, packet, 1224 1232 NULL); 1225 if ( phone >= 0) {1233 if (sess) { 1226 1234 /* Unreachable ICMP if no routing */ 1227 icmp_destination_unreachable_msg( phone,1235 icmp_destination_unreachable_msg(sess, 1228 1236 ICMP_HOST_UNREACH, 0, packet); 1229 1237 } … … 1251 1259 * @return ENOENT if device is not found. 1252 1260 */ 1253 static int ip_device_state_message(device_id_t device_id, device_state_t state) 1261 static int ip_device_state_message(nic_device_id_t device_id, 1262 nic_device_state_t state) 1254 1263 { 1255 1264 ip_netif_t *netif; … … 1265 1274 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1266 1275 1267 printf("%s: Device %d changed state to %d\n", NAME, device_id, state); 1276 printf("%s: Device %d changed state to '%s'\n", NAME, device_id, 1277 nic_device_state_to_string(state)); 1268 1278 1269 1279 return EOK; … … 1305 1315 * tl_received_msg() function. 1306 1316 */ 1307 static int ip_deliver_local( device_id_t device_id, packet_t *packet,1317 static int ip_deliver_local(nic_device_id_t device_id, packet_t *packet, 1308 1318 ip_header_t *header, services_t error) 1309 1319 { 1310 1320 ip_proto_t *proto; 1311 int phone;1321 async_sess_t *sess; 1312 1322 services_t service; 1313 1323 tl_received_msg_t received_msg; … … 1319 1329 int rc; 1320 1330 1321 if (( header->flags& IPFLAG_MORE_FRAGMENTS) ||1331 if ((GET_IP_HEADER_FLAGS(header) & IPFLAG_MORE_FRAGMENTS) || 1322 1332 IP_FRAGMENT_OFFSET(header)) { 1323 1333 // TODO fragmented … … 1325 1335 } 1326 1336 1327 switch ( header->version) {1337 switch (GET_IP_HEADER_VERSION(header)) { 1328 1338 case IPVERSION: 1329 1339 addrlen = sizeof(src_in); … … 1362 1372 if (!proto) { 1363 1373 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1364 phone = ip_prepare_icmp_and_get_phone(error, packet, header);1365 if ( phone >= 0) {1374 sess = ip_prepare_icmp_and_get_session(error, packet, header); 1375 if (sess) { 1366 1376 /* Unreachable ICMP */ 1367 icmp_destination_unreachable_msg( phone,1377 icmp_destination_unreachable_msg(sess, 1368 1378 ICMP_PROT_UNREACH, 0, packet); 1369 1379 } … … 1377 1387 rc = received_msg(device_id, packet, service, error); 1378 1388 } else { 1379 rc = tl_received_msg(proto-> phone, device_id, packet,1389 rc = tl_received_msg(proto->sess, device_id, packet, 1380 1390 proto->service, error); 1381 1391 fibril_rwlock_read_unlock(&ip_globals.protos_lock); … … 1406 1416 * is disabled. 1407 1417 */ 1408 static int ip_process_packet( device_id_t device_id, packet_t *packet)1418 static int ip_process_packet(nic_device_id_t device_id, packet_t *packet) 1409 1419 { 1410 1420 ip_header_t *header; 1411 1421 in_addr_t dest; 1412 1422 ip_route_t *route; 1413 int phone;1423 async_sess_t *sess; 1414 1424 struct sockaddr *addr; 1415 1425 struct sockaddr_in addr_in; … … 1424 1434 if ((header->header_checksum) && 1425 1435 (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) { 1426 phone = ip_prepare_icmp_and_get_phone(0, packet, header);1427 if ( phone >= 0) {1436 sess = ip_prepare_icmp_and_get_session(0, packet, header); 1437 if (sess) { 1428 1438 /* Checksum error ICMP */ 1429 icmp_parameter_problem_msg( phone, ICMP_PARAM_POINTER,1439 icmp_parameter_problem_msg(sess, ICMP_PARAM_POINTER, 1430 1440 ((size_t) ((void *) &header->header_checksum)) - 1431 1441 ((size_t) ((void *) header)), packet); … … 1435 1445 1436 1446 if (header->ttl <= 1) { 1437 phone = ip_prepare_icmp_and_get_phone(0, packet, header);1438 if ( phone >= 0) {1447 sess = ip_prepare_icmp_and_get_session(0, packet, header); 1448 if (sess) { 1439 1449 /* TTL exceeded ICMP */ 1440 icmp_time_exceeded_msg( phone, ICMP_EXC_TTL, packet);1450 icmp_time_exceeded_msg(sess, ICMP_EXC_TTL, packet); 1441 1451 } 1442 1452 return EINVAL; … … 1447 1457 1448 1458 /* Set the destination address */ 1449 switch ( header->version) {1459 switch (GET_IP_HEADER_VERSION(header)) { 1450 1460 case IPVERSION: 1451 1461 addrlen = sizeof(addr_in); … … 1466 1476 route = ip_find_route(dest); 1467 1477 if (!route) { 1468 phone = ip_prepare_icmp_and_get_phone(0, packet, header);1469 if ( phone >= 0) {1478 sess = ip_prepare_icmp_and_get_session(0, packet, header); 1479 if (sess) { 1470 1480 /* Unreachable ICMP */ 1471 icmp_destination_unreachable_msg( phone,1481 icmp_destination_unreachable_msg(sess, 1472 1482 ICMP_HOST_UNREACH, 0, packet); 1473 1483 } … … 1486 1496 } 1487 1497 1488 phone = ip_prepare_icmp_and_get_phone(0, packet, header);1489 if ( phone >= 0) {1498 sess = ip_prepare_icmp_and_get_session(0, packet, header); 1499 if (sess) { 1490 1500 /* Unreachable ICMP if no routing */ 1491 icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0,1501 icmp_destination_unreachable_msg(sess, ICMP_HOST_UNREACH, 0, 1492 1502 packet); 1493 1503 } … … 1496 1506 } 1497 1507 1498 /** Return sthe device packet dimensions for sending.1499 * 1500 * @param[in] phone The service module phone.1501 * @param[ in] message The service specific message.1502 * @param[ in] device_id The device identifier.1503 * @param[out] addr_len The minimum reserved address length.1504 * @param[out] prefix The minimum reserved prefix size.1505 * @param[out] content The maximum content size.1506 * @ param[out] suffix The minimum reserved suffix size.1507 * @return EOK on success.1508 */ 1509 static int ip_packet_size_message( device_id_t device_id, size_t *addr_len,1508 /** Return the device packet dimensions for sending. 1509 * 1510 * @param[in] device_id Device identifier. 1511 * @param[out] addr_len Minimum reserved address length. 1512 * @param[out] prefix Minimum reserved prefix size. 1513 * @param[out] content Maximum content size. 1514 * @param[out] suffix Minimum reserved suffix size. 1515 * 1516 * @return EOK on success. 1517 * 1518 */ 1519 static int ip_packet_size_message(nic_device_id_t device_id, size_t *addr_len, 1510 1520 size_t *prefix, size_t *content, size_t *suffix) 1511 1521 { … … 1565 1575 * @return ENOENT if device is not found. 1566 1576 */ 1567 static int ip_mtu_changed_message( device_id_t device_id, size_t mtu)1577 static int ip_mtu_changed_message(nic_device_id_t device_id, size_t mtu) 1568 1578 { 1569 1579 ip_netif_t *netif; … … 1587 1597 * @param[in] iid Message identifier. 1588 1598 * @param[in,out] icall Message parameters. 1589 * 1590 */ 1591 static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall) 1599 * @param[in] arg Local argument. 1600 * 1601 */ 1602 static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg) 1592 1603 { 1593 1604 packet_t *packet; … … 1603 1614 1604 1615 case NET_IL_RECEIVED: 1605 rc = packet_translate_remote(ip_globals.net_ phone, &packet,1616 rc = packet_translate_remote(ip_globals.net_sess, &packet, 1606 1617 IPC_GET_PACKET(*icall)); 1607 1618 if (rc == EOK) { … … 1621 1632 async_answer_0(iid, (sysarg_t) rc); 1622 1633 break; 1623 1634 case NET_IL_ADDR_CHANGED: 1635 async_answer_0(iid, (sysarg_t) EOK); 1636 break; 1637 1624 1638 default: 1625 1639 async_answer_0(iid, (sysarg_t) ENOTSUP); … … 1630 1644 } 1631 1645 1632 /** Register sthe transport layer protocol.1646 /** Register the transport layer protocol. 1633 1647 * 1634 1648 * The traffic of this protocol will be supplied using either the receive 1635 1649 * function or IPC message. 1636 1650 * 1637 * @param[in] protocol The transport layer module protocol. 1638 * @param[in] service The transport layer module service. 1639 * @param[in] phone The transport layer module phone. 1640 * @param[in] received_msg The receiving function. 1641 * @return EOK on success. 1642 * @return EINVAL if the protocol parameter and/or the service 1643 * parameter is zero. 1644 * @return EINVAL if the phone parameter is not a positive number 1645 * and the tl_receive_msg is NULL. 1646 * @return ENOMEM if there is not enough memory left. 1647 */ 1648 static int 1649 ip_register(int protocol, services_t service, int phone, 1651 * @param[in] protocol Transport layer module protocol. 1652 * @param[in] service Transport layer module service. 1653 * @param[in] sess Transport layer module session. 1654 * @param[in] received_msg Receiving function. 1655 * 1656 * @return EOK on success. 1657 * @return EINVAL if the protocol parameter and/or the service 1658 * parameter is zero. 1659 * @return EINVAL if the phone parameter is not a positive number 1660 * and the tl_receive_msg is NULL. 1661 * @return ENOMEM if there is not enough memory left. 1662 * 1663 */ 1664 static int ip_register(int protocol, services_t service, async_sess_t *sess, 1650 1665 tl_received_msg_t received_msg) 1651 1666 { … … 1653 1668 int index; 1654 1669 1655 if ( !protocol || !service || ((phone < 0) && !received_msg))1670 if ((!protocol) || (!service) || ((!sess) && (!received_msg))) 1656 1671 return EINVAL; 1657 1672 1658 1673 proto = (ip_proto_t *) malloc(sizeof(ip_protos_t)); 1659 1674 if (!proto) … … 1662 1677 proto->protocol = protocol; 1663 1678 proto->service = service; 1664 proto-> phone = phone;1679 proto->sess = sess; 1665 1680 proto->received_msg = received_msg; 1666 1681 … … 1674 1689 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1675 1690 1676 printf("%s: Protocol registered (protocol: %d , phone: %d)\n",1677 NAME, proto->protocol , proto->phone);1691 printf("%s: Protocol registered (protocol: %d)\n", 1692 NAME, proto->protocol); 1678 1693 1679 1694 return EOK; 1680 1695 } 1681 1696 1682 1683 static int 1684 ip_add_route_req_local(int ip_phone, device_id_t device_id, in_addr_t address, 1697 static int ip_add_route_req_local(nic_device_id_t device_id, in_addr_t address, 1685 1698 in_addr_t netmask, in_addr_t gateway) 1686 1699 { … … 1716 1729 } 1717 1730 1718 static int 1719 ip_set_gateway_req_local(int ip_phone, device_id_t device_id,in_addr_t gateway)1731 static int ip_set_gateway_req_local(nic_device_id_t device_id, 1732 in_addr_t gateway) 1720 1733 { 1721 1734 ip_netif_t *netif; … … 1741 1754 /** Notify the IP module about the received error notification packet. 1742 1755 * 1743 * @param[in] ip_phone The IP module phone used for (semi)remote calls. 1744 * @param[in] device_id The device identifier. 1745 * @param[in] packet The received packet or the received packet queue. 1746 * @param[in] target The target internetwork module service to be 1747 * delivered to. 1748 * @param[in] error The packet error reporting service. Prefixes the 1749 * received packet. 1750 * @return EOK on success. 1751 * 1752 */ 1753 static int 1754 ip_received_error_msg_local(int ip_phone, device_id_t device_id, 1756 * @param[in] device_id Device identifier. 1757 * @param[in] packet Received packet or the received packet queue. 1758 * @param[in] target Target internetwork module service to be 1759 * delivered to. 1760 * @param[in] error Packet error reporting service. Prefixes the 1761 * received packet. 1762 * 1763 * @return EOK on success. 1764 * 1765 */ 1766 static int ip_received_error_msg_local(nic_device_id_t device_id, 1755 1767 packet_t *packet, services_t target, services_t error) 1756 1768 { … … 1797 1809 address.value = (uint8_t *) &header->destination_address; 1798 1810 address.length = sizeof(header->destination_address); 1799 arp_clear_address_req(netif->arp-> phone,1811 arp_clear_address_req(netif->arp->sess, 1800 1812 netif->device_id, SERVICE_IP, &address); 1801 1813 } … … 1811 1823 } 1812 1824 1813 static int 1814 ip_get_route_req_local(int ip_phone, ip_protocol_t protocol, 1825 static int ip_get_route_req_local(ip_protocol_t protocol, 1815 1826 const struct sockaddr *destination, socklen_t addrlen, 1816 device_id_t *device_id, void **header, size_t *headerlen)1827 nic_device_id_t *device_id, void **header, size_t *headerlen) 1817 1828 { 1818 1829 struct sockaddr_in *address_in; … … 1905 1916 size_t suffix; 1906 1917 size_t content; 1907 device_id_t device_id;1918 nic_device_id_t device_id; 1908 1919 int rc; 1909 1920 1910 1921 *answer_count = 0; 1922 1923 if (!IPC_GET_IMETHOD(*call)) 1924 return EOK; 1925 1926 async_sess_t *callback = 1927 async_callback_receive_start(EXCHANGE_SERIALIZE, call); 1928 if (callback) 1929 return ip_register(IL_GET_PROTO(*call), IL_GET_SERVICE(*call), 1930 callback, NULL); 1931 1911 1932 switch (IPC_GET_IMETHOD(*call)) { 1912 case IPC_M_PHONE_HUNGUP:1913 return EOK;1914 1915 case IPC_M_CONNECT_TO_ME:1916 return ip_register(IL_GET_PROTO(*call), IL_GET_SERVICE(*call),1917 IPC_GET_PHONE(*call), NULL);1918 1919 1933 case NET_IP_DEVICE: 1920 return ip_device_req_local( 0,IPC_GET_DEVICE(*call),1934 return ip_device_req_local(IPC_GET_DEVICE(*call), 1921 1935 IPC_GET_SERVICE(*call)); 1922 1936 1923 1937 case NET_IP_RECEIVED_ERROR: 1924 rc = packet_translate_remote(ip_globals.net_ phone, &packet,1938 rc = packet_translate_remote(ip_globals.net_sess, &packet, 1925 1939 IPC_GET_PACKET(*call)); 1926 1940 if (rc != EOK) 1927 1941 return rc; 1928 return ip_received_error_msg_local( 0,IPC_GET_DEVICE(*call),1942 return ip_received_error_msg_local(IPC_GET_DEVICE(*call), 1929 1943 packet, IPC_GET_TARGET(*call), IPC_GET_ERROR(*call)); 1930 1944 1931 1945 case NET_IP_ADD_ROUTE: 1932 return ip_add_route_req_local( 0,IPC_GET_DEVICE(*call),1946 return ip_add_route_req_local(IPC_GET_DEVICE(*call), 1933 1947 IP_GET_ADDRESS(*call), IP_GET_NETMASK(*call), 1934 1948 IP_GET_GATEWAY(*call)); 1935 1949 1936 1950 case NET_IP_SET_GATEWAY: 1937 return ip_set_gateway_req_local( 0,IPC_GET_DEVICE(*call),1951 return ip_set_gateway_req_local(IPC_GET_DEVICE(*call), 1938 1952 IP_GET_GATEWAY(*call)); 1939 1953 … … 1944 1958 return rc; 1945 1959 1946 rc = ip_get_route_req_local( 0,IP_GET_PROTOCOL(*call), addr,1960 rc = ip_get_route_req_local(IP_GET_PROTOCOL(*call), addr, 1947 1961 (socklen_t) addrlen, &device_id, &header, &headerlen); 1948 1962 if (rc != EOK) … … 1975 1989 1976 1990 case NET_IP_SEND: 1977 rc = packet_translate_remote(ip_globals.net_ phone, &packet,1991 rc = packet_translate_remote(ip_globals.net_sess, &packet, 1978 1992 IPC_GET_PACKET(*call)); 1979 1993 if (rc != EOK) 1980 1994 return rc; 1981 1995 1982 return ip_send_msg_local( 0,IPC_GET_DEVICE(*call), packet, 0,1996 return ip_send_msg_local(IPC_GET_DEVICE(*call), packet, 0, 1983 1997 IPC_GET_ERROR(*call)); 1984 1998 } -
uspace/srv/net/il/ip/ip.h
r2bdf8313 rb0f00a9 38 38 #define NET_IP_H_ 39 39 40 #include <async.h> 40 41 #include <fibril_synch.h> 41 42 #include <ipc/services.h> … … 91 92 in_addr_t broadcast; 92 93 /** Device identifier. */ 93 device_id_t device_id;94 nic_device_id_t device_id; 94 95 /** Indicates whether using DHCP. */ 95 96 int dhcp; … … 98 99 /** Packet dimension. */ 99 100 packet_dimension_t packet_dimension; 100 /** Netif module phone. */101 int phone;101 /** Netif module session. */ 102 async_sess_t *sess; 102 103 /** Routing table. */ 103 104 ip_routes_t routes; … … 107 108 services_t service; 108 109 /** Device state. */ 109 device_state_t state;110 nic_device_state_t state; 110 111 }; 111 112 112 113 /** IP protocol specific data. */ 113 114 struct ip_proto { 114 /** Protocol module phone. */115 int phone;115 /** Protocol module session. */ 116 async_sess_t *sess; 116 117 /** Protocol number. */ 117 118 int protocol; … … 142 143 /** Known support modules. */ 143 144 modules_t modules; 144 /** Networking module phone. */145 int net_phone;145 /** Networking module session. */ 146 async_sess_t *net_sess; 146 147 /** Registered network interfaces. */ 147 148 ip_netifs_t netifs; -
uspace/srv/net/net/Makefile
r2bdf8313 rb0f00a9 30 30 USPACE_PREFIX = ../../.. 31 31 ROOT_PATH = $(USPACE_PREFIX)/.. 32 LIBS = $(LIBNET_PREFIX)/libnet.a $(LIBPACKET_PREFIX)/libpacket.a33 EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include -I$(LIBPACKET_PREFIX)/include32 LIBS = $(LIBNET_PREFIX)/libnet.a 33 EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include 34 34 35 35 COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common … … 43 43 SOURCES = \ 44 44 net.c \ 45 net_standalone.c45 packet_server.c 46 46 47 47 include $(USPACE_PREFIX)/Makefile.common -
uspace/srv/net/net/net.c
r2bdf8313 rb0f00a9 1 1 /* 2 2 * Copyright (c) 2009 Lukas Mejdrech 3 * Copyright (c) 2011 Radim Vansa 3 4 * All rights reserved. 4 5 * … … 31 32 */ 32 33 33 /** @file 34 * Networking subsystem central module implementation. 35 * 36 */ 37 38 #include "net.h" 39 34 #include <assert.h> 40 35 #include <async.h> 41 36 #include <ctype.h> 42 37 #include <ddi.h> 43 38 #include <errno.h> 39 #include <str_error.h> 44 40 #include <malloc.h> 45 41 #include <stdio.h> 46 42 #include <str.h> 43 #include <devman.h> 47 44 #include <str_error.h> 48 45 #include <ns.h> 49 46 #include <ipc/services.h> 50 47 #include <ipc/net.h> 51 48 #include <ipc/net_net.h> 52 49 #include <ipc/il.h> 50 #include <ipc/ip.h> 53 51 #include <ipc/nil.h> 54 55 #include <net/modules.h>56 52 #include <net/packet.h> 57 53 #include <net/device.h> 58 59 54 #include <adt/char_map.h> 60 55 #include <adt/generic_char_map.h> 61 56 #include <adt/measured_strings.h> 62 57 #include <adt/module_map.h> 63 64 #include <netif_remote.h>65 58 #include <nil_remote.h> 66 59 #include <net_interface.h> 67 60 #include <ip_interface.h> 68 69 /** Networking module name. */ 70 #define NAME "net" 71 72 /** File read buffer size. */ 73 #define BUFFER_SIZE 256 61 #include <device/nic.h> 62 #include <dirent.h> 63 #include <fcntl.h> 64 #include <cfg.h> 65 #include "net.h" 66 #include "packet_server.h" 67 68 #define MAX_PATH_LENGTH 1024 74 69 75 70 /** Networking module global data. */ … … 78 73 GENERIC_CHAR_MAP_IMPLEMENT(measured_strings, measured_string_t); 79 74 DEVICE_MAP_IMPLEMENT(netifs, netif_t); 80 81 static int startup(void);82 75 83 76 /** Add the configured setting to the configuration map. … … 91 84 * 92 85 */ 93 int add_configuration(measured_strings_t *configuration, const uint8_t *name,94 const uint8_t * value)86 static int add_configuration(measured_strings_t *configuration, 87 const uint8_t *name, const uint8_t *value) 95 88 { 96 89 int rc; … … 113 106 /** Generate new system-unique device identifier. 114 107 * 115 * @return The system-unique devic identifier. 116 */ 117 static device_id_t generate_new_device_id(void) 108 * @return The system-unique devic identifier. 109 * 110 */ 111 static nic_device_id_t generate_new_device_id(void) 118 112 { 119 113 return device_assign_devno(); 120 }121 122 static int parse_line(measured_strings_t *configuration, uint8_t *line)123 {124 int rc;125 126 /* From the beginning */127 uint8_t *name = line;128 129 /* Skip comments and blank lines */130 if ((*name == '#') || (*name == '\0'))131 return EOK;132 133 /* Skip spaces */134 while (isspace(*name))135 name++;136 137 /* Remember the name start */138 uint8_t *value = name;139 140 /* Skip the name */141 while (isalnum(*value) || (*value == '_'))142 value++;143 144 if (*value == '=') {145 /* Terminate the name */146 *value = '\0';147 } else {148 /* Terminate the name */149 *value = '\0';150 151 /* Skip until '=' */152 value++;153 while ((*value) && (*value != '='))154 value++;155 156 /* Not found? */157 if (*value != '=')158 return EINVAL;159 }160 161 value++;162 163 /* Skip spaces */164 while (isspace(*value))165 value++;166 167 /* Create a bulk measured string till the end */168 measured_string_t *setting =169 measured_string_create_bulk(value, 0);170 if (!setting)171 return ENOMEM;172 173 /* Add the configuration setting */174 rc = measured_strings_add(configuration, name, 0, setting);175 if (rc != EOK) {176 free(setting);177 return rc;178 }179 180 return EOK;181 114 } 182 115 … … 186 119 printf("%s: Reading configuration file %s/%s\n", NAME, directory, filename); 187 120 188 /* Construct the full filename */ 189 char fname[BUFFER_SIZE]; 190 if (snprintf(fname, BUFFER_SIZE, "%s/%s", directory, filename) > BUFFER_SIZE) 191 return EOVERFLOW; 192 193 /* Open the file */ 194 FILE *cfg = fopen(fname, "r"); 195 if (!cfg) 121 cfg_file_t cfg; 122 int rc = cfg_load_path(directory, filename, &cfg); 123 if (rc != EOK) 124 return rc; 125 126 if (cfg_anonymous(&cfg) == NULL) { 127 cfg_unload(&cfg); 196 128 return ENOENT; 197 198 /* 199 * Read the configuration line by line 200 * until an error or the end of file 201 */ 202 unsigned int line_number = 0; 203 size_t index = 0; 204 uint8_t line[BUFFER_SIZE]; 205 206 while (!ferror(cfg) && !feof(cfg)) { 207 int read = fgetc(cfg); 208 if ((read > 0) && (read != '\n') && (read != '\r')) { 209 if (index >= BUFFER_SIZE) { 210 line[BUFFER_SIZE - 1] = '\0'; 211 fprintf(stderr, "%s: Configuration line %u too " 212 "long: %s\n", NAME, line_number, (char *) line); 213 214 /* No space left in the line buffer */ 215 return EOVERFLOW; 216 } 217 /* Append the character */ 218 line[index] = (uint8_t) read; 219 index++; 220 } else { 221 /* On error or new line */ 222 line[index] = '\0'; 223 line_number++; 224 if (parse_line(configuration, line) != EOK) { 225 fprintf(stderr, "%s: Configuration error on " 226 "line %u: %s\n", NAME, line_number, (char *) line); 227 } 228 229 index = 0; 129 } 130 131 cfg_section_foreach(cfg_anonymous(&cfg), link) { 132 const cfg_entry_t *entry = cfg_entry_instance(link); 133 134 rc = add_configuration(configuration, 135 (uint8_t *) entry->key, (uint8_t *) entry->value); 136 if (rc != EOK) { 137 cfg_unload(&cfg); 138 return rc; 230 139 } 231 140 } 232 141 233 fclose(cfg);142 cfg_unload(&cfg); 234 143 return EOK; 235 144 } … … 259 168 return read_configuration_file(CONF_DIR, CONF_GENERAL_FILE, 260 169 &net_globals.configuration); 261 }262 263 /** Initialize the networking module.264 *265 * @param[in] client_connection The client connection processing266 * function. The module skeleton propagates267 * its own one.268 *269 * @return EOK on success.270 * @return ENOMEM if there is not enough memory left.271 *272 */273 static int net_initialize(async_client_conn_t client_connection)274 {275 int rc;276 277 netifs_initialize(&net_globals.netifs);278 char_map_initialize(&net_globals.netif_names);279 modules_initialize(&net_globals.modules);280 measured_strings_initialize(&net_globals.configuration);281 282 /* TODO: dynamic configuration */283 rc = read_configuration();284 if (rc != EOK)285 return rc;286 287 rc = add_module(NULL, &net_globals.modules, (uint8_t *) LO_NAME,288 (uint8_t *) LO_FILENAME, SERVICE_LO, 0, connect_to_service);289 if (rc != EOK)290 return rc;291 292 rc = add_module(NULL, &net_globals.modules, (uint8_t *) NE2000_NAME,293 (uint8_t *) NE2000_FILENAME, SERVICE_NE2000, 0, connect_to_service);294 if (rc != EOK)295 return rc;296 297 rc = add_module(NULL, &net_globals.modules, (uint8_t *) ETHERNET_NAME,298 (uint8_t *) ETHERNET_FILENAME, SERVICE_ETHERNET, 0, connect_to_service);299 if (rc != EOK)300 return rc;301 302 rc = add_module(NULL, &net_globals.modules, (uint8_t *) NILDUMMY_NAME,303 (uint8_t *) NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, connect_to_service);304 if (rc != EOK)305 return rc;306 307 /* Build specific initialization */308 return net_initialize_build(client_connection);309 }310 311 /** Start the networking module.312 *313 * Initializes the client connection serving function,314 * initializes the module, registers the module service315 * and starts the async manager, processing IPC messages316 * in an infinite loop.317 *318 * @param[in] client_connection The client connection319 * processing function. The320 * module skeleton propagates321 * its own one.322 *323 * @return EOK on successful module termination.324 * @return Other error codes as defined for the net_initialize() function.325 * @return Other error codes as defined for the REGISTER_ME() macro function.326 *327 */328 static int net_module_start(async_client_conn_t client_connection)329 {330 int rc;331 332 async_set_client_connection(client_connection);333 rc = pm_init();334 if (rc != EOK)335 return rc;336 337 rc = net_initialize(client_connection);338 if (rc != EOK)339 goto out;340 341 rc = async_connect_to_me(PHONE_NS, SERVICE_NETWORKING, 0, 0, NULL);342 if (rc != EOK)343 goto out;344 345 rc = startup();346 if (rc != EOK)347 goto out;348 349 task_retval(0);350 async_manager();351 352 out:353 pm_destroy();354 return rc;355 170 } 356 171 … … 368 183 */ 369 184 static int net_get_conf(measured_strings_t *netif_conf, 370 measured_string_t *configuration, size_t count , uint8_t **data)371 { 372 if ( data)373 *data = NULL;185 measured_string_t *configuration, size_t count) 186 { 187 if ((!configuration) || (count <= 0)) 188 return EINVAL; 374 189 375 190 size_t index; … … 393 208 } 394 209 395 int net_get_conf_req(int net_phone, measured_string_t **configuration, 396 size_t count, uint8_t **data) 397 { 398 if (!configuration || (count <= 0)) 399 return EINVAL; 400 401 return net_get_conf(NULL, *configuration, count, data); 402 } 403 404 int net_get_device_conf_req(int net_phone, device_id_t device_id, 405 measured_string_t **configuration, size_t count, uint8_t **data) 406 { 407 if ((!configuration) || (count == 0)) 408 return EINVAL; 409 210 static int net_get_device_conf(nic_device_id_t device_id, 211 measured_string_t *configuration, size_t count) 212 { 410 213 netif_t *netif = netifs_find(&net_globals.netifs, device_id); 411 214 if (netif) 412 return net_get_conf(&netif->configuration, *configuration, count, data);215 return net_get_conf(&netif->configuration, configuration, count); 413 216 else 414 return net_get_conf(NULL, *configuration, count, data); 415 } 416 417 void net_free_settings(measured_string_t *settings, uint8_t *data) 418 { 217 return net_get_conf(NULL, configuration, count); 218 } 219 220 static int net_get_devices(measured_string_t **devices, size_t *dev_count) 221 { 222 if (!devices) 223 return EBADMEM; 224 225 size_t max_count = netifs_count(&net_globals.netifs); 226 *devices = malloc(max_count * sizeof(measured_string_t)); 227 if (*devices == NULL) 228 return ENOMEM; 229 230 size_t count = 0; 231 for (size_t i = 0; i < max_count; i++) { 232 netif_t *item = netifs_get_index(&net_globals.netifs, i); 233 if (item->sess != NULL) { 234 /* 235 * Use format "device_id:device_name" 236 * FIXME: This typecasting looks really ugly 237 */ 238 (*devices)[count].length = asprintf( 239 (char **) &((*devices)[count].value), 240 NIC_DEVICE_PRINT_FMT ":%s", item->id, 241 (const char *) item->name); 242 count++; 243 } 244 } 245 246 *dev_count = (size_t) count; 247 return EOK; 248 } 249 250 static int net_get_devices_count() 251 { 252 size_t max_count = netifs_count(&net_globals.netifs); 253 254 size_t count = 0; 255 for (size_t i = 0; i < max_count; i++) { 256 netif_t *item = netifs_get_index(&net_globals.netifs, i); 257 if (item->sess != NULL) 258 count++; 259 } 260 261 return count; 262 } 263 264 static void net_free_devices(measured_string_t *devices, size_t count) 265 { 266 size_t i; 267 for (i = 0; i < count; ++i) 268 free(devices[i].value); 269 270 free(devices); 419 271 } 420 272 … … 435 287 * 436 288 */ 437 static int start_device(netif_t *netif) 438 { 439 int rc; 440 441 /* Mandatory netif */ 442 measured_string_t *setting = 443 measured_strings_find(&netif->configuration, (uint8_t *) CONF_NETIF, 0); 444 445 netif->driver = get_running_module(&net_globals.modules, setting->value); 446 if (!netif->driver) { 447 fprintf(stderr, "%s: Failed to start network interface driver '%s'\n", 448 NAME, setting->value); 449 return EINVAL; 289 static int init_device(netif_t *netif, devman_handle_t handle) 290 { 291 printf("%s: Initializing device '%s'\n", NAME, netif->name); 292 293 netif->handle = handle; 294 netif->sess = devman_device_connect(EXCHANGE_SERIALIZE, netif->handle, 295 IPC_FLAG_BLOCKING); 296 if (netif->sess == NULL) { 297 printf("%s: Unable to connect to device\n", NAME); 298 return EREFUSED; 450 299 } 451 300 452 301 /* Optional network interface layer */ 453 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_NIL, 0); 302 measured_string_t *setting = measured_strings_find(&netif->configuration, 303 (uint8_t *) CONF_NIL, 0); 454 304 if (setting) { 455 netif->nil = get_running_module(&net_globals.modules, setting->value); 305 netif->nil = get_running_module(&net_globals.modules, 306 setting->value); 456 307 if (!netif->nil) { 457 fprintf(stderr, "%s: Failed to startnetwork interface layer '%s'\n",308 printf("%s: Unable to connect to network interface layer '%s'\n", 458 309 NAME, setting->value); 459 310 return EINVAL; … … 463 314 464 315 /* Mandatory internet layer */ 465 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IL, 0); 466 netif->il = get_running_module(&net_globals.modules, setting->value); 316 setting = measured_strings_find(&netif->configuration, 317 (uint8_t *) CONF_IL, 0); 318 netif->il = get_running_module(&net_globals.modules, 319 setting->value); 467 320 if (!netif->il) { 468 fprintf(stderr, "%s: Failed to startinternet layer '%s'\n",321 printf("%s: Unable to connect to internet layer '%s'\n", 469 322 NAME, setting->value); 470 323 return EINVAL; 471 324 } 472 325 473 /* Hardware configuration */474 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IRQ, 0);475 int irq = setting ? strtol((char *) setting->value, NULL, 10) : 0;476 477 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IO, 0);478 uintptr_t io = setting ? strtol((char *) setting->value, NULL, 16) : 0;479 480 rc = netif_probe_req(netif->driver->phone, netif->id, irq, (void *) io);481 if (rc != EOK)482 return rc;483 484 326 /* Network interface layer startup */ 485 services_t internet_service; 327 int rc; 328 services_t nil_service; 486 329 if (netif->nil) { 487 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_MTU, 0); 330 setting = measured_strings_find(&netif->configuration, 331 (uint8_t *) CONF_MTU, 0); 488 332 if (!setting) 489 333 setting = measured_strings_find(&net_globals.configuration, 490 334 (uint8_t *) CONF_MTU, 0); 491 335 492 int mtu = setting ? strtol((char *) setting->value, NULL, 10) : 0; 493 494 rc = nil_device_req(netif->nil->phone, netif->id, mtu, 495 netif->driver->service); 496 if (rc != EOK) 336 int mtu = setting ? 337 strtol((const char *) setting->value, NULL, 10) : 0; 338 rc = nil_device_req(netif->nil->sess, netif->id, 339 netif->handle, mtu); 340 if (rc != EOK) { 341 printf("%s: Unable to start network interface layer\n", 342 NAME); 497 343 return rc; 498 499 internet_service = netif->nil->service; 344 } 345 346 nil_service = netif->nil->service; 500 347 } else 501 internet_service = netif->driver->service;348 nil_service = -1; 502 349 503 350 /* Inter-network layer startup */ 504 351 switch (netif->il->service) { 505 352 case SERVICE_IP: 506 rc = ip_device_req(netif->il-> phone, netif->id,507 internet_service);508 if (rc != EOK)353 rc = ip_device_req(netif->il->sess, netif->id, nil_service); 354 if (rc != EOK) { 355 printf("%s: Unable to start internet layer\n", NAME); 509 356 return rc; 357 } 358 510 359 break; 511 360 default: … … 513 362 } 514 363 515 return netif_start_req(netif->driver->phone, netif->id); 516 } 517 518 /** Read the configuration and start all network interfaces. 519 * 520 * @return EOK on success. 521 * @return EXDEV if there is no available system-unique device identifier. 522 * @return EINVAL if any of the network interface names are not configured. 523 * @return ENOMEM if there is not enough memory left. 524 * @return Other error codes as defined for the read_configuration() 525 * function. 526 * @return Other error codes as defined for the read_netif_configuration() 527 * function. 528 * @return Other error codes as defined for the start_device() function. 529 * 530 */ 531 static int startup(void) 532 { 533 const char *conf_files[] = { 534 "lo", 535 "ne2k" 536 }; 537 size_t count = sizeof(conf_files) / sizeof(char *); 538 int rc; 539 540 size_t i; 541 for (i = 0; i < count; i++) { 542 netif_t *netif = (netif_t *) malloc(sizeof(netif_t)); 543 if (!netif) 544 return ENOMEM; 545 546 netif->id = generate_new_device_id(); 547 if (!netif->id) 548 return EXDEV; 549 550 rc = measured_strings_initialize(&netif->configuration); 364 printf("%s: Activating device '%s'\n", NAME, netif->name); 365 return nic_set_state(netif->sess, NIC_STATE_ACTIVE); 366 } 367 368 static int net_port_ready(devman_handle_t handle) 369 { 370 char hwpath[MAX_PATH_LENGTH]; 371 int rc = devman_fun_get_path(handle, hwpath, MAX_PATH_LENGTH); 372 if (rc != EOK) 373 return EINVAL; 374 375 int index = char_map_find(&net_globals.netif_hwpaths, 376 (uint8_t *) hwpath, 0); 377 if (index == CHAR_MAP_NULL) 378 return ENOENT; 379 380 netif_t *netif = netifs_get_index(&net_globals.netifs, index); 381 if (netif == NULL) 382 return ENOENT; 383 384 rc = init_device(netif, handle); 385 if (rc != EOK) 386 return rc; 387 388 /* Increment module usage */ 389 if (netif->nil) 390 netif->nil->usage++; 391 392 netif->il->usage++; 393 394 return EOK; 395 } 396 397 static int net_driver_ready_local(devman_handle_t handle) 398 { 399 devman_handle_t *funs; 400 size_t count; 401 int rc = devman_dev_get_functions(handle, &funs, &count); 402 if (rc != EOK) 403 return rc; 404 405 for (size_t i = 0; i < count; i++) { 406 rc = net_port_ready(funs[i]); 551 407 if (rc != EOK) 552 408 return rc; 553 554 /* Read configuration files */555 rc = read_netif_configuration(conf_files[i], netif);556 if (rc != EOK) {557 measured_strings_destroy(&netif->configuration, free);558 free(netif);559 return rc;560 }561 562 /* Mandatory name */563 measured_string_t *setting =564 measured_strings_find(&netif->configuration, (uint8_t *) CONF_NAME, 0);565 if (!setting) {566 fprintf(stderr, "%s: Network interface name is missing\n", NAME);567 measured_strings_destroy(&netif->configuration, free);568 free(netif);569 return EINVAL;570 }571 netif->name = setting->value;572 573 /* Add to the netifs map */574 int index = netifs_add(&net_globals.netifs, netif->id, netif);575 if (index < 0) {576 measured_strings_destroy(&netif->configuration, free);577 free(netif);578 return index;579 }580 581 /*582 * Add to the netif names map and start network interfaces583 * and needed modules.584 */585 rc = char_map_add(&net_globals.netif_names, netif->name, 0,586 index);587 if (rc != EOK) {588 measured_strings_destroy(&netif->configuration, free);589 netifs_exclude_index(&net_globals.netifs, index, free);590 return rc;591 }592 593 rc = start_device(netif);594 if (rc != EOK) {595 printf("%s: Ignoring failed interface %s (%s)\n", NAME,596 netif->name, str_error(rc));597 measured_strings_destroy(&netif->configuration, free);598 netifs_exclude_index(&net_globals.netifs, index, free);599 continue;600 }601 602 /* Increment modules' usage */603 netif->driver->usage++;604 if (netif->nil)605 netif->nil->usage++;606 netif->il->usage++;607 608 printf("%s: Network interface started (name: %s, id: %d, driver: %s, "609 "nil: %s, il: %s)\n", NAME, netif->name, netif->id,610 netif->driver->name, netif->nil ? (char *) netif->nil->name : "[none]",611 netif->il->name);612 409 } 613 410 … … 630 427 * 631 428 */ 632 int net_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,633 size_t *answer_count)429 static int net_message(ipc_callid_t callid, ipc_call_t *call, 430 ipc_call_t *answer, size_t *answer_count) 634 431 { 635 432 measured_string_t *strings; 636 433 uint8_t *data; 637 434 int rc; 435 size_t count; 638 436 639 437 *answer_count = 0; 438 439 if (!IPC_GET_IMETHOD(*call)) 440 return EOK; 441 640 442 switch (IPC_GET_IMETHOD(*call)) { 641 case IPC_M_PHONE_HUNGUP:642 return EOK;643 443 case NET_NET_GET_DEVICE_CONF: 644 444 rc = measured_strings_receive(&strings, &data, … … 646 446 if (rc != EOK) 647 447 return rc; 648 net_get_device_conf_req(0, IPC_GET_DEVICE(*call), &strings, 649 IPC_GET_COUNT(*call), NULL); 650 651 /* Strings should not contain received data anymore */ 652 free(data); 448 449 net_get_device_conf(IPC_GET_DEVICE(*call), strings, 450 IPC_GET_COUNT(*call)); 653 451 654 452 rc = measured_strings_reply(strings, IPC_GET_COUNT(*call)); 655 453 free(strings); 454 free(data); 656 455 return rc; 657 456 case NET_NET_GET_CONF: … … 660 459 if (rc != EOK) 661 460 return rc; 662 net_get_conf_req(0, &strings, IPC_GET_COUNT(*call), NULL); 663 664 /* Strings should not contain received data anymore */ 665 free(data); 461 462 net_get_conf(NULL, strings, IPC_GET_COUNT(*call)); 666 463 667 464 rc = measured_strings_reply(strings, IPC_GET_COUNT(*call)); 668 465 free(strings); 669 return rc; 670 case NET_NET_STARTUP: 671 return startup(); 672 } 673 674 return ENOTSUP; 466 free(data); 467 return rc; 468 case NET_NET_GET_DEVICES_COUNT: 469 count = (size_t) net_get_devices_count(); 470 IPC_SET_ARG1(*answer, count); 471 *answer_count = 1; 472 return EOK; 473 case NET_NET_GET_DEVICES: 474 rc = net_get_devices(&strings, &count); 475 if (rc != EOK) 476 return rc; 477 478 rc = measured_strings_reply(strings, count); 479 net_free_devices(strings, count); 480 return rc; 481 case NET_NET_DRIVER_READY: 482 rc = net_driver_ready_local(IPC_GET_ARG1(*call)); 483 *answer_count = 0; 484 return rc; 485 default: 486 return ENOTSUP; 487 } 675 488 } 676 489 677 490 /** Default thread for new connections. 678 491 * 679 * @param[in] iid The initial message identifier.492 * @param[in] iid The initial message identifier. 680 493 * @param[in] icall The initial message call structure. 681 * 682 */ 683 static void net_client_connection(ipc_callid_t iid, ipc_call_t *icall) 494 * @param[in] arg Local argument. 495 * 496 */ 497 static void net_client_connection(ipc_callid_t iid, ipc_call_t *icall, 498 void *arg) 684 499 { 685 500 /* … … 692 507 /* Clear the answer structure */ 693 508 ipc_call_t answer; 694 size_t answer_count;695 refresh_answer(&answer, & answer_count);509 size_t count; 510 refresh_answer(&answer, &count); 696 511 697 512 /* Fetch the next message */ … … 700 515 701 516 /* Process the message */ 702 int res = net_module_message(callid, &call, &answer, &answer_count); 517 int res; 518 if (IS_NET_PACKET_MESSAGE(call)) 519 res = packet_server_message(callid, &call, &answer, &count); 520 else 521 res = net_message(callid, &call, &answer, &count); 703 522 704 523 /* End if told to either by the message or the processing result */ 705 if (( IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))524 if ((!IPC_GET_IMETHOD(call)) || (res == EHANGUP)) 706 525 return; 707 526 708 527 /* Answer the message */ 709 answer_call(callid, res, &answer, answer_count);528 answer_call(callid, res, &answer, count); 710 529 } 711 530 } … … 713 532 int main(int argc, char *argv[]) 714 533 { 715 return net_module_start(net_client_connection); 534 netifs_initialize(&net_globals.netifs); 535 char_map_initialize(&net_globals.netif_hwpaths); 536 modules_initialize(&net_globals.modules); 537 measured_strings_initialize(&net_globals.configuration); 538 async_set_client_connection(net_client_connection); 539 540 int rc = pm_init(); 541 if (rc != EOK) { 542 printf("%s: Unable to initialize packet management\n", NAME); 543 return rc; 544 } 545 546 rc = packet_server_init(); 547 if (rc != EOK) { 548 printf("%s: Unable to initialize packet server\n", NAME); 549 pm_destroy(); 550 return rc; 551 } 552 553 rc = read_configuration(); 554 if (rc != EOK) { 555 printf("%s: Error reading configuration\n", NAME); 556 pm_destroy(); 557 return rc; 558 } 559 560 DIR *config_dir = opendir(CONF_DIR); 561 if (config_dir != NULL) { 562 struct dirent *dir_entry; 563 while ((dir_entry = readdir(config_dir))) { 564 /* Ignore files without the CONF_EXT extension */ 565 if ((str_size(dir_entry->d_name) < str_size(CONF_EXT)) || 566 (str_cmp(dir_entry->d_name + str_size(dir_entry->d_name) - 567 str_size(CONF_EXT), CONF_EXT) != 0)) 568 continue; 569 570 571 netif_t *netif = (netif_t *) malloc(sizeof(netif_t)); 572 if (!netif) 573 continue; 574 575 netif->handle = -1; 576 netif->sess = NULL; 577 578 netif->id = generate_new_device_id(); 579 if (!netif->id) { 580 free(netif); 581 continue; 582 } 583 584 rc = measured_strings_initialize(&netif->configuration); 585 if (rc != EOK) { 586 free(netif); 587 continue; 588 } 589 590 rc = read_netif_configuration(dir_entry->d_name, netif); 591 if (rc != EOK) { 592 printf("%s: Error reading configuration %s\n", NAME, 593 dir_entry->d_name); 594 free(netif); 595 continue; 596 } 597 598 measured_string_t *name = measured_strings_find(&netif->configuration, 599 (uint8_t *) CONF_NAME, 0); 600 if (!name) { 601 printf("%s: Network interface name is missing in %s\n", 602 NAME, dir_entry->d_name); 603 measured_strings_destroy(&netif->configuration, free); 604 free(netif); 605 continue; 606 } 607 608 netif->name = name->value; 609 610 /* Mandatory hardware path */ 611 measured_string_t *hwpath = measured_strings_find( 612 &netif->configuration, (const uint8_t *) CONF_HWPATH, 0); 613 if (!hwpath) { 614 printf("%s: Hardware path is missing in %s\n", 615 NAME, dir_entry->d_name); 616 measured_strings_destroy(&netif->configuration, free); 617 free(netif); 618 continue; 619 } 620 621 int index = netifs_add(&net_globals.netifs, netif->id, netif); 622 if (index < 0) { 623 measured_strings_destroy(&netif->configuration, free); 624 free(netif); 625 continue; 626 } 627 628 /* 629 * Add to the hardware paths map and init network interfaces 630 * and needed modules. 631 */ 632 rc = char_map_add(&net_globals.netif_hwpaths, hwpath->value, 0, index); 633 if (rc != EOK) { 634 measured_strings_destroy(&netif->configuration, free); 635 netifs_exclude_index(&net_globals.netifs, index, free); 636 continue; 637 } 638 } 639 640 closedir(config_dir); 641 } 642 643 rc = add_module(NULL, &net_globals.modules, (uint8_t *) ETHERNET_NAME, 644 (uint8_t *) ETHERNET_FILENAME, SERVICE_ETHERNET, 0, connect_to_service); 645 if (rc != EOK) { 646 printf("%s: Error adding module '%s'\n", NAME, ETHERNET_NAME); 647 pm_destroy(); 648 return rc; 649 } 650 651 rc = add_module(NULL, &net_globals.modules, (uint8_t *) NILDUMMY_NAME, 652 (uint8_t *) NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, connect_to_service); 653 if (rc != EOK) { 654 printf("%s: Error adding module '%s'\n", NAME, NILDUMMY_NAME); 655 pm_destroy(); 656 return rc; 657 } 658 659 task_id_t task_id = net_spawn((uint8_t *) IP_FILENAME); 660 if (!task_id) { 661 printf("%s: Error spawning IP module\n", NAME); 662 pm_destroy(); 663 return EINVAL; 664 } 665 666 rc = add_module(NULL, &net_globals.modules, (uint8_t *) IP_NAME, 667 (uint8_t *) IP_FILENAME, SERVICE_IP, task_id, ip_connect_module); 668 if (rc != EOK) { 669 printf("%s: Error adding module '%s'\n", NAME, IP_NAME); 670 pm_destroy(); 671 return rc; 672 } 673 674 if (!net_spawn((uint8_t *) "/srv/icmp")) { 675 printf("%s: Error spawning ICMP module\n", NAME); 676 pm_destroy(); 677 return EINVAL; 678 } 679 680 if (!net_spawn((uint8_t *) "/srv/udp")) { 681 printf("%s: Error spawning UDP module\n", NAME); 682 pm_destroy(); 683 return EINVAL; 684 } 685 686 if (!net_spawn((uint8_t *) "/srv/tcp")) { 687 printf("%s: Error spawning TCP module\n", NAME); 688 pm_destroy(); 689 return EINVAL; 690 } 691 692 rc = service_register(SERVICE_NETWORKING); 693 if (rc != EOK) { 694 printf("%s: Error registering service\n", NAME); 695 pm_destroy(); 696 return rc; 697 } 698 699 task_retval(0); 700 async_manager(); 701 return 0; 716 702 } 717 703 -
uspace/srv/net/net/net.h
r2bdf8313 rb0f00a9 1 1 /* 2 2 * Copyright (c) 2009 Lukas Mejdrech 3 * Copyright (c) 2011 Radim Vansa 3 4 * All rights reserved. 4 5 * … … 31 32 */ 32 33 33 /** @file34 * Networking subsystem central module.35 *36 */37 38 34 #ifndef NET_NET_H_ 39 35 #define NET_NET_H_ … … 45 41 #include <adt/module_map.h> 46 42 #include <net/packet.h> 43 #include <devman.h> 47 44 48 /** @name Modules definitions 49 * @{ 50 */ 51 52 #define NE2000_FILENAME "/srv/ne2000" 53 #define NE2000_NAME "ne2000" 45 #define NAME "net" 54 46 55 47 #define ETHERNET_FILENAME "/srv/eth" … … 58 50 #define IP_FILENAME "/srv/ip" 59 51 #define IP_NAME "ip" 60 61 #define LO_FILENAME "/srv/lo"62 #define LO_NAME "lo"63 52 64 53 #define NILDUMMY_FILENAME "/srv/nildummy" … … 77 66 #define CONF_MTU "MTU" /**< Maximum transmission unit configuration label. */ 78 67 #define CONF_NAME "NAME" /**< Network interface name configuration label. */ 79 #define CONF_ NETIF "NETIF" /**< Network interface module name configurationlabel. */68 #define CONF_HWPATH "HWPATH" /**< Network interface hardware pathname label. */ 80 69 #define CONF_NIL "NIL" /**< Network interface layer module name configuration label. */ 81 70 … … 85 74 #define CONF_DIR "/cfg/net" /**< Configuration directory. */ 86 75 #define CONF_GENERAL_FILE "general" /**< General configuration file. */ 76 #define CONF_EXT ".nic" /**< Extension for NIC's configuration files. */ 87 77 88 78 /** Configuration settings. … … 98 88 */ 99 89 typedef struct { 100 measured_strings_t configuration; /**< Configuration. */ 90 /** System-unique network interface name. */ 91 uint8_t *name; 92 /** System-unique network interface identifier. */ 93 nic_device_id_t id; 94 /** Configuration. */ 95 measured_strings_t configuration; 101 96 102 97 /** Serving network interface driver module index. */ 103 module_t *driver; 98 devman_handle_t handle; /**< Handle for devman */ 99 async_sess_t *sess; /**< Driver session. */ 104 100 105 device_id_t id; /**< System-unique network interface identifier. */ 106 module_t *il; /**< Serving internet layer module index. */ 107 uint8_t *name; /**< System-unique network interface name. */ 108 module_t *nil; /**< Serving link layer module index. */ 101 module_t *nil; /**< Serving link layer module index. */ 102 module_t *il; /**< Serving internet layer module index. */ 109 103 } netif_t; 110 104 … … 124 118 modules_t modules; /**< Available modules. */ 125 119 126 /** Network interface structure indices by names. */127 char_map_t netif_ names;120 /** Network interface structure indices by hardware path. */ 121 char_map_t netif_hwpaths; 128 122 129 123 /** Present network interfaces. */ … … 131 125 } net_globals_t; 132 126 133 extern int add_configuration(measured_strings_t *, const uint8_t *,134 const uint8_t *);135 extern int net_module_message(ipc_callid_t, ipc_call_t *, ipc_call_t *, size_t *);136 extern int net_initialize_build(async_client_conn_t);137 extern int net_message(ipc_callid_t, ipc_call_t *, ipc_call_t *, size_t *);138 139 127 #endif 140 128 -
uspace/srv/net/net/packet_server.c
r2bdf8313 rb0f00a9 1 1 /* 2 2 * Copyright (c) 2009 Lukas Mejdrech 3 * Copyright (c) 2011 Radim Vansa 3 4 * All rights reserved. 4 5 * … … 35 36 */ 36 37 37 #include <packet_server.h>38 39 38 #include <align.h> 40 39 #include <assert.h> 41 40 #include <async.h> 42 41 #include <errno.h> 42 #include <str_error.h> 43 #include <stdio.h> 43 44 #include <fibril_synch.h> 44 45 #include <unistd.h> … … 49 50 #include <net/packet_header.h> 50 51 52 #include "packet_server.h" 53 54 #define PACKET_SERVER_PROFILE 1 55 56 /** Number of queues cacheing the unused packets */ 51 57 #define FREE_QUEUES_COUNT 7 58 /** Maximum number of packets in each queue */ 59 #define FREE_QUEUE_MAX_LENGTH 16 52 60 53 61 /** The default address length reserved for new packets. */ … … 59 67 /** The default suffix reserved for new packets. */ 60 68 #define DEFAULT_SUFFIX 64 69 70 /** The queue with unused packets */ 71 typedef struct packet_queue { 72 packet_t *first; /**< First packet in the queue */ 73 size_t packet_size; /**< Maximal size of the packets in this queue */ 74 int count; /**< Length of the queue */ 75 } packet_queue_t; 61 76 62 77 /** Packet server global data. */ … … 65 80 fibril_mutex_t lock; 66 81 /** Free packet queues. */ 67 packet_t *free[FREE_QUEUES_COUNT]; 68 69 /** 70 * Packet length upper bounds of the free packet queues. The maximal 71 * lengths of packets in each queue in the ascending order. The last 72 * queue is not limited. 73 */ 74 size_t sizes[FREE_QUEUES_COUNT]; 82 packet_queue_t free_queues[FREE_QUEUES_COUNT]; 75 83 76 84 /** Total packets allocated. */ 77 unsigned int count;85 packet_id_t next_id; 78 86 } ps_globals = { 79 87 .lock = FIBRIL_MUTEX_INITIALIZER(ps_globals.lock), 80 .free = {81 NULL,82 NULL,83 NULL,84 NULL,85 NULL,86 NULL,87 NULL88 .free_queues = { 89 { NULL, PAGE_SIZE, 0}, 90 { NULL, PAGE_SIZE * 2, 0}, 91 { NULL, PAGE_SIZE * 4, 0}, 92 { NULL, PAGE_SIZE * 8, 0}, 93 { NULL, PAGE_SIZE * 16, 0}, 94 { NULL, PAGE_SIZE * 32, 0}, 95 { NULL, PAGE_SIZE * 64, 0}, 88 96 }, 89 .sizes = { 90 PAGE_SIZE, 91 PAGE_SIZE * 2, 92 PAGE_SIZE * 4, 93 PAGE_SIZE * 8, 94 PAGE_SIZE * 16, 95 PAGE_SIZE * 32, 96 PAGE_SIZE * 64 97 }, 98 .count = 0 97 .next_id = 1 99 98 }; 100 99 … … 108 107 * @param[in] max_suffix The maximal suffix length in bytes. 109 108 */ 110 static void 111 packet_init(packet_t *packet, size_t addr_len, size_t max_prefix, 112 size_t max_content, size_t max_suffix) 109 static void packet_init(packet_t *packet, 110 size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix) 113 111 { 114 112 /* Clear the packet content */ … … 121 119 packet->previous = 0; 122 120 packet->next = 0; 121 packet->offload_info = 0; 122 packet->offload_mask = 0; 123 123 packet->addr_len = 0; 124 124 packet->src_addr = sizeof(packet_t); … … 128 128 packet->data_start = packet->dest_addr + addr_len + packet->max_prefix; 129 129 packet->data_end = packet->data_start; 130 } 131 132 /** 133 * Releases the memory allocated for the packet 134 * 135 * @param[in] packet Pointer to the memory where the packet was allocated 136 */ 137 static void packet_dealloc(packet_t *packet) 138 { 139 pm_remove(packet); 140 munmap(packet, packet->length); 130 141 } 131 142 … … 142 153 * @return NULL if there is not enough memory left. 143 154 */ 144 static packet_t * 145 packet_create(size_t length, size_t addr_len, size_t max_prefix, 146 size_t max_content, size_t max_suffix) 155 static packet_t *packet_alloc(size_t length, size_t addr_len, 156 size_t max_prefix, size_t max_content, size_t max_suffix) 147 157 { 148 158 packet_t *packet; 149 159 int rc; 150 160 161 /* Global lock is locked */ 151 162 assert(fibril_mutex_is_locked(&ps_globals.lock)); 152 153 /* Already locked */ 163 /* The length is some multiple of PAGE_SIZE */ 164 assert(!(length & (PAGE_SIZE - 1))); 165 154 166 packet = (packet_t *) mmap(NULL, length, PROTO_READ | PROTO_WRITE, 155 MAP_SHARED | MAP_ANONYMOUS, 0, 0);167 MAP_SHARED | MAP_ANONYMOUS, 0, 0); 156 168 if (packet == MAP_FAILED) 157 169 return NULL; 158 159 ps_globals.count++; 160 packet->packet_id = ps_globals.count; 170 171 /* Using 32bit packet_id the id could overflow */ 172 packet_id_t pid; 173 do { 174 pid = ps_globals.next_id; 175 ps_globals.next_id++; 176 } while (!pid || pm_find(pid)); 177 packet->packet_id = pid; 178 161 179 packet->length = length; 162 180 packet_init(packet, addr_len, max_prefix, max_content, max_suffix); … … 164 182 rc = pm_add(packet); 165 183 if (rc != EOK) { 166 munmap(packet, packet->length);184 packet_dealloc(packet); 167 185 return NULL; 168 186 } … … 185 203 * @return NULL if there is not enough memory left. 186 204 */ 187 static packet_t * 188 packet_get_local(size_t addr_len, size_t max_prefix, size_t max_content, 189 size_t max_suffix) 190 { 191 size_t length = ALIGN_UP(sizeof(packet_t) + 2 * addr_len + 192 max_prefix + max_content + max_suffix, PAGE_SIZE); 193 205 static packet_t *packet_get_local(size_t addr_len, 206 size_t max_prefix, size_t max_content, size_t max_suffix) 207 { 208 size_t length = ALIGN_UP(sizeof(packet_t) + 2 * addr_len 209 + max_prefix + max_content + max_suffix, PAGE_SIZE); 210 211 if (length > PACKET_MAX_LENGTH) 212 return NULL; 213 194 214 fibril_mutex_lock(&ps_globals.lock); 195 215 … … 198 218 199 219 for (index = 0; index < FREE_QUEUES_COUNT; index++) { 200 if ((length > ps_globals. sizes[index]) &&201 (index < FREE_QUEUES_COUNT - 1))220 if ((length > ps_globals.free_queues[index].packet_size) && 221 (index < FREE_QUEUES_COUNT - 1)) 202 222 continue; 203 223 204 packet = ps_globals.free [index];224 packet = ps_globals.free_queues[index].first; 205 225 while (packet_is_valid(packet) && (packet->length < length)) 206 226 packet = pm_find(packet->next); 207 227 208 228 if (packet_is_valid(packet)) { 209 if (packet == ps_globals.free[index]) 210 ps_globals.free[index] = pq_detach(packet); 211 else 229 ps_globals.free_queues[index].count--; 230 if (packet == ps_globals.free_queues[index].first) { 231 ps_globals.free_queues[index].first = pq_detach(packet); 232 } else { 212 233 pq_detach(packet); 234 } 213 235 214 packet_init(packet, addr_len, max_prefix, max_content, 215 max_suffix); 236 packet_init(packet, addr_len, max_prefix, max_content, max_suffix); 216 237 fibril_mutex_unlock(&ps_globals.lock); 217 238 … … 220 241 } 221 242 222 packet = packet_ create(length, addr_len, max_prefix, max_content,223 max_suffix);243 packet = packet_alloc(length, addr_len, 244 max_prefix, max_content, max_suffix); 224 245 225 246 fibril_mutex_unlock(&ps_globals.lock); … … 241 262 242 263 for (index = 0; (index < FREE_QUEUES_COUNT - 1) && 243 (packet->length > ps_globals. sizes[index]); index++) {264 (packet->length > ps_globals.free_queues[index].packet_size); index++) { 244 265 ; 245 266 } 246 267 247 result = pq_add(&ps_globals.free[index], packet, packet->length, 248 packet->length); 268 ps_globals.free_queues[index].count++; 269 result = pq_add(&ps_globals.free_queues[index].first, packet, 270 packet->length, packet->length); 249 271 assert(result == EOK); 250 272 } … … 317 339 * packet_release_wrapper() function. 318 340 */ 319 int 320 packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 341 int packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 321 342 size_t *answer_count) 322 343 { 323 344 packet_t *packet; 324 345 346 if (!IPC_GET_IMETHOD(*call)) 347 return EOK; 348 325 349 *answer_count = 0; 326 350 switch (IPC_GET_IMETHOD(*call)) { 327 case IPC_M_PHONE_HUNGUP:328 return EOK;329 330 351 case NET_PACKET_CREATE_1: 331 352 packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, 332 IPC_GET_CONTENT(*call), DEFAULT_SUFFIX);353 IPC_GET_CONTENT(*call), DEFAULT_SUFFIX); 333 354 if (!packet) 334 355 return ENOMEM; … … 340 361 case NET_PACKET_CREATE_4: 341 362 packet = packet_get_local( 342 ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(*call)) ?363 ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(*call)) ? 343 364 IPC_GET_ADDR_LEN(*call) : DEFAULT_ADDR_LEN), 344 365 DEFAULT_PREFIX + IPC_GET_PREFIX(*call), … … 354 375 case NET_PACKET_GET: 355 376 packet = pm_find(IPC_GET_ID(*call)); 356 if (!packet_is_valid(packet)) 377 if (!packet_is_valid(packet)) { 357 378 return ENOENT; 379 } 358 380 return packet_reply(packet); 359 381 … … 373 395 } 374 396 397 int packet_server_init() 398 { 399 return EOK; 400 } 401 375 402 /** @} 376 403 */ -
uspace/srv/net/net/packet_server.h
r2bdf8313 rb0f00a9 1 1 /* 2 2 * Copyright (c) 2009 Lukas Mejdrech 3 * Copyright (c) 2011 Radim Vansa 3 4 * All rights reserved. 4 5 * … … 42 43 */ 43 44 44 #ifndef LIBPACKET_PACKET_SERVER_H_45 #define LIBPACKET_PACKET_SERVER_H_45 #ifndef NET_PACKET_SERVER_H_ 46 #define NET_PACKET_SERVER_H_ 46 47 47 48 #include <ipc/common.h> 48 49 50 extern int packet_server_init(void); 49 51 extern int packet_server_message(ipc_callid_t, ipc_call_t *, ipc_call_t *, 50 52 size_t *); -
uspace/srv/net/nil/eth/eth.c
r2bdf8313 rb0f00a9 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> … … 52 54 #include <protocol_map.h> 53 55 #include <net/device.h> 54 #include <netif_remote.h>55 56 #include <net_interface.h> 56 57 #include <il_remote.h> … … 58 59 #include <packet_client.h> 59 60 #include <packet_remote.h> 61 #include <device/nic.h> 60 62 #include <nil_skel.h> 61 62 63 #include "eth.h" 63 64 … … 168 169 INT_MAP_IMPLEMENT(eth_protos, eth_proto_t); 169 170 170 int nil_device_state_msg_local( int nil_phone, device_id_t device_id, int state)171 int nil_device_state_msg_local(nic_device_id_t device_id, sysarg_t state) 171 172 { 172 173 int index; … … 177 178 index--) { 178 179 proto = eth_protos_get_index(ð_globals.protos, index); 179 if ( proto && proto->phone) {180 il_device_state_msg(proto-> phone, device_id, state,180 if ((proto) && (proto->sess)) { 181 il_device_state_msg(proto->sess, device_id, state, 181 182 proto->service); 182 183 } … … 187 188 } 188 189 189 int nil_initialize( int net_phone)190 int nil_initialize(async_sess_t *sess) 190 191 { 191 192 int rc; … … 196 197 fibril_rwlock_write_lock(ð_globals.devices_lock); 197 198 fibril_rwlock_write_lock(ð_globals.protos_lock); 198 eth_globals.net_phone = net_phone; 199 200 eth_globals.broadcast_addr = 201 measured_string_create_bulk((uint8_t *) "\xFF\xFF\xFF\xFF\xFF\xFF", ETH_ADDR); 202 if (!eth_globals.broadcast_addr) { 203 rc = ENOMEM; 204 goto out; 205 } 199 eth_globals.net_sess = sess; 200 memcpy(eth_globals.broadcast_addr, "\xFF\xFF\xFF\xFF\xFF\xFF", 201 ETH_ADDR); 206 202 207 203 rc = eth_devices_initialize(ð_globals.devices); … … 216 212 eth_devices_destroy(ð_globals.devices, free); 217 213 } 214 218 215 out: 219 216 fibril_rwlock_write_unlock(ð_globals.protos_lock); … … 223 220 } 224 221 225 /** Processes IPC messages from the registered device driver modules in an 226 * infinite loop. 227 * 228 * @param[in] iid The message identifier. 229 * @param[in,out] icall The message parameters. 230 */ 231 static void eth_receiver(ipc_callid_t iid, ipc_call_t *icall) 232 { 233 packet_t *packet; 234 int rc; 235 236 while (true) { 237 switch (IPC_GET_IMETHOD(*icall)) { 238 case NET_NIL_DEVICE_STATE: 239 nil_device_state_msg_local(0, IPC_GET_DEVICE(*icall), 240 IPC_GET_STATE(*icall)); 241 async_answer_0(iid, EOK); 242 break; 243 case NET_NIL_RECEIVED: 244 rc = packet_translate_remote(eth_globals.net_phone, 245 &packet, IPC_GET_PACKET(*icall)); 246 if (rc == EOK) 247 rc = nil_received_msg_local(0, 248 IPC_GET_DEVICE(*icall), packet, 0); 249 250 async_answer_0(iid, (sysarg_t) rc); 251 break; 252 default: 253 async_answer_0(iid, (sysarg_t) ENOTSUP); 254 } 255 256 iid = async_get_call(icall); 257 } 258 } 259 260 /** Registers new device or updates the MTU of an existing one. 261 * 262 * Determines the device local hardware address. 263 * 264 * @param[in] device_id The new device identifier. 265 * @param[in] service The device driver service. 266 * @param[in] mtu The device maximum transmission unit. 267 * @return EOK on success. 268 * @return EEXIST if the device with the different service exists. 269 * @return ENOMEM if there is not enough memory left. 270 * @return Other error codes as defined for the 271 * net_get_device_conf_req() function. 272 * @return Other error codes as defined for the 273 * netif_bind_service() function. 274 * @return Other error codes as defined for the 275 * netif_get_addr_req() function. 276 */ 277 static int eth_device_message(device_id_t device_id, services_t service, 222 /** Register new device or updates the MTU of an existing one. 223 * 224 * Determine the device local hardware address. 225 * 226 * @param[in] device_id New device identifier. 227 * @param[in] handle Device driver handle. 228 * @param[in] mtu Device maximum transmission unit. 229 * 230 * @return EOK on success. 231 * @return EEXIST if the device with the different service exists. 232 * @return ENOMEM if there is not enough memory left. 233 * 234 */ 235 static int eth_device_message(nic_device_id_t device_id, devman_handle_t handle, 278 236 size_t mtu) 279 237 { … … 300 258 device = eth_devices_find(ð_globals.devices, device_id); 301 259 if (device) { 302 if (device-> service != service) {260 if (device->handle != handle) { 303 261 printf("Device %d already exists\n", device->device_id); 304 262 fibril_rwlock_write_unlock(ð_globals.devices_lock); … … 322 280 proto = eth_protos_get_index(ð_globals.protos, 323 281 index); 324 if (proto-> phone) {325 il_mtu_changed_msg(proto-> phone,282 if (proto->sess) { 283 il_mtu_changed_msg(proto->sess, 326 284 device->device_id, device->mtu, 327 285 proto->service); … … 339 297 340 298 device->device_id = device_id; 341 device-> service = service;299 device->handle = handle; 342 300 device->flags = 0; 343 301 if ((mtu > 0) && (mtu <= ETH_MAX_TAGGED_CONTENT(device->flags))) … … 347 305 348 306 configuration = &names[0]; 349 rc = net_get_device_conf_req(eth_globals.net_ phone, device->device_id,307 rc = net_get_device_conf_req(eth_globals.net_sess, device->device_id, 350 308 &configuration, count, &data); 351 309 if (rc != EOK) { … … 376 334 377 335 /* Bind the device driver */ 378 device-> phone = netif_bind_service(device->service, device->device_id,379 SERVICE_ETHERNET, eth_receiver);380 if (device-> phone < 0) {336 device->sess = devman_device_connect(EXCHANGE_SERIALIZE, handle, 337 IPC_FLAG_BLOCKING); 338 if (device->sess == NULL) { 381 339 fibril_rwlock_write_unlock(ð_globals.devices_lock); 382 340 free(device); 383 return device->phone; 384 } 341 return ENOENT; 342 } 343 344 nic_connect_to_nil(device->sess, SERVICE_ETHERNET, device_id); 385 345 386 346 /* Get hardware address */ 387 rc = netif_get_addr_req(device->phone, device->device_id, &device->addr, 388 &device->addr_data); 347 rc = nic_get_address(device->sess, &device->addr); 389 348 if (rc != EOK) { 390 349 fibril_rwlock_write_unlock(ð_globals.devices_lock); … … 398 357 if (index < 0) { 399 358 fibril_rwlock_write_unlock(ð_globals.devices_lock); 400 free(device->addr);401 free(device->addr_data);402 359 free(device); 403 360 return index; 404 361 } 405 362 406 printf("%s: Device registered (id: %d, service: %d: mtu: %zu, " 407 "mac: %02x:%02x:%02x:%02x:%02x:%02x, flags: 0x%x)\n", 408 NAME, device->device_id, device->service, device->mtu, 409 device->addr_data[0], device->addr_data[1], 410 device->addr_data[2], device->addr_data[3], 411 device->addr_data[4], device->addr_data[5], device->flags); 363 printf("%s: Device registered (id: %d, handle: %zu: mtu: %zu, " 364 "mac: " PRIMAC ", flags: 0x%x)\n", NAME, 365 device->device_id, device->handle, device->mtu, 366 ARGSMAC(device->addr.address), device->flags); 412 367 413 368 fibril_rwlock_write_unlock(ð_globals.devices_lock); … … 455 410 fcs = (eth_fcs_t *) data + length - sizeof(eth_fcs_t); 456 411 length -= sizeof(eth_fcs_t); 457 } else if (type <= ETH_MAX_CONTENT) {412 } else if (type <= ETH_MAX_CONTENT) { 458 413 /* Translate "LSAP" values */ 459 414 if ((header->lsap.dsap == ETH_LSAP_GLSAP) && … … 461 416 /* Raw packet -- discard */ 462 417 return NULL; 463 } else if ((header->lsap.dsap == ETH_LSAP_SNAP) &&418 } else if ((header->lsap.dsap == ETH_LSAP_SNAP) && 464 419 (header->lsap.ssap == ETH_LSAP_SNAP)) { 465 420 /* … … 468 423 */ 469 424 type = ntohs(header->snap.ethertype); 470 prefix = sizeof(eth_header_t) + 471 sizeof(eth_header_lsap_t) + 425 prefix = sizeof(eth_header_t) + sizeof(eth_header_lsap_t) + 472 426 sizeof(eth_header_snap_t); 473 427 } else { 474 428 /* IEEE 802.3 + 802.2 LSAP */ 475 429 type = lsap_map(header->lsap.dsap); 476 prefix = sizeof(eth_header_t) + 477 sizeof(eth_header_lsap_t); 430 prefix = sizeof(eth_header_t) + sizeof(eth_header_lsap_t); 478 431 } 479 432 … … 505 458 } 506 459 507 int nil_received_msg_local(int nil_phone, device_id_t device_id, 508 packet_t *packet, services_t target) 460 int nil_received_msg_local(nic_device_id_t device_id, packet_t *packet) 509 461 { 510 462 eth_proto_t *proto; … … 522 474 flags = device->flags; 523 475 fibril_rwlock_read_unlock(ð_globals.devices_lock); 524 525 476 fibril_rwlock_read_lock(ð_globals.protos_lock); 477 526 478 do { 527 479 next = pq_detach(packet); 528 480 proto = eth_process_packet(flags, packet); 529 481 if (proto) { 530 il_received_msg(proto-> phone, device_id, packet,482 il_received_msg(proto->sess, device_id, packet, 531 483 proto->service); 532 484 } else { 533 485 /* Drop invalid/unknown */ 534 pq_release_remote(eth_globals.net_ phone,486 pq_release_remote(eth_globals.net_sess, 535 487 packet_get_id(packet)); 536 488 } 537 489 packet = next; 538 } while (packet);490 } while (packet); 539 491 540 492 fibril_rwlock_read_unlock(ð_globals.protos_lock); … … 553 505 * @return ENOENT if there is no such device. 554 506 */ 555 static int eth_packet_space_message( device_id_t device_id, size_t *addr_len,507 static int eth_packet_space_message(nic_device_id_t device_id, size_t *addr_len, 556 508 size_t *prefix, size_t *content, size_t *suffix) 557 509 { … … 578 530 } 579 531 580 /** Returnsthe device hardware address.532 /** Send the device hardware address. 581 533 * 582 534 * @param[in] device_id The device identifier. 583 535 * @param[in] type Type of the desired address. 584 * @param[out] address The device hardware address.585 536 * @return EOK on success. 586 537 * @return EBADMEM if the address parameter is NULL. 587 538 * @return ENOENT if there no such device. 588 539 */ 589 static int eth_addr_message(device_id_t device_id, eth_addr_type_t type, 590 measured_string_t **address) 591 { 592 eth_device_t *device; 593 594 if (!address) 595 return EBADMEM; 596 597 if (type == ETH_BROADCAST_ADDR) { 598 *address = eth_globals.broadcast_addr; 599 } else { 540 static int eth_addr_message(nic_device_id_t device_id, eth_addr_type_t type) 541 { 542 eth_device_t *device = NULL; 543 uint8_t *address; 544 size_t max_len; 545 ipc_callid_t callid; 546 547 if (type == ETH_BROADCAST_ADDR) 548 address = eth_globals.broadcast_addr; 549 else { 600 550 fibril_rwlock_read_lock(ð_globals.devices_lock); 601 551 device = eth_devices_find(ð_globals.devices, device_id); … … 604 554 return ENOENT; 605 555 } 606 *address = device->addr; 556 557 address = (uint8_t *) &device->addr.address; 558 } 559 560 int rc = EOK; 561 if (!async_data_read_receive(&callid, &max_len)) { 562 rc = EREFUSED; 563 goto end; 564 } 565 566 if (max_len < ETH_ADDR) { 567 async_data_read_finalize(callid, NULL, 0); 568 rc = ELIMIT; 569 goto end; 570 } 571 572 rc = async_data_read_finalize(callid, address, ETH_ADDR); 573 if (rc != EOK) 574 goto end; 575 576 end: 577 578 if (type == ETH_LOCAL_ADDR) 607 579 fibril_rwlock_read_unlock(ð_globals.devices_lock); 608 } 609 610 return (*address) ? EOK : ENOENT; 611 } 612 613 /** Registers receiving module service. 614 * 615 * Passes received packets for this service. 616 * 617 * @param[in] service The module service. 618 * @param[in] phone The service phone. 619 * @return EOK on success. 620 * @return ENOENT if the service is not known. 621 * @return ENOMEM if there is not enough memory left. 622 */ 623 static int eth_register_message(services_t service, int phone) 580 581 return rc; 582 } 583 584 /** Register receiving module service. 585 * 586 * Pass received packets for this service. 587 * 588 * @param[in] service Module service. 589 * @param[in] sess Service session. 590 * 591 * @return EOK on success. 592 * @return ENOENT if the service is not known. 593 * @return ENOMEM if there is not enough memory left. 594 * 595 */ 596 static int eth_register_message(services_t service, async_sess_t *sess) 624 597 { 625 598 eth_proto_t *proto; … … 634 607 proto = eth_protos_find(ð_globals.protos, protocol); 635 608 if (proto) { 636 proto-> phone = phone;609 proto->sess = sess; 637 610 fibril_rwlock_write_unlock(ð_globals.protos_lock); 638 611 return EOK; … … 646 619 proto->service = service; 647 620 proto->protocol = protocol; 648 proto-> phone = phone;621 proto->sess = sess; 649 622 650 623 index = eth_protos_add(ð_globals.protos, protocol, proto); … … 656 629 } 657 630 658 printf("%s: Protocol registered (protocol: %d, service: % d, phone: "659 "%d)\n", NAME, proto->protocol, proto->service, proto->phone);631 printf("%s: Protocol registered (protocol: %d, service: %#x)\n", 632 NAME, proto->protocol, proto->service); 660 633 661 634 fibril_rwlock_write_unlock(ð_globals.protos_lock); … … 694 667 if (i < 0) 695 668 return i; 669 696 670 if (i != ETH_ADDR) 697 671 return EINVAL; 672 673 for (i = 0; i < ETH_ADDR; i++) { 674 if (src[i]) { 675 src_addr = src; 676 break; 677 } 678 } 698 679 699 680 length = packet_get_data_length(packet); … … 719 700 memcpy(header_dix->destination_address, dest, ETH_ADDR); 720 701 src = &header_dix->destination_address[0]; 721 } else if (IS_8023_2_LSAP(flags)) {702 } else if (IS_8023_2_LSAP(flags)) { 722 703 header_lsap = PACKET_PREFIX(packet, eth_header_lsap_t); 723 704 if (!header_lsap) … … 732 713 memcpy(header_lsap->header.destination_address, dest, ETH_ADDR); 733 714 src = &header_lsap->header.destination_address[0]; 734 } else if (IS_8023_2_SNAP(flags)) {715 } else if (IS_8023_2_SNAP(flags)) { 735 716 header = PACKET_PREFIX(packet, eth_header_snap_t); 736 717 if (!header) … … 743 724 header->lsap.ctrl = IEEE_8023_2_UI; 744 725 745 for (i = 0; i < 3; ++ i)726 for (i = 0; i < 3; i++) 746 727 header->snap.protocol[i] = 0; 747 728 … … 757 738 return ENOMEM; 758 739 759 for (i = 0; i < 7; ++ i)740 for (i = 0; i < 7; i++) 760 741 preamble->preamble[i] = ETH_PREAMBLE; 761 742 … … 784 765 * @return EINVAL if the service parameter is not known. 785 766 */ 786 static int eth_send_message( device_id_t device_id, packet_t *packet,767 static int eth_send_message(nic_device_id_t device_id, packet_t *packet, 787 768 services_t sender) 788 769 { … … 795 776 ethertype = htons(protocol_map(SERVICE_ETHERNET, sender)); 796 777 if (!ethertype) { 797 pq_release_remote(eth_globals.net_ phone, packet_get_id(packet));778 pq_release_remote(eth_globals.net_sess, packet_get_id(packet)); 798 779 return EINVAL; 799 780 } … … 810 791 do { 811 792 rc = eth_prepare_packet(device->flags, next, 812 (uint8_t *) device->addr->value, ethertype, device->mtu);793 (uint8_t *) &device->addr.address, ethertype, device->mtu); 813 794 if (rc != EOK) { 814 795 /* Release invalid packet */ … … 816 797 if (next == packet) 817 798 packet = tmp; 818 pq_release_remote(eth_globals.net_ phone,799 pq_release_remote(eth_globals.net_sess, 819 800 packet_get_id(next)); 820 801 next = tmp; … … 822 803 next = pq_next(next); 823 804 } 824 } while (next);805 } while (next); 825 806 826 807 /* Send packet queue */ 827 if (packet) { 828 netif_send_msg(device->phone, device_id, packet, 829 SERVICE_ETHERNET); 830 } 831 808 if (packet) 809 nic_send_message(device->sess, packet_get_id(packet)); 810 832 811 fibril_rwlock_read_unlock(ð_globals.devices_lock); 833 812 return EOK; 834 813 } 835 814 815 static int eth_addr_changed(nic_device_id_t device_id) 816 { 817 nic_address_t address; 818 size_t length; 819 ipc_callid_t data_callid; 820 if (!async_data_write_receive(&data_callid, &length)) { 821 async_answer_0(data_callid, EINVAL); 822 return EINVAL; 823 } 824 if (length > sizeof (nic_address_t)) { 825 async_answer_0(data_callid, ELIMIT); 826 return ELIMIT; 827 } 828 if (async_data_write_finalize(data_callid, &address, length) != EOK) { 829 return EINVAL; 830 } 831 832 fibril_rwlock_write_lock(ð_globals.devices_lock); 833 /* An existing device? */ 834 eth_device_t *device = eth_devices_find(ð_globals.devices, device_id); 835 if (device) { 836 printf("Device %d changing address from " PRIMAC " to " PRIMAC "\n", 837 device_id, ARGSMAC(device->addr.address), ARGSMAC(address.address)); 838 memcpy(&device->addr, &address, sizeof (nic_address_t)); 839 fibril_rwlock_write_unlock(ð_globals.devices_lock); 840 841 /* Notify all upper layer modules */ 842 fibril_rwlock_read_lock(ð_globals.protos_lock); 843 int index; 844 for (index = 0; index < eth_protos_count(ð_globals.protos); index++) { 845 eth_proto_t *proto = eth_protos_get_index(ð_globals.protos, index); 846 if (proto->sess != NULL) { 847 il_addr_changed_msg(proto->sess, device->device_id, 848 ETH_ADDR, address.address); 849 } 850 } 851 852 fibril_rwlock_read_unlock(ð_globals.protos_lock); 853 return EOK; 854 } else { 855 return ENOENT; 856 } 857 } 858 836 859 int nil_module_message(ipc_callid_t callid, ipc_call_t *call, 837 860 ipc_call_t *answer, size_t *answer_count) 838 861 { 839 measured_string_t *address;840 862 packet_t *packet; 841 863 size_t addrlen; … … 846 868 847 869 *answer_count = 0; 870 871 if (!IPC_GET_IMETHOD(*call)) 872 return EOK; 873 874 async_sess_t *callback = 875 async_callback_receive_start(EXCHANGE_SERIALIZE, call); 876 if (callback) 877 return eth_register_message(NIL_GET_PROTO(*call), callback); 878 848 879 switch (IPC_GET_IMETHOD(*call)) { 849 case IPC_M_PHONE_HUNGUP:850 return EOK;851 852 880 case NET_NIL_DEVICE: 853 881 return eth_device_message(IPC_GET_DEVICE(*call), 854 IPC_GET_ SERVICE(*call), IPC_GET_MTU(*call));882 IPC_GET_DEVICE_HANDLE(*call), IPC_GET_MTU(*call)); 855 883 case NET_NIL_SEND: 856 rc = packet_translate_remote(eth_globals.net_ phone, &packet,884 rc = packet_translate_remote(eth_globals.net_sess, &packet, 857 885 IPC_GET_PACKET(*call)); 858 886 if (rc != EOK) 859 887 return rc; 888 860 889 return eth_send_message(IPC_GET_DEVICE(*call), packet, 861 890 IPC_GET_SERVICE(*call)); … … 865 894 if (rc != EOK) 866 895 return rc; 896 867 897 IPC_SET_ADDR(*answer, addrlen); 868 898 IPC_SET_PREFIX(*answer, prefix); … … 870 900 IPC_SET_SUFFIX(*answer, suffix); 871 901 *answer_count = 4; 902 872 903 return EOK; 873 904 case NET_NIL_ADDR: 874 rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_LOCAL_ADDR, 875 &address); 905 rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_LOCAL_ADDR); 876 906 if (rc != EOK) 877 907 return rc; 878 return measured_strings_reply(address, 1); 908 909 IPC_SET_ADDR(*answer, ETH_ADDR); 910 *answer_count = 1; 911 912 return EOK; 879 913 case NET_NIL_BROADCAST_ADDR: 880 rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_BROADCAST_ADDR, 881 &address); 914 rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_BROADCAST_ADDR); 882 915 if (rc != EOK) 883 return EOK; 884 return measured_strings_reply(address, 1); 885 case IPC_M_CONNECT_TO_ME: 886 return eth_register_message(NIL_GET_PROTO(*call), 887 IPC_GET_PHONE(*call)); 916 return rc; 917 918 IPC_SET_ADDR(*answer, ETH_ADDR); 919 *answer_count = 1; 920 921 return EOK; 922 case NET_NIL_DEVICE_STATE: 923 nil_device_state_msg_local(IPC_GET_DEVICE(*call), IPC_GET_STATE(*call)); 924 async_answer_0(callid, EOK); 925 return EOK; 926 case NET_NIL_RECEIVED: 927 rc = packet_translate_remote(eth_globals.net_sess, &packet, 928 IPC_GET_ARG2(*call)); 929 if (rc == EOK) 930 rc = nil_received_msg_local(IPC_GET_ARG1(*call), packet); 931 932 async_answer_0(callid, (sysarg_t) rc); 933 return rc; 934 case NET_NIL_ADDR_CHANGED: 935 rc = eth_addr_changed(IPC_GET_DEVICE(*call)); 936 async_answer_0(callid, (sysarg_t) rc); 937 return rc; 888 938 } 889 939 -
uspace/srv/net/nil/eth/eth.h
r2bdf8313 rb0f00a9 1 1 /* 2 2 * Copyright (c) 2009 Lukas Mejdrech 3 * Copyright (c) 2011 Radim Vansa 3 4 * All rights reserved. 4 5 * … … 38 39 #define NET_ETH_H_ 39 40 41 #include <async.h> 40 42 #include <fibril_synch.h> 41 43 #include <ipc/services.h> 42 43 44 #include <net/device.h> 44 45 #include <adt/measured_strings.h> 46 #include <devman.h> 45 47 46 48 /** Ethernet address length. */ … … 220 222 struct eth_device { 221 223 /** Device identifier. */ 222 device_id_t device_id;223 /** Device driver service.*/224 services_t service;225 /** Driver phone. */226 int phone;224 nic_device_id_t device_id; 225 /** Device handle */ 226 devman_handle_t handle; 227 /** Driver session. */ 228 async_sess_t *sess; 227 229 /** Maximal transmission unit. */ 228 230 size_t mtu; … … 236 238 237 239 /** Actual device hardware address. */ 238 measured_string_t *addr; 239 240 /** Actual device hardware address data. */ 241 uint8_t *addr_data; 240 nic_address_t addr; 242 241 }; 243 242 … … 248 247 /** Protocol identifier. */ 249 248 int protocol; 250 /** Protocol module phone. */251 int phone;249 /** Protocol module session. */ 250 async_sess_t *sess; 252 251 }; 253 252 254 253 /** Ethernet global data. */ 255 254 struct eth_globals { 256 /** Networking module phone. */257 int net_phone;255 /** Networking module session. */ 256 async_sess_t *net_sess; 258 257 /** Safety lock for devices. */ 259 258 fibril_rwlock_t devices_lock; … … 265 264 /** 266 265 * Protocol map. 267 * Service phonemap for each protocol.266 * Service map for each protocol. 268 267 */ 269 268 eth_protos_t protos; 270 269 271 270 /** Broadcast device hardware address. */ 272 measured_string_t *broadcast_addr;271 uint8_t broadcast_addr[ETH_ADDR]; 273 272 }; 274 273 -
uspace/srv/net/nil/nildummy/nildummy.c
r2bdf8313 rb0f00a9 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 &nildummy_globals.devices_lock);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 -
uspace/srv/net/nil/nildummy/nildummy.h
r2bdf8313 rb0f00a9 1 1 /* 2 2 * Copyright (c) 2009 Lukas Mejdrech 3 * Copyright (c) 2011 Radim Vansa 3 4 * All rights reserved. 4 5 * … … 38 39 #define NET_NILDUMMY_H_ 39 40 41 #include <async.h> 40 42 #include <fibril_synch.h> 41 43 #include <ipc/services.h> 42 44 #include <ipc/devman.h> 43 45 #include <net/device.h> 44 46 #include <adt/measured_strings.h> … … 76 78 struct nildummy_device { 77 79 /** Device identifier. */ 78 device_id_t device_id; 79 80 /** Device driver service. */ 81 services_t service; 82 83 /** Driver phone. */ 84 int phone; 80 nic_device_id_t device_id; 81 /** Device driver handle. */ 82 devman_handle_t handle; 83 /** Driver session. */ 84 async_sess_t *sess; 85 85 86 86 /** Maximal transmission unit. */ … … 88 88 89 89 /** Actual device hardware address. */ 90 measured_string_t *addr; 91 92 /** Actual device hardware address data. */ 93 uint8_t *addr_data; 90 nic_address_t addr; 91 /** Actual device hardware address length. */ 92 size_t addr_len; 94 93 }; 95 94 … … 99 98 services_t service; 100 99 101 /** Protocol module phone. */102 int phone;100 /** Protocol module session. */ 101 async_sess_t *sess; 103 102 }; 104 103 105 104 /** Dummy nil global data. */ 106 105 struct nildummy_globals { 107 /** Networking module phone. */108 int net_phone;106 /** Networking module session. */ 107 async_sess_t *net_sess; 109 108 110 109 /** Lock for devices. */ -
uspace/srv/net/tl/icmp/icmp.c
r2bdf8313 rb0f00a9 118 118 119 119 /** Global data */ 120 static int phone_net = -1;121 static int phone_ip = -1;120 static async_sess_t *net_sess = NULL; 121 static async_sess_t *ip_sess = NULL; 122 122 static bool error_reporting = true; 123 123 static bool echo_replying = true; … … 173 173 static void icmp_release(packet_t *packet) 174 174 { 175 pq_release_remote( phone_net, packet_get_id(packet));175 pq_release_remote(net_sess, packet_get_id(packet)); 176 176 } 177 177 … … 225 225 } 226 226 227 return ip_send_msg( phone_ip, -1, packet, SERVICE_ICMP, error);227 return ip_send_msg(ip_sess, -1, packet, SERVICE_ICMP, error); 228 228 } 229 229 … … 297 297 size_t length = (size_t) addrlen; 298 298 299 packet_t *packet = packet_get_4_remote( phone_net, size,299 packet_t *packet = packet_get_4_remote(net_sess, size, 300 300 icmp_dimension.addr_len, ICMP_HEADER_SIZE + icmp_dimension.prefix, 301 301 icmp_dimension.suffix); … … 595 595 case ICMP_SKIP: 596 596 case ICMP_PHOTURIS: 597 ip_received_error_msg( phone_ip, -1, packet,597 ip_received_error_msg(ip_sess, -1, packet, 598 598 SERVICE_IP, SERVICE_ICMP); 599 599 return EOK; … … 608 608 * @param[in] iid Message identifier. 609 609 * @param[in,out] icall Message parameters. 610 * 611 * /612 static void icmp_receiver(ipc_callid_t iid, ipc_call_t *icall) 613 { 614 bool loop = true; 610 * @param[in] arg Local argument. 611 * 612 */ 613 static void icmp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg) 614 { 615 615 packet_t *packet; 616 616 int rc; 617 617 618 while (loop) { 618 while (true) { 619 if (!IPC_GET_IMETHOD(*icall)) 620 break; 621 619 622 switch (IPC_GET_IMETHOD(*icall)) { 620 623 case NET_TL_RECEIVED: 621 rc = packet_translate_remote( phone_net, &packet,624 rc = packet_translate_remote(net_sess, &packet, 622 625 IPC_GET_PACKET(*icall)); 623 626 if (rc == EOK) { … … 629 632 async_answer_0(iid, (sysarg_t) rc); 630 633 break; 631 case IPC_M_PHONE_HUNGUP:632 loop = false;633 continue;634 634 default: 635 635 async_answer_0(iid, (sysarg_t) ENOTSUP); … … 642 642 /** Initialize the ICMP module. 643 643 * 644 * @param[in] net_phone Network module phone.644 * @param[in] sess Network module session. 645 645 * 646 646 * @return EOK on success. … … 648 648 * 649 649 */ 650 int tl_initialize( int net_phone)650 int tl_initialize(async_sess_t *sess) 651 651 { 652 652 measured_string_t names[] = { … … 670 670 atomic_set(&icmp_client, 0); 671 671 672 phone_net = net_phone;673 phone_ip= ip_bind_service(SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP,672 net_sess = sess; 673 ip_sess = ip_bind_service(SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, 674 674 icmp_receiver); 675 if ( phone_ip < 0)676 return phone_ip;677 678 int rc = ip_packet_size_req( phone_ip, -1, &icmp_dimension);675 if (ip_sess == NULL) 676 return ENOENT; 677 678 int rc = ip_packet_size_req(ip_sess, -1, &icmp_dimension); 679 679 if (rc != EOK) 680 680 return rc; … … 685 685 /* Get configuration */ 686 686 configuration = &names[0]; 687 rc = net_get_conf_req( phone_net, &configuration, count, &data);687 rc = net_get_conf_req(net_sess, &configuration, count, &data); 688 688 if (rc != EOK) 689 689 return rc; … … 753 753 return rc; 754 754 755 rc = icmp_echo(icmp_id, icmp_seq, ICMP_GET_SIZE(*call), 755 rc = icmp_echo(icmp_id, icmp_seq, ICMP_GET_SIZE(*call), 756 756 ICMP_GET_TIMEOUT(*call), ICMP_GET_TTL(*call), 757 757 ICMP_GET_TOS(*call), ICMP_GET_DONT_FRAGMENT(*call), … … 763 763 764 764 case NET_ICMP_DEST_UNREACH: 765 rc = packet_translate_remote( phone_net, &packet,765 rc = packet_translate_remote(net_sess, &packet, 766 766 IPC_GET_PACKET(*call)); 767 767 if (rc != EOK) … … 772 772 773 773 case NET_ICMP_SOURCE_QUENCH: 774 rc = packet_translate_remote( phone_net, &packet,774 rc = packet_translate_remote(net_sess, &packet, 775 775 IPC_GET_PACKET(*call)); 776 776 if (rc != EOK) … … 780 780 781 781 case NET_ICMP_TIME_EXCEEDED: 782 rc = packet_translate_remote( phone_net, &packet,782 rc = packet_translate_remote(net_sess, &packet, 783 783 IPC_GET_PACKET(*call)); 784 784 if (rc != EOK) … … 788 788 789 789 case NET_ICMP_PARAMETERPROB: 790 rc = packet_translate_remote( phone_net, &packet,790 rc = packet_translate_remote(net_sess, &packet, 791 791 IPC_GET_PACKET(*call)); 792 792 if (rc != EOK) -
uspace/srv/net/tl/tcp/tcp.c
r2bdf8313 rb0f00a9 169 169 static int tcp_release_after_timeout(void *); 170 170 171 static int tcp_process_packet( device_id_t, packet_t *, services_t);171 static int tcp_process_packet(nic_device_id_t, packet_t *, services_t); 172 172 static int tcp_connect_core(socket_core_t *, socket_cores_t *, 173 173 struct sockaddr *, socklen_t); … … 177 177 size_t); 178 178 static packet_t *tcp_get_packets_to_send(socket_core_t *, tcp_socket_data_t *); 179 static void tcp_send_packets( device_id_t, packet_t *);179 static void tcp_send_packets(nic_device_id_t, packet_t *); 180 180 181 181 static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *, … … 205 205 static void tcp_queue_received_end_of_data(socket_core_t *socket); 206 206 207 static int tcp_received_msg(device_id_t, packet_t *, services_t, services_t); 208 static int tcp_process_client_messages(ipc_callid_t, ipc_call_t); 207 static int tcp_received_msg(nic_device_id_t, packet_t *, services_t, services_t); 208 static int tcp_process_client_messages(async_sess_t *, ipc_callid_t, 209 ipc_call_t); 209 210 210 211 static int tcp_listen_message(socket_cores_t *, int, int); … … 219 220 tcp_globals_t tcp_globals; 220 221 221 int tcp_received_msg( device_id_t device_id, packet_t *packet,222 int tcp_received_msg(nic_device_id_t device_id, packet_t *packet, 222 223 services_t receiver, services_t error) 223 224 { … … 237 238 } 238 239 239 int tcp_process_packet( device_id_t device_id, packet_t *packet, services_t error)240 int tcp_process_packet(nic_device_id_t device_id, packet_t *packet, services_t error) 240 241 { 241 242 size_t length; … … 324 325 325 326 if (!socket) { 326 if (tl_prepare_icmp_packet(tcp_globals.net_ phone,327 tcp_globals.icmp_ phone, packet, error) == EOK) {328 icmp_destination_unreachable_msg(tcp_globals.icmp_ phone,327 if (tl_prepare_icmp_packet(tcp_globals.net_sess, 328 tcp_globals.icmp_sess, packet, error) == EOK) { 329 icmp_destination_unreachable_msg(tcp_globals.icmp_sess, 329 330 ICMP_PORT_UNREACH, 0, packet); 330 331 } … … 397 398 fibril_rwlock_write_unlock(socket_data->local_lock); 398 399 399 rc = tl_prepare_icmp_packet(tcp_globals.net_ phone,400 tcp_globals.icmp_ phone, packet, error);400 rc = tl_prepare_icmp_packet(tcp_globals.net_sess, 401 tcp_globals.icmp_sess, packet, error); 401 402 if (rc == EOK) { 402 403 /* Checksum error ICMP */ 403 icmp_parameter_problem_msg(tcp_globals.icmp_ phone,404 icmp_parameter_problem_msg(tcp_globals.icmp_sess, 404 405 ICMP_PARAM_POINTER, 405 406 ((size_t) ((void *) &header->checksum)) - … … 439 440 break; 440 441 default: 441 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));442 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 442 443 } 443 444 … … 476 477 old_incoming = socket_data->next_incoming; 477 478 478 if ( header->finalize) {479 if (GET_TCP_HEADER_FINALIZE(header)) { 479 480 socket_data->fin_incoming = new_sequence_number + 480 481 total_length - TCP_HEADER_LENGTH(header); … … 502 503 /* Release the acknowledged packets */ 503 504 next_packet = pq_next(packet); 504 pq_release_remote(tcp_globals.net_ phone,505 pq_release_remote(tcp_globals.net_sess, 505 506 packet_get_id(packet)); 506 507 packet = next_packet; … … 561 562 next_packet = pq_next(next_packet); 562 563 pq_insert_after(tmp_packet, next_packet); 563 pq_release_remote(tcp_globals.net_ phone,564 pq_release_remote(tcp_globals.net_sess, 564 565 packet_get_id(tmp_packet)); 565 566 } … … 598 599 if (packet == socket_data->incoming) 599 600 socket_data->incoming = next_packet; 600 pq_release_remote(tcp_globals.net_ phone,601 pq_release_remote(tcp_globals.net_sess, 601 602 packet_get_id(packet)); 602 603 packet = next_packet; … … 621 622 if (length <= 0) { 622 623 /* Remove the empty packet */ 623 pq_release_remote(tcp_globals.net_ phone,624 pq_release_remote(tcp_globals.net_sess, 624 625 packet_get_id(packet)); 625 626 packet = next_packet; … … 668 669 } 669 670 /* Remove the duplicit or corrupted packet */ 670 pq_release_remote(tcp_globals.net_ phone,671 pq_release_remote(tcp_globals.net_sess, 671 672 packet_get_id(packet)); 672 673 packet = next_packet; … … 695 696 if (rc != EOK) { 696 697 /* Remove the corrupted packets */ 697 pq_release_remote(tcp_globals.net_ phone,698 pq_release_remote(tcp_globals.net_sess, 698 699 packet_get_id(packet)); 699 pq_release_remote(tcp_globals.net_ phone,700 pq_release_remote(tcp_globals.net_sess, 700 701 packet_get_id(next_packet)); 701 702 } else { … … 708 709 new_sequence_number, length); 709 710 if (rc != EOK) { 710 pq_release_remote(tcp_globals.net_ phone,711 pq_release_remote(tcp_globals.net_sess, 711 712 packet_get_id(next_packet)); 712 713 } 713 714 rc = pq_insert_after(packet, next_packet); 714 715 if (rc != EOK) { 715 pq_release_remote(tcp_globals.net_ phone,716 pq_release_remote(tcp_globals.net_sess, 716 717 packet_get_id(next_packet)); 717 718 } … … 722 723 printf("unexpected\n"); 723 724 /* Release duplicite or restricted */ 724 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));725 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 725 726 forced_ack = true; 726 727 } … … 790 791 if (rc != EOK) 791 792 return tcp_release_and_return(packet, rc); 792 rc = tl_get_ip_packet_dimension(tcp_globals.ip_ phone,793 rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess, 793 794 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension); 794 795 if (rc != EOK) … … 799 800 800 801 /* Notify the destination socket */ 801 async_ msg_5(socket->phone, NET_SOCKET_RECEIVED,802 (sysarg_t) socket->socket_id,802 async_exch_t *exch = async_exchange_begin(socket->sess); 803 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) socket->socket_id, 803 804 ((packet_dimension->content < socket_data->data_fragment_size) ? 804 805 packet_dimension->content : socket_data->data_fragment_size), 0, 0, 805 806 (sysarg_t) fragments); 807 async_exchange_end(exch); 806 808 807 809 return EOK; … … 820 822 821 823 /* Notify the destination socket */ 822 async_ msg_5(socket->phone, NET_SOCKET_RECEIVED,823 (sysarg_t) socket->socket_id,824 0, 0, 0, 825 (sysarg_t) 0 /* 0 fragments == no more data */);824 async_exch_t *exch = async_exchange_begin(socket->sess); 825 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) socket->socket_id, 826 0, 0, 0, (sysarg_t) 0 /* 0 fragments == no more data */); 827 async_exchange_end(exch); 826 828 } 827 829 … … 838 840 assert(packet); 839 841 840 if (! header->synchronize)842 if (!GET_TCP_HEADER_SYNCHRONIZE(header)) 841 843 return tcp_release_and_return(packet, EINVAL); 842 844 … … 849 851 next_packet = pq_detach(packet); 850 852 if (next_packet) { 851 pq_release_remote(tcp_globals.net_ phone,853 pq_release_remote(tcp_globals.net_sess, 852 854 packet_get_id(next_packet)); 853 855 } … … 903 905 assert(packet); 904 906 905 if (! header->synchronize)907 if (!GET_TCP_HEADER_SYNCHRONIZE(header)) 906 908 return tcp_release_and_return(packet, EINVAL); 907 909 … … 936 938 /* Create a socket */ 937 939 socket_id = -1; 938 rc = socket_create(socket_data->local_sockets, listening_socket-> phone,940 rc = socket_create(socket_data->local_sockets, listening_socket->sess, 939 941 socket_data, &socket_id); 940 942 if (rc != EOK) { … … 989 991 fibril_rwlock_write_unlock(&tcp_globals.lock); 990 992 if (rc != EOK) { 991 socket_destroy(tcp_globals.net_ phone, socket->socket_id,993 socket_destroy(tcp_globals.net_sess, socket->socket_id, 992 994 socket_data->local_sockets, &tcp_globals.sockets, 993 995 tcp_free_socket_data); … … 1001 1003 next_packet = pq_detach(packet); 1002 1004 if (next_packet) { 1003 pq_release_remote(tcp_globals.net_ phone,1005 pq_release_remote(tcp_globals.net_sess, 1004 1006 packet_get_id(next_packet)); 1005 1007 } … … 1010 1012 packet_get_data_length(packet) - sizeof(*header)); 1011 1013 if (rc != EOK) { 1012 socket_destroy(tcp_globals.net_ phone, socket->socket_id,1014 socket_destroy(tcp_globals.net_sess, socket->socket_id, 1013 1015 socket_data->local_sockets, &tcp_globals.sockets, 1014 1016 tcp_free_socket_data); … … 1021 1023 rc = tcp_queue_packet(socket, socket_data, packet, 1); 1022 1024 if (rc != EOK) { 1023 socket_destroy(tcp_globals.net_ phone, socket->socket_id,1025 socket_destroy(tcp_globals.net_sess, socket->socket_id, 1024 1026 socket_data->local_sockets, &tcp_globals.sockets, 1025 1027 tcp_free_socket_data); … … 1029 1031 packet = tcp_get_packets_to_send(socket, socket_data); 1030 1032 if (!packet) { 1031 socket_destroy(tcp_globals.net_ phone, socket->socket_id,1033 socket_destroy(tcp_globals.net_sess, socket->socket_id, 1032 1034 socket_data->local_sockets, &tcp_globals.sockets, 1033 1035 tcp_free_socket_data); … … 1057 1059 assert(packet); 1058 1060 1059 if (! header->acknowledge)1061 if (!GET_TCP_HEADER_ACKNOWLEDGE(header)) 1060 1062 return tcp_release_and_return(packet, EINVAL); 1061 1063 … … 1064 1066 1065 1067 socket_data->next_incoming = ntohl(header->sequence_number); /* + 1; */ 1066 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));1068 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 1067 1069 socket_data->state = TCP_SOCKET_ESTABLISHED; 1068 1070 listening_socket = socket_cores_find(socket_data->local_sockets, … … 1078 1080 if (rc == EOK) { 1079 1081 /* Notify the destination socket */ 1080 async_msg_5(socket->phone, NET_SOCKET_ACCEPTED, 1082 async_exch_t *exch = async_exchange_begin(socket->sess); 1083 async_msg_5(exch, NET_SOCKET_ACCEPTED, 1081 1084 (sysarg_t) listening_socket->socket_id, 1082 1085 socket_data->data_fragment_size, TCP_HEADER_SIZE, 1083 1086 0, (sysarg_t) socket->socket_id); 1087 async_exchange_end(exch); 1084 1088 1085 1089 fibril_rwlock_write_unlock(socket_data->local_lock); … … 1126 1130 assert(header); 1127 1131 1128 if (! header->acknowledge)1132 if (!GET_TCP_HEADER_ACKNOWLEDGE(header)) 1129 1133 return; 1130 1134 … … 1177 1181 /* Add to acknowledged or release */ 1178 1182 if (pq_add(&acknowledged, packet, 0, 0) != EOK) 1179 pq_release_remote(tcp_globals.net_ phone,1183 pq_release_remote(tcp_globals.net_sess, 1180 1184 packet_get_id(packet)); 1181 1185 packet = next; … … 1186 1190 /* Release acknowledged */ 1187 1191 if (acknowledged) { 1188 pq_release_remote(tcp_globals.net_ phone,1192 pq_release_remote(tcp_globals.net_sess, 1189 1193 packet_get_id(acknowledged)); 1190 1194 } … … 1230 1234 assert(answer); 1231 1235 assert(answer_count); 1232 1236 1233 1237 *answer_count = 0; 1234 switch (IPC_GET_IMETHOD(*call)) { 1235 case IPC_M_CONNECT_TO_ME: 1236 return tcp_process_client_messages(callid, *call); 1237 } 1238 1238 1239 async_sess_t *callback = 1240 async_callback_receive_start(EXCHANGE_SERIALIZE, call); 1241 if (callback) 1242 return tcp_process_client_messages(callback, callid, *call); 1243 1239 1244 return ENOTSUP; 1240 1245 } … … 1246 1251 bzero(socket_data, sizeof(*socket_data)); 1247 1252 socket_data->state = TCP_SOCKET_INITIAL; 1248 socket_data->device_id = DEVICE_INVALID_ID;1253 socket_data->device_id = NIC_DEVICE_INVALID_ID; 1249 1254 socket_data->window = NET_DEFAULT_TCP_WINDOW; 1250 1255 socket_data->treshold = socket_data->window; … … 1266 1271 } 1267 1272 1268 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call) 1273 int tcp_process_client_messages(async_sess_t *sess, ipc_callid_t callid, 1274 ipc_call_t call) 1269 1275 { 1270 1276 int res; 1271 bool keep_on_going = true;1272 1277 socket_cores_t local_sockets; 1273 int app_phone = IPC_GET_PHONE(call);1274 1278 struct sockaddr *addr; 1275 1279 int socket_id; … … 1293 1297 fibril_rwlock_initialize(&lock); 1294 1298 1295 while ( keep_on_going) {1299 while (true) { 1296 1300 1297 1301 /* Answer the call */ … … 1301 1305 /* Get the next call */ 1302 1306 callid = async_get_call(&call); 1307 1308 if (!IPC_GET_IMETHOD(call)) { 1309 res = EHANGUP; 1310 break; 1311 } 1303 1312 1304 1313 /* Process the call */ 1305 1314 switch (IPC_GET_IMETHOD(call)) { 1306 case IPC_M_PHONE_HUNGUP:1307 keep_on_going = false;1308 res = EHANGUP;1309 break;1310 1311 1315 case NET_SOCKET: 1312 1316 socket_data = … … 1322 1326 fibril_rwlock_write_lock(&lock); 1323 1327 socket_id = SOCKET_GET_SOCKET_ID(call); 1324 res = socket_create(&local_sockets, app_phone,1328 res = socket_create(&local_sockets, sess, 1325 1329 socket_data, &socket_id); 1326 1330 SOCKET_SET_SOCKET_ID(answer, socket_id); … … 1330 1334 break; 1331 1335 } 1332 if (tl_get_ip_packet_dimension(tcp_globals.ip_ phone,1333 &tcp_globals.dimensions, DEVICE_INVALID_ID,1336 if (tl_get_ip_packet_dimension(tcp_globals.ip_sess, 1337 &tcp_globals.dimensions, NIC_DEVICE_INVALID_ID, 1334 1338 &packet_dimension) == EOK) { 1335 1339 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, … … 1505 1509 } 1506 1510 1507 /* Release the application phone*/1508 async_hangup( app_phone);1511 /* Release the application session */ 1512 async_hangup(sess); 1509 1513 1510 1514 printf("release\n"); 1511 1515 /* Release all local sockets */ 1512 socket_cores_release(tcp_globals.net_ phone, &local_sockets,1516 socket_cores_release(tcp_globals.net_sess, &local_sockets, 1513 1517 &tcp_globals.sockets, tcp_free_socket_data); 1514 1518 … … 1618 1622 local_lock = socket_data->local_lock; 1619 1623 fibril_rwlock_write_lock(local_lock); 1620 socket_destroy(tcp_globals.net_ phone,1624 socket_destroy(tcp_globals.net_sess, 1621 1625 timeout->socket_id, timeout->local_sockets, 1622 1626 &tcp_globals.sockets, tcp_free_socket_data); … … 1647 1651 /* Sent packet? */ 1648 1652 packet = pq_find(socket_data->outgoing, sequence_number); 1649 printf("retransmit %d\n", packet_get_id(packet));1650 1653 if (packet) { 1651 1654 pq_get_order(packet, NULL, &data_length); … … 1751 1754 } 1752 1755 1753 rc = ip_get_route_req(tcp_globals.ip_ phone, IPPROTO_TCP,1756 rc = ip_get_route_req(tcp_globals.ip_sess, IPPROTO_TCP, 1754 1757 addr, addrlen, &socket_data->device_id, 1755 1758 &socket_data->pseudo_header, &socket_data->headerlen); … … 1786 1789 1787 1790 /* Send the packet */ 1788 printf("connecting %d\n", packet_get_id(packet));1789 1791 tcp_send_packets(socket_data->device_id, packet); 1790 1792 … … 1833 1835 1834 1836 /* Remember the outgoing FIN */ 1835 if ( header->finalize)1837 if (GET_TCP_HEADER_FINALIZE(header)) 1836 1838 socket_data->fin_outgoing = socket_data->next_outgoing; 1837 1839 … … 1899 1901 rc = pq_insert_after(previous, copy); 1900 1902 if (rc != EOK) { 1901 pq_release_remote(tcp_globals.net_ phone,1903 pq_release_remote(tcp_globals.net_sess, 1902 1904 packet_get_id(copy)); 1903 1905 return sending; … … 1936 1938 socket_data->headerlen, packet_get_data_length(packet)); 1937 1939 if (rc != EOK) { 1938 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));1940 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 1939 1941 return NULL; 1940 1942 } … … 1943 1945 header = (tcp_header_t *) packet_get_data(packet); 1944 1946 if (!header) { 1945 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));1947 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 1946 1948 return NULL; 1947 1949 } … … 1952 1954 header->acknowledgement_number = 1953 1955 htonl(socket_data->next_incoming); 1954 header->acknowledge = 1;1956 SET_TCP_HEADER_ACKNOWLEDGE(header, 1); 1955 1957 } 1956 1958 header->window = htons(socket_data->window); … … 1968 1970 rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0); 1969 1971 if (rc != EOK) { 1970 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));1972 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 1971 1973 return NULL; 1972 1974 } … … 1975 1977 sequence_number, socket_data->state, socket_data->timeout, true); 1976 1978 if (rc != EOK) { 1977 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));1979 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 1978 1980 return NULL; 1979 1981 } … … 1992 1994 1993 1995 /* Make a copy of the packet */ 1994 copy = packet_get_copy(tcp_globals.net_ phone, packet);1996 copy = packet_get_copy(tcp_globals.net_sess, packet); 1995 1997 if (!copy) 1996 1998 return NULL; … … 2000 2002 } 2001 2003 2002 void tcp_send_packets( device_id_t device_id, packet_t *packet)2004 void tcp_send_packets(nic_device_id_t device_id, packet_t *packet) 2003 2005 { 2004 2006 packet_t *next; … … 2006 2008 while (packet) { 2007 2009 next = pq_detach(packet); 2008 ip_send_msg(tcp_globals.ip_ phone, device_id, packet,2010 ip_send_msg(tcp_globals.ip_sess, device_id, packet, 2009 2011 SERVICE_TCP, 0); 2010 2012 packet = next; … … 2024 2026 header->source_port = htons(socket->port); 2025 2027 header->source_port = htons(socket_data->dest_port); 2026 header->header_length = TCP_COMPUTE_HEADER_LENGTH(sizeof(*header)); 2027 header->synchronize = synchronize; 2028 header->finalize = finalize; 2028 SET_TCP_HEADER_LENGTH(header, 2029 TCP_COMPUTE_HEADER_LENGTH(sizeof(*header))); 2030 SET_TCP_HEADER_SYNCHRONIZE(header, synchronize); 2031 SET_TCP_HEADER_FINALIZE(header, finalize); 2029 2032 } 2030 2033 … … 2118 2121 return NO_DATA; 2119 2122 2120 rc = packet_translate_remote(tcp_globals.net_ phone, &packet, packet_id);2123 rc = packet_translate_remote(tcp_globals.net_sess, &packet, packet_id); 2121 2124 if (rc != EOK) 2122 2125 return rc; … … 2129 2132 /* Release the packet */ 2130 2133 dyn_fifo_pop(&socket->received); 2131 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));2134 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 2132 2135 2133 2136 /* Return the total length */ … … 2167 2170 return ENOTCONN; 2168 2171 2169 rc = tl_get_ip_packet_dimension(tcp_globals.ip_ phone,2172 rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess, 2170 2173 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension); 2171 2174 if (rc != EOK) … … 2178 2181 for (index = 0; index < fragments; index++) { 2179 2182 /* Read the data fragment */ 2180 result = tl_socket_read_packet_data(tcp_globals.net_ phone,2183 result = tl_socket_read_packet_data(tcp_globals.net_sess, 2181 2184 &packet, TCP_HEADER_SIZE, packet_dimension, 2182 2185 socket_data->addr, socket_data->addrlen); … … 2241 2244 default: 2242 2245 /* Just destroy */ 2243 rc = socket_destroy(tcp_globals.net_ phone, socket_id,2246 rc = socket_destroy(tcp_globals.net_sess, socket_id, 2244 2247 local_sockets, &tcp_globals.sockets, 2245 2248 tcp_free_socket_data); … … 2289 2292 2290 2293 /* Get the device packet dimension */ 2291 rc = tl_get_ip_packet_dimension(tcp_globals.ip_ phone,2294 rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess, 2292 2295 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension); 2293 2296 if (rc != EOK) … … 2295 2298 2296 2299 /* Get a new packet */ 2297 *packet = packet_get_4_remote(tcp_globals.net_ phone, TCP_HEADER_SIZE,2300 *packet = packet_get_4_remote(tcp_globals.net_sess, TCP_HEADER_SIZE, 2298 2301 packet_dimension->addr_len, packet_dimension->prefix, 2299 2302 packet_dimension->suffix); … … 2358 2361 if (rc != EOK) 2359 2362 return rc; 2360 rc = tl_get_ip_packet_dimension(tcp_globals.ip_ phone,2363 rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess, 2361 2364 &tcp_globals.dimensions, socket_data->device_id, 2362 2365 &packet_dimension); … … 2430 2433 int tcp_release_and_return(packet_t *packet, int result) 2431 2434 { 2432 pq_release_remote(tcp_globals.net_ phone, packet_get_id(packet));2435 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet)); 2433 2436 return result; 2434 2437 } … … 2438 2441 * @param[in] iid Message identifier. 2439 2442 * @param[in,out] icall Message parameters. 2443 * @param[in] arg Local argument. 2440 2444 * 2441 2445 */ 2442 static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall )2446 static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg) 2443 2447 { 2444 2448 packet_t *packet; … … 2448 2452 switch (IPC_GET_IMETHOD(*icall)) { 2449 2453 case NET_TL_RECEIVED: 2450 rc = packet_translate_remote(tcp_globals.net_ phone, &packet,2454 rc = packet_translate_remote(tcp_globals.net_sess, &packet, 2451 2455 IPC_GET_PACKET(*icall)); 2452 2456 if (rc == EOK) … … 2466 2470 /** Initialize the TCP module. 2467 2471 * 2468 * @param[in] net_phone Network module phone.2472 * @param[in] sess Network module session. 2469 2473 * 2470 2474 * @return EOK on success. … … 2472 2476 * 2473 2477 */ 2474 int tl_initialize( int net_phone)2478 int tl_initialize(async_sess_t *sess) 2475 2479 { 2476 2480 fibril_rwlock_initialize(&tcp_globals.lock); 2477 2481 fibril_rwlock_write_lock(&tcp_globals.lock); 2478 2482 2479 tcp_globals.net_ phone = net_phone;2483 tcp_globals.net_sess = sess; 2480 2484 2481 tcp_globals.icmp_ phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT);2482 tcp_globals.ip_ phone= ip_bind_service(SERVICE_IP, IPPROTO_TCP,2485 tcp_globals.icmp_sess = icmp_connect_module(); 2486 tcp_globals.ip_sess = ip_bind_service(SERVICE_IP, IPPROTO_TCP, 2483 2487 SERVICE_TCP, tcp_receiver); 2484 if (tcp_globals.ip_ phone < 0) {2488 if (tcp_globals.ip_sess == NULL) { 2485 2489 fibril_rwlock_write_unlock(&tcp_globals.lock); 2486 return tcp_globals.ip_phone;2490 return ENOENT; 2487 2491 } 2488 2492 -
uspace/srv/net/tl/tcp/tcp.h
r2bdf8313 rb0f00a9 38 38 #define NET_TCP_H_ 39 39 40 #include <async.h> 40 41 #include <fibril_synch.h> 41 42 42 #include <net/packet.h> 43 43 #include <net/device.h> … … 182 182 183 183 /** Device identifier. */ 184 device_id_t device_id;184 nic_device_id_t device_id; 185 185 186 186 /** … … 284 284 /** TCP global data. */ 285 285 struct tcp_globals { 286 /** Networking module phone. */287 int net_phone;288 /** IP module phone. */289 int ip_phone;290 /** ICMP module phone. */291 int icmp_phone;286 /** Networking module session. */ 287 async_sess_t *net_sess; 288 /** IP module session. */ 289 async_sess_t *ip_sess; 290 /** ICMP module session. */ 291 async_sess_t *icmp_sess; 292 292 /** Last used free port. */ 293 293 int last_used_port; -
uspace/srv/net/tl/tcp/tcp_header.h
r2bdf8313 rb0f00a9 47 47 * @param[in] header The TCP packet header. 48 48 */ 49 #define TCP_HEADER_LENGTH(header) ( (header)->header_length* 4U)49 #define TCP_HEADER_LENGTH(header) (GET_TCP_HEADER_LENGTH(header) * 4U) 50 50 51 51 /** Returns the TCP header length. … … 73 73 uint32_t sequence_number; 74 74 uint32_t acknowledgement_number; 75 76 #ifdef ARCH_IS_BIG_ENDIAN77 uint8_t header_length:4;78 uint8_t reserved1:4;79 #else80 uint8_t reserved1:4;81 uint8_t header_length:4;82 #endif83 75 84 #ifdef ARCH_IS_BIG_ENDIAN 85 uint8_t reserved2:2; 86 uint8_t urgent:1; 87 uint8_t acknowledge:1; 88 uint8_t push:1; 89 uint8_t reset:1; 90 uint8_t synchronize:1; 91 uint8_t finalize:1; 92 #else 93 uint8_t finalize:1; 94 uint8_t synchronize:1; 95 uint8_t reset:1; 96 uint8_t push:1; 97 uint8_t acknowledge:1; 98 uint8_t urgent:1; 99 uint8_t reserved2:2; 100 #endif 76 uint8_t hlr; /* header length, reserved1 */ 77 78 #define GET_TCP_HEADER_LENGTH(header) \ 79 (((header)->hlr & 0xf0) >> 4) 80 #define SET_TCP_HEADER_LENGTH(header, length) \ 81 ((header)->hlr = \ 82 ((length & 0x0f) << 4) | ((header)->hlr & 0x0f)) 83 84 #define GET_TCP_HEADER_RESERVED1(header) \ 85 ((header)->hlr & 0x0f) 86 #define SET_TCP_HEADER_RESERVED1(header, reserved1) \ 87 ((header)->hlr = \ 88 (reserved1 & 0x0f) | ((header)->hlr & 0xf0)) 89 90 /* reserved2, urgent, acknowledge, push, reset, synchronize, finalize */ 91 uint8_t ruaprsf; 92 93 #define GET_TCP_HEADER_RESERVED2(header) \ 94 (((header)->ruaprsf & 0xc0) >> 6) 95 #define SET_TCP_HEADER_RESERVED2(header, reserved2) \ 96 ((header)->ruaprsf = \ 97 ((reserved2 & 0x03) << 6) | ((header)->ruaprsf & 0x3f)) 98 99 #define GET_TCP_HEADER_URGENT(header) \ 100 (((header)->ruaprsf & 0x20) >> 5) 101 #define SET_TCP_HEADER_URGENT(header, urgent) \ 102 ((header)->ruaprsf = \ 103 ((urgent & 0x01) << 5) | ((header)->ruaprsf & 0xdf)) 104 105 #define GET_TCP_HEADER_ACKNOWLEDGE(header) \ 106 (((header)->ruaprsf & 0x10) >> 4) 107 #define SET_TCP_HEADER_ACKNOWLEDGE(header, acknowledge) \ 108 ((header)->ruaprsf = \ 109 ((acknowledge & 0x01) << 4) | ((header)->ruaprsf & 0xef)) 110 111 #define GET_TCP_HEADER_PUSH(header) \ 112 (((header)->ruaprsf & 0x08) >> 3) 113 #define SET_TCP_HEADER_PUSH(header, push) \ 114 ((header)->ruaprsf = \ 115 ((push & 0x01) << 3) | ((header)->ruaprsf & 0xf7)) 116 117 #define GET_TCP_HEADER_RESET(header) \ 118 (((header)->ruaprsf & 0x04) >> 2) 119 #define SET_TCP_HEADER_RESET(header, reset) \ 120 ((header)->ruaprsf = \ 121 ((reset & 0x01) << 2) | ((header)->ruaprsf & 0xfb)) 122 123 #define GET_TCP_HEADER_SYNCHRONIZE(header) \ 124 (((header)->ruaprsf & 0x02) >> 1) 125 #define SET_TCP_HEADER_SYNCHRONIZE(header, synchronize) \ 126 ((header)->ruaprsf = \ 127 ((synchronize & 0x01) << 1) | ((header)->ruaprsf & 0xfd)) 128 129 #define GET_TCP_HEADER_FINALIZE(header) \ 130 ((header)->ruaprsf & 0x01) 131 #define SET_TCP_HEADER_FINALIZE(header, finalize) \ 132 ((header)->ruaprsf = \ 133 (finalize & 0x01) | ((header)->ruaprsf & 0xfe)) 101 134 102 135 uint16_t window; -
uspace/srv/net/tl/udp/udp.c
r2bdf8313 rb0f00a9 99 99 static int udp_release_and_return(packet_t *packet, int result) 100 100 { 101 pq_release_remote(udp_globals.net_ phone, packet_get_id(packet));101 pq_release_remote(udp_globals.net_sess, packet_get_id(packet)); 102 102 return result; 103 103 } … … 124 124 * ip_client_process_packet() function. 125 125 */ 126 static int udp_process_packet( device_id_t device_id, packet_t *packet,126 static int udp_process_packet(nic_device_id_t device_id, packet_t *packet, 127 127 services_t error) 128 128 { … … 192 192 ntohs(header->destination_port), (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0); 193 193 if (!socket) { 194 if (tl_prepare_icmp_packet(udp_globals.net_ phone,195 udp_globals.icmp_ phone, packet, error) == EOK) {196 icmp_destination_unreachable_msg(udp_globals.icmp_ phone,194 if (tl_prepare_icmp_packet(udp_globals.net_sess, 195 udp_globals.icmp_sess, packet, error) == EOK) { 196 icmp_destination_unreachable_msg(udp_globals.icmp_sess, 197 197 ICMP_PORT_UNREACH, 0, packet); 198 198 } … … 251 251 while (tmp_packet) { 252 252 next_packet = pq_detach(tmp_packet); 253 pq_release_remote(udp_globals.net_ phone,253 pq_release_remote(udp_globals.net_sess, 254 254 packet_get_id(tmp_packet)); 255 255 tmp_packet = next_packet; … … 274 274 if (flip_checksum(compact_checksum(checksum)) != 275 275 IP_CHECKSUM_ZERO) { 276 if (tl_prepare_icmp_packet(udp_globals.net_ phone,277 udp_globals.icmp_ phone, packet, error) == EOK) {276 if (tl_prepare_icmp_packet(udp_globals.net_sess, 277 udp_globals.icmp_sess, packet, error) == EOK) { 278 278 /* Checksum error ICMP */ 279 279 icmp_parameter_problem_msg( 280 udp_globals.icmp_ phone, ICMP_PARAM_POINTER,280 udp_globals.icmp_sess, ICMP_PARAM_POINTER, 281 281 ((size_t) ((void *) &header->checksum)) - 282 282 ((size_t) ((void *) header)), packet); … … 292 292 return udp_release_and_return(packet, rc); 293 293 294 rc = tl_get_ip_packet_dimension(udp_globals.ip_ phone,294 rc = tl_get_ip_packet_dimension(udp_globals.ip_sess, 295 295 &udp_globals.dimensions, device_id, &packet_dimension); 296 296 if (rc != EOK) … … 299 299 /* Notify the destination socket */ 300 300 fibril_rwlock_write_unlock(&udp_globals.lock); 301 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, 302 (sysarg_t) socket->socket_id, packet_dimension->content, 0, 0, 303 (sysarg_t) fragments); 301 302 async_exch_t *exch = async_exchange_begin(socket->sess); 303 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) socket->socket_id, 304 packet_dimension->content, 0, 0, (sysarg_t) fragments); 305 async_exchange_end(exch); 304 306 305 307 return EOK; … … 320 322 * udp_process_packet() function. 321 323 */ 322 static int udp_received_msg( device_id_t device_id, packet_t *packet,324 static int udp_received_msg(nic_device_id_t device_id, packet_t *packet, 323 325 services_t receiver, services_t error) 324 326 { … … 337 339 * @param[in] iid Message identifier. 338 340 * @param[in,out] icall Message parameters. 339 * 340 */ 341 static void udp_receiver(ipc_callid_t iid, ipc_call_t *icall) 341 * @param[in] arg Local argument. 342 * 343 */ 344 static void udp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg) 342 345 { 343 346 packet_t *packet; … … 347 350 switch (IPC_GET_IMETHOD(*icall)) { 348 351 case NET_TL_RECEIVED: 349 rc = packet_translate_remote(udp_globals.net_ phone, &packet,352 rc = packet_translate_remote(udp_globals.net_sess, &packet, 350 353 IPC_GET_PACKET(*icall)); 351 354 if (rc == EOK) … … 365 368 /** Initialize the UDP module. 366 369 * 367 * @param[in] net_phone Network module phone.370 * @param[in] sess Network module session. 368 371 * 369 372 * @return EOK on success. … … 371 374 * 372 375 */ 373 int tl_initialize( int net_phone)376 int tl_initialize(async_sess_t *sess) 374 377 { 375 378 measured_string_t names[] = { … … 390 393 fibril_rwlock_write_lock(&udp_globals.lock); 391 394 392 udp_globals.net_phone = net_phone; 393 394 udp_globals.icmp_phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT); 395 396 udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, 397 SERVICE_UDP, udp_receiver); 398 if (udp_globals.ip_phone < 0) { 399 fibril_rwlock_write_unlock(&udp_globals.lock); 400 return udp_globals.ip_phone; 395 udp_globals.net_sess = sess; 396 udp_globals.icmp_sess = icmp_connect_module(); 397 398 udp_globals.ip_sess = ip_bind_service(SERVICE_IP, IPPROTO_UDP, 399 SERVICE_UDP, udp_receiver); 400 if (udp_globals.ip_sess == NULL) { 401 fibril_rwlock_write_unlock(&udp_globals.lock); 402 return ENOENT; 401 403 } 402 404 403 405 /* Read default packet dimensions */ 404 int rc = ip_packet_size_req(udp_globals.ip_ phone, -1,406 int rc = ip_packet_size_req(udp_globals.ip_sess, -1, 405 407 &udp_globals.packet_dimension); 406 408 if (rc != EOK) { … … 431 433 /* Get configuration */ 432 434 configuration = &names[0]; 433 rc = net_get_conf_req(udp_globals.net_ phone, &configuration, count,435 rc = net_get_conf_req(udp_globals.net_sess, &configuration, count, 434 436 &data); 435 437 if (rc != EOK) { … … 497 499 void *ip_header; 498 500 size_t headerlen; 499 device_id_t device_id;501 nic_device_id_t device_id; 500 502 packet_dimension_t *packet_dimension; 501 503 size_t size; … … 525 527 526 528 if (udp_globals.checksum_computing) { 527 rc = ip_get_route_req(udp_globals.ip_ phone, IPPROTO_UDP, addr,529 rc = ip_get_route_req(udp_globals.ip_sess, IPPROTO_UDP, addr, 528 530 addrlen, &device_id, &ip_header, &headerlen); 529 531 if (rc != EOK) 530 532 return rc; 531 533 /* Get the device packet dimension */ 532 // rc = tl_get_ip_packet_dimension(udp_globals.ip_ phone,534 // rc = tl_get_ip_packet_dimension(udp_globals.ip_sess, 533 535 // &udp_globals.dimensions, device_id, &packet_dimension); 534 536 // if (rc != EOK) … … 537 539 // } else { 538 540 /* Do not ask all the time */ 539 rc = ip_packet_size_req(udp_globals.ip_ phone, -1,541 rc = ip_packet_size_req(udp_globals.ip_sess, -1, 540 542 &udp_globals.packet_dimension); 541 543 if (rc != EOK) … … 555 557 556 558 /* Read the first packet fragment */ 557 result = tl_socket_read_packet_data(udp_globals.net_ phone, &packet,559 result = tl_socket_read_packet_data(udp_globals.net_sess, &packet, 558 560 UDP_HEADER_SIZE, packet_dimension, addr, addrlen); 559 561 if (result < 0) … … 576 578 /* Read the rest of the packet fragments */ 577 579 for (index = 1; index < fragments; index++) { 578 result = tl_socket_read_packet_data(udp_globals.net_ phone,580 result = tl_socket_read_packet_data(udp_globals.net_sess, 579 581 &next_packet, 0, packet_dimension, addr, addrlen); 580 582 if (result < 0) … … 615 617 htons(flip_checksum(compact_checksum(checksum))); 616 618 free(ip_header); 617 } else { 618 device_id = DEVICE_INVALID_ID; 619 } 619 } else 620 device_id = NIC_DEVICE_INVALID_ID; 620 621 621 622 /* Prepare the first packet fragment */ … … 628 629 629 630 /* Send the packet */ 630 ip_send_msg(udp_globals.ip_ phone, device_id, packet, SERVICE_UDP, 0);631 ip_send_msg(udp_globals.ip_sess, device_id, packet, SERVICE_UDP, 0); 631 632 632 633 return EOK; … … 675 676 return NO_DATA; 676 677 677 rc = packet_translate_remote(udp_globals.net_ phone, &packet, packet_id);678 rc = packet_translate_remote(udp_globals.net_sess, &packet, packet_id); 678 679 if (rc != EOK) { 679 680 (void) dyn_fifo_pop(&socket->received); … … 735 736 } 736 737 737 /** Processes the socket client messages. 738 * 739 * Runs until the client module disconnects. 740 * 741 * @param[in] callid The message identifier. 742 * @param[in] call The message parameters. 743 * @return EOK on success. 744 * 745 * @see socket.h 746 */ 747 static int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call) 738 /** Process the socket client messages. 739 * 740 * Run until the client module disconnects. 741 * 742 * @see socket.h 743 * 744 * @param[in] sess Callback session. 745 * @param[in] callid Message identifier. 746 * @param[in] call Message parameters. 747 * 748 * @return EOK on success. 749 * 750 */ 751 static int udp_process_client_messages(async_sess_t *sess, ipc_callid_t callid, 752 ipc_call_t call) 748 753 { 749 754 int res; 750 bool keep_on_going = true;751 755 socket_cores_t local_sockets; 752 int app_phone = IPC_GET_PHONE(call);753 756 struct sockaddr *addr; 754 757 int socket_id; … … 773 776 socket_cores_initialize(&local_sockets); 774 777 775 while ( keep_on_going) {778 while (true) { 776 779 777 780 /* Answer the call */ … … 785 788 786 789 /* Process the call */ 787 switch (IPC_GET_IMETHOD(call)) { 788 case IPC_M_PHONE_HUNGUP: 789 keep_on_going = false; 790 if (!IPC_GET_IMETHOD(call)) { 790 791 res = EHANGUP; 791 792 break; 792 793 } 794 795 switch (IPC_GET_IMETHOD(call)) { 793 796 case NET_SOCKET: 794 797 socket_id = SOCKET_GET_SOCKET_ID(call); 795 res = socket_create(&local_sockets, app_phone, NULL,798 res = socket_create(&local_sockets, sess, NULL, 796 799 &socket_id); 797 800 SOCKET_SET_SOCKET_ID(answer, socket_id); … … 801 804 802 805 size = MAX_UDP_FRAGMENT_SIZE; 803 if (tl_get_ip_packet_dimension(udp_globals.ip_ phone,804 &udp_globals.dimensions, DEVICE_INVALID_ID,806 if (tl_get_ip_packet_dimension(udp_globals.ip_sess, 807 &udp_globals.dimensions, NIC_DEVICE_INVALID_ID, 805 808 &packet_dimension) == EOK) { 806 809 if (packet_dimension->content < size) … … 865 868 case NET_SOCKET_CLOSE: 866 869 fibril_rwlock_write_lock(&udp_globals.lock); 867 res = socket_destroy(udp_globals.net_ phone,870 res = socket_destroy(udp_globals.net_sess, 868 871 SOCKET_GET_SOCKET_ID(call), &local_sockets, 869 872 &udp_globals.sockets, NULL); … … 879 882 } 880 883 881 /* Release the application phone*/882 async_hangup( app_phone);884 /* Release the application session */ 885 async_hangup(sess); 883 886 884 887 /* Release all local sockets */ 885 socket_cores_release(udp_globals.net_ phone, &local_sockets,888 socket_cores_release(udp_globals.net_sess, &local_sockets, 886 889 &udp_globals.sockets, NULL); 887 890 … … 913 916 { 914 917 *answer_count = 0; 915 916 switch (IPC_GET_IMETHOD(*call)) {917 case IPC_M_CONNECT_TO_ME:918 return udp_process_client_messages(callid, *call);919 }920 918 919 async_sess_t *callback = 920 async_callback_receive_start(EXCHANGE_SERIALIZE, call); 921 if (callback) 922 return udp_process_client_messages(callback, callid, *call); 923 921 924 return ENOTSUP; 922 925 } -
uspace/srv/net/tl/udp/udp.h
r2bdf8313 rb0f00a9 38 38 #define NET_UDP_H_ 39 39 40 #include <async.h> 40 41 #include <fibril_synch.h> 41 42 #include <socket_core.h> … … 49 50 /** UDP global data. */ 50 51 struct udp_globals { 51 /** Networking module phone. */52 int net_phone;53 /** IP module phone. */54 int ip_phone;55 /** ICMP module phone. */56 int icmp_phone;52 /** Networking module session. */ 53 async_sess_t *net_sess; 54 /** IP module session. */ 55 async_sess_t *ip_sess; 56 /** ICMP module session. */ 57 async_sess_t *icmp_sess; 57 58 /** Packet dimension. */ 58 59 packet_dimension_t packet_dimension;
Note:
See TracChangeset
for help on using the changeset viewer.
