Changeset b0f00a9 in mainline for uspace/srv/net


Ignore:
Timestamp:
2011-11-06T22:21:05Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
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.
Message:

Merge mainline changes.

Location:
uspace/srv/net
Files:
3 deleted
17 edited
4 moved

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/cfg/lo.nic

    r2bdf8313 rb0f00a9  
    33NAME=lo
    44
    5 NETIF=lo
     5HWPATH=/virt/lo/port0
    66NIL=nildummy
    77IL=ip
  • uspace/srv/net/cfg/ne2k.nic

    r2bdf8313 rb0f00a9  
    1 # DP8390 (NE2k) configuration
     1# NE2000 configuration
    22
    33NAME=ne2k
    44
    5 NETIF=ne2000
     5HWPATH=/hw/pci0/00:01.0/ne2k/port0
    66NIL=eth
    77IL=ip
    8 
    9 IRQ=5
    10 IO=300
    118
    129# 8023_2_LSAP, 8023_2_SNAP
     
    1714IP_ADDR=10.0.2.15
    1815IP_ROUTING=yes
    19 IP_NETMASK=255.255.255.0
     16IP_NETMASK=255.255.255.240
    2017IP_BROADCAST=10.0.2.255
    2118IP_GATEWAY=10.0.2.2
  • uspace/srv/net/il/arp/arp.c

    r2bdf8313 rb0f00a9  
    164164}
    165165
    166 static int arp_clean_cache_req(int arp_phone)
     166static int arp_clean_cache_req(void)
    167167{
    168168        int count;
     
    174174                    count);
    175175               
    176                 if (device) {
     176                if (device)
    177177                        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                 }
    184178        }
    185179       
     
    190184}
    191185
    192 static int arp_clear_address_req(int arp_phone, device_id_t device_id,
     186static int arp_clear_address_req(nic_device_id_t device_id,
    193187    services_t protocol, measured_string_t *address)
    194188{
     
    218212}
    219213
    220 static int arp_clear_device_req(int arp_phone, device_id_t device_id)
     214static int arp_clear_device_req(nic_device_id_t device_id)
    221215{
    222216        fibril_mutex_lock(&arp_globals.lock);
     
    289283 *
    290284 */
    291 static int arp_receive_message(device_id_t device_id, packet_t *packet)
     285static int arp_receive_message(nic_device_id_t device_id, packet_t *packet)
    292286{
    293287        int rc;
     
    365359                        memcpy(src_proto, proto->addr->value,
    366360                            header->protocol_length);
    367                         memcpy(src_hw, device->addr->value,
     361                        memcpy(src_hw, device->addr,
    368362                            device->packet_dimension.addr_len);
    369363                        memcpy(des_hw, trans->hw_addr->value,
     
    375369                                return rc;
    376370                       
    377                         nil_send_msg(device->phone, device_id, packet,
     371                        nil_send_msg(device->sess, device_id, packet,
    378372                            SERVICE_ARP);
    379373                        return 1;
     
    393387 *
    394388 */
    395 static int arp_mtu_changed_message(device_id_t device_id, size_t mtu)
     389static int arp_mtu_changed_message(nic_device_id_t device_id, size_t mtu)
    396390{
    397391        fibril_mutex_lock(&arp_globals.lock);
     
    412406}
    413407
     408static 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
    414440/** Process IPC messages from the registered device driver modules
    415441 *
    416442 * @param[in]     iid   Message identifier.
    417443 * @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 */
     447static void arp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    421448{
    422449        packet_t *packet;
     
    431458               
    432459                case NET_IL_RECEIVED:
    433                         rc = packet_translate_remote(arp_globals.net_phone, &packet,
     460                        rc = packet_translate_remote(arp_globals.net_sess, &packet,
    434461                            IPC_GET_PACKET(*icall));
    435462                        if (rc == EOK) {
     
    439466                                        rc = arp_receive_message(IPC_GET_DEVICE(*icall), packet);
    440467                                        if (rc != 1) {
    441                                                 pq_release_remote(arp_globals.net_phone,
     468                                                pq_release_remote(arp_globals.net_sess,
    442469                                                    packet_get_id(packet));
    443470                                        }
     
    455482                        async_answer_0(iid, (sysarg_t) rc);
    456483                        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);
    457487               
    458488                default:
     
    482512 *
    483513 */
    484 static int arp_device_message(device_id_t device_id, services_t service,
     514static int arp_device_message(nic_device_id_t device_id, services_t service,
    485515    services_t protocol, measured_string_t *address)
    486516{
     
    564594               
    565595                /* Bind */
    566                 device->phone = nil_bind_service(device->service,
     596                device->sess = nil_bind_service(device->service,
    567597                    (sysarg_t) device->device_id, SERVICE_ARP,
    568598                    arp_receiver);
    569                 if (device->phone < 0) {
     599                if (device->sess == NULL) {
    570600                        fibril_mutex_unlock(&arp_globals.lock);
    571601                        arp_protos_destroy(&device->protos, free);
     
    575605               
    576606                /* Get packet dimensions */
    577                 rc = nil_packet_size_req(device->phone, device_id,
     607                rc = nil_packet_size_req(device->sess, device_id,
    578608                    &device->packet_dimension);
    579609                if (rc != EOK) {
     
    585615               
    586616                /* 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);
    589642                if (rc != EOK) {
    590643                        fibril_mutex_unlock(&arp_globals.lock);
     
    593646                        return rc;
    594647                }
    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                 }
    620648                printf("%s: Device registered (id: %d, type: 0x%x, service: %d,"
    621649                    " proto: %d)\n", NAME, device->device_id, device->hardware,
     
    627655}
    628656
    629 int il_initialize(int net_phone)
     657int il_initialize(async_sess_t *net_sess)
    630658{
    631659        fibril_mutex_initialize(&arp_globals.lock);
    632660       
    633661        fibril_mutex_lock(&arp_globals.lock);
    634         arp_globals.net_phone = net_phone;
     662        arp_globals.net_sess = net_sess;
    635663        int rc = arp_cache_initialize(&arp_globals.cache);
    636664        fibril_mutex_unlock(&arp_globals.lock);
     
    639667}
    640668
    641 static int arp_send_request(device_id_t device_id, services_t protocol,
     669static int arp_send_request(nic_device_id_t device_id, services_t protocol,
    642670    measured_string_t *target, arp_device_t *device, arp_proto_t *proto)
    643671{
    644672        /* 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);
    646674        if (length > device->packet_dimension.content)
    647675                return ELIMIT;
    648676       
    649         packet_t *packet = packet_get_4_remote(arp_globals.net_phone,
     677        packet_t *packet = packet_get_4_remote(arp_globals.net_sess,
    650678            device->packet_dimension.addr_len, device->packet_dimension.prefix,
    651679            length, device->packet_dimension.suffix);
     
    655683        arp_header_t *header = (arp_header_t *) packet_suffix(packet, length);
    656684        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));
    658686                return ENOMEM;
    659687        }
    660688       
    661689        header->hardware = htons(device->hardware);
    662         header->hardware_length = (uint8_t) device->addr->length;
     690        header->hardware_length = (uint8_t) device->addr_len;
    663691        header->protocol = htons(protocol_map(device->service, protocol));
    664692        header->protocol_length = (uint8_t) proto->addr->length;
     
    666694       
    667695        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;
    672699        memcpy(((uint8_t *) header) + length, proto->addr->value,
    673700            proto->addr->length);
    674701        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;
    677704        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);
    681708        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));
    683710                return rc;
    684711        }
    685712       
    686         nil_send_msg(device->phone, device_id, packet, SERVICE_ARP);
     713        nil_send_msg(device->sess, device_id, packet, SERVICE_ARP);
    687714        return EOK;
    688715}
     
    703730 *
    704731 */
    705 static int arp_translate_message(device_id_t device_id, services_t protocol,
     732static int arp_translate_message(nic_device_id_t device_id, services_t protocol,
    706733    measured_string_t *target, measured_string_t **translation)
    707734{
     
    845872       
    846873        *count = 0;
     874       
     875        if (!IPC_GET_IMETHOD(*call))
     876                return EOK;
     877       
    847878        switch (IPC_GET_IMETHOD(*call)) {
    848         case IPC_M_PHONE_HUNGUP:
    849                 return EOK;
    850        
    851879        case NET_ARP_DEVICE:
    852880                rc = measured_strings_receive(&address, &data, 1);
     
    889917       
    890918        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));
    892920       
    893921        case NET_ARP_CLEAR_ADDRESS:
     
    896924                        return rc;
    897925               
    898                 arp_clear_address_req(0, IPC_GET_DEVICE(*call),
     926                arp_clear_address_req(IPC_GET_DEVICE(*call),
    899927                    IPC_GET_SERVICE(*call), address);
    900928                free(address);
     
    903931       
    904932        case NET_ARP_CLEAN_CACHE:
    905                 return arp_clean_cache_req(0);
     933                return arp_clean_cache_req();
    906934        }
    907935       
  • uspace/srv/net/il/arp/arp.h

    r2bdf8313 rb0f00a9  
    3838#define NET_ARP_H_
    3939
     40#include <async.h>
    4041#include <fibril_synch.h>
    4142#include <ipc/services.h>
     
    9192struct arp_device {
    9293        /** 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;
    9697        /** 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;
    100101        /** Device identifier. */
    101         device_id_t device_id;
     102        nic_device_id_t device_id;
    102103        /** Hardware type. */
    103104        hw_type_t hardware;
    104105        /** Packet dimension. */
    105106        packet_dimension_t packet_dimension;
    106         /** Device module phone. */
    107         int phone;
     107        /** Device module session. */
     108        async_sess_t *sess;
    108109       
    109110        /**
     
    122123        arp_cache_t cache;
    123124       
    124         /** Networking module phone. */
    125         int net_phone;
     125        /** Networking module session. */
     126        async_sess_t *net_sess;
     127       
    126128        /** Safety lock. */
    127129        fibril_mutex_t lock;
  • uspace/srv/net/il/ip/ip.c

    r2bdf8313 rb0f00a9  
    120120GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t);
    121121
    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.
     122static 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 *
    129131 */
    130132static int ip_release_and_return(packet_t *packet, int result)
    131133{
    132         pq_release_remote(ip_globals.net_phone, packet_get_id(packet));
     134        pq_release_remote(ip_globals.net_sess, packet_get_id(packet));
    133135        return result;
    134136}
    135137
    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 */
     146static async_sess_t *ip_get_icmp_session(void)
     147{
    148148        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;
    151151        fibril_rwlock_read_unlock(&ip_globals.protos_lock);
    152         return phone;
     152       
     153        return sess;
    153154}
    154155
     
    179180        next = pq_detach(packet);
    180181        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));
    182183
    183184        if (!header) {
     
    201202
    202203        /* Set the destination address */
    203         switch (header->version) {
     204        switch (GET_IP_HEADER_VERSION(header)) {
    204205        case IPVERSION:
    205206                addrlen = sizeof(dest_in);
     
    218219}
    219220
    220 /** Prepares the ICMP notification packet.
    221  *
    222  * Releases additional packets and keeps only the first one.
     221/** Prepare the ICMP notification packet.
     222 *
     223 * Release additional packets and keep only the first one.
    223224 * All packets are released on error.
    224225 *
    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 */
     236static 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
     249int il_initialize(async_sess_t *net_sess)
    246250{
    247251        fibril_rwlock_initialize(&ip_globals.lock);
     
    250254        fibril_rwlock_initialize(&ip_globals.netifs_lock);
    251255       
    252         ip_globals.net_phone = net_phone;
     256        ip_globals.net_sess = net_sess;
    253257        ip_globals.packet_counter = 0;
    254258        ip_globals.gateway.address.s_addr = 0;
     
    350354        ip_netif->routing = NET_DEFAULT_IP_ROUTING;
    351355        configuration = &names[0];
    352 
     356       
    353357        /* 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,
    355359            &configuration, count, &data);
    356360        if (rc != EOK)
     
    402406                        return ENOTSUP;
    403407                }
    404 
     408               
    405409                if (configuration[6].value) {
    406410                        ip_netif->arp = get_running_module(&ip_globals.modules,
     
    413417                        }
    414418                }
     419               
    415420                if (configuration[7].value)
    416421                        ip_netif->routing = (configuration[7].value[0] == 'y');
    417 
     422               
    418423                net_free_settings(configuration, data);
    419424        }
    420 
     425       
    421426        /* 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,
    423428            (sysarg_t) ip_netif->device_id, SERVICE_IP,
    424429            ip_receiver);
    425         if (ip_netif->phone < 0) {
     430        if (ip_netif->sess == NULL) {
    426431                printf("Failed to contact the nil service %d\n",
    427432                    ip_netif->service);
    428                 return ip_netif->phone;
    429         }
    430 
     433                return ENOENT;
     434        }
     435       
    431436        /* Has to be after the device netif module initialization */
    432437        if (ip_netif->arp) {
     
    435440                        address.length = sizeof(in_addr_t);
    436441                       
    437                         rc = arp_device_req(ip_netif->arp->phone,
     442                        rc = arp_device_req(ip_netif->arp->sess,
    438443                            ip_netif->device_id, SERVICE_IP, ip_netif->service,
    439444                            &address);
     
    444449                }
    445450        }
    446 
     451       
    447452        /* 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,
    449454            &ip_netif->packet_dimension);
    450455        if (rc != EOK)
     
    457462                ip_netif->packet_dimension.content = IP_MIN_CONTENT;
    458463        }
    459 
     464       
    460465        index = ip_netifs_add(&ip_globals.netifs, ip_netif->device_id, ip_netif);
    461466        if (index < 0)
     
    474479                printf("%s: Default gateway (%s)\n", NAME, defgateway);
    475480        }
    476 
     481       
    477482        return EOK;
    478483}
    479484
    480 static int ip_device_req_local(int il_phone, device_id_t device_id,
    481     services_t netif)
     485static int ip_device_req_local(nic_device_id_t device_id, services_t netif)
    482486{
    483487        ip_netif_t *ip_netif;
     
    495499                return rc;
    496500        }
    497 
     501       
    498502        ip_netif->device_id = device_id;
    499503        ip_netif->service = netif;
    500         ip_netif->state = NETIF_STOPPED;
    501 
     504        ip_netif->state = NIC_STATE_STOPPED;
     505       
    502506        fibril_rwlock_write_lock(&ip_globals.netifs_lock);
    503507
     
    513517
    514518        /* 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,
    517521            ip_netif->dhcp ? "dhcp" : "static");
    518522       
     
    591595        while (index >= 0) {
    592596                netif = ip_netifs_get_index(&ip_globals.netifs, index);
    593                 if (netif && (netif->state == NETIF_ACTIVE)) {
     597                if (netif && (netif->state == NIC_STATE_ACTIVE)) {
    594598                        route = ip_netif_find_route(netif, destination);
    595599                        if (route)
     
    635639
    636640        /* Process all IP options */
    637         while (next < first->header_length) {
     641        while (next < GET_IP_HEADER_LENGTH(first)) {
    638642                option = (ip_option_t *) (((uint8_t *) first) + next);
    639643                /* Skip end or noop */
     
    656660        if (length % 4) {
    657661                bzero(((uint8_t *) last) + length, 4 - (length % 4));
    658                 last->header_length = length / 4 + 1;
     662                SET_IP_HEADER_LENGTH(last, (length / 4 + 1));
    659663        } else {
    660                 last->header_length = length / 4;
     664                SET_IP_HEADER_LENGTH(last, (length / 4));
    661665        }
    662666
     
    706710                return rc;
    707711       
    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);
    710714        header->fragment_offset_low = 0;
    711715        header->header_checksum = 0;
     
    735739                        memcpy(middle_header, last_header,
    736740                            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));
    738743                        middle_header->total_length =
    739744                            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));
    742747                        middle_header->fragment_offset_low =
    743748                            IP_COMPUTE_FRAGMENT_OFFSET_LOW(length);
     
    768773                middle_header->total_length =
    769774                    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));
    772777                middle_header->fragment_offset_low =
    773778                    IP_COMPUTE_FRAGMENT_OFFSET_LOW(length);
     
    785790                length += packet_get_data_length(next);
    786791                free(last_header);
    787                 header->flags |= IPFLAG_MORE_FRAGMENTS;
     792                SET_IP_HEADER_FLAGS(header,
     793                    (GET_IP_HEADER_FLAGS(header) | IPFLAG_MORE_FRAGMENTS));
    788794        }
    789795
     
    834840        new_header->total_length = htons(IP_HEADER_LENGTH(new_header) + length);
    835841        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));
    838844        new_header->fragment_offset_low =
    839845            IP_COMPUTE_FRAGMENT_OFFSET_LOW(offset);
     
    865871                return NULL;
    866872        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));
    868875        return middle;
    869876}
     
    922929
    923930        /* Fragmentation forbidden? */
    924         if(header->flags & IPFLAG_DONT_FRAGMENT)
     931        if(GET_IP_HEADER_FLAGS(header) & IPFLAG_DONT_FRAGMENT)
    925932                return EPERM;
    926933
    927934        /* 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,
    929936            suffix, ((addrlen > addr_len) ? addrlen : addr_len));
    930937        if (!new_packet)
     
    958965
    959966        /* 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));
    961969
    962970        /* Create middle fragments */
    963971        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,
    965973                    length, suffix,
    966974                    ((addrlen >= addr_len) ? addrlen : addr_len));
     
    987995}
    988996
    989 /** Checks the packet queue lengths and fragments the packets if needed.
     997/** Check the packet queue lengths and fragments the packets if needed.
    990998 *
    991999 * The ICMP_FRAG_NEEDED error notification may be sent if the packet needs to
    9921000 * be fragmented and the fragmentation is not allowed.
    9931001 *
    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 */
     1013static 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)
    10061015{
    10071016        size_t length;
     
    10091018        packet_t *new_packet;
    10101019        int result;
    1011         int phone;
    1012 
     1020        async_sess_t *sess;
     1021       
    10131022        next = packet;
    10141023        /* Check all packets */
     
    10321041                        /* Fragmentation needed? */
    10331042                        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) {
    10371045                                        /* Fragmentation necessary ICMP */
    1038                                         icmp_destination_unreachable_msg(phone,
     1046                                        icmp_destination_unreachable_msg(sess,
    10391047                                            ICMP_FRAG_NEEDED, content, next);
    10401048                                }
    10411049                        } else {
    1042                                 pq_release_remote(ip_globals.net_phone,
     1050                                pq_release_remote(ip_globals.net_sess,
    10431051                                    packet_get_id(next));
    10441052                        }
     
    10541062}
    10551063
    1056 /** Sends the packet or the packet queue via the specified route.
     1064/** Send the packet or the packet queue via the specified route.
    10571065 *
    10581066 * The ICMP_HOST_UNREACH error notification may be sent if route hardware
    10591067 * destination address is found.
    10601068 *
    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 *
    10721080 */
    10731081static int ip_send_route(packet_t *packet, ip_netif_t *netif,
     
    10771085        measured_string_t *translation;
    10781086        uint8_t *data;
    1079         int phone;
     1087        async_sess_t *sess;
    10801088        int rc;
    10811089
     
    10861094                destination.length = sizeof(dest.s_addr);
    10871095
    1088                 rc = arp_translate_req(netif->arp->phone, netif->device_id,
     1096                rc = arp_translate_req(netif->arp->sess, netif->device_id,
    10891097                    SERVICE_IP, &destination, &translation, &data);
    10901098                if (rc != EOK) {
    1091                         pq_release_remote(ip_globals.net_phone,
     1099                        pq_release_remote(ip_globals.net_sess,
    10921100                            packet_get_id(packet));
    10931101                        return rc;
     
    10991107                                free(data);
    11001108                        }
    1101                         phone = ip_prepare_icmp_and_get_phone(error, packet,
     1109                        sess = ip_prepare_icmp_and_get_session(error, packet,
    11021110                            NULL);
    1103                         if (phone >= 0) {
     1111                        if (sess) {
    11041112                                /* Unreachable ICMP if no routing */
    1105                                 icmp_destination_unreachable_msg(phone,
     1113                                icmp_destination_unreachable_msg(sess,
    11061114                                    ICMP_HOST_UNREACH, 0, packet);
    11071115                        }
     
    11151123        rc = ip_prepare_packet(src, dest, packet, translation);
    11161124        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));
    11181126        } else {
    11191127                packet = ip_split_packet(packet, netif->packet_dimension.prefix,
     
    11221130                    netif->packet_dimension.addr_len, error);
    11231131                if (packet) {
    1124                         nil_send_msg(netif->phone, netif->device_id, packet,
     1132                        nil_send_msg(netif->sess, netif->device_id, packet,
    11251133                            SERVICE_IP);
    11261134                }
     
    11351143}
    11361144
    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)
     1145static int ip_send_msg_local(nic_device_id_t device_id, packet_t *packet,
     1146    services_t sender, services_t error)
    11391147{
    11401148        int addrlen;
     
    11451153        in_addr_t *dest;
    11461154        in_addr_t *src;
    1147         int phone;
     1155        async_sess_t *sess;
    11481156        int rc;
    11491157
     
    11901198        if (!netif || !route) {
    11911199                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) {
    11941202                        /* Unreachable ICMP if no routing */
    1195                         icmp_destination_unreachable_msg(phone,
     1203                        icmp_destination_unreachable_msg(sess,
    11961204                            ICMP_NET_UNREACH, 0, packet);
    11971205                }
     
    12211229                if (!netif || !route) {
    12221230                        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,
    12241232                            NULL);
    1225                         if (phone >= 0) {
     1233                        if (sess) {
    12261234                                /* Unreachable ICMP if no routing */
    1227                                 icmp_destination_unreachable_msg(phone,
     1235                                icmp_destination_unreachable_msg(sess,
    12281236                                    ICMP_HOST_UNREACH, 0, packet);
    12291237                        }
     
    12511259 * @return              ENOENT if device is not found.
    12521260 */
    1253 static int ip_device_state_message(device_id_t device_id, device_state_t state)
     1261static int ip_device_state_message(nic_device_id_t device_id,
     1262    nic_device_state_t state)
    12541263{
    12551264        ip_netif_t *netif;
     
    12651274        fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    12661275
    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));
    12681278
    12691279        return EOK;
     
    13051315 *                      tl_received_msg() function.
    13061316 */
    1307 static int ip_deliver_local(device_id_t device_id, packet_t *packet,
     1317static int ip_deliver_local(nic_device_id_t device_id, packet_t *packet,
    13081318    ip_header_t *header, services_t error)
    13091319{
    13101320        ip_proto_t *proto;
    1311         int phone;
     1321        async_sess_t *sess;
    13121322        services_t service;
    13131323        tl_received_msg_t received_msg;
     
    13191329        int rc;
    13201330
    1321         if ((header->flags & IPFLAG_MORE_FRAGMENTS) ||
     1331        if ((GET_IP_HEADER_FLAGS(header) & IPFLAG_MORE_FRAGMENTS) ||
    13221332            IP_FRAGMENT_OFFSET(header)) {
    13231333                // TODO fragmented
     
    13251335        }
    13261336       
    1327         switch (header->version) {
     1337        switch (GET_IP_HEADER_VERSION(header)) {
    13281338        case IPVERSION:
    13291339                addrlen = sizeof(src_in);
     
    13621372        if (!proto) {
    13631373                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) {
    13661376                        /* Unreachable ICMP */
    1367                         icmp_destination_unreachable_msg(phone,
     1377                        icmp_destination_unreachable_msg(sess,
    13681378                            ICMP_PROT_UNREACH, 0, packet);
    13691379                }
     
    13771387                rc = received_msg(device_id, packet, service, error);
    13781388        } else {
    1379                 rc = tl_received_msg(proto->phone, device_id, packet,
     1389                rc = tl_received_msg(proto->sess, device_id, packet,
    13801390                    proto->service, error);
    13811391                fibril_rwlock_read_unlock(&ip_globals.protos_lock);
     
    14061416 *                      is disabled.
    14071417 */
    1408 static int ip_process_packet(device_id_t device_id, packet_t *packet)
     1418static int ip_process_packet(nic_device_id_t device_id, packet_t *packet)
    14091419{
    14101420        ip_header_t *header;
    14111421        in_addr_t dest;
    14121422        ip_route_t *route;
    1413         int phone;
     1423        async_sess_t *sess;
    14141424        struct sockaddr *addr;
    14151425        struct sockaddr_in addr_in;
     
    14241434        if ((header->header_checksum) &&
    14251435            (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) {
    14281438                        /* Checksum error ICMP */
    1429                         icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER,
     1439                        icmp_parameter_problem_msg(sess, ICMP_PARAM_POINTER,
    14301440                            ((size_t) ((void *) &header->header_checksum)) -
    14311441                            ((size_t) ((void *) header)), packet);
     
    14351445
    14361446        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) {
    14391449                        /* TTL exceeded ICMP */
    1440                         icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet);
     1450                        icmp_time_exceeded_msg(sess, ICMP_EXC_TTL, packet);
    14411451                }
    14421452                return EINVAL;
     
    14471457
    14481458        /* Set the destination address */
    1449         switch (header->version) {
     1459        switch (GET_IP_HEADER_VERSION(header)) {
    14501460        case IPVERSION:
    14511461                addrlen = sizeof(addr_in);
     
    14661476        route = ip_find_route(dest);
    14671477        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) {
    14701480                        /* Unreachable ICMP */
    1471                         icmp_destination_unreachable_msg(phone,
     1481                        icmp_destination_unreachable_msg(sess,
    14721482                            ICMP_HOST_UNREACH, 0, packet);
    14731483                }
     
    14861496        }
    14871497
    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) {
    14901500                /* 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,
    14921502                    packet);
    14931503        }
     
    14961506}
    14971507
    1498 /** Returns the 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 */
     1519static int ip_packet_size_message(nic_device_id_t device_id, size_t *addr_len,
    15101520    size_t *prefix, size_t *content, size_t *suffix)
    15111521{
     
    15651575 * @return              ENOENT if device is not found.
    15661576 */
    1567 static int ip_mtu_changed_message(device_id_t device_id, size_t mtu)
     1577static int ip_mtu_changed_message(nic_device_id_t device_id, size_t mtu)
    15681578{
    15691579        ip_netif_t *netif;
     
    15871597 * @param[in]     iid   Message identifier.
    15881598 * @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 */
     1602static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    15921603{
    15931604        packet_t *packet;
     
    16031614               
    16041615                case NET_IL_RECEIVED:
    1605                         rc = packet_translate_remote(ip_globals.net_phone, &packet,
     1616                        rc = packet_translate_remote(ip_globals.net_sess, &packet,
    16061617                            IPC_GET_PACKET(*icall));
    16071618                        if (rc == EOK) {
     
    16211632                        async_answer_0(iid, (sysarg_t) rc);
    16221633                        break;
    1623                
     1634                case NET_IL_ADDR_CHANGED:
     1635                        async_answer_0(iid, (sysarg_t) EOK);
     1636                        break;
     1637
    16241638                default:
    16251639                        async_answer_0(iid, (sysarg_t) ENOTSUP);
     
    16301644}
    16311645
    1632 /** Registers the transport layer protocol.
     1646/** Register the transport layer protocol.
    16331647 *
    16341648 * The traffic of this protocol will be supplied using either the receive
    16351649 * function or IPC message.
    16361650 *
    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 */
     1664static int ip_register(int protocol, services_t service, async_sess_t *sess,
    16501665    tl_received_msg_t received_msg)
    16511666{
     
    16531668        int index;
    16541669
    1655         if (!protocol || !service || ((phone < 0) && !received_msg))
     1670        if ((!protocol) || (!service) || ((!sess) && (!received_msg)))
    16561671                return EINVAL;
    1657 
     1672       
    16581673        proto = (ip_proto_t *) malloc(sizeof(ip_protos_t));
    16591674        if (!proto)
     
    16621677        proto->protocol = protocol;
    16631678        proto->service = service;
    1664         proto->phone = phone;
     1679        proto->sess = sess;
    16651680        proto->received_msg = received_msg;
    16661681
     
    16741689        fibril_rwlock_write_unlock(&ip_globals.protos_lock);
    16751690
    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);
    16781693
    16791694        return EOK;
    16801695}
    16811696
    1682 
    1683 static int
    1684 ip_add_route_req_local(int ip_phone, device_id_t device_id, in_addr_t address,
     1697static int ip_add_route_req_local(nic_device_id_t device_id, in_addr_t address,
    16851698    in_addr_t netmask, in_addr_t gateway)
    16861699{
     
    17161729}
    17171730
    1718 static int
    1719 ip_set_gateway_req_local(int ip_phone, device_id_t device_id, in_addr_t gateway)
     1731static int ip_set_gateway_req_local(nic_device_id_t device_id,
     1732    in_addr_t gateway)
    17201733{
    17211734        ip_netif_t *netif;
     
    17411754/** Notify the IP module about the received error notification packet.
    17421755 *
    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 */
     1766static int ip_received_error_msg_local(nic_device_id_t device_id,
    17551767    packet_t *packet, services_t target, services_t error)
    17561768{
     
    17971809                        address.value = (uint8_t *) &header->destination_address;
    17981810                        address.length = sizeof(header->destination_address);
    1799                         arp_clear_address_req(netif->arp->phone,
     1811                        arp_clear_address_req(netif->arp->sess,
    18001812                            netif->device_id, SERVICE_IP, &address);
    18011813                }
     
    18111823}
    18121824
    1813 static int
    1814 ip_get_route_req_local(int ip_phone, ip_protocol_t protocol,
     1825static int ip_get_route_req_local(ip_protocol_t protocol,
    18151826    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)
    18171828{
    18181829        struct sockaddr_in *address_in;
     
    19051916        size_t suffix;
    19061917        size_t content;
    1907         device_id_t device_id;
     1918        nic_device_id_t device_id;
    19081919        int rc;
    19091920       
    19101921        *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       
    19111932        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        
    19191933        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),
    19211935                    IPC_GET_SERVICE(*call));
    19221936       
    19231937        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,
    19251939                    IPC_GET_PACKET(*call));
    19261940                if (rc != EOK)
    19271941                        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),
    19291943                    packet, IPC_GET_TARGET(*call), IPC_GET_ERROR(*call));
    19301944       
    19311945        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),
    19331947                    IP_GET_ADDRESS(*call), IP_GET_NETMASK(*call),
    19341948                    IP_GET_GATEWAY(*call));
    19351949
    19361950        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),
    19381952                    IP_GET_GATEWAY(*call));
    19391953
     
    19441958                        return rc;
    19451959               
    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,
    19471961                    (socklen_t) addrlen, &device_id, &header, &headerlen);
    19481962                if (rc != EOK)
     
    19751989       
    19761990        case NET_IP_SEND:
    1977                 rc = packet_translate_remote(ip_globals.net_phone, &packet,
     1991                rc = packet_translate_remote(ip_globals.net_sess, &packet,
    19781992                    IPC_GET_PACKET(*call));
    19791993                if (rc != EOK)
    19801994                        return rc;
    19811995               
    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,
    19831997                    IPC_GET_ERROR(*call));
    19841998        }
  • uspace/srv/net/il/ip/ip.h

    r2bdf8313 rb0f00a9  
    3838#define NET_IP_H_
    3939
     40#include <async.h>
    4041#include <fibril_synch.h>
    4142#include <ipc/services.h>
     
    9192        in_addr_t broadcast;
    9293        /** Device identifier. */
    93         device_id_t device_id;
     94        nic_device_id_t device_id;
    9495        /** Indicates whether using DHCP. */
    9596        int dhcp;
     
    9899        /** Packet dimension. */
    99100        packet_dimension_t packet_dimension;
    100         /** Netif module phone. */
    101         int phone;
     101        /** Netif module session. */
     102        async_sess_t *sess;
    102103        /** Routing table. */
    103104        ip_routes_t routes;
     
    107108        services_t service;
    108109        /** Device state. */
    109         device_state_t state;
     110        nic_device_state_t state;
    110111};
    111112
    112113/** IP protocol specific data. */
    113114struct ip_proto {
    114         /** Protocol module phone. */
    115         int phone;
     115        /** Protocol module session. */
     116        async_sess_t *sess;
    116117        /** Protocol number. */
    117118        int protocol;
     
    142143        /** Known support modules. */
    143144        modules_t modules;
    144         /** Networking module phone. */
    145         int net_phone;
     145        /** Networking module session. */
     146        async_sess_t *net_sess;
    146147        /** Registered network interfaces. */
    147148        ip_netifs_t netifs;
  • uspace/srv/net/net/Makefile

    r2bdf8313 rb0f00a9  
    3030USPACE_PREFIX = ../../..
    3131ROOT_PATH = $(USPACE_PREFIX)/..
    32 LIBS = $(LIBNET_PREFIX)/libnet.a $(LIBPACKET_PREFIX)/libpacket.a
    33 EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include -I$(LIBPACKET_PREFIX)/include
     32LIBS = $(LIBNET_PREFIX)/libnet.a
     33EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include
    3434
    3535COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
     
    4343SOURCES = \
    4444        net.c \
    45         net_standalone.c
     45        packet_server.c
    4646
    4747include $(USPACE_PREFIX)/Makefile.common
  • uspace/srv/net/net/net.c

    r2bdf8313 rb0f00a9  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    3132 */
    3233
    33 /** @file
    34  * Networking subsystem central module implementation.
    35  *
    36  */
    37 
    38 #include "net.h"
    39 
     34#include <assert.h>
    4035#include <async.h>
    4136#include <ctype.h>
    4237#include <ddi.h>
    4338#include <errno.h>
     39#include <str_error.h>
    4440#include <malloc.h>
    4541#include <stdio.h>
    4642#include <str.h>
     43#include <devman.h>
    4744#include <str_error.h>
    48 
     45#include <ns.h>
    4946#include <ipc/services.h>
    5047#include <ipc/net.h>
    5148#include <ipc/net_net.h>
    5249#include <ipc/il.h>
     50#include <ipc/ip.h>
    5351#include <ipc/nil.h>
    54 
    55 #include <net/modules.h>
    5652#include <net/packet.h>
    5753#include <net/device.h>
    58 
    5954#include <adt/char_map.h>
    6055#include <adt/generic_char_map.h>
    6156#include <adt/measured_strings.h>
    6257#include <adt/module_map.h>
    63 
    64 #include <netif_remote.h>
    6558#include <nil_remote.h>
    6659#include <net_interface.h>
    6760#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
    7469
    7570/** Networking module global data. */
     
    7873GENERIC_CHAR_MAP_IMPLEMENT(measured_strings, measured_string_t);
    7974DEVICE_MAP_IMPLEMENT(netifs, netif_t);
    80 
    81 static int startup(void);
    8275
    8376/** Add the configured setting to the configuration map.
     
    9184 *
    9285 */
    93 int add_configuration(measured_strings_t *configuration, const uint8_t *name,
    94     const uint8_t *value)
     86static int add_configuration(measured_strings_t *configuration,
     87    const uint8_t *name, const uint8_t *value)
    9588{
    9689        int rc;
     
    113106/** Generate new system-unique device identifier.
    114107 *
    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 */
     111static nic_device_id_t generate_new_device_id(void)
    118112{
    119113        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;
    181114}
    182115
     
    186119        printf("%s: Reading configuration file %s/%s\n", NAME, directory, filename);
    187120       
    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);
    196128                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;
    230139                }
    231140        }
    232141       
    233         fclose(cfg);
     142        cfg_unload(&cfg);
    234143        return EOK;
    235144}
     
    259168        return read_configuration_file(CONF_DIR, CONF_GENERAL_FILE,
    260169            &net_globals.configuration);
    261 }
    262 
    263 /** Initialize the networking module.
    264  *
    265  * @param[in] client_connection The client connection processing
    266  *                              function. The module skeleton propagates
    267  *                              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 service
    315  * and starts the async manager, processing IPC messages
    316  * in an infinite loop.
    317  *
    318  * @param[in] client_connection The client connection
    319  *                              processing function. The
    320  *                              module skeleton propagates
    321  *                              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;
    355170}
    356171
     
    368183 */
    369184static 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;
    374189       
    375190        size_t index;
     
    393208}
    394209
    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 
     210static int net_get_device_conf(nic_device_id_t device_id,
     211    measured_string_t *configuration, size_t count)
     212{
    410213        netif_t *netif = netifs_find(&net_globals.netifs, device_id);
    411214        if (netif)
    412                 return net_get_conf(&netif->configuration, *configuration, count, data);
     215                return net_get_conf(&netif->configuration, configuration, count);
    413216        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
     220static 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
     250static 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
     264static 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);
    419271}
    420272
     
    435287 *
    436288 */
    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;
     289static 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;
    450299        }
    451300       
    452301        /* 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);
    454304        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);
    456307                if (!netif->nil) {
    457                         fprintf(stderr, "%s: Failed to start network interface layer '%s'\n",
     308                        printf("%s: Unable to connect to network interface layer '%s'\n",
    458309                            NAME, setting->value);
    459310                        return EINVAL;
     
    463314       
    464315        /* 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);
    467320        if (!netif->il) {
    468                 fprintf(stderr, "%s: Failed to start internet layer '%s'\n",
     321                printf("%s: Unable to connect to internet layer '%s'\n",
    469322                    NAME, setting->value);
    470323                return EINVAL;
    471324        }
    472325       
    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        
    484326        /* Network interface layer startup */
    485         services_t internet_service;
     327        int rc;
     328        services_t nil_service;
    486329        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);
    488332                if (!setting)
    489333                        setting = measured_strings_find(&net_globals.configuration,
    490334                            (uint8_t *) CONF_MTU, 0);
    491335               
    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);
    497343                        return rc;
    498                
    499                 internet_service = netif->nil->service;
     344                }
     345               
     346                nil_service = netif->nil->service;
    500347        } else
    501                 internet_service = netif->driver->service;
     348                nil_service = -1;
    502349       
    503350        /* Inter-network layer startup */
    504351        switch (netif->il->service) {
    505352        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);
    509356                        return rc;
     357                }
     358               
    510359                break;
    511360        default:
     
    513362        }
    514363       
    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
     368static 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
     397static 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]);
    551407                if (rc != EOK)
    552408                        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 interfaces
    583                  * 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);
    612409        }
    613410       
     
    630427 *
    631428 */
    632 int net_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
    633     size_t *answer_count)
     429static int net_message(ipc_callid_t callid, ipc_call_t *call,
     430    ipc_call_t *answer, size_t *answer_count)
    634431{
    635432        measured_string_t *strings;
    636433        uint8_t *data;
    637434        int rc;
     435        size_t count;
    638436       
    639437        *answer_count = 0;
     438       
     439        if (!IPC_GET_IMETHOD(*call))
     440                return EOK;
     441       
    640442        switch (IPC_GET_IMETHOD(*call)) {
    641         case IPC_M_PHONE_HUNGUP:
    642                 return EOK;
    643443        case NET_NET_GET_DEVICE_CONF:
    644444                rc = measured_strings_receive(&strings, &data,
     
    646446                if (rc != EOK)
    647447                        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));
    653451               
    654452                rc = measured_strings_reply(strings, IPC_GET_COUNT(*call));
    655453                free(strings);
     454                free(data);
    656455                return rc;
    657456        case NET_NET_GET_CONF:
     
    660459                if (rc != EOK)
    661460                        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));
    666463               
    667464                rc = measured_strings_reply(strings, IPC_GET_COUNT(*call));
    668465                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        }
    675488}
    676489
    677490/** Default thread for new connections.
    678491 *
    679  * @param[in] iid The initial message identifier.
     492 * @param[in] iid   The initial message identifier.
    680493 * @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 */
     497static void net_client_connection(ipc_callid_t iid, ipc_call_t *icall,
     498    void *arg)
    684499{
    685500        /*
     
    692507                /* Clear the answer structure */
    693508                ipc_call_t answer;
    694                 size_t answer_count;
    695                 refresh_answer(&answer, &answer_count);
     509                size_t count;
     510                refresh_answer(&answer, &count);
    696511               
    697512                /* Fetch the next message */
     
    700515               
    701516                /* 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);
    703522               
    704523                /* 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))
    706525                        return;
    707526               
    708527                /* Answer the message */
    709                 answer_call(callid, res, &answer, answer_count);
     528                answer_call(callid, res, &answer, count);
    710529        }
    711530}
     
    713532int main(int argc, char *argv[])
    714533{
    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;
    716702}
    717703
  • uspace/srv/net/net/net.h

    r2bdf8313 rb0f00a9  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    3132 */
    3233
    33 /** @file
    34  * Networking subsystem central module.
    35  *
    36  */
    37 
    3834#ifndef NET_NET_H_
    3935#define NET_NET_H_
     
    4541#include <adt/module_map.h>
    4642#include <net/packet.h>
     43#include <devman.h>
    4744
    48 /** @name Modules definitions
    49  * @{
    50  */
    51 
    52 #define NE2000_FILENAME  "/srv/ne2000"
    53 #define NE2000_NAME      "ne2000"
     45#define NAME  "net"
    5446
    5547#define ETHERNET_FILENAME  "/srv/eth"
     
    5850#define IP_FILENAME  "/srv/ip"
    5951#define IP_NAME      "ip"
    60 
    61 #define LO_FILENAME  "/srv/lo"
    62 #define LO_NAME      "lo"
    6352
    6453#define NILDUMMY_FILENAME  "/srv/nildummy"
     
    7766#define CONF_MTU    "MTU"    /**< Maximum transmission unit configuration label. */
    7867#define CONF_NAME   "NAME"   /**< Network interface name configuration label. */
    79 #define CONF_NETIF  "NETIF"  /**< Network interface module name configuration label. */
     68#define CONF_HWPATH "HWPATH" /**< Network interface hardware pathname label. */
    8069#define CONF_NIL    "NIL"    /**< Network interface layer module name configuration label. */
    8170
     
    8574#define CONF_DIR           "/cfg/net"  /**< Configuration directory. */
    8675#define CONF_GENERAL_FILE  "general"   /**< General configuration file. */
     76#define CONF_EXT           ".nic"      /**< Extension for NIC's configuration files. */
    8777
    8878/** Configuration settings.
     
    9888 */
    9989typedef 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;
    10196       
    10297        /** 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. */
    104100       
    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. */
    109103} netif_t;
    110104
     
    124118        modules_t modules;                 /**< Available modules. */
    125119       
    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;
    128122       
    129123        /** Present network interfaces. */
     
    131125} net_globals_t;
    132126
    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 
    139127#endif
    140128
  • uspace/srv/net/net/packet_server.c

    r2bdf8313 rb0f00a9  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    3536 */
    3637
    37 #include <packet_server.h>
    38 
    3938#include <align.h>
    4039#include <assert.h>
    4140#include <async.h>
    4241#include <errno.h>
     42#include <str_error.h>
     43#include <stdio.h>
    4344#include <fibril_synch.h>
    4445#include <unistd.h>
     
    4950#include <net/packet_header.h>
    5051
     52#include "packet_server.h"
     53
     54#define PACKET_SERVER_PROFILE 1
     55
     56/** Number of queues cacheing the unused packets */
    5157#define FREE_QUEUES_COUNT       7
     58/** Maximum number of packets in each queue */
     59#define FREE_QUEUE_MAX_LENGTH   16
    5260
    5361/** The default address length reserved for new packets. */
     
    5967/** The default suffix reserved for new packets. */
    6068#define DEFAULT_SUFFIX          64
     69
     70/** The queue with unused packets */
     71typedef 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;
    6176
    6277/** Packet server global data. */
     
    6580        fibril_mutex_t lock;
    6681        /** 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];
    7583       
    7684        /** Total packets allocated. */
    77         unsigned int count;
     85        packet_id_t next_id;
    7886} ps_globals = {
    7987        .lock = FIBRIL_MUTEX_INITIALIZER(ps_globals.lock),
    80         .free = {
    81                 NULL,
    82                 NULL,
    83                 NULL,
    84                 NULL,
    85                 NULL,
    86                 NULL,
    87                 NULL
     88        .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},
    8896        },
    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
    9998};
    10099
     
    108107 * @param[in] max_suffix The maximal suffix length in bytes.
    109108 */
    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)
     109static void packet_init(packet_t *packet,
     110        size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix)
    113111{
    114112        /* Clear the packet content */
     
    121119        packet->previous = 0;
    122120        packet->next = 0;
     121        packet->offload_info = 0;
     122        packet->offload_mask = 0;
    123123        packet->addr_len = 0;
    124124        packet->src_addr = sizeof(packet_t);
     
    128128        packet->data_start = packet->dest_addr + addr_len + packet->max_prefix;
    129129        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 */
     137static void packet_dealloc(packet_t *packet)
     138{
     139        pm_remove(packet);
     140        munmap(packet, packet->length);
    130141}
    131142
     
    142153 * @return              NULL if there is not enough memory left.
    143154 */
    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)
     155static packet_t *packet_alloc(size_t length, size_t addr_len,
     156        size_t max_prefix, size_t max_content, size_t max_suffix)
    147157{
    148158        packet_t *packet;
    149159        int rc;
    150160
     161        /* Global lock is locked */
    151162        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
    154166        packet = (packet_t *) mmap(NULL, length, PROTO_READ | PROTO_WRITE,
    155             MAP_SHARED | MAP_ANONYMOUS, 0, 0);
     167                MAP_SHARED | MAP_ANONYMOUS, 0, 0);
    156168        if (packet == MAP_FAILED)
    157169                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
    161179        packet->length = length;
    162180        packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
     
    164182        rc = pm_add(packet);
    165183        if (rc != EOK) {
    166                 munmap(packet, packet->length);
     184                packet_dealloc(packet);
    167185                return NULL;
    168186        }
     
    185203 * @return              NULL if there is not enough memory left.
    186204 */
    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        
     205static 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
    194214        fibril_mutex_lock(&ps_globals.lock);
    195215       
     
    198218       
    199219        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))
    202222                        continue;
    203223               
    204                 packet = ps_globals.free[index];
     224                packet = ps_globals.free_queues[index].first;
    205225                while (packet_is_valid(packet) && (packet->length < length))
    206226                        packet = pm_find(packet->next);
    207227               
    208228                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 {
    212233                                pq_detach(packet);
     234                        }
    213235                       
    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);
    216237                        fibril_mutex_unlock(&ps_globals.lock);
    217238                       
     
    220241        }
    221242       
    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);
    224245       
    225246        fibril_mutex_unlock(&ps_globals.lock);
     
    241262
    242263        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++) {
    244265                ;
    245266        }
    246267       
    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);
    249271        assert(result == EOK);
    250272}
     
    317339 *                      packet_release_wrapper() function.
    318340 */
    319 int
    320 packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
     341int packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
    321342    size_t *answer_count)
    322343{
    323344        packet_t *packet;
    324 
     345       
     346        if (!IPC_GET_IMETHOD(*call))
     347                return EOK;
     348       
    325349        *answer_count = 0;
    326350        switch (IPC_GET_IMETHOD(*call)) {
    327         case IPC_M_PHONE_HUNGUP:
    328                 return EOK;
    329        
    330351        case NET_PACKET_CREATE_1:
    331352                packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX,
    332                     IPC_GET_CONTENT(*call), DEFAULT_SUFFIX);
     353                        IPC_GET_CONTENT(*call), DEFAULT_SUFFIX);
    333354                if (!packet)
    334355                        return ENOMEM;
     
    340361        case NET_PACKET_CREATE_4:
    341362                packet = packet_get_local(
    342                     ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(*call)) ?
     363                        ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(*call)) ?
    343364                    IPC_GET_ADDR_LEN(*call) : DEFAULT_ADDR_LEN),
    344365                    DEFAULT_PREFIX + IPC_GET_PREFIX(*call),
     
    354375        case NET_PACKET_GET:
    355376                packet = pm_find(IPC_GET_ID(*call));
    356                 if (!packet_is_valid(packet))
     377                if (!packet_is_valid(packet)) {
    357378                        return ENOENT;
     379                }
    358380                return packet_reply(packet);
    359381       
     
    373395}
    374396
     397int packet_server_init()
     398{
     399        return EOK;
     400}
     401
    375402/** @}
    376403 */
  • uspace/srv/net/net/packet_server.h

    r2bdf8313 rb0f00a9  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    4243 */
    4344
    44 #ifndef LIBPACKET_PACKET_SERVER_H_
    45 #define LIBPACKET_PACKET_SERVER_H_
     45#ifndef NET_PACKET_SERVER_H_
     46#define NET_PACKET_SERVER_H_
    4647
    4748#include <ipc/common.h>
    4849
     50extern int packet_server_init(void);
    4951extern int packet_server_message(ipc_callid_t, ipc_call_t *, ipc_call_t *,
    5052    size_t *);
  • uspace/srv/net/nil/eth/eth.c

    r2bdf8313 rb0f00a9  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    3637 */
    3738
     39#include <assert.h>
    3840#include <async.h>
    3941#include <malloc.h>
     
    5254#include <protocol_map.h>
    5355#include <net/device.h>
    54 #include <netif_remote.h>
    5556#include <net_interface.h>
    5657#include <il_remote.h>
     
    5859#include <packet_client.h>
    5960#include <packet_remote.h>
     61#include <device/nic.h>
    6062#include <nil_skel.h>
    61 
    6263#include "eth.h"
    6364
     
    168169INT_MAP_IMPLEMENT(eth_protos, eth_proto_t);
    169170
    170 int nil_device_state_msg_local(int nil_phone, device_id_t device_id, int state)
     171int nil_device_state_msg_local(nic_device_id_t device_id, sysarg_t state)
    171172{
    172173        int index;
     
    177178            index--) {
    178179                proto = eth_protos_get_index(&eth_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,
    181182                            proto->service);
    182183                }
     
    187188}
    188189
    189 int nil_initialize(int net_phone)
     190int nil_initialize(async_sess_t *sess)
    190191{
    191192        int rc;
     
    196197        fibril_rwlock_write_lock(&eth_globals.devices_lock);
    197198        fibril_rwlock_write_lock(&eth_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);
    206202
    207203        rc = eth_devices_initialize(&eth_globals.devices);
     
    216212                eth_devices_destroy(&eth_globals.devices, free);
    217213        }
     214       
    218215out:
    219216        fibril_rwlock_write_unlock(&eth_globals.protos_lock);
     
    223220}
    224221
    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 */
     235static int eth_device_message(nic_device_id_t device_id, devman_handle_t handle,
    278236    size_t mtu)
    279237{
     
    300258        device = eth_devices_find(&eth_globals.devices, device_id);
    301259        if (device) {
    302                 if (device->service != service) {
     260                if (device->handle != handle) {
    303261                        printf("Device %d already exists\n", device->device_id);
    304262                        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     
    322280                        proto = eth_protos_get_index(&eth_globals.protos,
    323281                            index);
    324                         if (proto->phone) {
    325                                 il_mtu_changed_msg(proto->phone,
     282                        if (proto->sess) {
     283                                il_mtu_changed_msg(proto->sess,
    326284                                    device->device_id, device->mtu,
    327285                                    proto->service);
     
    339297
    340298        device->device_id = device_id;
    341         device->service = service;
     299        device->handle = handle;
    342300        device->flags = 0;
    343301        if ((mtu > 0) && (mtu <= ETH_MAX_TAGGED_CONTENT(device->flags)))
     
    347305
    348306        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,
    350308            &configuration, count, &data);
    351309        if (rc != EOK) {
     
    376334       
    377335        /* 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) {
    381339                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    382340                free(device);
    383                 return device->phone;
    384         }
     341                return ENOENT;
     342        }
     343       
     344        nic_connect_to_nil(device->sess, SERVICE_ETHERNET, device_id);
    385345       
    386346        /* 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);
    389348        if (rc != EOK) {
    390349                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     
    398357        if (index < 0) {
    399358                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    400                 free(device->addr);
    401                 free(device->addr_data);
    402359                free(device);
    403360                return index;
    404361        }
    405362       
    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);
    412367
    413368        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     
    455410                fcs = (eth_fcs_t *) data + length - sizeof(eth_fcs_t);
    456411                length -= sizeof(eth_fcs_t);
    457         } else if(type <= ETH_MAX_CONTENT) {
     412        } else if (type <= ETH_MAX_CONTENT) {
    458413                /* Translate "LSAP" values */
    459414                if ((header->lsap.dsap == ETH_LSAP_GLSAP) &&
     
    461416                        /* Raw packet -- discard */
    462417                        return NULL;
    463                 } else if((header->lsap.dsap == ETH_LSAP_SNAP) &&
     418                } else if ((header->lsap.dsap == ETH_LSAP_SNAP) &&
    464419                    (header->lsap.ssap == ETH_LSAP_SNAP)) {
    465420                        /*
     
    468423                         */
    469424                        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) +
    472426                            sizeof(eth_header_snap_t);
    473427                } else {
    474428                        /* IEEE 802.3 + 802.2 LSAP */
    475429                        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);
    478431                }
    479432
     
    505458}
    506459
    507 int nil_received_msg_local(int nil_phone, device_id_t device_id,
    508     packet_t *packet, services_t target)
     460int nil_received_msg_local(nic_device_id_t device_id, packet_t *packet)
    509461{
    510462        eth_proto_t *proto;
     
    522474        flags = device->flags;
    523475        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    524        
    525476        fibril_rwlock_read_lock(&eth_globals.protos_lock);
     477       
    526478        do {
    527479                next = pq_detach(packet);
    528480                proto = eth_process_packet(flags, packet);
    529481                if (proto) {
    530                         il_received_msg(proto->phone, device_id, packet,
     482                        il_received_msg(proto->sess, device_id, packet,
    531483                            proto->service);
    532484                } else {
    533485                        /* Drop invalid/unknown */
    534                         pq_release_remote(eth_globals.net_phone,
     486                        pq_release_remote(eth_globals.net_sess,
    535487                            packet_get_id(packet));
    536488                }
    537489                packet = next;
    538         } while(packet);
     490        } while (packet);
    539491
    540492        fibril_rwlock_read_unlock(&eth_globals.protos_lock);
     
    553505 * @return              ENOENT if there is no such device.
    554506 */
    555 static int eth_packet_space_message(device_id_t device_id, size_t *addr_len,
     507static int eth_packet_space_message(nic_device_id_t device_id, size_t *addr_len,
    556508    size_t *prefix, size_t *content, size_t *suffix)
    557509{
     
    578530}
    579531
    580 /** Returns the device hardware address.
     532/** Send the device hardware address.
    581533 *
    582534 * @param[in] device_id The device identifier.
    583535 * @param[in] type      Type of the desired address.
    584  * @param[out] address  The device hardware address.
    585536 * @return              EOK on success.
    586537 * @return              EBADMEM if the address parameter is NULL.
    587538 * @return              ENOENT if there no such device.
    588539 */
    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 {
     540static 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 {
    600550                fibril_rwlock_read_lock(&eth_globals.devices_lock);
    601551                device = eth_devices_find(&eth_globals.devices, device_id);
     
    604554                        return ENOENT;
    605555                }
    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       
     576end:
     577       
     578        if (type == ETH_LOCAL_ADDR)
    607579                fibril_rwlock_read_unlock(&eth_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 */
     596static int eth_register_message(services_t service, async_sess_t *sess)
    624597{
    625598        eth_proto_t *proto;
     
    634607        proto = eth_protos_find(&eth_globals.protos, protocol);
    635608        if (proto) {
    636                 proto->phone = phone;
     609                proto->sess = sess;
    637610                fibril_rwlock_write_unlock(&eth_globals.protos_lock);
    638611                return EOK;
     
    646619                proto->service = service;
    647620                proto->protocol = protocol;
    648                 proto->phone = phone;
     621                proto->sess = sess;
    649622
    650623                index = eth_protos_add(&eth_globals.protos, protocol, proto);
     
    656629        }
    657630       
    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);
    660633       
    661634        fibril_rwlock_write_unlock(&eth_globals.protos_lock);
     
    694667        if (i < 0)
    695668                return i;
     669       
    696670        if (i != ETH_ADDR)
    697671                return EINVAL;
     672       
     673        for (i = 0; i < ETH_ADDR; i++) {
     674                if (src[i]) {
     675                        src_addr = src;
     676                        break;
     677                }
     678        }
    698679
    699680        length = packet_get_data_length(packet);
     
    719700                memcpy(header_dix->destination_address, dest, ETH_ADDR);
    720701                src = &header_dix->destination_address[0];
    721         } else if(IS_8023_2_LSAP(flags)) {
     702        } else if (IS_8023_2_LSAP(flags)) {
    722703                header_lsap = PACKET_PREFIX(packet, eth_header_lsap_t);
    723704                if (!header_lsap)
     
    732713                memcpy(header_lsap->header.destination_address, dest, ETH_ADDR);
    733714                src = &header_lsap->header.destination_address[0];
    734         } else if(IS_8023_2_SNAP(flags)) {
     715        } else if (IS_8023_2_SNAP(flags)) {
    735716                header = PACKET_PREFIX(packet, eth_header_snap_t);
    736717                if (!header)
     
    743724                header->lsap.ctrl = IEEE_8023_2_UI;
    744725               
    745                 for (i = 0; i < 3; ++ i)
     726                for (i = 0; i < 3; i++)
    746727                        header->snap.protocol[i] = 0;
    747728               
     
    757738                        return ENOMEM;
    758739               
    759                 for (i = 0; i < 7; ++ i)
     740                for (i = 0; i < 7; i++)
    760741                        preamble->preamble[i] = ETH_PREAMBLE;
    761742               
     
    784765 * @return              EINVAL if the service parameter is not known.
    785766 */
    786 static int eth_send_message(device_id_t device_id, packet_t *packet,
     767static int eth_send_message(nic_device_id_t device_id, packet_t *packet,
    787768    services_t sender)
    788769{
     
    795776        ethertype = htons(protocol_map(SERVICE_ETHERNET, sender));
    796777        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));
    798779                return EINVAL;
    799780        }
     
    810791        do {
    811792                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);
    813794                if (rc != EOK) {
    814795                        /* Release invalid packet */
     
    816797                        if (next == packet)
    817798                                packet = tmp;
    818                         pq_release_remote(eth_globals.net_phone,
     799                        pq_release_remote(eth_globals.net_sess,
    819800                            packet_get_id(next));
    820801                        next = tmp;
     
    822803                        next = pq_next(next);
    823804                }
    824         } while(next);
     805        } while (next);
    825806       
    826807        /* 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       
    832811        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    833812        return EOK;
    834813}
    835814
     815static 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(&eth_globals.devices_lock);
     833        /* An existing device? */
     834        eth_device_t *device = eth_devices_find(&eth_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(&eth_globals.devices_lock);
     840
     841                /* Notify all upper layer modules */
     842                fibril_rwlock_read_lock(&eth_globals.protos_lock);
     843                int index;
     844                for (index = 0; index < eth_protos_count(&eth_globals.protos); index++) {
     845                        eth_proto_t *proto = eth_protos_get_index(&eth_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(&eth_globals.protos_lock);
     853                return EOK;
     854        } else {
     855                return ENOENT;
     856        }
     857}
     858
    836859int nil_module_message(ipc_callid_t callid, ipc_call_t *call,
    837860    ipc_call_t *answer, size_t *answer_count)
    838861{
    839         measured_string_t *address;
    840862        packet_t *packet;
    841863        size_t addrlen;
     
    846868       
    847869        *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       
    848879        switch (IPC_GET_IMETHOD(*call)) {
    849         case IPC_M_PHONE_HUNGUP:
    850                 return EOK;
    851        
    852880        case NET_NIL_DEVICE:
    853881                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));
    855883        case NET_NIL_SEND:
    856                 rc = packet_translate_remote(eth_globals.net_phone, &packet,
     884                rc = packet_translate_remote(eth_globals.net_sess, &packet,
    857885                    IPC_GET_PACKET(*call));
    858886                if (rc != EOK)
    859887                        return rc;
     888               
    860889                return eth_send_message(IPC_GET_DEVICE(*call), packet,
    861890                    IPC_GET_SERVICE(*call));
     
    865894                if (rc != EOK)
    866895                        return rc;
     896               
    867897                IPC_SET_ADDR(*answer, addrlen);
    868898                IPC_SET_PREFIX(*answer, prefix);
     
    870900                IPC_SET_SUFFIX(*answer, suffix);
    871901                *answer_count = 4;
     902               
    872903                return EOK;
    873904        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);
    876906                if (rc != EOK)
    877907                        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;
    879913        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);
    882915                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;
    888938        }
    889939       
  • uspace/srv/net/nil/eth/eth.h

    r2bdf8313 rb0f00a9  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    3839#define NET_ETH_H_
    3940
     41#include <async.h>
    4042#include <fibril_synch.h>
    4143#include <ipc/services.h>
    42 
    4344#include <net/device.h>
    4445#include <adt/measured_strings.h>
     46#include <devman.h>
    4547
    4648/** Ethernet address length. */
     
    220222struct eth_device {
    221223        /** 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;
    227229        /** Maximal transmission unit. */
    228230        size_t mtu;
     
    236238       
    237239        /** 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;
    242241};
    243242
     
    248247        /** Protocol identifier. */
    249248        int protocol;
    250         /** Protocol module phone. */
    251         int phone;
     249        /** Protocol module session. */
     250        async_sess_t *sess;
    252251};
    253252
    254253/** Ethernet global data. */
    255254struct eth_globals {
    256         /** Networking module phone. */
    257         int net_phone;
     255        /** Networking module session. */
     256        async_sess_t *net_sess;
    258257        /** Safety lock for devices. */
    259258        fibril_rwlock_t devices_lock;
     
    265264        /**
    266265         * Protocol map.
    267          * Service phone map for each protocol.
     266         * Service map for each protocol.
    268267         */
    269268        eth_protos_t protos;
    270269       
    271270        /** Broadcast device hardware address. */
    272         measured_string_t *broadcast_addr;
     271        uint8_t broadcast_addr[ETH_ADDR];
    273272};
    274273
  • uspace/srv/net/nil/nildummy/nildummy.c

    r2bdf8313 rb0f00a9  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    3637 */
    3738
     39#include <assert.h>
    3840#include <async.h>
    3941#include <malloc.h>
     
    4446#include <ipc/net.h>
    4547#include <ipc/services.h>
    46 
    4748#include <net/modules.h>
    4849#include <net/device.h>
     
    5152#include <net/packet.h>
    5253#include <packet_remote.h>
    53 #include <netif_remote.h>
     54#include <packet_client.h>
     55#include <devman.h>
     56#include <device/nic.h>
    5457#include <nil_skel.h>
    55 
    5658#include "nildummy.h"
    5759
     
    6769DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t);
    6870
    69 int nil_device_state_msg_local(int nil_phone, device_id_t device_id, int state)
     71int nil_device_state_msg_local(nic_device_id_t device_id, sysarg_t state)
    7072{
    7173        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,
    7476                    state, nildummy_globals.proto.service);
    7577        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
     
    7880}
    7981
    80 int nil_initialize(int net_phone)
     82int nil_initialize(async_sess_t *sess)
    8183{
    8284        fibril_rwlock_initialize(&nildummy_globals.devices_lock);
     
    8587        fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
    8688       
    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;
    8991        int rc = nildummy_devices_initialize(&nildummy_globals.devices);
    9092       
     
    9395       
    9496        return rc;
    95 }
    96 
    97 /** Process IPC messages from the registered device driver modules
    98  *
    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         }
    13297}
    13398
     
    149114 *
    150115 */
    151 static int nildummy_device_message(device_id_t device_id, services_t service,
    152     size_t mtu)
     116static int nildummy_device_message(nic_device_id_t device_id,
     117    devman_handle_t handle, size_t mtu)
    153118{
    154119        fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
     
    158123            nildummy_devices_find(&nildummy_globals.devices, device_id);
    159124        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);
    164129                        return EEXIST;
    165130                }
     
    171136                        device->mtu = NET_DEFAULT_MTU;
    172137               
    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);
    175140                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    176141               
    177142                /* Notify the upper layer module */
    178143                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,
    181146                            device->device_id, device->mtu,
    182147                            nildummy_globals.proto.service);
     
    193158       
    194159        device->device_id = device_id;
    195         device->service = service;
     160        device->handle = handle;
    196161        if (mtu > 0)
    197162                device->mtu = mtu;
    198163        else
    199164                device->mtu = NET_DEFAULT_MTU;
    200 
     165       
    201166        /* 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) {
    205170                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    206171                free(device);
    207                 return device->phone;
    208         }
     172                return ENOENT;
     173        }
     174       
     175        nic_connect_to_nil(device->sess, SERVICE_NILDUMMY, device_id);
    209176       
    210177        /* 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);
    213179        if (rc != EOK) {
    214180                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     
    216182                return rc;
    217183        }
     184       
     185        device->addr_len = ETH_ADDR;
    218186       
    219187        /* Add to the cache */
     
    222190        if (index < 0) {
    223191                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    224                 free(device->addr);
    225                 free(device->addr_data);
    226192                free(device);
    227193                return index;
    228194        }
    229195       
    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);
    232198        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    233199        return EOK;
     
    244210 *
    245211 */
    246 static int nildummy_addr_message(device_id_t device_id,
    247     measured_string_t **address)
    248 {
    249         if (!address)
     212static int nildummy_addr_message(nic_device_id_t device_id, size_t *addrlen)
     213{
     214        if (!addrlen)
    250215                return EBADMEM;
    251216       
     
    259224        }
    260225       
    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;
    262247       
    263248        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    264        
    265         return (*address) ? EOK : ENOENT;
     249        return EOK;
    266250}
    267251
     
    279263 *
    280264 */
    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)
     265static 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)
    283267{
    284268        if ((!addr_len) || (!prefix) || (!content) || (!suffix))
     
    304288}
    305289
    306 int nil_received_msg_local(int nil_phone, device_id_t device_id,
    307     packet_t *packet, services_t target)
     290int nil_received_msg_local(nic_device_id_t device_id, packet_t *packet)
    308291{
    309292        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
    310293       
    311         if (nildummy_globals.proto.phone) {
     294        if (nildummy_globals.proto.sess) {
    312295                do {
    313296                        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,
    315298                            packet, nildummy_globals.proto.service);
    316299                        packet = next;
     
    328311 *
    329312 * @param[in] service Module service.
    330  * @param[in] phone   Service phone.
     313 * @param[in] sess    Service session.
    331314 *
    332315 * @return EOK on success.
     
    335318 *
    336319 */
    337 static int nildummy_register_message(services_t service, int phone)
     320static int nildummy_register_message(services_t service, async_sess_t *sess)
    338321{
    339322        fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
    340323        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);
    345328       
    346329        fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
     
    359342 *
    360343 */
    361 static int nildummy_send_message(device_id_t device_id, packet_t *packet,
     344static int nildummy_send_message(nic_device_id_t device_id, packet_t *packet,
    362345    services_t sender)
    363346{
     
    373356        /* Send packet queue */
    374357        if (packet)
    375                 netif_send_msg(device->phone, device_id, packet,
    376                     SERVICE_NILDUMMY);
     358                nic_send_message(device->sess, packet_get_id(packet));
    377359       
    378360        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
     
    384366    ipc_call_t *answer, size_t *answer_count)
    385367{
    386         measured_string_t *address;
    387368        packet_t *packet;
    388369        size_t addrlen;
     
    393374       
    394375        *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       
    395385        switch (IPC_GET_IMETHOD(*call)) {
    396         case IPC_M_PHONE_HUNGUP:
    397                 return EOK;
    398        
    399386        case NET_NIL_DEVICE:
    400387                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));
    402389       
    403390        case NET_NIL_SEND:
    404                 rc = packet_translate_remote(nildummy_globals.net_phone,
     391                rc = packet_translate_remote(nildummy_globals.net_sess,
    405392                    &packet, IPC_GET_PACKET(*call));
    406393                if (rc != EOK)
     
    422409       
    423410        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);
    425413                if (rc != EOK)
    426414                        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;
    438433        }
    439434       
  • uspace/srv/net/nil/nildummy/nildummy.h

    r2bdf8313 rb0f00a9  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    3839#define NET_NILDUMMY_H_
    3940
     41#include <async.h>
    4042#include <fibril_synch.h>
    4143#include <ipc/services.h>
    42 
     44#include <ipc/devman.h>
    4345#include <net/device.h>
    4446#include <adt/measured_strings.h>
     
    7678struct nildummy_device {
    7779        /** 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;
    8585       
    8686        /** Maximal transmission unit. */
     
    8888       
    8989        /** 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;
    9493};
    9594
     
    9998        services_t service;
    10099       
    101         /** Protocol module phone. */
    102         int phone;
     100        /** Protocol module session. */
     101        async_sess_t *sess;
    103102};
    104103
    105104/** Dummy nil global data. */
    106105struct nildummy_globals {
    107         /** Networking module phone. */
    108         int net_phone;
     106        /** Networking module session. */
     107        async_sess_t *net_sess;
    109108       
    110109        /** Lock for devices. */
  • uspace/srv/net/tl/icmp/icmp.c

    r2bdf8313 rb0f00a9  
    118118
    119119/** Global data */
    120 static int phone_net = -1;
    121 static int phone_ip = -1;
     120static async_sess_t *net_sess = NULL;
     121static async_sess_t *ip_sess = NULL;
    122122static bool error_reporting = true;
    123123static bool echo_replying = true;
     
    173173static void icmp_release(packet_t *packet)
    174174{
    175         pq_release_remote(phone_net, packet_get_id(packet));
     175        pq_release_remote(net_sess, packet_get_id(packet));
    176176}
    177177
     
    225225        }
    226226       
    227         return ip_send_msg(phone_ip, -1, packet, SERVICE_ICMP, error);
     227        return ip_send_msg(ip_sess, -1, packet, SERVICE_ICMP, error);
    228228}
    229229
     
    297297        size_t length = (size_t) addrlen;
    298298       
    299         packet_t *packet = packet_get_4_remote(phone_net, size,
     299        packet_t *packet = packet_get_4_remote(net_sess, size,
    300300            icmp_dimension.addr_len, ICMP_HEADER_SIZE + icmp_dimension.prefix,
    301301            icmp_dimension.suffix);
     
    595595        case ICMP_SKIP:
    596596        case ICMP_PHOTURIS:
    597                 ip_received_error_msg(phone_ip, -1, packet,
     597                ip_received_error_msg(ip_sess, -1, packet,
    598598                    SERVICE_IP, SERVICE_ICMP);
    599599                return EOK;
     
    608608 * @param[in]     iid   Message identifier.
    609609 * @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 */
     613static void icmp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
     614{
    615615        packet_t *packet;
    616616        int rc;
    617617       
    618         while (loop) {
     618        while (true) {
     619                if (!IPC_GET_IMETHOD(*icall))
     620                        break;
     621               
    619622                switch (IPC_GET_IMETHOD(*icall)) {
    620623                case NET_TL_RECEIVED:
    621                         rc = packet_translate_remote(phone_net, &packet,
     624                        rc = packet_translate_remote(net_sess, &packet,
    622625                            IPC_GET_PACKET(*icall));
    623626                        if (rc == EOK) {
     
    629632                        async_answer_0(iid, (sysarg_t) rc);
    630633                        break;
    631                 case IPC_M_PHONE_HUNGUP:
    632                         loop = false;
    633                         continue;
    634634                default:
    635635                        async_answer_0(iid, (sysarg_t) ENOTSUP);
     
    642642/** Initialize the ICMP module.
    643643 *
    644  * @param[in] net_phone Network module phone.
     644 * @param[in] sess Network module session.
    645645 *
    646646 * @return EOK on success.
     
    648648 *
    649649 */
    650 int tl_initialize(int net_phone)
     650int tl_initialize(async_sess_t *sess)
    651651{
    652652        measured_string_t names[] = {
     
    670670        atomic_set(&icmp_client, 0);
    671671       
    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,
    674674            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);
    679679        if (rc != EOK)
    680680                return rc;
     
    685685        /* Get configuration */
    686686        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);
    688688        if (rc != EOK)
    689689                return rc;
     
    753753                        return rc;
    754754               
    755                 rc = icmp_echo(icmp_id, icmp_seq, ICMP_GET_SIZE(*call), 
     755                rc = icmp_echo(icmp_id, icmp_seq, ICMP_GET_SIZE(*call),
    756756                    ICMP_GET_TIMEOUT(*call), ICMP_GET_TTL(*call),
    757757                    ICMP_GET_TOS(*call), ICMP_GET_DONT_FRAGMENT(*call),
     
    763763       
    764764        case NET_ICMP_DEST_UNREACH:
    765                 rc = packet_translate_remote(phone_net, &packet,
     765                rc = packet_translate_remote(net_sess, &packet,
    766766                    IPC_GET_PACKET(*call));
    767767                if (rc != EOK)
     
    772772       
    773773        case NET_ICMP_SOURCE_QUENCH:
    774                 rc = packet_translate_remote(phone_net, &packet,
     774                rc = packet_translate_remote(net_sess, &packet,
    775775                    IPC_GET_PACKET(*call));
    776776                if (rc != EOK)
     
    780780       
    781781        case NET_ICMP_TIME_EXCEEDED:
    782                 rc = packet_translate_remote(phone_net, &packet,
     782                rc = packet_translate_remote(net_sess, &packet,
    783783                    IPC_GET_PACKET(*call));
    784784                if (rc != EOK)
     
    788788       
    789789        case NET_ICMP_PARAMETERPROB:
    790                 rc = packet_translate_remote(phone_net, &packet,
     790                rc = packet_translate_remote(net_sess, &packet,
    791791                    IPC_GET_PACKET(*call));
    792792                if (rc != EOK)
  • uspace/srv/net/tl/tcp/tcp.c

    r2bdf8313 rb0f00a9  
    169169static int tcp_release_after_timeout(void *);
    170170
    171 static int tcp_process_packet(device_id_t, packet_t *, services_t);
     171static int tcp_process_packet(nic_device_id_t, packet_t *, services_t);
    172172static int tcp_connect_core(socket_core_t *, socket_cores_t *,
    173173    struct sockaddr *, socklen_t);
     
    177177    size_t);
    178178static 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 *);
     179static void tcp_send_packets(nic_device_id_t, packet_t *);
    180180
    181181static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *,
     
    205205static void tcp_queue_received_end_of_data(socket_core_t *socket);
    206206
    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);
     207static int tcp_received_msg(nic_device_id_t, packet_t *, services_t, services_t);
     208static int tcp_process_client_messages(async_sess_t *, ipc_callid_t,
     209    ipc_call_t);
    209210
    210211static int tcp_listen_message(socket_cores_t *, int, int);
     
    219220tcp_globals_t tcp_globals;
    220221
    221 int tcp_received_msg(device_id_t device_id, packet_t *packet,
     222int tcp_received_msg(nic_device_id_t device_id, packet_t *packet,
    222223    services_t receiver, services_t error)
    223224{
     
    237238}
    238239
    239 int tcp_process_packet(device_id_t device_id, packet_t *packet, services_t error)
     240int tcp_process_packet(nic_device_id_t device_id, packet_t *packet, services_t error)
    240241{
    241242        size_t length;
     
    324325
    325326        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,
    329330                            ICMP_PORT_UNREACH, 0, packet);
    330331                }
     
    397398                fibril_rwlock_write_unlock(socket_data->local_lock);
    398399
    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);
    401402                if (rc == EOK) {
    402403                        /* Checksum error ICMP */
    403                         icmp_parameter_problem_msg(tcp_globals.icmp_phone,
     404                        icmp_parameter_problem_msg(tcp_globals.icmp_sess,
    404405                            ICMP_PARAM_POINTER,
    405406                            ((size_t) ((void *) &header->checksum)) -
     
    439440                break;
    440441        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));
    442443        }
    443444
     
    476477        old_incoming = socket_data->next_incoming;
    477478
    478         if (header->finalize) {
     479        if (GET_TCP_HEADER_FINALIZE(header)) {
    479480                socket_data->fin_incoming = new_sequence_number +
    480481                    total_length - TCP_HEADER_LENGTH(header);
     
    502503                        /* Release the acknowledged packets */
    503504                        next_packet = pq_next(packet);
    504                         pq_release_remote(tcp_globals.net_phone,
     505                        pq_release_remote(tcp_globals.net_sess,
    505506                            packet_get_id(packet));
    506507                        packet = next_packet;
     
    561562                        next_packet = pq_next(next_packet);
    562563                        pq_insert_after(tmp_packet, next_packet);
    563                         pq_release_remote(tcp_globals.net_phone,
     564                        pq_release_remote(tcp_globals.net_sess,
    564565                            packet_get_id(tmp_packet));
    565566                }
     
    598599                                if (packet == socket_data->incoming)
    599600                                        socket_data->incoming = next_packet;
    600                                 pq_release_remote(tcp_globals.net_phone,
     601                                pq_release_remote(tcp_globals.net_sess,
    601602                                    packet_get_id(packet));
    602603                                packet = next_packet;
     
    621622                                if (length <= 0) {
    622623                                        /* Remove the empty packet */
    623                                         pq_release_remote(tcp_globals.net_phone,
     624                                        pq_release_remote(tcp_globals.net_sess,
    624625                                            packet_get_id(packet));
    625626                                        packet = next_packet;
     
    668669                                }
    669670                                /* Remove the duplicit or corrupted packet */
    670                                 pq_release_remote(tcp_globals.net_phone,
     671                                pq_release_remote(tcp_globals.net_sess,
    671672                                    packet_get_id(packet));
    672673                                packet = next_packet;
     
    695696                if (rc != EOK) {
    696697                        /* Remove the corrupted packets */
    697                         pq_release_remote(tcp_globals.net_phone,
     698                        pq_release_remote(tcp_globals.net_sess,
    698699                            packet_get_id(packet));
    699                         pq_release_remote(tcp_globals.net_phone,
     700                        pq_release_remote(tcp_globals.net_sess,
    700701                            packet_get_id(next_packet));
    701702                } else {
     
    708709                                    new_sequence_number, length);
    709710                                if (rc != EOK) {
    710                                         pq_release_remote(tcp_globals.net_phone,
     711                                        pq_release_remote(tcp_globals.net_sess,
    711712                                            packet_get_id(next_packet));
    712713                                }
    713714                                rc = pq_insert_after(packet, next_packet);
    714715                                if (rc != EOK) {
    715                                         pq_release_remote(tcp_globals.net_phone,
     716                                        pq_release_remote(tcp_globals.net_sess,
    716717                                            packet_get_id(next_packet));
    717718                                }
     
    722723                printf("unexpected\n");
    723724                /* 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));
    725726                forced_ack = true;
    726727        }
     
    790791        if (rc != EOK)
    791792                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,
    793794            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
    794795        if (rc != EOK)
     
    799800
    800801        /* 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,
    803804            ((packet_dimension->content < socket_data->data_fragment_size) ?
    804805            packet_dimension->content : socket_data->data_fragment_size), 0, 0,
    805806            (sysarg_t) fragments);
     807        async_exchange_end(exch);
    806808
    807809        return EOK;
     
    820822
    821823        /* 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);
    826828}
    827829
     
    838840        assert(packet);
    839841
    840         if (!header->synchronize)
     842        if (!GET_TCP_HEADER_SYNCHRONIZE(header))
    841843                return tcp_release_and_return(packet, EINVAL);
    842844       
     
    849851        next_packet = pq_detach(packet);
    850852        if (next_packet) {
    851                 pq_release_remote(tcp_globals.net_phone,
     853                pq_release_remote(tcp_globals.net_sess,
    852854                    packet_get_id(next_packet));
    853855        }
     
    903905        assert(packet);
    904906
    905         if (!header->synchronize)
     907        if (!GET_TCP_HEADER_SYNCHRONIZE(header))
    906908                return tcp_release_and_return(packet, EINVAL);
    907909
     
    936938        /* Create a socket */
    937939        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,
    939941            socket_data, &socket_id);
    940942        if (rc != EOK) {
     
    989991        fibril_rwlock_write_unlock(&tcp_globals.lock);
    990992        if (rc != EOK) {
    991                 socket_destroy(tcp_globals.net_phone, socket->socket_id,
     993                socket_destroy(tcp_globals.net_sess, socket->socket_id,
    992994                    socket_data->local_sockets, &tcp_globals.sockets,
    993995                    tcp_free_socket_data);
     
    10011003        next_packet = pq_detach(packet);
    10021004        if (next_packet) {
    1003                 pq_release_remote(tcp_globals.net_phone,
     1005                pq_release_remote(tcp_globals.net_sess,
    10041006                    packet_get_id(next_packet));
    10051007        }
     
    10101012                    packet_get_data_length(packet) - sizeof(*header));
    10111013                if (rc != EOK) {
    1012                         socket_destroy(tcp_globals.net_phone, socket->socket_id,
     1014                        socket_destroy(tcp_globals.net_sess, socket->socket_id,
    10131015                            socket_data->local_sockets, &tcp_globals.sockets,
    10141016                            tcp_free_socket_data);
     
    10211023        rc = tcp_queue_packet(socket, socket_data, packet, 1);
    10221024        if (rc != EOK) {
    1023                 socket_destroy(tcp_globals.net_phone, socket->socket_id,
     1025                socket_destroy(tcp_globals.net_sess, socket->socket_id,
    10241026                    socket_data->local_sockets, &tcp_globals.sockets,
    10251027                    tcp_free_socket_data);
     
    10291031        packet = tcp_get_packets_to_send(socket, socket_data);
    10301032        if (!packet) {
    1031                 socket_destroy(tcp_globals.net_phone, socket->socket_id,
     1033                socket_destroy(tcp_globals.net_sess, socket->socket_id,
    10321034                    socket_data->local_sockets, &tcp_globals.sockets,
    10331035                    tcp_free_socket_data);
     
    10571059        assert(packet);
    10581060
    1059         if (!header->acknowledge)
     1061        if (!GET_TCP_HEADER_ACKNOWLEDGE(header))
    10601062                return tcp_release_and_return(packet, EINVAL);
    10611063
     
    10641066
    10651067        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));
    10671069        socket_data->state = TCP_SOCKET_ESTABLISHED;
    10681070        listening_socket = socket_cores_find(socket_data->local_sockets,
     
    10781080                if (rc == EOK) {
    10791081                        /* 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,
    10811084                            (sysarg_t) listening_socket->socket_id,
    10821085                            socket_data->data_fragment_size, TCP_HEADER_SIZE,
    10831086                            0, (sysarg_t) socket->socket_id);
     1087                        async_exchange_end(exch);
    10841088
    10851089                        fibril_rwlock_write_unlock(socket_data->local_lock);
     
    11261130        assert(header);
    11271131
    1128         if (!header->acknowledge)
     1132        if (!GET_TCP_HEADER_ACKNOWLEDGE(header))
    11291133                return;
    11301134
     
    11771181                                /* Add to acknowledged or release */
    11781182                                if (pq_add(&acknowledged, packet, 0, 0) != EOK)
    1179                                         pq_release_remote(tcp_globals.net_phone,
     1183                                        pq_release_remote(tcp_globals.net_sess,
    11801184                                            packet_get_id(packet));
    11811185                                packet = next;
     
    11861190                /* Release acknowledged */
    11871191                if (acknowledged) {
    1188                         pq_release_remote(tcp_globals.net_phone,
     1192                        pq_release_remote(tcp_globals.net_sess,
    11891193                            packet_get_id(acknowledged));
    11901194                }
     
    12301234        assert(answer);
    12311235        assert(answer_count);
    1232 
     1236       
    12331237        *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       
    12391244        return ENOTSUP;
    12401245}
     
    12461251        bzero(socket_data, sizeof(*socket_data));
    12471252        socket_data->state = TCP_SOCKET_INITIAL;
    1248         socket_data->device_id = DEVICE_INVALID_ID;
     1253        socket_data->device_id = NIC_DEVICE_INVALID_ID;
    12491254        socket_data->window = NET_DEFAULT_TCP_WINDOW;
    12501255        socket_data->treshold = socket_data->window;
     
    12661271}
    12671272
    1268 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
     1273int tcp_process_client_messages(async_sess_t *sess, ipc_callid_t callid,
     1274    ipc_call_t call)
    12691275{
    12701276        int res;
    1271         bool keep_on_going = true;
    12721277        socket_cores_t local_sockets;
    1273         int app_phone = IPC_GET_PHONE(call);
    12741278        struct sockaddr *addr;
    12751279        int socket_id;
     
    12931297        fibril_rwlock_initialize(&lock);
    12941298
    1295         while (keep_on_going) {
     1299        while (true) {
    12961300
    12971301                /* Answer the call */
     
    13011305                /* Get the next call */
    13021306                callid = async_get_call(&call);
     1307               
     1308                if (!IPC_GET_IMETHOD(call)) {
     1309                        res = EHANGUP;
     1310                        break;
     1311                }
    13031312
    13041313                /* Process the call */
    13051314                switch (IPC_GET_IMETHOD(call)) {
    1306                 case IPC_M_PHONE_HUNGUP:
    1307                         keep_on_going = false;
    1308                         res = EHANGUP;
    1309                         break;
    1310 
    13111315                case NET_SOCKET:
    13121316                        socket_data =
     
    13221326                        fibril_rwlock_write_lock(&lock);
    13231327                        socket_id = SOCKET_GET_SOCKET_ID(call);
    1324                         res = socket_create(&local_sockets, app_phone,
     1328                        res = socket_create(&local_sockets, sess,
    13251329                            socket_data, &socket_id);
    13261330                        SOCKET_SET_SOCKET_ID(answer, socket_id);
     
    13301334                                break;
    13311335                        }
    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,
    13341338                            &packet_dimension) == EOK) {
    13351339                                SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
     
    15051509        }
    15061510
    1507         /* Release the application phone */
    1508         async_hangup(app_phone);
     1511        /* Release the application session */
     1512        async_hangup(sess);
    15091513
    15101514        printf("release\n");
    15111515        /* Release all local sockets */
    1512         socket_cores_release(tcp_globals.net_phone, &local_sockets,
     1516        socket_cores_release(tcp_globals.net_sess, &local_sockets,
    15131517            &tcp_globals.sockets, tcp_free_socket_data);
    15141518
     
    16181622                        local_lock = socket_data->local_lock;
    16191623                        fibril_rwlock_write_lock(local_lock);
    1620                         socket_destroy(tcp_globals.net_phone,
     1624                        socket_destroy(tcp_globals.net_sess,
    16211625                            timeout->socket_id, timeout->local_sockets,
    16221626                            &tcp_globals.sockets, tcp_free_socket_data);
     
    16471651        /* Sent packet? */
    16481652        packet = pq_find(socket_data->outgoing, sequence_number);
    1649         printf("retransmit %d\n", packet_get_id(packet));
    16501653        if (packet) {
    16511654                pq_get_order(packet, NULL, &data_length);
     
    17511754        }
    17521755
    1753         rc = ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
     1756        rc = ip_get_route_req(tcp_globals.ip_sess, IPPROTO_TCP,
    17541757            addr, addrlen, &socket_data->device_id,
    17551758            &socket_data->pseudo_header, &socket_data->headerlen);
     
    17861789
    17871790                        /* Send the packet */
    1788                         printf("connecting %d\n", packet_get_id(packet));
    17891791                        tcp_send_packets(socket_data->device_id, packet);
    17901792
     
    18331835
    18341836        /* Remember the outgoing FIN */
    1835         if (header->finalize)
     1837        if (GET_TCP_HEADER_FINALIZE(header))
    18361838                socket_data->fin_outgoing = socket_data->next_outgoing;
    18371839       
     
    18991901                        rc = pq_insert_after(previous, copy);
    19001902                        if (rc != EOK) {
    1901                                 pq_release_remote(tcp_globals.net_phone,
     1903                                pq_release_remote(tcp_globals.net_sess,
    19021904                                    packet_get_id(copy));
    19031905                                return sending;
     
    19361938            socket_data->headerlen, packet_get_data_length(packet));
    19371939        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));
    19391941                return NULL;
    19401942        }
     
    19431945        header = (tcp_header_t *) packet_get_data(packet);
    19441946        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));
    19461948                return NULL;
    19471949        }
     
    19521954                header->acknowledgement_number =
    19531955                    htonl(socket_data->next_incoming);
    1954                 header->acknowledge = 1;
     1956                SET_TCP_HEADER_ACKNOWLEDGE(header, 1);
    19551957        }
    19561958        header->window = htons(socket_data->window);
     
    19681970        rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
    19691971        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));
    19711973                return NULL;
    19721974        }
     
    19751977            sequence_number, socket_data->state, socket_data->timeout, true);
    19761978        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));
    19781980                return NULL;
    19791981        }
     
    19921994
    19931995        /* 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);
    19951997        if (!copy)
    19961998                return NULL;
     
    20002002}
    20012003
    2002 void tcp_send_packets(device_id_t device_id, packet_t *packet)
     2004void tcp_send_packets(nic_device_id_t device_id, packet_t *packet)
    20032005{
    20042006        packet_t *next;
     
    20062008        while (packet) {
    20072009                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,
    20092011                    SERVICE_TCP, 0);
    20102012                packet = next;
     
    20242026        header->source_port = htons(socket->port);
    20252027        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);
    20292032}
    20302033
     
    21182121                return NO_DATA;
    21192122
    2120         rc = packet_translate_remote(tcp_globals.net_phone, &packet, packet_id);
     2123        rc = packet_translate_remote(tcp_globals.net_sess, &packet, packet_id);
    21212124        if (rc != EOK)
    21222125                return rc;
     
    21292132        /* Release the packet */
    21302133        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));
    21322135
    21332136        /* Return the total length */
     
    21672170                return ENOTCONN;
    21682171
    2169         rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
     2172        rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess,
    21702173            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
    21712174        if (rc != EOK)
     
    21782181        for (index = 0; index < fragments; index++) {
    21792182                /* 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,
    21812184                    &packet, TCP_HEADER_SIZE, packet_dimension,
    21822185                    socket_data->addr, socket_data->addrlen);
     
    22412244        default:
    22422245                /* Just destroy */
    2243                 rc = socket_destroy(tcp_globals.net_phone, socket_id,
     2246                rc = socket_destroy(tcp_globals.net_sess, socket_id,
    22442247                    local_sockets, &tcp_globals.sockets,
    22452248                    tcp_free_socket_data);
     
    22892292
    22902293        /* 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,
    22922295            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
    22932296        if (rc != EOK)
     
    22952298
    22962299        /* 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,
    22982301            packet_dimension->addr_len, packet_dimension->prefix,
    22992302            packet_dimension->suffix);
     
    23582361                        if (rc != EOK)
    23592362                                return rc;
    2360                         rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
     2363                        rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess,
    23612364                            &tcp_globals.dimensions, socket_data->device_id,
    23622365                            &packet_dimension);
     
    24302433int tcp_release_and_return(packet_t *packet, int result)
    24312434{
    2432         pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     2435        pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
    24332436        return result;
    24342437}
     
    24382441 * @param[in]     iid   Message identifier.
    24392442 * @param[in,out] icall Message parameters.
     2443 * @param[in]     arg   Local argument.
    24402444 *
    24412445 */
    2442 static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall)
     2446static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    24432447{
    24442448        packet_t *packet;
     
    24482452                switch (IPC_GET_IMETHOD(*icall)) {
    24492453                case NET_TL_RECEIVED:
    2450                         rc = packet_translate_remote(tcp_globals.net_phone, &packet,
     2454                        rc = packet_translate_remote(tcp_globals.net_sess, &packet,
    24512455                            IPC_GET_PACKET(*icall));
    24522456                        if (rc == EOK)
     
    24662470/** Initialize the TCP module.
    24672471 *
    2468  * @param[in] net_phone Network module phone.
     2472 * @param[in] sess Network module session.
    24692473 *
    24702474 * @return EOK on success.
     
    24722476 *
    24732477 */
    2474 int tl_initialize(int net_phone)
     2478int tl_initialize(async_sess_t *sess)
    24752479{
    24762480        fibril_rwlock_initialize(&tcp_globals.lock);
    24772481        fibril_rwlock_write_lock(&tcp_globals.lock);
    24782482       
    2479         tcp_globals.net_phone = net_phone;
     2483        tcp_globals.net_sess = sess;
    24802484       
    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,
    24832487            SERVICE_TCP, tcp_receiver);
    2484         if (tcp_globals.ip_phone < 0) {
     2488        if (tcp_globals.ip_sess == NULL) {
    24852489                fibril_rwlock_write_unlock(&tcp_globals.lock);
    2486                 return tcp_globals.ip_phone;
     2490                return ENOENT;
    24872491        }
    24882492       
  • uspace/srv/net/tl/tcp/tcp.h

    r2bdf8313 rb0f00a9  
    3838#define NET_TCP_H_
    3939
     40#include <async.h>
    4041#include <fibril_synch.h>
    41 
    4242#include <net/packet.h>
    4343#include <net/device.h>
     
    182182       
    183183        /** Device identifier. */
    184         device_id_t device_id;
     184        nic_device_id_t device_id;
    185185       
    186186        /**
     
    284284/** TCP global data. */
    285285struct 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;
    292292        /** Last used free port. */
    293293        int last_used_port;
  • uspace/srv/net/tl/tcp/tcp_header.h

    r2bdf8313 rb0f00a9  
    4747 * @param[in] header The TCP packet header.
    4848 */
    49 #define TCP_HEADER_LENGTH(header)               ((header)->header_length * 4U)
     49#define TCP_HEADER_LENGTH(header)               (GET_TCP_HEADER_LENGTH(header) * 4U)
    5050
    5151/** Returns the TCP header length.
     
    7373        uint32_t sequence_number;
    7474        uint32_t acknowledgement_number;
    75        
    76 #ifdef ARCH_IS_BIG_ENDIAN
    77         uint8_t header_length:4;
    78         uint8_t reserved1:4;
    79 #else
    80         uint8_t reserved1:4;
    81         uint8_t header_length:4;
    82 #endif
    8375
    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))
    101134
    102135        uint16_t window;
  • uspace/srv/net/tl/udp/udp.c

    r2bdf8313 rb0f00a9  
    9999static int udp_release_and_return(packet_t *packet, int result)
    100100{
    101         pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
     101        pq_release_remote(udp_globals.net_sess, packet_get_id(packet));
    102102        return result;
    103103}
     
    124124 *                      ip_client_process_packet() function.
    125125 */
    126 static int udp_process_packet(device_id_t device_id, packet_t *packet,
     126static int udp_process_packet(nic_device_id_t device_id, packet_t *packet,
    127127    services_t error)
    128128{
     
    192192            ntohs(header->destination_port), (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
    193193        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,
    197197                            ICMP_PORT_UNREACH, 0, packet);
    198198                }
     
    251251                        while (tmp_packet) {
    252252                                next_packet = pq_detach(tmp_packet);
    253                                 pq_release_remote(udp_globals.net_phone,
     253                                pq_release_remote(udp_globals.net_sess,
    254254                                    packet_get_id(tmp_packet));
    255255                                tmp_packet = next_packet;
     
    274274                if (flip_checksum(compact_checksum(checksum)) !=
    275275                    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) {
    278278                                /* Checksum error ICMP */
    279279                                icmp_parameter_problem_msg(
    280                                     udp_globals.icmp_phone, ICMP_PARAM_POINTER,
     280                                    udp_globals.icmp_sess, ICMP_PARAM_POINTER,
    281281                                    ((size_t) ((void *) &header->checksum)) -
    282282                                    ((size_t) ((void *) header)), packet);
     
    292292                return udp_release_and_return(packet, rc);
    293293               
    294         rc = tl_get_ip_packet_dimension(udp_globals.ip_phone,
     294        rc = tl_get_ip_packet_dimension(udp_globals.ip_sess,
    295295            &udp_globals.dimensions, device_id, &packet_dimension);
    296296        if (rc != EOK)
     
    299299        /* Notify the destination socket */
    300300        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);
    304306
    305307        return EOK;
     
    320322 *                      udp_process_packet() function.
    321323 */
    322 static int udp_received_msg(device_id_t device_id, packet_t *packet,
     324static int udp_received_msg(nic_device_id_t device_id, packet_t *packet,
    323325    services_t receiver, services_t error)
    324326{
     
    337339 * @param[in]     iid   Message identifier.
    338340 * @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 */
     344static void udp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    342345{
    343346        packet_t *packet;
     
    347350                switch (IPC_GET_IMETHOD(*icall)) {
    348351                case NET_TL_RECEIVED:
    349                         rc = packet_translate_remote(udp_globals.net_phone, &packet,
     352                        rc = packet_translate_remote(udp_globals.net_sess, &packet,
    350353                            IPC_GET_PACKET(*icall));
    351354                        if (rc == EOK)
     
    365368/** Initialize the UDP module.
    366369 *
    367  * @param[in] net_phone Network module phone.
     370 * @param[in] sess Network module session.
    368371 *
    369372 * @return EOK on success.
     
    371374 *
    372375 */
    373 int tl_initialize(int net_phone)
     376int tl_initialize(async_sess_t *sess)
    374377{
    375378        measured_string_t names[] = {
     
    390393        fibril_rwlock_write_lock(&udp_globals.lock);
    391394       
    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;
    401403        }
    402404       
    403405        /* 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,
    405407            &udp_globals.packet_dimension);
    406408        if (rc != EOK) {
     
    431433        /* Get configuration */
    432434        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,
    434436            &data);
    435437        if (rc != EOK) {
     
    497499        void *ip_header;
    498500        size_t headerlen;
    499         device_id_t device_id;
     501        nic_device_id_t device_id;
    500502        packet_dimension_t *packet_dimension;
    501503        size_t size;
     
    525527
    526528        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,
    528530                    addrlen, &device_id, &ip_header, &headerlen);
    529531                if (rc != EOK)
    530532                        return rc;
    531533                /* 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,
    533535//                  &udp_globals.dimensions, device_id, &packet_dimension);
    534536//              if (rc != EOK)
     
    537539//      } else {
    538540                /* 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,
    540542                    &udp_globals.packet_dimension);
    541543                if (rc != EOK)
     
    555557
    556558        /* 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,
    558560            UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
    559561        if (result < 0)
     
    576578        /* Read the rest of the packet fragments */
    577579        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,
    579581                    &next_packet, 0, packet_dimension, addr, addrlen);
    580582                if (result < 0)
     
    615617                    htons(flip_checksum(compact_checksum(checksum)));
    616618                free(ip_header);
    617         } else {
    618                 device_id = DEVICE_INVALID_ID;
    619         }
     619        } else
     620                device_id = NIC_DEVICE_INVALID_ID;
    620621
    621622        /* Prepare the first packet fragment */
     
    628629
    629630        /* 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);
    631632
    632633        return EOK;
     
    675676                return NO_DATA;
    676677       
    677         rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id);
     678        rc = packet_translate_remote(udp_globals.net_sess, &packet, packet_id);
    678679        if (rc != EOK) {
    679680                (void) dyn_fifo_pop(&socket->received);
     
    735736}
    736737
    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 */
     751static int udp_process_client_messages(async_sess_t *sess, ipc_callid_t callid,
     752    ipc_call_t call)
    748753{
    749754        int res;
    750         bool keep_on_going = true;
    751755        socket_cores_t local_sockets;
    752         int app_phone = IPC_GET_PHONE(call);
    753756        struct sockaddr *addr;
    754757        int socket_id;
     
    773776        socket_cores_initialize(&local_sockets);
    774777
    775         while (keep_on_going) {
     778        while (true) {
    776779
    777780                /* Answer the call */
     
    785788
    786789                /* 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)) {
    790791                        res = EHANGUP;
    791792                        break;
    792 
     793                }
     794               
     795                switch (IPC_GET_IMETHOD(call)) {
    793796                case NET_SOCKET:
    794797                        socket_id = SOCKET_GET_SOCKET_ID(call);
    795                         res = socket_create(&local_sockets, app_phone, NULL,
     798                        res = socket_create(&local_sockets, sess, NULL,
    796799                            &socket_id);
    797800                        SOCKET_SET_SOCKET_ID(answer, socket_id);
     
    801804                       
    802805                        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,
    805808                            &packet_dimension) == EOK) {
    806809                                if (packet_dimension->content < size)
     
    865868                case NET_SOCKET_CLOSE:
    866869                        fibril_rwlock_write_lock(&udp_globals.lock);
    867                         res = socket_destroy(udp_globals.net_phone,
     870                        res = socket_destroy(udp_globals.net_sess,
    868871                            SOCKET_GET_SOCKET_ID(call), &local_sockets,
    869872                            &udp_globals.sockets, NULL);
     
    879882        }
    880883
    881         /* Release the application phone */
    882         async_hangup(app_phone);
     884        /* Release the application session */
     885        async_hangup(sess);
    883886
    884887        /* Release all local sockets */
    885         socket_cores_release(udp_globals.net_phone, &local_sockets,
     888        socket_cores_release(udp_globals.net_sess, &local_sockets,
    886889            &udp_globals.sockets, NULL);
    887890
     
    913916{
    914917        *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       
    921924        return ENOTSUP;
    922925}
  • uspace/srv/net/tl/udp/udp.h

    r2bdf8313 rb0f00a9  
    3838#define NET_UDP_H_
    3939
     40#include <async.h>
    4041#include <fibril_synch.h>
    4142#include <socket_core.h>
     
    4950/** UDP global data. */
    5051struct 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;
    5758        /** Packet dimension. */
    5859        packet_dimension_t packet_dimension;
Note: See TracChangeset for help on using the changeset viewer.