Changeset 609243f4 in mainline for uspace/srv/net


Ignore:
Timestamp:
2011-10-07T15:46:01Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e2c50e1
Parents:
f51b1d3
Message:

cherrypick general networking improvements from lp:~helenos-nicf/helenos/nicf (after sanitization)
remove obsolete networking drivers
this renders the networking non-functional for the time being

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

Legend:

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

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

    rf51b1d3 r609243f4  
    1 # DP8390 (NE2k) configuration
     1# NE2000 configuration
    22
    33NAME=ne2k
    44
    5 NETIF=ne2000
     5HWPATH=/hw/pci0/00:01.0/ne2000/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
    2219ARP=arp
    2320
    24 MTU=1500
     21MTU=1492
  • uspace/srv/net/il/arp/arp.c

    rf51b1d3 r609243f4  
    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(device_id_t device_id, services_t protocol,
    193     measured_string_t *address)
     186static int arp_clear_address_req(nic_device_id_t device_id,
     187    services_t protocol, measured_string_t *address)
    194188{
    195189        fibril_mutex_lock(&arp_globals.lock);
     
    218212}
    219213
    220 static int arp_clear_device_req(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,
     
    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);
     
    409403        printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu);
    410404       
     405        return EOK;
     406}
     407
     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);
    411437        return EOK;
    412438}
     
    456482                        async_answer_0(iid, (sysarg_t) rc);
    457483                        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);
    458487               
    459488                default:
     
    483512 *
    484513 */
    485 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,
    486515    services_t protocol, measured_string_t *address)
    487516{
     
    586615               
    587616                /* Get hardware address */
    588                 rc = nil_get_addr_req(device->sess, device_id, &device->addr,
    589                     &device->addr_data);
    590                 if (rc != EOK) {
     617                int len = nil_get_addr_req(device->sess, device_id, device->addr,
     618                    NIC_MAX_ADDRESS_LENGTH);
     619                if (len < 0) {
    591620                        fibril_mutex_unlock(&arp_globals.lock);
    592621                        arp_protos_destroy(&device->protos, free);
    593622                        free(device);
    594                         return rc;
    595                 }
     623                        return len;
     624                }
     625               
     626                device->addr_len = len;
    596627               
    597628                /* Get broadcast address */
    598                 rc = nil_get_broadcast_addr_req(device->sess, device_id,
    599                     &device->broadcast_addr, &device->broadcast_data);
    600                 if (rc != EOK) {
    601                         fibril_mutex_unlock(&arp_globals.lock);
    602                         free(device->addr);
    603                         free(device->addr_data);
     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);
    604633                        arp_protos_destroy(&device->protos, free);
    605634                        free(device);
    606                         return rc;
    607                 }
     635                        return len;
     636                }
     637               
     638                device->broadcast_addr_len = len;
    608639               
    609640                rc = arp_cache_add(&arp_globals.cache, device->device_id,
     
    611642                if (rc != EOK) {
    612643                        fibril_mutex_unlock(&arp_globals.lock);
    613                         free(device->addr);
    614                         free(device->addr_data);
    615                         free(device->broadcast_addr);
    616                         free(device->broadcast_data);
    617644                        arp_protos_destroy(&device->protos, free);
    618645                        free(device);
     
    640667}
    641668
    642 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,
    643670    measured_string_t *target, arp_device_t *device, arp_proto_t *proto)
    644671{
    645672        /* ARP packet content size = header + (address + translation) * 2 */
    646         size_t length = 8 + 2 * (proto->addr->length + device->addr->length);
     673        size_t length = 8 + 2 * (proto->addr->length + device->addr_len);
    647674        if (length > device->packet_dimension.content)
    648675                return ELIMIT;
     
    661688       
    662689        header->hardware = htons(device->hardware);
    663         header->hardware_length = (uint8_t) device->addr->length;
     690        header->hardware_length = (uint8_t) device->addr_len;
    664691        header->protocol = htons(protocol_map(device->service, protocol));
    665692        header->protocol_length = (uint8_t) proto->addr->length;
     
    667694       
    668695        length = sizeof(arp_header_t);
    669        
    670         memcpy(((uint8_t *) header) + length, device->addr->value,
    671             device->addr->length);
    672         length += device->addr->length;
     696        memcpy(((uint8_t *) header) + length, device->addr,
     697            device->addr_len);
     698        length += device->addr_len;
    673699        memcpy(((uint8_t *) header) + length, proto->addr->value,
    674700            proto->addr->length);
    675701        length += proto->addr->length;
    676         bzero(((uint8_t *) header) + length, device->addr->length);
    677         length += device->addr->length;
     702        bzero(((uint8_t *) header) + length, device->addr_len);
     703        length += device->addr_len;
    678704        memcpy(((uint8_t *) header) + length, target->value, target->length);
    679        
    680         int rc = packet_set_addr(packet, (uint8_t *) device->addr->value,
    681             (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);
    682708        if (rc != EOK) {
    683709                pq_release_remote(arp_globals.net_sess, packet_get_id(packet));
     
    704730 *
    705731 */
    706 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,
    707733    measured_string_t *target, measured_string_t **translation)
    708734{
  • uspace/srv/net/il/arp/arp.h

    rf51b1d3 r609243f4  
    9292struct arp_device {
    9393        /** Actual device hardware address. */
    94         measured_string_t *addr;
    95         /** Actual device hardware address data. */
    96         uint8_t *addr_data;
     94        uint8_t addr[NIC_MAX_ADDRESS_LENGTH];
     95        /** Actual device hardware address length. */
     96        size_t addr_len;
    9797        /** Broadcast device hardware address. */
    98         measured_string_t *broadcast_addr;
    99         /** Broadcast device hardware address data. */
    100         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;
    101101        /** Device identifier. */
    102         device_id_t device_id;
     102        nic_device_id_t device_id;
    103103        /** Hardware type. */
    104104        hw_type_t hardware;
  • uspace/srv/net/il/ip/ip.c

    rf51b1d3 r609243f4  
    482482}
    483483
    484 static int ip_device_req_local(device_id_t device_id, services_t netif)
     484static int ip_device_req_local(nic_device_id_t device_id, services_t netif)
    485485{
    486486        ip_netif_t *ip_netif;
     
    501501        ip_netif->device_id = device_id;
    502502        ip_netif->service = netif;
    503         ip_netif->state = NETIF_STOPPED;
     503        ip_netif->state = NIC_STATE_STOPPED;
    504504
    505505        fibril_rwlock_write_lock(&ip_globals.netifs_lock);
     
    594594        while (index >= 0) {
    595595                netif = ip_netifs_get_index(&ip_globals.netifs, index);
    596                 if (netif && (netif->state == NETIF_ACTIVE)) {
     596                if (netif && (netif->state == NIC_STATE_ACTIVE)) {
    597597                        route = ip_netif_find_route(netif, destination);
    598598                        if (route)
     
    11421142}
    11431143
    1144 static int ip_send_msg_local(device_id_t device_id, packet_t *packet,
     1144static int ip_send_msg_local(nic_device_id_t device_id, packet_t *packet,
    11451145    services_t sender, services_t error)
    11461146{
     
    12581258 * @return              ENOENT if device is not found.
    12591259 */
    1260 static int ip_device_state_message(device_id_t device_id, device_state_t state)
     1260static int ip_device_state_message(nic_device_id_t device_id,
     1261    nic_device_state_t state)
    12611262{
    12621263        ip_netif_t *netif;
     
    12721273        fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    12731274
    1274         printf("%s: Device %d changed state to %d\n", NAME, device_id, state);
     1275        printf("%s: Device %d changed state to '%s'\n", NAME, device_id,
     1276            nic_device_state_to_string(state));
    12751277
    12761278        return EOK;
     
    13121314 *                      tl_received_msg() function.
    13131315 */
    1314 static int ip_deliver_local(device_id_t device_id, packet_t *packet,
     1316static int ip_deliver_local(nic_device_id_t device_id, packet_t *packet,
    13151317    ip_header_t *header, services_t error)
    13161318{
     
    14131415 *                      is disabled.
    14141416 */
    1415 static int ip_process_packet(device_id_t device_id, packet_t *packet)
     1417static int ip_process_packet(nic_device_id_t device_id, packet_t *packet)
    14161418{
    14171419        ip_header_t *header;
     
    15141516 *
    15151517 */
    1516 static int ip_packet_size_message(device_id_t device_id, size_t *addr_len,
     1518static int ip_packet_size_message(nic_device_id_t device_id, size_t *addr_len,
    15171519    size_t *prefix, size_t *content, size_t *suffix)
    15181520{
     
    15721574 * @return              ENOENT if device is not found.
    15731575 */
    1574 static int ip_mtu_changed_message(device_id_t device_id, size_t mtu)
     1576static int ip_mtu_changed_message(nic_device_id_t device_id, size_t mtu)
    15751577{
    15761578        ip_netif_t *netif;
     
    16291631                        async_answer_0(iid, (sysarg_t) rc);
    16301632                        break;
    1631                
     1633                case NET_IL_ADDR_CHANGED:
     1634                        async_answer_0(iid, (sysarg_t) EOK);
     1635                        break;
     1636
    16321637                default:
    16331638                        async_answer_0(iid, (sysarg_t) ENOTSUP);
     
    16891694}
    16901695
    1691 static int ip_add_route_req_local(device_id_t device_id, in_addr_t address,
     1696static int ip_add_route_req_local(nic_device_id_t device_id, in_addr_t address,
    16921697    in_addr_t netmask, in_addr_t gateway)
    16931698{
     
    17231728}
    17241729
    1725 static int ip_set_gateway_req_local(device_id_t device_id, in_addr_t gateway)
     1730static int ip_set_gateway_req_local(nic_device_id_t device_id,
     1731    in_addr_t gateway)
    17261732{
    17271733        ip_netif_t *netif;
     
    17571763 *
    17581764 */
    1759 static int ip_received_error_msg_local(device_id_t device_id,
     1765static int ip_received_error_msg_local(nic_device_id_t device_id,
    17601766    packet_t *packet, services_t target, services_t error)
    17611767{
     
    18181824static int ip_get_route_req_local(ip_protocol_t protocol,
    18191825    const struct sockaddr *destination, socklen_t addrlen,
    1820     device_id_t *device_id, void **header, size_t *headerlen)
     1826    nic_device_id_t *device_id, void **header, size_t *headerlen)
    18211827{
    18221828        struct sockaddr_in *address_in;
     
    19091915        size_t suffix;
    19101916        size_t content;
    1911         device_id_t device_id;
     1917        nic_device_id_t device_id;
    19121918        int rc;
    19131919       
  • uspace/srv/net/il/ip/ip.h

    rf51b1d3 r609243f4  
    9292        in_addr_t broadcast;
    9393        /** Device identifier. */
    94         device_id_t device_id;
     94        nic_device_id_t device_id;
    9595        /** Indicates whether using DHCP. */
    9696        int dhcp;
     
    108108        services_t service;
    109109        /** Device state. */
    110         device_state_t state;
     110        nic_device_state_t state;
    111111};
    112112
  • uspace/srv/net/net/Makefile

    rf51b1d3 r609243f4  
    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        net_standalone.c \
     46        packet_server.c
    4647
    4748include $(USPACE_PREFIX)/Makefile.common
  • uspace/srv/net/net/net.c

    rf51b1d3 r609243f4  
    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 <ctype.h>
    4042#include <ddi.h>
    4143#include <errno.h>
     44#include <str_error.h>
    4245#include <malloc.h>
    4346#include <stdio.h>
    4447#include <str.h>
     48#include <devman.h>
    4549#include <str_error.h>
    4650#include <ns.h>
     
    4953#include <ipc/net_net.h>
    5054#include <ipc/il.h>
     55#include <ipc/ip.h>
    5156#include <ipc/nil.h>
    5257#include <net/modules.h>
     
    5762#include <adt/measured_strings.h>
    5863#include <adt/module_map.h>
    59 #include <netif_remote.h>
    6064#include <nil_remote.h>
    6165#include <net_interface.h>
    6266#include <ip_interface.h>
     67#include <device/nic.h>
     68#include <dirent.h>
     69#include <fcntl.h>
     70#include <cfg.h>
    6371#include "net.h"
     72#include "packet_server.h"
    6473
    6574/** Networking module name. */
    6675#define NAME  "net"
    6776
    68 /** File read buffer size. */
    69 #define BUFFER_SIZE  256
     77#define MAX_PATH_LENGTH  1024
    7078
    7179/** Networking module global data. */
     
    109117/** Generate new system-unique device identifier.
    110118 *
    111  * @return              The system-unique devic identifier.
    112  */
    113 static device_id_t generate_new_device_id(void)
     119 * @return The system-unique devic identifier.
     120 *
     121 */
     122static nic_device_id_t generate_new_device_id(void)
    114123{
    115124        return device_assign_devno();
    116 }
    117 
    118 static int parse_line(measured_strings_t *configuration, uint8_t *line)
    119 {
    120         int rc;
    121        
    122         /* From the beginning */
    123         uint8_t *name = line;
    124        
    125         /* Skip comments and blank lines */
    126         if ((*name == '#') || (*name == '\0'))
    127                 return EOK;
    128        
    129         /* Skip spaces */
    130         while (isspace(*name))
    131                 name++;
    132        
    133         /* Remember the name start */
    134         uint8_t *value = name;
    135        
    136         /* Skip the name */
    137         while (isalnum(*value) || (*value == '_'))
    138                 value++;
    139        
    140         if (*value == '=') {
    141                 /* Terminate the name */
    142                 *value = '\0';
    143         } else {
    144                 /* Terminate the name */
    145                 *value = '\0';
    146                
    147                 /* Skip until '=' */
    148                 value++;
    149                 while ((*value) && (*value != '='))
    150                         value++;
    151                
    152                 /* Not found? */
    153                 if (*value != '=')
    154                         return EINVAL;
    155         }
    156        
    157         value++;
    158        
    159         /* Skip spaces */
    160         while (isspace(*value))
    161                 value++;
    162        
    163         /* Create a bulk measured string till the end */
    164         measured_string_t *setting =
    165             measured_string_create_bulk(value, 0);
    166         if (!setting)
    167                 return ENOMEM;
    168        
    169         /* Add the configuration setting */
    170         rc = measured_strings_add(configuration, name, 0, setting);
    171         if (rc != EOK) {
    172                 free(setting);
    173                 return rc;
    174         }
    175        
    176         return EOK;
    177125}
    178126
     
    182130        printf("%s: Reading configuration file %s/%s\n", NAME, directory, filename);
    183131       
    184         /* Construct the full filename */
    185         char fname[BUFFER_SIZE];
    186         if (snprintf(fname, BUFFER_SIZE, "%s/%s", directory, filename) > BUFFER_SIZE)
    187                 return EOVERFLOW;
    188        
    189         /* Open the file */
    190         FILE *cfg = fopen(fname, "r");
    191         if (!cfg)
     132        cfg_file_t cfg;
     133        int rc = cfg_load_path(directory, filename, &cfg);
     134        if (rc != EOK)
     135                return rc;
     136       
     137        if (cfg_anonymous(&cfg) == NULL) {
     138                cfg_unload(&cfg);
    192139                return ENOENT;
    193        
    194         /*
    195          * Read the configuration line by line
    196          * until an error or the end of file
    197          */
    198         unsigned int line_number = 0;
    199         size_t index = 0;
    200         uint8_t line[BUFFER_SIZE];
    201        
    202         while (!ferror(cfg) && !feof(cfg)) {
    203                 int read = fgetc(cfg);
    204                 if ((read > 0) && (read != '\n') && (read != '\r')) {
    205                         if (index >= BUFFER_SIZE) {
    206                                 line[BUFFER_SIZE - 1] = '\0';
    207                                 fprintf(stderr, "%s: Configuration line %u too "
    208                                     "long: %s\n", NAME, line_number, (char *) line);
    209                                
    210                                 /* No space left in the line buffer */
    211                                 return EOVERFLOW;
    212                         }
    213                         /* Append the character */
    214                         line[index] = (uint8_t) read;
    215                         index++;
    216                 } else {
    217                         /* On error or new line */
    218                         line[index] = '\0';
    219                         line_number++;
    220                         if (parse_line(configuration, line) != EOK) {
    221                                 fprintf(stderr, "%s: Configuration error on "
    222                                     "line %u: %s\n", NAME, line_number, (char *) line);
    223                         }
    224                        
    225                         index = 0;
    226                 }
    227         }
    228        
    229         fclose(cfg);
     140        }
     141       
     142        cfg_section_foreach(cfg_anonymous(&cfg), link) {
     143                const cfg_entry_t *entry = cfg_entry_instance(link);
     144               
     145                rc = add_configuration(configuration,
     146                    (uint8_t *) entry->key, (uint8_t *) entry->value);
     147                if (rc != EOK) {
     148                        printf("%s: Error processing configuration\n", NAME);
     149                        cfg_unload(&cfg);
     150                        return rc;
     151                }
     152        }
     153       
     154        cfg_unload(&cfg);
    230155        return EOK;
    231156}
     
    272197       
    273198        netifs_initialize(&net_globals.netifs);
    274         char_map_initialize(&net_globals.netif_names);
     199        char_map_initialize(&net_globals.netif_hwpaths);
    275200        modules_initialize(&net_globals.modules);
    276201        measured_strings_initialize(&net_globals.configuration);
    277202       
    278         /* TODO: dynamic configuration */
    279203        rc = read_configuration();
    280         if (rc != EOK)
    281                 return rc;
    282        
    283         rc = add_module(NULL, &net_globals.modules, (uint8_t *) LO_NAME,
    284             (uint8_t *) LO_FILENAME, SERVICE_LO, 0, connect_to_service);
    285         if (rc != EOK)
    286                 return rc;
    287        
    288         rc = add_module(NULL, &net_globals.modules, (uint8_t *) NE2000_NAME,
    289             (uint8_t *) NE2000_FILENAME, SERVICE_NE2000, 0, connect_to_service);
    290204        if (rc != EOK)
    291205                return rc;
     
    331245                return rc;
    332246       
     247        rc = packet_server_init();
     248        if (rc != EOK)
     249                goto out;
     250       
    333251        rc = net_initialize(client_connection);
    334252        if (rc != EOK)
    335253                goto out;
    336254       
     255        rc = startup();
     256        if (rc != EOK)
     257                goto out;
     258       
    337259        rc = service_register(SERVICE_NETWORKING);
    338         if (rc != EOK)
    339                 goto out;
    340        
    341         rc = startup();
    342260        if (rc != EOK)
    343261                goto out;
     
    364282 */
    365283static int net_get_conf(measured_strings_t *netif_conf,
    366     measured_string_t *configuration, size_t count, uint8_t **data)
    367 {
    368         if (data)
    369                 *data = NULL;
     284    measured_string_t *configuration, size_t count)
     285{
     286        if ((!configuration) || (count <= 0))
     287                        return EINVAL;
    370288       
    371289        size_t index;
     
    389307}
    390308
    391 static int net_get_conf_req_local(measured_string_t **configuration,
    392     size_t count, uint8_t **data)
    393 {
    394         if (!configuration || (count <= 0))
    395                 return EINVAL;
    396        
    397         return net_get_conf(NULL, *configuration, count, data);
    398 }
    399 
    400 static int net_get_device_conf_req_local(device_id_t device_id,
    401     measured_string_t **configuration, size_t count, uint8_t **data)
    402 {
    403         if ((!configuration) || (count == 0))
    404                 return EINVAL;
    405 
     309static int net_get_device_conf(nic_device_id_t device_id,
     310    measured_string_t *configuration, size_t count)
     311{
    406312        netif_t *netif = netifs_find(&net_globals.netifs, device_id);
    407313        if (netif)
    408                 return net_get_conf(&netif->configuration, *configuration, count, data);
     314                return net_get_conf(&netif->configuration, configuration, count);
    409315        else
    410                 return net_get_conf(NULL, *configuration, count, data);
    411 }
    412 
    413 void net_free_settings(measured_string_t *settings, uint8_t *data)
    414 {
     316                return net_get_conf(NULL, configuration, count);
     317}
     318
     319static int net_get_devices(measured_string_t **devices, size_t *dev_count)
     320{
     321        if (!devices)
     322                return EBADMEM;
     323       
     324        size_t max_count = netifs_count(&net_globals.netifs);
     325        *devices = malloc(max_count * sizeof(measured_string_t));
     326        if (*devices == NULL)
     327                return ENOMEM;
     328       
     329        size_t count = 0;
     330        for (size_t i = 0; i < max_count; i++) {
     331                netif_t *item = netifs_get_index(&net_globals.netifs, i);
     332                if (item->sess != NULL) {
     333                        /*
     334                         * Use format "device_id:device_name"
     335                         * FIXME: This typecasting looks really ugly
     336                         */
     337                        (*devices)[count].length = asprintf(
     338                            (char **) &((*devices)[count].value),
     339                            NIC_DEVICE_PRINT_FMT ":%s", item->id,
     340                            (const char *) item->name);
     341                        count++;
     342                }
     343        }
     344       
     345        *dev_count = (size_t) count;
     346        return EOK;
     347}
     348
     349static int net_get_devices_count()
     350{
     351        size_t max_count = netifs_count(&net_globals.netifs);
     352       
     353        size_t count = 0;
     354        for (size_t i = 0; i < max_count; i++) {
     355                netif_t *item = netifs_get_index(&net_globals.netifs, i);
     356                if (item->sess != NULL)
     357                        count++;
     358        }
     359       
     360        return count;
     361}
     362
     363static void net_free_devices(measured_string_t *devices, size_t count)
     364{
     365        size_t i;
     366        for (i = 0; i < count; ++i)
     367                free(devices[i].value);
     368       
     369        free(devices);
    415370}
    416371
     
    431386 *
    432387 */
    433 static int start_device(netif_t *netif)
    434 {
    435         int rc;
    436        
    437         /* Mandatory netif */
    438         measured_string_t *setting =
    439             measured_strings_find(&netif->configuration, (uint8_t *) CONF_NETIF, 0);
    440        
    441         netif->driver = get_running_module(&net_globals.modules, setting->value);
    442         if (!netif->driver) {
    443                 fprintf(stderr, "%s: Failed to start network interface driver '%s'\n",
     388static int init_device(netif_t *netif, devman_handle_t handle)
     389{
     390        netif->handle = handle;
     391        netif->sess = devman_device_connect(EXCHANGE_SERIALIZE, netif->handle,
     392            IPC_FLAG_BLOCKING);
     393        if (netif->sess == NULL) {
     394                printf("%s: Unable to connect to device\n", NAME);
     395                return EREFUSED;
     396        }
     397       
     398        /* Optional network interface layer */
     399        measured_string_t *setting = measured_strings_find(&netif->configuration,
     400            (uint8_t *) CONF_NIL, 0);
     401        if (setting) {
     402                netif->nil = get_running_module(&net_globals.modules,
     403                    setting->value);
     404                if (!netif->nil) {
     405                        printf("%s: Unable to connect to network interface layer '%s'\n",
     406                            NAME, setting->value);
     407                        return EINVAL;
     408                }
     409        } else
     410                netif->nil = NULL;
     411       
     412        /* Mandatory internet layer */
     413        setting = measured_strings_find(&netif->configuration,
     414            (uint8_t *) CONF_IL, 0);
     415        netif->il = get_running_module(&net_globals.modules,
     416            setting->value);
     417        if (!netif->il) {
     418                printf("%s: Unable to connect to internet layer '%s'\n",
    444419                    NAME, setting->value);
    445420                return EINVAL;
    446421        }
    447422       
    448         /* Optional network interface layer */
    449         setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_NIL, 0);
    450         if (setting) {
    451                 netif->nil = get_running_module(&net_globals.modules, setting->value);
    452                 if (!netif->nil) {
    453                         fprintf(stderr, "%s: Failed to start network interface layer '%s'\n",
    454                             NAME, setting->value);
    455                         return EINVAL;
    456                 }
    457         } else
    458                 netif->nil = NULL;
    459        
    460         /* Mandatory internet layer */
    461         setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IL, 0);
    462         netif->il = get_running_module(&net_globals.modules, setting->value);
    463         if (!netif->il) {
    464                 fprintf(stderr, "%s: Failed to start internet layer '%s'\n",
    465                     NAME, setting->value);
    466                 return EINVAL;
    467         }
    468        
    469         /* Hardware configuration */
    470         setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IRQ, 0);
    471         int irq = setting ? strtol((char *) setting->value, NULL, 10) : 0;
    472        
    473         setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IO, 0);
    474         uintptr_t io = setting ? strtol((char *) setting->value, NULL, 16) : 0;
    475        
    476         rc = netif_probe_req(netif->driver->sess, netif->id, irq, (void *) io);
    477         if (rc != EOK)
    478                 return rc;
    479        
    480423        /* Network interface layer startup */
    481         services_t internet_service;
     424        int rc;
     425        services_t nil_service;
    482426        if (netif->nil) {
    483                 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_MTU, 0);
     427                setting = measured_strings_find(&netif->configuration,
     428                    (uint8_t *) CONF_MTU, 0);
    484429                if (!setting)
    485430                        setting = measured_strings_find(&net_globals.configuration,
    486431                            (uint8_t *) CONF_MTU, 0);
    487432               
    488                 int mtu = setting ? strtol((char *) setting->value, NULL, 10) : 0;
    489                 rc = nil_device_req(netif->nil->sess, netif->id, mtu,
    490                     netif->driver->service);
     433                int mtu = setting ?
     434                    strtol((const char *) setting->value, NULL, 10) : 0;
     435                rc = nil_device_req(netif->nil->sess, netif->id,
     436                    netif->handle, mtu);
     437                if (rc != EOK) {
     438                        printf("%s: Unable to start network interface layer\n",
     439                            NAME);
     440                        return rc;
     441                }
     442               
     443                nil_service = netif->nil->service;
     444        } else
     445                nil_service = -1;
     446       
     447        /* Inter-network layer startup */
     448        switch (netif->il->service) {
     449        case SERVICE_IP:
     450                rc = ip_device_req(netif->il->sess, netif->id, nil_service);
     451                if (rc != EOK) {
     452                        printf("%s: Unable to start internet layer\n", NAME);
     453                        return rc;
     454                }
     455               
     456                break;
     457        default:
     458                return ENOENT;
     459        }
     460       
     461        return nic_set_state(netif->sess, NIC_STATE_ACTIVE);
     462}
     463
     464static int net_driver_port_ready(devman_handle_t handle)
     465{
     466        char hwpath[MAX_PATH_LENGTH];
     467        int rc = devman_fun_get_path(handle, hwpath, MAX_PATH_LENGTH);
     468        if (rc != EOK)
     469                return EINVAL;
     470       
     471        int index = char_map_find(&net_globals.netif_hwpaths,
     472            (uint8_t *) hwpath, 0);
     473        if (index == CHAR_MAP_NULL)
     474                return ENOENT;
     475       
     476        netif_t *netif = netifs_get_index(&net_globals.netifs, index);
     477        if (netif == NULL)
     478                return ENOENT;
     479       
     480        rc = init_device(netif, handle);
     481        if (rc != EOK)
     482                return rc;
     483       
     484        /* Increment module usage */
     485        if (netif->nil)
     486                netif->nil->usage++;
     487       
     488        netif->il->usage++;
     489       
     490        return EOK;
     491}
     492
     493static int net_driver_ready_local(devman_handle_t handle)
     494{
     495        devman_handle_t *funs;
     496        size_t count;
     497        int rc = devman_dev_get_functions(handle, &funs, &count);
     498        if (rc != EOK)
     499                return rc;
     500       
     501        for (size_t i = 0; i < count; i++) {
     502                rc = net_driver_port_ready(funs[i]);
    491503                if (rc != EOK)
    492504                        return rc;
    493                
    494                 internet_service = netif->nil->service;
    495         } else
    496                 internet_service = netif->driver->service;
    497        
    498         /* Inter-network layer startup */
    499         rc = ip_device_req(netif->il->sess, netif->id, internet_service);
    500         if (rc != EOK)
    501                 return rc;
    502        
    503         return netif_start_req(netif->driver->sess, netif->id);
     505        }
     506       
     507        return EOK;
    504508}
    505509
     
    519523static int startup(void)
    520524{
    521         const char *conf_files[] = {
    522                 "lo",
    523                 "ne2k"
    524         };
    525         size_t count = sizeof(conf_files) / sizeof(char *);
    526525        int rc;
    527526       
    528         size_t i;
    529         for (i = 0; i < count; i++) {
     527        DIR *config_dir = opendir(CONF_DIR);
     528        if (config_dir == NULL)
     529                return ENOENT;
     530       
     531        struct dirent *dir_entry;
     532        while ((dir_entry = readdir(config_dir))) {
     533                if (str_cmp(dir_entry->d_name + str_length(dir_entry->d_name)
     534                        - str_length(CONF_EXT), CONF_EXT) != 0)
     535                        continue;
     536               
    530537                netif_t *netif = (netif_t *) malloc(sizeof(netif_t));
    531538                if (!netif)
    532                         return ENOMEM;
     539                        continue;
     540               
     541                netif->handle = -1;
     542                netif->sess = NULL;
    533543               
    534544                netif->id = generate_new_device_id();
    535                 if (!netif->id)
    536                         return EXDEV;
     545                if (!netif->id) {
     546                        free(netif);
     547                        continue;
     548                }
    537549               
    538550                rc = measured_strings_initialize(&netif->configuration);
    539                 if (rc != EOK)
    540                         return rc;
    541                
    542                 /* Read configuration files */
    543                 rc = read_netif_configuration(conf_files[i], netif);
    544551                if (rc != EOK) {
     552                        free(netif);
     553                        continue;
     554                }
     555               
     556                rc = read_netif_configuration(dir_entry->d_name, netif);
     557                if (rc != EOK) {
     558                        free(netif);
     559                        continue;
     560                }
     561               
     562                /* Mandatory name */
     563                measured_string_t *name = measured_strings_find(&netif->configuration,
     564                    (uint8_t *) CONF_NAME, 0);
     565                if (!name) {
     566                        printf("%s: Network interface name is missing\n", NAME);
    545567                        measured_strings_destroy(&netif->configuration, free);
    546568                        free(netif);
    547                         return rc;
    548                 }
    549                
    550                 /* Mandatory name */
    551                 measured_string_t *setting =
    552                     measured_strings_find(&netif->configuration, (uint8_t *) CONF_NAME, 0);
    553                 if (!setting) {
    554                         fprintf(stderr, "%s: Network interface name is missing\n", NAME);
     569                        continue;
     570                }
     571               
     572                netif->name = name->value;
     573               
     574                /* Mandatory hardware path */
     575                measured_string_t *hwpath = measured_strings_find(
     576                    &netif->configuration, (const uint8_t *) CONF_HWPATH, 0);
     577                if (!hwpath) {
    555578                        measured_strings_destroy(&netif->configuration, free);
    556579                        free(netif);
    557                         return EINVAL;
    558                 }
    559                 netif->name = setting->value;
     580                }
    560581               
    561582                /* Add to the netifs map */
     
    564585                        measured_strings_destroy(&netif->configuration, free);
    565586                        free(netif);
    566                         return index;
     587                        continue;
    567588                }
    568589               
    569590                /*
    570                  * Add to the netif names map and start network interfaces
     591                 * Add to the hardware paths map and init network interfaces
    571592                 * and needed modules.
    572593                 */
    573                 rc = char_map_add(&net_globals.netif_names, netif->name, 0,
    574                     index);
     594                rc = char_map_add(&net_globals.netif_hwpaths, hwpath->value, 0, index);
    575595                if (rc != EOK) {
    576596                        measured_strings_destroy(&netif->configuration, free);
    577597                        netifs_exclude_index(&net_globals.netifs, index, free);
    578                         return rc;
    579                 }
    580                
    581                 rc = start_device(netif);
    582                 if (rc != EOK) {
    583                         printf("%s: Ignoring failed interface %s (%s)\n", NAME,
    584                             netif->name, str_error(rc));
    585                         measured_strings_destroy(&netif->configuration, free);
    586                         netifs_exclude_index(&net_globals.netifs, index, free);
    587598                        continue;
    588599                }
    589                
    590                 /* Increment modules' usage */
    591                 netif->driver->usage++;
    592                 if (netif->nil)
    593                         netif->nil->usage++;
    594                 netif->il->usage++;
    595                
    596                 printf("%s: Network interface started (name: %s, id: %d, driver: %s, "
    597                     "nil: %s, il: %s)\n", NAME, netif->name, netif->id,
    598                     netif->driver->name, netif->nil ? (char *) netif->nil->name : "[none]",
    599                     netif->il->name);
    600         }
    601        
     600        }
     601       
     602        closedir(config_dir);
    602603        return EOK;
    603604}
     
    624625        uint8_t *data;
    625626        int rc;
     627        size_t count;
    626628       
    627629        *answer_count = 0;
     
    636638                if (rc != EOK)
    637639                        return rc;
    638                 net_get_device_conf_req_local(IPC_GET_DEVICE(*call), &strings,
    639                     IPC_GET_COUNT(*call), NULL);
    640                
    641                 /* Strings should not contain received data anymore */
    642                 free(data);
     640               
     641                net_get_device_conf(IPC_GET_DEVICE(*call), strings,
     642                    IPC_GET_COUNT(*call));
    643643               
    644644                rc = measured_strings_reply(strings, IPC_GET_COUNT(*call));
    645645                free(strings);
     646                free(data);
    646647                return rc;
    647648        case NET_NET_GET_CONF:
     
    650651                if (rc != EOK)
    651652                        return rc;
    652                 net_get_conf_req_local(&strings, IPC_GET_COUNT(*call), NULL);
    653                
    654                 /* Strings should not contain received data anymore */
    655                 free(data);
     653               
     654                net_get_conf(NULL, strings, IPC_GET_COUNT(*call));
    656655               
    657656                rc = measured_strings_reply(strings, IPC_GET_COUNT(*call));
    658657                free(strings);
    659                 return rc;
    660         case NET_NET_STARTUP:
    661                 return startup();
    662         }
    663        
    664         return ENOTSUP;
     658                free(data);
     659                return rc;
     660        case NET_NET_GET_DEVICES_COUNT:
     661                count = (size_t) net_get_devices_count();
     662                IPC_SET_ARG1(*answer, count);
     663                *answer_count = 1;
     664                return EOK;
     665        case NET_NET_GET_DEVICES:
     666                rc = net_get_devices(&strings, &count);
     667                if (rc != EOK)
     668                        return rc;
     669               
     670                rc = measured_strings_reply(strings, count);
     671                net_free_devices(strings, count);
     672                return rc;
     673        case NET_NET_DRIVER_READY:
     674                rc = net_driver_ready_local(IPC_GET_ARG1(*call));
     675                *answer_count = 0;
     676                return rc;
     677        default:
     678                return ENOTSUP;
     679        }
    665680}
    666681
  • uspace/srv/net/net/net.h

    rf51b1d3 r609243f4  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    4546#include <adt/module_map.h>
    4647#include <net/packet.h>
     48#include <devman.h>
    4749
    4850/** @name Modules definitions
    4951 * @{
    5052 */
    51 
    52 #define NE2000_FILENAME  "/srv/ne2000"
    53 #define NE2000_NAME      "ne2000"
    5453
    5554#define ETHERNET_FILENAME  "/srv/eth"
     
    5857#define IP_FILENAME  "/srv/ip"
    5958#define IP_NAME      "ip"
    60 
    61 #define LO_FILENAME  "/srv/lo"
    62 #define LO_NAME      "lo"
    6359
    6460#define NILDUMMY_FILENAME  "/srv/nildummy"
     
    7773#define CONF_MTU    "MTU"    /**< Maximum transmission unit configuration label. */
    7874#define CONF_NAME   "NAME"   /**< Network interface name configuration label. */
    79 #define CONF_NETIF  "NETIF"  /**< Network interface module name configuration label. */
     75#define CONF_HWPATH "HWPATH" /**< Network interface hardware pathname label. */
    8076#define CONF_NIL    "NIL"    /**< Network interface layer module name configuration label. */
    8177
     
    8581#define CONF_DIR           "/cfg/net"  /**< Configuration directory. */
    8682#define CONF_GENERAL_FILE  "general"   /**< General configuration file. */
     83#define CONF_EXT           ".nic"      /**< Extension for NIC's configuration files. */
    8784
    8885/** Configuration settings.
     
    9895 */
    9996typedef struct {
     97        uint8_t *name;                          /**< System-unique network interface name. */
     98        nic_device_id_t id;             /**< System-unique network interface identifier. */
    10099        measured_strings_t configuration;  /**< Configuration. */
    101100       
    102101        /** Serving network interface driver module index. */
    103         module_t *driver;
     102        devman_handle_t handle;         /**< Handle for devman */
     103        async_sess_t *sess;                     /**< Driver session. */
    104104       
    105         device_id_t id;  /**< System-unique network interface identifier. */
     105        module_t *nil;   /**< Serving link layer module index. */
    106106        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. */
    109107} netif_t;
    110108
     
    124122        modules_t modules;                 /**< Available modules. */
    125123       
    126         /** Network interface structure indices by names. */
    127         char_map_t netif_names;
     124        /** Network interface structure indices by hardware path. */
     125        char_map_t netif_hwpaths;
    128126       
    129127        /** Present network interfaces. */
  • uspace/srv/net/net/net_standalone.c

    rf51b1d3 r609243f4  
    3535 */
    3636
    37 #include "net.h"
    38 
    3937#include <str.h>
    4038#include <adt/measured_strings.h>
     
    4240#include <ipc/net.h>
    4341#include <errno.h>
    44 
    4542#include <ip_interface.h>
    46 #include <packet_server.h>
     43#include "net.h"
     44#include "packet_server.h"
    4745
    4846/** Networking module global data. */
     
    6260        int rc;
    6361       
    64         task_id_t task_id = net_spawn((uint8_t *) "/srv/ip");
     62        task_id_t task_id = net_spawn((uint8_t *) IP_FILENAME);
    6563        if (!task_id)
    6664                return EINVAL;
  • uspace/srv/net/net/packet_server.c

    rf51b1d3 r609243f4  
    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>
    3838#include <align.h>
    3939#include <assert.h>
    4040#include <async.h>
    4141#include <errno.h>
     42#include <str_error.h>
     43#include <stdio.h>
    4244#include <fibril_synch.h>
    4345#include <unistd.h>
     
    4850#include <net/packet_header.h>
    4951
     52#include "packet_server.h"
     53
     54#define PACKET_SERVER_PROFILE 1
     55
     56/** Number of queues cacheing the unused packets */
    5057#define FREE_QUEUES_COUNT       7
     58/** Maximum number of packets in each queue */
     59#define FREE_QUEUE_MAX_LENGTH   16
    5160
    5261/** The default address length reserved for new packets. */
     
    5867/** The default suffix reserved for new packets. */
    5968#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;
    6076
    6177/** Packet server global data. */
     
    6480        fibril_mutex_t lock;
    6581        /** Free packet queues. */
    66         packet_t *free[FREE_QUEUES_COUNT];
    67        
    68         /**
    69          * Packet length upper bounds of the free packet queues. The maximal
    70          * lengths of packets in each queue in the ascending order. The last
    71          * queue is not limited.
    72          */
    73         size_t sizes[FREE_QUEUES_COUNT];
     82        packet_queue_t free_queues[FREE_QUEUES_COUNT];
    7483       
    7584        /** Total packets allocated. */
    76         unsigned int count;
     85        packet_id_t next_id;
    7786} ps_globals = {
    7887        .lock = FIBRIL_MUTEX_INITIALIZER(ps_globals.lock),
    79         .free = {
    80                 NULL,
    81                 NULL,
    82                 NULL,
    83                 NULL,
    84                 NULL,
    85                 NULL,
    86                 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},
    8796        },
    88         .sizes = {
    89                 PAGE_SIZE,
    90                 PAGE_SIZE * 2,
    91                 PAGE_SIZE * 4,
    92                 PAGE_SIZE * 8,
    93                 PAGE_SIZE * 16,
    94                 PAGE_SIZE * 32,
    95                 PAGE_SIZE * 64
    96         },
    97         .count = 0
     97        .next_id = 1
    9898};
    9999
     
    107107 * @param[in] max_suffix The maximal suffix length in bytes.
    108108 */
    109 static void
    110 packet_init(packet_t *packet, size_t addr_len, size_t max_prefix,
    111     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)
    112111{
    113112        /* Clear the packet content */
     
    120119        packet->previous = 0;
    121120        packet->next = 0;
     121        packet->offload_info = 0;
     122        packet->offload_mask = 0;
    122123        packet->addr_len = 0;
    123124        packet->src_addr = sizeof(packet_t);
     
    127128        packet->data_start = packet->dest_addr + addr_len + packet->max_prefix;
    128129        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);
    129141}
    130142
     
    141153 * @return              NULL if there is not enough memory left.
    142154 */
    143 static packet_t *
    144 packet_create(size_t length, size_t addr_len, size_t max_prefix,
    145     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)
    146157{
    147158        packet_t *packet;
    148159        int rc;
    149160
     161        /* Global lock is locked */
    150162        assert(fibril_mutex_is_locked(&ps_globals.lock));
    151 
    152         /* Already locked */
     163        /* The length is some multiple of PAGE_SIZE */
     164        assert(!(length & (PAGE_SIZE - 1)));
     165
    153166        packet = (packet_t *) mmap(NULL, length, PROTO_READ | PROTO_WRITE,
    154             MAP_SHARED | MAP_ANONYMOUS, 0, 0);
     167                MAP_SHARED | MAP_ANONYMOUS, 0, 0);
    155168        if (packet == MAP_FAILED)
    156169                return NULL;
    157 
    158         ps_globals.count++;
    159         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
    160179        packet->length = length;
    161180        packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
     
    163182        rc = pm_add(packet);
    164183        if (rc != EOK) {
    165                 munmap(packet, packet->length);
     184                packet_dealloc(packet);
    166185                return NULL;
    167186        }
     
    184203 * @return              NULL if there is not enough memory left.
    185204 */
    186 static packet_t *
    187 packet_get_local(size_t addr_len, size_t max_prefix, size_t max_content,
    188     size_t max_suffix)
    189 {
    190         size_t length = ALIGN_UP(sizeof(packet_t) + 2 * addr_len +
    191             max_prefix + max_content + max_suffix, PAGE_SIZE);
    192        
     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
    193214        fibril_mutex_lock(&ps_globals.lock);
    194215       
     
    197218       
    198219        for (index = 0; index < FREE_QUEUES_COUNT; index++) {
    199                 if ((length > ps_globals.sizes[index]) &&
    200                     (index < FREE_QUEUES_COUNT - 1))
     220                if ((length > ps_globals.free_queues[index].packet_size) &&
     221                        (index < FREE_QUEUES_COUNT - 1))
    201222                        continue;
    202223               
    203                 packet = ps_globals.free[index];
     224                packet = ps_globals.free_queues[index].first;
    204225                while (packet_is_valid(packet) && (packet->length < length))
    205226                        packet = pm_find(packet->next);
    206227               
    207228                if (packet_is_valid(packet)) {
    208                         if (packet == ps_globals.free[index])
    209                                 ps_globals.free[index] = pq_detach(packet);
    210                         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 {
    211233                                pq_detach(packet);
     234                        }
    212235                       
    213                         packet_init(packet, addr_len, max_prefix, max_content,
    214                             max_suffix);
     236                        packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
    215237                        fibril_mutex_unlock(&ps_globals.lock);
    216238                       
     
    219241        }
    220242       
    221         packet = packet_create(length, addr_len, max_prefix, max_content,
    222             max_suffix);
     243        packet = packet_alloc(length, addr_len,
     244                max_prefix, max_content, max_suffix);
    223245       
    224246        fibril_mutex_unlock(&ps_globals.lock);
     
    240262
    241263        for (index = 0; (index < FREE_QUEUES_COUNT - 1) &&
    242             (packet->length > ps_globals.sizes[index]); index++) {
     264            (packet->length > ps_globals.free_queues[index].packet_size); index++) {
    243265                ;
    244266        }
    245267       
    246         result = pq_add(&ps_globals.free[index], packet, packet->length,
    247             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);
    248271        assert(result == EOK);
    249272}
     
    328351        case NET_PACKET_CREATE_1:
    329352                packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX,
    330                     IPC_GET_CONTENT(*call), DEFAULT_SUFFIX);
     353                        IPC_GET_CONTENT(*call), DEFAULT_SUFFIX);
    331354                if (!packet)
    332355                        return ENOMEM;
     
    338361        case NET_PACKET_CREATE_4:
    339362                packet = packet_get_local(
    340                     ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(*call)) ?
     363                        ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(*call)) ?
    341364                    IPC_GET_ADDR_LEN(*call) : DEFAULT_ADDR_LEN),
    342365                    DEFAULT_PREFIX + IPC_GET_PREFIX(*call),
     
    352375        case NET_PACKET_GET:
    353376                packet = pm_find(IPC_GET_ID(*call));
    354                 if (!packet_is_valid(packet))
     377                if (!packet_is_valid(packet)) {
    355378                        return ENOENT;
     379                }
    356380                return packet_reply(packet);
    357381       
     
    371395}
    372396
     397int packet_server_init()
     398{
     399        return EOK;
     400}
     401
    373402/** @}
    374403 */
  • uspace/srv/net/net/packet_server.h

    rf51b1d3 r609243f4  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    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

    rf51b1d3 r609243f4  
    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>
    6163#include "eth.h"
     
    167169INT_MAP_IMPLEMENT(eth_protos, eth_proto_t);
    168170
    169 int nil_device_state_msg_local(device_id_t device_id, sysarg_t state)
     171int nil_device_state_msg_local(nic_device_id_t device_id, sysarg_t state)
    170172{
    171173        int index;
     
    196198        fibril_rwlock_write_lock(&eth_globals.protos_lock);
    197199        eth_globals.net_sess = sess;
    198 
    199         eth_globals.broadcast_addr =
    200             measured_string_create_bulk((uint8_t *) "\xFF\xFF\xFF\xFF\xFF\xFF", ETH_ADDR);
    201         if (!eth_globals.broadcast_addr) {
    202                 rc = ENOMEM;
    203                 goto out;
    204         }
     200        memcpy(eth_globals.broadcast_addr, "\xFF\xFF\xFF\xFF\xFF\xFF",
     201                        ETH_ADDR);
    205202
    206203        rc = eth_devices_initialize(&eth_globals.devices);
     
    215212                eth_devices_destroy(&eth_globals.devices, free);
    216213        }
     214       
    217215out:
    218216        fibril_rwlock_write_unlock(&eth_globals.protos_lock);
     
    222220}
    223221
    224 /** Process IPC messages from the registered device driver modules in an
    225  * infinite loop.
    226  *
    227  * @param[in]     iid   Message identifier.
    228  * @param[in,out] icall Message parameters.
    229  * @param[in]     arg   Local argument.
    230  *
    231  */
    232 static void eth_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    233 {
    234         packet_t *packet;
    235         int rc;
    236 
    237         while (true) {
    238                 switch (IPC_GET_IMETHOD(*icall)) {
    239                 case NET_NIL_DEVICE_STATE:
    240                         nil_device_state_msg_local(IPC_GET_DEVICE(*icall),
    241                             IPC_GET_STATE(*icall));
    242                         async_answer_0(iid, EOK);
    243                         break;
    244                 case NET_NIL_RECEIVED:
    245                         rc = packet_translate_remote(eth_globals.net_sess,
    246                             &packet, IPC_GET_PACKET(*icall));
    247                         if (rc == EOK)
    248                                 rc = nil_received_msg_local(IPC_GET_DEVICE(*icall),
    249                                     packet, 0);
    250                        
    251                         async_answer_0(iid, (sysarg_t) rc);
    252                         break;
    253                 default:
    254                         async_answer_0(iid, (sysarg_t) ENOTSUP);
    255                 }
    256                
    257                 iid = async_get_call(icall);
    258         }
    259 }
    260 
    261 /** Registers new device or updates the MTU of an existing one.
    262  *
    263  * Determines the device local hardware address.
    264  *
    265  * @param[in] device_id The new device identifier.
    266  * @param[in] service   The device driver service.
    267  * @param[in] mtu       The device maximum transmission unit.
    268  * @return              EOK on success.
    269  * @return              EEXIST if the device with the different service exists.
    270  * @return              ENOMEM if there is not enough memory left.
    271  * @return              Other error codes as defined for the
    272  *                      net_get_device_conf_req() function.
    273  * @return              Other error codes as defined for the
    274  *                      netif_bind_service() function.
    275  * @return              Other error codes as defined for the
    276  *                      netif_get_addr_req() function.
    277  */
    278 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,
    279236    size_t mtu)
    280237{
     
    301258        device = eth_devices_find(&eth_globals.devices, device_id);
    302259        if (device) {
    303                 if (device->service != service) {
     260                if (device->handle != handle) {
    304261                        printf("Device %d already exists\n", device->device_id);
    305262                        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     
    340297
    341298        device->device_id = device_id;
    342         device->service = service;
     299        device->handle = handle;
    343300        device->flags = 0;
    344301        if ((mtu > 0) && (mtu <= ETH_MAX_TAGGED_CONTENT(device->flags)))
     
    377334       
    378335        /* Bind the device driver */
    379         device->sess = netif_bind_service(device->service, device->device_id,
    380             SERVICE_ETHERNET, eth_receiver);
     336        device->sess = devman_device_connect(EXCHANGE_SERIALIZE, handle,
     337            IPC_FLAG_BLOCKING);
    381338        if (device->sess == NULL) {
    382339                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     
    385342        }
    386343       
     344        nic_connect_to_nil(device->sess, SERVICE_ETHERNET, device_id);
     345       
    387346        /* Get hardware address */
    388         rc = netif_get_addr_req(device->sess, device->device_id, &device->addr,
    389             &device->addr_data);
     347        rc = nic_get_address(device->sess, &device->addr);
    390348        if (rc != EOK) {
    391349                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     
    399357        if (index < 0) {
    400358                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    401                 free(device->addr);
    402                 free(device->addr_data);
    403359                free(device);
    404360                return index;
    405361        }
    406362       
    407         printf("%s: Device registered (id: %d, service: %d: mtu: %zu, "
    408             "mac: %02x:%02x:%02x:%02x:%02x:%02x, flags: 0x%x)\n",
    409             NAME, device->device_id, device->service, device->mtu,
    410             device->addr_data[0], device->addr_data[1],
    411             device->addr_data[2], device->addr_data[3],
    412             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);
    413367
    414368        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     
    456410                fcs = (eth_fcs_t *) data + length - sizeof(eth_fcs_t);
    457411                length -= sizeof(eth_fcs_t);
    458         } else if(type <= ETH_MAX_CONTENT) {
     412        } else if (type <= ETH_MAX_CONTENT) {
    459413                /* Translate "LSAP" values */
    460414                if ((header->lsap.dsap == ETH_LSAP_GLSAP) &&
     
    462416                        /* Raw packet -- discard */
    463417                        return NULL;
    464                 } else if((header->lsap.dsap == ETH_LSAP_SNAP) &&
     418                } else if ((header->lsap.dsap == ETH_LSAP_SNAP) &&
    465419                    (header->lsap.ssap == ETH_LSAP_SNAP)) {
    466420                        /*
     
    469423                         */
    470424                        type = ntohs(header->snap.ethertype);
    471                         prefix = sizeof(eth_header_t) +
    472                             sizeof(eth_header_lsap_t) +
     425                        prefix = sizeof(eth_header_t) + sizeof(eth_header_lsap_t) +
    473426                            sizeof(eth_header_snap_t);
    474427                } else {
    475428                        /* IEEE 802.3 + 802.2 LSAP */
    476429                        type = lsap_map(header->lsap.dsap);
    477                         prefix = sizeof(eth_header_t) +
    478                             sizeof(eth_header_lsap_t);
     430                        prefix = sizeof(eth_header_t) + sizeof(eth_header_lsap_t);
    479431                }
    480432
     
    506458}
    507459
    508 int nil_received_msg_local(device_id_t device_id, packet_t *packet,
    509     services_t target)
     460int nil_received_msg_local(nic_device_id_t device_id, packet_t *packet)
    510461{
    511462        eth_proto_t *proto;
     
    523474        flags = device->flags;
    524475        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    525        
    526476        fibril_rwlock_read_lock(&eth_globals.protos_lock);
     477       
    527478        do {
    528479                next = pq_detach(packet);
     
    537488                }
    538489                packet = next;
    539         } while(packet);
     490        } while (packet);
    540491
    541492        fibril_rwlock_read_unlock(&eth_globals.protos_lock);
     
    554505 * @return              ENOENT if there is no such device.
    555506 */
    556 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,
    557508    size_t *prefix, size_t *content, size_t *suffix)
    558509{
     
    579530}
    580531
    581 /** Returns the device hardware address.
     532/** Send the device hardware address.
    582533 *
    583534 * @param[in] device_id The device identifier.
    584535 * @param[in] type      Type of the desired address.
    585  * @param[out] address  The device hardware address.
    586536 * @return              EOK on success.
    587537 * @return              EBADMEM if the address parameter is NULL.
    588538 * @return              ENOENT if there no such device.
    589539 */
    590 static int eth_addr_message(device_id_t device_id, eth_addr_type_t type,
    591     measured_string_t **address)
    592 {
    593         eth_device_t *device;
    594 
    595         if (!address)
    596                 return EBADMEM;
    597 
    598         if (type == ETH_BROADCAST_ADDR) {
    599                 *address = eth_globals.broadcast_addr;
    600         } 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 {
    601550                fibril_rwlock_read_lock(&eth_globals.devices_lock);
    602551                device = eth_devices_find(&eth_globals.devices, device_id);
     
    605554                        return ENOENT;
    606555                }
    607                 *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)
    608579                fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    609         }
    610        
    611         return (*address) ? EOK : ENOENT;
     580       
     581        return rc;
    612582}
    613583
     
    697667        if (i < 0)
    698668                return i;
     669       
    699670        if (i != ETH_ADDR)
    700671                return EINVAL;
     672       
     673        for (i = 0; i < ETH_ADDR; i++) {
     674                if (src[i]) {
     675                        src_addr = src;
     676                        break;
     677                }
     678        }
    701679
    702680        length = packet_get_data_length(packet);
     
    722700                memcpy(header_dix->destination_address, dest, ETH_ADDR);
    723701                src = &header_dix->destination_address[0];
    724         } else if(IS_8023_2_LSAP(flags)) {
     702        } else if (IS_8023_2_LSAP(flags)) {
    725703                header_lsap = PACKET_PREFIX(packet, eth_header_lsap_t);
    726704                if (!header_lsap)
     
    735713                memcpy(header_lsap->header.destination_address, dest, ETH_ADDR);
    736714                src = &header_lsap->header.destination_address[0];
    737         } else if(IS_8023_2_SNAP(flags)) {
     715        } else if (IS_8023_2_SNAP(flags)) {
    738716                header = PACKET_PREFIX(packet, eth_header_snap_t);
    739717                if (!header)
     
    746724                header->lsap.ctrl = IEEE_8023_2_UI;
    747725               
    748                 for (i = 0; i < 3; ++ i)
     726                for (i = 0; i < 3; i++)
    749727                        header->snap.protocol[i] = 0;
    750728               
     
    760738                        return ENOMEM;
    761739               
    762                 for (i = 0; i < 7; ++ i)
     740                for (i = 0; i < 7; i++)
    763741                        preamble->preamble[i] = ETH_PREAMBLE;
    764742               
     
    787765 * @return              EINVAL if the service parameter is not known.
    788766 */
    789 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,
    790768    services_t sender)
    791769{
     
    813791        do {
    814792                rc = eth_prepare_packet(device->flags, next,
    815                     (uint8_t *) device->addr->value, ethertype, device->mtu);
     793                    (uint8_t *) &device->addr.address, ethertype, device->mtu);
    816794                if (rc != EOK) {
    817795                        /* Release invalid packet */
     
    825803                        next = pq_next(next);
    826804                }
    827         } while(next);
     805        } while (next);
    828806       
    829807        /* Send packet queue */
    830         if (packet) {
    831                 netif_send_msg(device->sess, device_id, packet,
    832                     SERVICE_ETHERNET);
    833         }
    834 
     808        if (packet)
     809                nic_send_message(device->sess, packet_get_id(packet));
     810       
    835811        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    836812        return EOK;
    837813}
    838814
     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
    839859int nil_module_message(ipc_callid_t callid, ipc_call_t *call,
    840860    ipc_call_t *answer, size_t *answer_count)
    841861{
    842         measured_string_t *address;
    843862        packet_t *packet;
    844863        size_t addrlen;
     
    861880        case NET_NIL_DEVICE:
    862881                return eth_device_message(IPC_GET_DEVICE(*call),
    863                     IPC_GET_SERVICE(*call), IPC_GET_MTU(*call));
     882                    IPC_GET_DEVICE_HANDLE(*call), IPC_GET_MTU(*call));
    864883        case NET_NIL_SEND:
    865884                rc = packet_translate_remote(eth_globals.net_sess, &packet,
     
    867886                if (rc != EOK)
    868887                        return rc;
     888               
    869889                return eth_send_message(IPC_GET_DEVICE(*call), packet,
    870890                    IPC_GET_SERVICE(*call));
     
    874894                if (rc != EOK)
    875895                        return rc;
     896               
    876897                IPC_SET_ADDR(*answer, addrlen);
    877898                IPC_SET_PREFIX(*answer, prefix);
     
    879900                IPC_SET_SUFFIX(*answer, suffix);
    880901                *answer_count = 4;
     902               
    881903                return EOK;
    882904        case NET_NIL_ADDR:
    883                 rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_LOCAL_ADDR,
    884                     &address);
     905                rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_LOCAL_ADDR);
    885906                if (rc != EOK)
    886907                        return rc;
    887                 return measured_strings_reply(address, 1);
     908               
     909                IPC_SET_ADDR(*answer, ETH_ADDR);
     910                *answer_count = 1;
     911               
     912                return EOK;
    888913        case NET_NIL_BROADCAST_ADDR:
    889                 rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_BROADCAST_ADDR,
    890                     &address);
     914                rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_BROADCAST_ADDR);
    891915                if (rc != EOK)
    892                         return EOK;
    893                 return measured_strings_reply(address, 1);
     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;
    894938        }
    895939       
  • uspace/srv/net/nil/eth/eth.h

    rf51b1d3 r609243f4  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    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;
     224        nic_device_id_t device_id;
     225        /** Device handle */
     226        devman_handle_t handle;
    225227        /** Driver session. */
    226228        async_sess_t *sess;
     
    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
     
    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

    rf51b1d3 r609243f4  
    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>
     
    5052#include <net/packet.h>
    5153#include <packet_remote.h>
    52 #include <netif_remote.h>
     54#include <packet_client.h>
     55#include <devman.h>
     56#include <device/nic.h>
    5357#include <nil_skel.h>
    5458#include "nildummy.h"
     
    6569DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t);
    6670
    67 int nil_device_state_msg_local(device_id_t device_id, sysarg_t state)
     71int nil_device_state_msg_local(nic_device_id_t device_id, sysarg_t state)
    6872{
    6973        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
     
    9195       
    9296        return rc;
    93 }
    94 
    95 /** Process IPC messages from the registered device driver modules
    96  *
    97  * @param[in]     iid   Message identifier.
    98  * @param[in,out] icall Message parameters.
    99  * @param[in]     arg    Local argument.
    100  *
    101  */
    102 static void nildummy_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    103 {
    104         packet_t *packet;
    105         int rc;
    106        
    107         while (true) {
    108                 switch (IPC_GET_IMETHOD(*icall)) {
    109                 case NET_NIL_DEVICE_STATE:
    110                         rc = nil_device_state_msg_local(IPC_GET_DEVICE(*icall),
    111                             IPC_GET_STATE(*icall));
    112                         async_answer_0(iid, (sysarg_t) rc);
    113                         break;
    114                
    115                 case NET_NIL_RECEIVED:
    116                         rc = packet_translate_remote(nildummy_globals.net_sess,
    117                             &packet, IPC_GET_PACKET(*icall));
    118                         if (rc == EOK)
    119                                 rc = nil_received_msg_local(IPC_GET_DEVICE(*icall),
    120                                     packet, 0);
    121                        
    122                         async_answer_0(iid, (sysarg_t) rc);
    123                         break;
    124                
    125                 default:
    126                         async_answer_0(iid, (sysarg_t) ENOTSUP);
    127                 }
    128                
    129                 iid = async_get_call(icall);
    130         }
    13197}
    13298
     
    148114 *
    149115 */
    150 static int nildummy_device_message(device_id_t device_id, services_t service,
    151     size_t mtu)
     116static int nildummy_device_message(nic_device_id_t device_id,
     117    devman_handle_t handle, size_t mtu)
    152118{
    153119        fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
     
    157123            nildummy_devices_find(&nildummy_globals.devices, device_id);
    158124        if (device) {
    159                 if (device->service != service) {
    160                         printf("Device %d already exists\n", device->device_id);
    161                         fibril_rwlock_write_unlock(
    162                             &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);
    163129                        return EEXIST;
    164130                }
     
    170136                        device->mtu = NET_DEFAULT_MTU;
    171137               
    172                 printf("Device %d already exists:\tMTU\t= %zu\n",
    173                     device->device_id, device->mtu);
     138                printf("Device %d already exists:\tMTU\t= %zu\n", device->device_id,
     139                    device->mtu);
    174140                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    175141               
     
    192158       
    193159        device->device_id = device_id;
    194         device->service = service;
     160        device->handle = handle;
    195161        if (mtu > 0)
    196162                device->mtu = mtu;
    197163        else
    198164                device->mtu = NET_DEFAULT_MTU;
    199 
     165       
    200166        /* Bind the device driver */
    201         device->sess = netif_bind_service(device->service, device->device_id,
    202             SERVICE_ETHERNET, nildummy_receiver);
     167        device->sess = devman_device_connect(EXCHANGE_SERIALIZE, handle,
     168            IPC_FLAG_BLOCKING);
    203169        if (device->sess == NULL) {
    204170                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     
    207173        }
    208174       
     175        nic_connect_to_nil(device->sess, SERVICE_NILDUMMY, device_id);
     176       
    209177        /* Get hardware address */
    210         int rc = netif_get_addr_req(device->sess, device->device_id,
    211             &device->addr, &device->addr_data);
     178        int rc = nic_get_address(device->sess, &device->addr);
    212179        if (rc != EOK) {
    213180                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     
    215182                return rc;
    216183        }
     184       
     185        device->addr_len = ETH_ADDR;
    217186       
    218187        /* Add to the cache */
     
    221190        if (index < 0) {
    222191                fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    223                 free(device->addr);
    224                 free(device->addr_data);
    225192                free(device);
    226193                return index;
    227194        }
    228195       
    229         printf("%s: Device registered (id: %d, service: %d, mtu: %zu)\n",
    230             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);
    231198        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    232199        return EOK;
     
    243210 *
    244211 */
    245 static int nildummy_addr_message(device_id_t device_id,
    246     measured_string_t **address)
    247 {
    248         if (!address)
     212static int nildummy_addr_message(nic_device_id_t device_id, size_t *addrlen)
     213{
     214        if (!addrlen)
    249215                return EBADMEM;
    250216       
     
    258224        }
    259225       
    260         *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;
    261247       
    262248        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    263        
    264         return (*address) ? EOK : ENOENT;
     249        return EOK;
    265250}
    266251
     
    278263 *
    279264 */
    280 static int nildummy_packet_space_message(device_id_t device_id, size_t *addr_len,
    281     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)
    282267{
    283268        if ((!addr_len) || (!prefix) || (!content) || (!suffix))
     
    303288}
    304289
    305 int nil_received_msg_local(device_id_t device_id, packet_t *packet,
    306     services_t target)
     290int nil_received_msg_local(nic_device_id_t device_id, packet_t *packet)
    307291{
    308292        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
     
    358342 *
    359343 */
    360 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,
    361345    services_t sender)
    362346{
     
    372356        /* Send packet queue */
    373357        if (packet)
    374                 netif_send_msg(device->sess, device_id, packet,
    375                     SERVICE_NILDUMMY);
     358                nic_send_message(device->sess, packet_get_id(packet));
    376359       
    377360        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
     
    383366    ipc_call_t *answer, size_t *answer_count)
    384367{
    385         measured_string_t *address;
    386368        packet_t *packet;
    387369        size_t addrlen;
     
    404386        case NET_NIL_DEVICE:
    405387                return nildummy_device_message(IPC_GET_DEVICE(*call),
    406                     IPC_GET_SERVICE(*call), IPC_GET_MTU(*call));
     388                    IPC_GET_DEVICE_HANDLE(*call), IPC_GET_MTU(*call));
    407389       
    408390        case NET_NIL_SEND:
     
    427409       
    428410        case NET_NIL_ADDR:
    429                 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);
    430413                if (rc != EOK)
    431414                        return rc;
    432                 return measured_strings_reply(address, 1);
    433        
    434         case NET_NIL_BROADCAST_ADDR:
    435                 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address);
    436                 if (rc != EOK)
    437                         return rc;
    438                 return measured_strings_reply(address, 1);
     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;
    439433        }
    440434       
  • uspace/srv/net/nil/nildummy/nildummy.h

    rf51b1d3 r609243f4  
    11/*
    22 * Copyright (c) 2009 Lukas Mejdrech
     3 * Copyright (c) 2011 Radim Vansa
    34 * All rights reserved.
    45 *
     
    4142#include <fibril_synch.h>
    4243#include <ipc/services.h>
     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        
     80        nic_device_id_t device_id;
     81        /** Device driver handle. */
     82        devman_handle_t handle;
    8383        /** Driver session. */
    8484        async_sess_t *sess;
     
    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
  • uspace/srv/net/tl/tcp/tcp.c

    rf51b1d3 r609243f4  
    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);
     207static int tcp_received_msg(nic_device_id_t, packet_t *, services_t, services_t);
    208208static int tcp_process_client_messages(async_sess_t *, ipc_callid_t,
    209209    ipc_call_t);
     
    220220tcp_globals_t tcp_globals;
    221221
    222 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,
    223223    services_t receiver, services_t error)
    224224{
     
    238238}
    239239
    240 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)
    241241{
    242242        size_t length;
     
    12511251        bzero(socket_data, sizeof(*socket_data));
    12521252        socket_data->state = TCP_SOCKET_INITIAL;
    1253         socket_data->device_id = DEVICE_INVALID_ID;
     1253        socket_data->device_id = NIC_DEVICE_INVALID_ID;
    12541254        socket_data->window = NET_DEFAULT_TCP_WINDOW;
    12551255        socket_data->treshold = socket_data->window;
     
    13351335                        }
    13361336                        if (tl_get_ip_packet_dimension(tcp_globals.ip_sess,
    1337                             &tcp_globals.dimensions, DEVICE_INVALID_ID,
     1337                            &tcp_globals.dimensions, NIC_DEVICE_INVALID_ID,
    13381338                            &packet_dimension) == EOK) {
    13391339                                SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
     
    16511651        /* Sent packet? */
    16521652        packet = pq_find(socket_data->outgoing, sequence_number);
    1653         printf("retransmit %d\n", packet_get_id(packet));
     1653        printf("retransmit %lu\n", packet_get_id(packet));
    16541654        if (packet) {
    16551655                pq_get_order(packet, NULL, &data_length);
     
    17901790
    17911791                        /* Send the packet */
    1792                         printf("connecting %d\n", packet_get_id(packet));
     1792                        printf("connecting %lu\n", packet_get_id(packet));
    17931793                        tcp_send_packets(socket_data->device_id, packet);
    17941794
     
    20042004}
    20052005
    2006 void tcp_send_packets(device_id_t device_id, packet_t *packet)
     2006void tcp_send_packets(nic_device_id_t device_id, packet_t *packet)
    20072007{
    20082008        packet_t *next;
  • uspace/srv/net/tl/tcp/tcp.h

    rf51b1d3 r609243f4  
    182182       
    183183        /** Device identifier. */
    184         device_id_t device_id;
     184        nic_device_id_t device_id;
    185185       
    186186        /**
  • uspace/srv/net/tl/udp/udp.c

    rf51b1d3 r609243f4  
    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{
     
    322322 *                      udp_process_packet() function.
    323323 */
    324 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,
    325325    services_t receiver, services_t error)
    326326{
     
    499499        void *ip_header;
    500500        size_t headerlen;
    501         device_id_t device_id;
     501        nic_device_id_t device_id;
    502502        packet_dimension_t *packet_dimension;
    503503        size_t size;
     
    617617                    htons(flip_checksum(compact_checksum(checksum)));
    618618                free(ip_header);
    619         } else {
    620                 device_id = DEVICE_INVALID_ID;
    621         }
     619        } else
     620                device_id = NIC_DEVICE_INVALID_ID;
    622621
    623622        /* Prepare the first packet fragment */
     
    806805                        size = MAX_UDP_FRAGMENT_SIZE;
    807806                        if (tl_get_ip_packet_dimension(udp_globals.ip_sess,
    808                             &udp_globals.dimensions, DEVICE_INVALID_ID,
     807                            &udp_globals.dimensions, NIC_DEVICE_INVALID_ID,
    809808                            &packet_dimension) == EOK) {
    810809                                if (packet_dimension->content < size)
Note: See TracChangeset for help on using the changeset viewer.