Changeset b5e68c8 in mainline for uspace/srv/net/nil/eth/eth.c


Ignore:
Timestamp:
2011-05-12T16:49:44Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f36787d7
Parents:
e80329d6 (diff), 750636a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/nil/eth/eth.c

    re80329d6 rb5e68c8  
    4242#include <byteorder.h>
    4343#include <str.h>
    44 #include <err.h>
    45 
    46 #include <ipc/ipc.h>
     44#include <errno.h>
     45#include <ipc/nil.h>
    4746#include <ipc/net.h>
    4847#include <ipc/services.h>
    49 
    5048#include <net/modules.h>
    5149#include <net_checksum.h>
     
    5452#include <protocol_map.h>
    5553#include <net/device.h>
    56 #include <netif_interface.h>
     54#include <netif_remote.h>
    5755#include <net_interface.h>
    58 #include <nil_interface.h>
    59 #include <il_interface.h>
     56#include <il_remote.h>
    6057#include <adt/measured_strings.h>
    6158#include <packet_client.h>
    6259#include <packet_remote.h>
    63 #include <nil_local.h>
     60#include <nil_skel.h>
    6461
    6562#include "eth.h"
    66 #include "eth_header.h"
    6763
    6864/** The module name. */
     
    7268#define ETH_PREFIX \
    7369        (sizeof(eth_header_t) + sizeof(eth_header_lsap_t) + \
    74         sizeof(eth_header_snap_t))
     70            sizeof(eth_header_snap_t))
    7571
    7672/** Reserved packet suffix length. */
    77 #define ETH_SUFFIX \
    78         sizeof(eth_fcs_t)
     73#define ETH_SUFFIX  (sizeof(eth_fcs_t))
    7974
    8075/** Maximum packet content length. */
    81 #define ETH_MAX_CONTENT 1500u
     76#define ETH_MAX_CONTENT  1500u
    8277
    8378/** Minimum packet content length. */
    84 #define ETH_MIN_CONTENT 46u
     79#define ETH_MIN_CONTENT  46u
    8580
    8681/** Maximum tagged packet content length. */
    8782#define ETH_MAX_TAGGED_CONTENT(flags) \
    8883        (ETH_MAX_CONTENT - \
    89         ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? \
    90         sizeof(eth_header_lsap_t) : 0) - \
    91         (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0))
     84            ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? \
     85            sizeof(eth_header_lsap_t) : 0) - \
     86            (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0))
    9287
    9388/** Minimum tagged packet content length. */
    9489#define ETH_MIN_TAGGED_CONTENT(flags) \
    9590        (ETH_MIN_CONTENT - \
    96         ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? \
    97         sizeof(eth_header_lsap_t) : 0) - \
    98         (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0))
     91            ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? \
     92            sizeof(eth_header_lsap_t) : 0) - \
     93            (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0))
    9994
    10095/** Dummy flag shift value. */
    101 #define ETH_DUMMY_SHIFT 0
     96#define ETH_DUMMY_SHIFT  0
    10297
    10398/** Mode flag shift value. */
    104 #define ETH_MODE_SHIFT  1
     99#define ETH_MODE_SHIFT  1
    105100
    106101/** Dummy device flag.
    107102 * Preamble and FCS are mandatory part of the packets.
    108103 */
    109 #define ETH_DUMMY               (1 << ETH_DUMMY_SHIFT)
     104#define ETH_DUMMY  (1 << ETH_DUMMY_SHIFT)
    110105
    111106/** Returns the dummy flag.
    112107 * @see ETH_DUMMY
    113108 */
    114 #define IS_DUMMY(flags)         ((flags) & ETH_DUMMY)
     109#define IS_DUMMY(flags)  ((flags) & ETH_DUMMY)
    115110
    116111/** Device mode flags.
     
    119114 * @see ETH_8023_2_SNAP
    120115 */
    121 #define ETH_MODE_MASK           (3 << ETH_MODE_SHIFT)
     116#define ETH_MODE_MASK  (3 << ETH_MODE_SHIFT)
    122117
    123118/** DIX Ethernet mode flag. */
    124 #define ETH_DIX                 (1 << ETH_MODE_SHIFT)
    125 
    126 /** Returns whether the DIX Ethernet mode flag is set.
    127  *
    128  * @param[in] flags     The ethernet flags.
     119#define ETH_DIX  (1 << ETH_MODE_SHIFT)
     120
     121/** Return whether the DIX Ethernet mode flag is set.
     122 *
     123 * @param[in] flags Ethernet flags.
    129124 * @see ETH_DIX
    130  */
    131 #define IS_DIX(flags)           (((flags) & ETH_MODE_MASK) == ETH_DIX)
     125 *
     126 */
     127#define IS_DIX(flags)  (((flags) & ETH_MODE_MASK) == ETH_DIX)
    132128
    133129/** 802.3 + 802.2 + LSAP mode flag. */
    134 #define ETH_8023_2_LSAP         (2 << ETH_MODE_SHIFT)
    135 
    136 /** Returns whether the 802.3 + 802.2 + LSAP mode flag is set.
    137  *
    138  * @param[in] flags     The ethernet flags.
     130#define ETH_8023_2_LSAP  (2 << ETH_MODE_SHIFT)
     131
     132/** Return whether the 802.3 + 802.2 + LSAP mode flag is set.
     133 *
     134 * @param[in] flags Ethernet flags.
    139135 * @see ETH_8023_2_LSAP
    140  */
    141 #define IS_8023_2_LSAP(flags)   (((flags) & ETH_MODE_MASK) == ETH_8023_2_LSAP)
     136 *
     137 */
     138#define IS_8023_2_LSAP(flags)  (((flags) & ETH_MODE_MASK) == ETH_8023_2_LSAP)
    142139
    143140/** 802.3 + 802.2 + LSAP + SNAP mode flag. */
    144 #define ETH_8023_2_SNAP         (3 << ETH_MODE_SHIFT)
    145 
    146 /** Returns whether the 802.3 + 802.2 + LSAP + SNAP mode flag is set.
    147  *
    148  * @param[in] flags     The ethernet flags.
     141#define ETH_8023_2_SNAP  (3 << ETH_MODE_SHIFT)
     142
     143/** Return whether the 802.3 + 802.2 + LSAP + SNAP mode flag is set.
     144 *
     145 * @param[in] flags Ethernet flags.
    149146 * @see ETH_8023_2_SNAP
    150  */
    151 #define IS_8023_2_SNAP(flags)   (((flags) & ETH_MODE_MASK) == ETH_8023_2_SNAP)
     147 *
     148 */
     149#define IS_8023_2_SNAP(flags)  (((flags) & ETH_MODE_MASK) == ETH_8023_2_SNAP)
    152150
    153151/** Type definition of the ethernet address type.
     
    155153 */
    156154typedef enum eth_addr_type eth_addr_type_t;
    157 
    158 /** Type definition of the ethernet address type pointer.
    159  * @see eth_addr_type
    160  */
    161 typedef eth_addr_type_t *eth_addr_type_ref;
    162155
    163156/** Ethernet address type. */
     
    178171{
    179172        int index;
    180         eth_proto_ref proto;
     173        eth_proto_t *proto;
    181174
    182175        fibril_rwlock_read_lock(&eth_globals.protos_lock);
     
    196189int nil_initialize(int net_phone)
    197190{
    198         ERROR_DECLARE;
     191        int rc;
    199192
    200193        fibril_rwlock_initialize(&eth_globals.devices_lock);
     
    204197        fibril_rwlock_write_lock(&eth_globals.protos_lock);
    205198        eth_globals.net_phone = net_phone;
     199
    206200        eth_globals.broadcast_addr =
    207             measured_string_create_bulk("\xFF\xFF\xFF\xFF\xFF\xFF",
    208             CONVERT_SIZE(uint8_t, char, ETH_ADDR));
     201            measured_string_create_bulk((uint8_t *) "\xFF\xFF\xFF\xFF\xFF\xFF", ETH_ADDR);
    209202        if (!eth_globals.broadcast_addr) {
    210                 ERROR_CODE = ENOMEM;
     203                rc = ENOMEM;
    211204                goto out;
    212205        }
    213         if (ERROR_OCCURRED(eth_devices_initialize(&eth_globals.devices))) {
     206
     207        rc = eth_devices_initialize(&eth_globals.devices);
     208        if (rc != EOK) {
    214209                free(eth_globals.broadcast_addr);
    215210                goto out;
    216211        }
    217         if (ERROR_OCCURRED(eth_protos_initialize(&eth_globals.protos))) {
     212
     213        rc = eth_protos_initialize(&eth_globals.protos);
     214        if (rc != EOK) {
    218215                free(eth_globals.broadcast_addr);
    219                 eth_devices_destroy(&eth_globals.devices);
     216                eth_devices_destroy(&eth_globals.devices, free);
    220217        }
    221218out:
     
    223220        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    224221       
    225         return ERROR_CODE;
     222        return rc;
    226223}
    227224
     
    234231static void eth_receiver(ipc_callid_t iid, ipc_call_t *icall)
    235232{
    236         ERROR_DECLARE;
    237 
    238         packet_t packet;
     233        packet_t *packet;
     234        int rc;
    239235
    240236        while (true) {
    241                 switch (IPC_GET_METHOD(*icall)) {
     237                switch (IPC_GET_IMETHOD(*icall)) {
    242238                case NET_NIL_DEVICE_STATE:
    243                         nil_device_state_msg_local(0, IPC_GET_DEVICE(icall),
    244                             IPC_GET_STATE(icall));
    245                         ipc_answer_0(iid, EOK);
     239                        nil_device_state_msg_local(0, IPC_GET_DEVICE(*icall),
     240                            IPC_GET_STATE(*icall));
     241                        async_answer_0(iid, EOK);
    246242                        break;
    247243                case NET_NIL_RECEIVED:
    248                         if (ERROR_NONE(packet_translate_remote(
    249                             eth_globals.net_phone, &packet,
    250                             IPC_GET_PACKET(icall)))) {
    251                                 ERROR_CODE = nil_received_msg_local(0,
    252                                     IPC_GET_DEVICE(icall), packet, 0);
    253                         }
    254                         ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
     244                        rc = packet_translate_remote(eth_globals.net_phone,
     245                            &packet, IPC_GET_PACKET(*icall));
     246                        if (rc == EOK)
     247                                rc = nil_received_msg_local(0,
     248                                    IPC_GET_DEVICE(*icall), packet, 0);
     249                       
     250                        async_answer_0(iid, (sysarg_t) rc);
    255251                        break;
    256252                default:
    257                         ipc_answer_0(iid, (ipcarg_t) ENOTSUP);
     253                        async_answer_0(iid, (sysarg_t) ENOTSUP);
    258254                }
    259255               
     
    269265 * @param[in] service   The device driver service.
    270266 * @param[in] mtu       The device maximum transmission unit.
    271  * @returns             EOK on success.
    272  * @returns             EEXIST if the device with the different service exists.
    273  * @returns             ENOMEM if there is not enough memory left.
    274  * @returns             Other error codes as defined for the
     267 * @return              EOK on success.
     268 * @return              EEXIST if the device with the different service exists.
     269 * @return              ENOMEM if there is not enough memory left.
     270 * @return              Other error codes as defined for the
    275271 *                      net_get_device_conf_req() function.
    276  * @returns             Other error codes as defined for the
     272 * @return              Other error codes as defined for the
    277273 *                      netif_bind_service() function.
    278  * @returns             Other error codes as defined for the
     274 * @return              Other error codes as defined for the
    279275 *                      netif_get_addr_req() function.
    280276 */
    281 static int
    282 eth_device_message(device_id_t device_id, services_t service, size_t mtu)
    283 {
    284         ERROR_DECLARE;
    285 
    286         eth_device_ref device;
     277static int eth_device_message(device_id_t device_id, services_t service,
     278    size_t mtu)
     279{
     280        eth_device_t *device;
    287281        int index;
    288282        measured_string_t names[2] = {
    289283                {
    290                         (char *) "ETH_MODE",
     284                        (uint8_t *) "ETH_MODE",
    291285                        8
    292286                },
    293287                {
    294                         (char *) "ETH_DUMMY",
     288                        (uint8_t *) "ETH_DUMMY",
    295289                        9
    296290                }
    297291        };
    298         measured_string_ref configuration;
     292        measured_string_t *configuration;
    299293        size_t count = sizeof(names) / sizeof(measured_string_t);
    300         char *data;
    301         eth_proto_ref proto;
     294        uint8_t *data;
     295        eth_proto_t *proto;
     296        int rc;
    302297
    303298        fibril_rwlock_write_lock(&eth_globals.devices_lock);
    304         // an existing device?
     299        /* An existing device? */
    305300        device = eth_devices_find(&eth_globals.devices, device_id);
    306301        if (device) {
     
    311306                }
    312307               
    313                 // update mtu
     308                /* Update mtu */
    314309                if ((mtu > 0) && (mtu <= ETH_MAX_TAGGED_CONTENT(device->flags)))
    315310                        device->mtu = mtu;
     
    317312                        device->mtu = ETH_MAX_TAGGED_CONTENT(device->flags);
    318313               
    319                 printf("Device %d already exists:\tMTU\t= %d\n",
     314                printf("Device %d already exists:\tMTU\t= %zu\n",
    320315                    device->device_id, device->mtu);
    321316                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    322317               
    323                 // notify all upper layer modules
     318                /* Notify all upper layer modules */
    324319                fibril_rwlock_read_lock(&eth_globals.protos_lock);
    325320                for (index = 0; index < eth_protos_count(&eth_globals.protos);
     
    333328                        }
    334329                }
     330
    335331                fibril_rwlock_read_unlock(&eth_globals.protos_lock);
    336332                return EOK;
    337333        }
    338334       
    339         // create a new device
    340         device = (eth_device_ref) malloc(sizeof(eth_device_t));
     335        /* Create a new device */
     336        device = (eth_device_t *) malloc(sizeof(eth_device_t));
    341337        if (!device)
    342338                return ENOMEM;
     
    351347
    352348        configuration = &names[0];
    353         if (ERROR_OCCURRED(net_get_device_conf_req(eth_globals.net_phone,
    354             device->device_id, &configuration, count, &data))) {
     349        rc = net_get_device_conf_req(eth_globals.net_phone, device->device_id,
     350            &configuration, count, &data);
     351        if (rc != EOK) {
    355352                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    356353                free(device);
    357                 return ERROR_CODE;
    358         }
     354                return rc;
     355        }
     356
    359357        if (configuration) {
    360                 if (!str_lcmp(configuration[0].value, "DIX",
     358                if (!str_lcmp((char *) configuration[0].value, "DIX",
    361359                    configuration[0].length)) {
    362360                        device->flags |= ETH_DIX;
    363                 } else if(!str_lcmp(configuration[0].value, "8023_2_LSAP",
     361                } else if(!str_lcmp((char *) configuration[0].value, "8023_2_LSAP",
    364362                    configuration[0].length)) {
    365363                        device->flags |= ETH_8023_2_LSAP;
     
    377375        }
    378376       
    379         // bind the device driver
     377        /* Bind the device driver */
    380378        device->phone = netif_bind_service(device->service, device->device_id,
    381379            SERVICE_ETHERNET, eth_receiver);
     
    386384        }
    387385       
    388         // get hardware address
    389         if (ERROR_OCCURRED(netif_get_addr_req(device->phone, device->device_id,
    390             &device->addr, &device->addr_data))) {
     386        /* Get hardware address */
     387        rc = netif_get_addr_req(device->phone, device->device_id, &device->addr,
     388            &device->addr_data);
     389        if (rc != EOK) {
    391390                fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    392391                free(device);
    393                 return ERROR_CODE;
    394         }
    395        
    396         // add to the cache
     392                return rc;
     393        }
     394       
     395        /* Add to the cache */
    397396        index = eth_devices_add(&eth_globals.devices, device->device_id,
    398397            device);
     
    405404        }
    406405       
    407         printf("%s: Device registered (id: %d, service: %d: mtu: %d, "
    408             "mac: %x:%x:%x:%x:%x:%x, flags: 0x%x)\n",
     406        printf("%s: Device registered (id: %d, service: %d: mtu: %zu, "
     407            "mac: %02x:%02x:%02x:%02x:%02x:%02x, flags: 0x%x)\n",
    409408            NAME, device->device_id, device->service, device->mtu,
    410409            device->addr_data[0], device->addr_data[1],
     
    420419 * @param[in] flags     The device flags.
    421420 * @param[in] packet    The packet.
    422  * @returns             The target registered module.
    423  * @returns             NULL if the packet is not long enough.
    424  * @returns             NULL if the packet is too long.
    425  * @returns             NULL if the raw ethernet protocol is used.
    426  * @returns             NULL if the dummy device FCS checksum is invalid.
    427  * @returns             NULL if the packet address length is not big enough.
    428  */
    429 static eth_proto_ref eth_process_packet(int flags, packet_t packet)
    430 {
    431         ERROR_DECLARE;
    432 
    433         eth_header_snap_ref header;
     421 * @return              The target registered module.
     422 * @return              NULL if the packet is not long enough.
     423 * @return              NULL if the packet is too long.
     424 * @return              NULL if the raw ethernet protocol is used.
     425 * @return              NULL if the dummy device FCS checksum is invalid.
     426 * @return              NULL if the packet address length is not big enough.
     427 */
     428static eth_proto_t *eth_process_packet(int flags, packet_t *packet)
     429{
     430        eth_header_snap_t *header;
    434431        size_t length;
    435432        eth_type_t type;
    436433        size_t prefix;
    437434        size_t suffix;
    438         eth_fcs_ref fcs;
    439         uint8_t * data;
     435        eth_fcs_t *fcs;
     436        uint8_t *data;
     437        int rc;
    440438
    441439        length = packet_get_data_length(packet);
     
    448446       
    449447        data = packet_get_data(packet);
    450         header = (eth_header_snap_ref) data;
     448        header = (eth_header_snap_t *) data;
    451449        type = ntohs(header->header.ethertype);
    452450       
    453451        if (type >= ETH_MIN_PROTO) {
    454                 // DIX Ethernet
     452                /* DIX Ethernet */
    455453                prefix = sizeof(eth_header_t);
    456454                suffix = 0;
    457                 fcs = (eth_fcs_ref) data + length - sizeof(eth_fcs_t);
     455                fcs = (eth_fcs_t *) data + length - sizeof(eth_fcs_t);
    458456                length -= sizeof(eth_fcs_t);
    459457        } else if(type <= ETH_MAX_CONTENT) {
    460                 // translate "LSAP" values
     458                /* Translate "LSAP" values */
    461459                if ((header->lsap.dsap == ETH_LSAP_GLSAP) &&
    462460                    (header->lsap.ssap == ETH_LSAP_GLSAP)) {
    463                         // raw packet
    464                         // discard
     461                        /* Raw packet -- discard */
    465462                        return NULL;
    466463                } else if((header->lsap.dsap == ETH_LSAP_SNAP) &&
    467464                    (header->lsap.ssap == ETH_LSAP_SNAP)) {
    468                         // IEEE 802.3 + 802.2 + LSAP + SNAP
    469                         // organization code not supported
     465                        /*
     466                         * IEEE 802.3 + 802.2 + LSAP + SNAP
     467                         * organization code not supported
     468                         */
    470469                        type = ntohs(header->snap.ethertype);
    471470                        prefix = sizeof(eth_header_t) +
     
    473472                            sizeof(eth_header_snap_t);
    474473                } else {
    475                         // IEEE 802.3 + 802.2 LSAP
     474                        /* IEEE 802.3 + 802.2 LSAP */
    476475                        type = lsap_map(header->lsap.dsap);
    477476                        prefix = sizeof(eth_header_t) +
    478477                            sizeof(eth_header_lsap_t);
    479478                }
     479
    480480                suffix = (type < ETH_MIN_CONTENT) ? ETH_MIN_CONTENT - type : 0U;
    481                 fcs = (eth_fcs_ref) data + prefix + type + suffix;
     481                fcs = (eth_fcs_t *) data + prefix + type + suffix;
    482482                suffix += length - prefix - type;
    483483                length = prefix + type + suffix;
    484484        } else {
    485                 // invalid length/type, should not occurr
     485                /* Invalid length/type, should not occur */
    486486                return NULL;
    487487        }
    488488       
    489489        if (IS_DUMMY(flags)) {
    490                 if ((~compute_crc32(~0U, data, length * 8)) != ntohl(*fcs))
     490                if (~compute_crc32(~0U, data, length * 8) != ntohl(*fcs))
    491491                        return NULL;
    492492                suffix += sizeof(eth_fcs_t);
    493493        }
    494494       
    495         if (ERROR_OCCURRED(packet_set_addr(packet,
    496             header->header.source_address, header->header.destination_address,
    497             ETH_ADDR)) || ERROR_OCCURRED(packet_trim(packet, prefix, suffix))) {
     495        rc = packet_set_addr(packet, header->header.source_address,
     496            header->header.destination_address, ETH_ADDR);
     497        if (rc != EOK)
    498498                return NULL;
    499         }
     499
     500        rc = packet_trim(packet, prefix, suffix);
     501        if (rc != EOK)
     502                return NULL;
    500503       
    501504        return eth_protos_find(&eth_globals.protos, type);
    502505}
    503506
    504 int
    505 nil_received_msg_local(int nil_phone, device_id_t device_id, packet_t packet,
    506     services_t target)
    507 {
    508         eth_proto_ref proto;
    509         packet_t next;
    510         eth_device_ref device;
     507int nil_received_msg_local(int nil_phone, device_id_t device_id,
     508    packet_t *packet, services_t target)
     509{
     510        eth_proto_t *proto;
     511        packet_t *next;
     512        eth_device_t *device;
    511513        int flags;
    512514
     
    517519                return ENOENT;
    518520        }
     521
    519522        flags = device->flags;
    520523        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
     
    528531                            proto->service);
    529532                } else {
    530                         // drop invalid/unknown
     533                        /* Drop invalid/unknown */
    531534                        pq_release_remote(eth_globals.net_phone,
    532535                            packet_get_id(packet));
     
    534537                packet = next;
    535538        } while(packet);
     539
    536540        fibril_rwlock_read_unlock(&eth_globals.protos_lock);
    537        
    538541        return EOK;
    539542}
     
    546549 * @param[out] content  The maximum content size.
    547550 * @param[out] suffix   The minimum reserved suffix size.
    548  * @returns             EOK on success.
    549  * @returns             EBADMEM if either one of the parameters is NULL.
    550  * @returns             ENOENT if there is no such device.
    551  */
    552 static int
    553 eth_packet_space_message(device_id_t device_id, size_t *addr_len,
     551 * @return              EOK on success.
     552 * @return              EBADMEM if either one of the parameters is NULL.
     553 * @return              ENOENT if there is no such device.
     554 */
     555static int eth_packet_space_message(device_id_t device_id, size_t *addr_len,
    554556    size_t *prefix, size_t *content, size_t *suffix)
    555557{
    556         eth_device_ref device;
     558        eth_device_t *device;
    557559
    558560        if (!addr_len || !prefix || !content || !suffix)
     
    565567                return ENOENT;
    566568        }
     569
    567570        *content = device->mtu;
    568571        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
     
    571574        *prefix = ETH_PREFIX;
    572575        *suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
     576
    573577        return EOK;
    574578}
     
    579583 * @param[in] type      Type of the desired address.
    580584 * @param[out] address  The device hardware address.
    581  * @returns             EOK on success.
    582  * @returns             EBADMEM if the address parameter is NULL.
    583  * @returns             ENOENT if there no such device.
    584  */
    585 static int
    586 eth_addr_message(device_id_t device_id, eth_addr_type_t type,
    587     measured_string_ref *address)
    588 {
    589         eth_device_ref device;
     585 * @return              EOK on success.
     586 * @return              EBADMEM if the address parameter is NULL.
     587 * @return              ENOENT if there no such device.
     588 */
     589static int eth_addr_message(device_id_t device_id, eth_addr_type_t type,
     590    measured_string_t **address)
     591{
     592        eth_device_t *device;
    590593
    591594        if (!address)
     
    614617 * @param[in] service   The module service.
    615618 * @param[in] phone     The service phone.
    616  * @returns             EOK on success.
    617  * @returns             ENOENT if the service is not known.
    618  * @returns             ENOMEM if there is not enough memory left.
     619 * @return              EOK on success.
     620 * @return              ENOENT if the service is not known.
     621 * @return              ENOMEM if there is not enough memory left.
    619622 */
    620623static int eth_register_message(services_t service, int phone)
    621624{
    622         eth_proto_ref proto;
     625        eth_proto_t *proto;
    623626        int protocol;
    624627        int index;
     
    635638                return EOK;
    636639        } else {
    637                 proto = (eth_proto_ref) malloc(sizeof(eth_proto_t));
     640                proto = (eth_proto_t *) malloc(sizeof(eth_proto_t));
    638641                if (!proto) {
    639642                        fibril_rwlock_write_unlock(&eth_globals.protos_lock);
    640643                        return ENOMEM;
    641644                }
     645
    642646                proto->service = service;
    643647                proto->protocol = protocol;
    644648                proto->phone = phone;
     649
    645650                index = eth_protos_add(&eth_globals.protos, protocol, proto);
    646651                if (index < 0) {
     
    665670 * @param[in] ethertype The ethernet protocol type.
    666671 * @param[in] mtu       The device maximum transmission unit.
    667  * @returns             EOK on success.
    668  * @returns             EINVAL if the packet addresses length is not long
     672 * @return              EOK on success.
     673 * @return              EINVAL if the packet addresses length is not long
    669674 *                      enough.
    670  * @returns             EINVAL if the packet is bigger than the device MTU.
    671  * @returns             ENOMEM if there is not enough memory in the packet.
     675 * @return              EINVAL if the packet is bigger than the device MTU.
     676 * @return              ENOMEM if there is not enough memory in the packet.
    672677 */
    673678static int
    674 eth_prepare_packet(int flags, packet_t packet, uint8_t *src_addr, int ethertype,
     679eth_prepare_packet(int flags, packet_t *packet, uint8_t *src_addr, int ethertype,
    675680    size_t mtu)
    676681{
    677         eth_header_snap_ref header;
    678         eth_header_lsap_ref header_lsap;
    679         eth_header_ref header_dix;
    680         eth_fcs_ref fcs;
     682        eth_header_snap_t *header;
     683        eth_header_lsap_t *header_lsap;
     684        eth_header_t *header_dix;
     685        eth_fcs_t *fcs;
    681686        uint8_t *src;
    682687        uint8_t *dest;
     
    684689        int i;
    685690        void *padding;
    686         eth_preamble_ref preamble;
     691        eth_preamble_t *preamble;
    687692
    688693        i = packet_get_addr(packet, &src, &dest);
     
    701706                if (!padding)
    702707                        return ENOMEM;
     708
    703709                bzero(padding, ETH_MIN_TAGGED_CONTENT(flags) - length);
    704710        }
     
    774780 * @param[in] packet    The packet queue.
    775781 * @param[in] sender    The sending module service.
    776  * @returns             EOK on success.
    777  * @returns             ENOENT if there no such device.
    778  * @returns             EINVAL if the service parameter is not known.
    779  */
    780 static int
    781 eth_send_message(device_id_t device_id, packet_t packet, services_t sender)
    782 {
    783         ERROR_DECLARE;
    784 
    785         eth_device_ref device;
    786         packet_t next;
    787         packet_t tmp;
     782 * @return              EOK on success.
     783 * @return              ENOENT if there no such device.
     784 * @return              EINVAL if the service parameter is not known.
     785 */
     786static int eth_send_message(device_id_t device_id, packet_t *packet,
     787    services_t sender)
     788{
     789        eth_device_t *device;
     790        packet_t *next;
     791        packet_t *tmp;
    788792        int ethertype;
     793        int rc;
    789794
    790795        ethertype = htons(protocol_map(SERVICE_ETHERNET, sender));
     
    801806        }
    802807       
    803         // process packet queue
     808        /* Process packet queue */
    804809        next = packet;
    805810        do {
    806                 if (ERROR_OCCURRED(eth_prepare_packet(device->flags, next,
    807                     (uint8_t *) device->addr->value, ethertype, device->mtu))) {
    808                         // release invalid packet
     811                rc = eth_prepare_packet(device->flags, next,
     812                    (uint8_t *) device->addr->value, ethertype, device->mtu);
     813                if (rc != EOK) {
     814                        /* Release invalid packet */
    809815                        tmp = pq_detach(next);
    810816                        if (next == packet)
     
    818824        } while(next);
    819825       
    820         // send packet queue
     826        /* Send packet queue */
    821827        if (packet) {
    822828                netif_send_msg(device->phone, device_id, packet,
    823829                    SERVICE_ETHERNET);
    824830        }
     831
    825832        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    826        
    827833        return EOK;
    828834}
    829835
    830 int
    831 nil_message_standalone(const char *name, ipc_callid_t callid, ipc_call_t *call,
    832     ipc_call_t *answer, int *answer_count)
    833 {
    834         ERROR_DECLARE;
    835        
    836         measured_string_ref address;
    837         packet_t packet;
     836int nil_module_message(ipc_callid_t callid, ipc_call_t *call,
     837    ipc_call_t *answer, size_t *answer_count)
     838{
     839        measured_string_t *address;
     840        packet_t *packet;
    838841        size_t addrlen;
    839842        size_t prefix;
    840843        size_t suffix;
    841844        size_t content;
     845        int rc;
    842846       
    843847        *answer_count = 0;
    844         switch (IPC_GET_METHOD(*call)) {
     848        switch (IPC_GET_IMETHOD(*call)) {
    845849        case IPC_M_PHONE_HUNGUP:
    846850                return EOK;
    847851       
    848852        case NET_NIL_DEVICE:
    849                 return eth_device_message(IPC_GET_DEVICE(call),
    850                     IPC_GET_SERVICE(call), IPC_GET_MTU(call));
     853                return eth_device_message(IPC_GET_DEVICE(*call),
     854                    IPC_GET_SERVICE(*call), IPC_GET_MTU(*call));
    851855        case NET_NIL_SEND:
    852                 ERROR_PROPAGATE(packet_translate_remote(eth_globals.net_phone,
    853                     &packet, IPC_GET_PACKET(call)));
    854                 return eth_send_message(IPC_GET_DEVICE(call), packet,
    855                     IPC_GET_SERVICE(call));
     856                rc = packet_translate_remote(eth_globals.net_phone, &packet,
     857                    IPC_GET_PACKET(*call));
     858                if (rc != EOK)
     859                        return rc;
     860                return eth_send_message(IPC_GET_DEVICE(*call), packet,
     861                    IPC_GET_SERVICE(*call));
    856862        case NET_NIL_PACKET_SPACE:
    857                 ERROR_PROPAGATE(eth_packet_space_message(IPC_GET_DEVICE(call),
    858                     &addrlen, &prefix, &content, &suffix));
    859                 IPC_SET_ADDR(answer, addrlen);
    860                 IPC_SET_PREFIX(answer, prefix);
    861                 IPC_SET_CONTENT(answer, content);
    862                 IPC_SET_SUFFIX(answer, suffix);
     863                rc = eth_packet_space_message(IPC_GET_DEVICE(*call), &addrlen,
     864                    &prefix, &content, &suffix);
     865                if (rc != EOK)
     866                        return rc;
     867                IPC_SET_ADDR(*answer, addrlen);
     868                IPC_SET_PREFIX(*answer, prefix);
     869                IPC_SET_CONTENT(*answer, content);
     870                IPC_SET_SUFFIX(*answer, suffix);
    863871                *answer_count = 4;
    864872                return EOK;
    865873        case NET_NIL_ADDR:
    866                 ERROR_PROPAGATE(eth_addr_message(IPC_GET_DEVICE(call),
    867                     ETH_LOCAL_ADDR, &address));
     874                rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_LOCAL_ADDR,
     875                    &address);
     876                if (rc != EOK)
     877                        return rc;
    868878                return measured_strings_reply(address, 1);
    869879        case NET_NIL_BROADCAST_ADDR:
    870                 ERROR_PROPAGATE(eth_addr_message(IPC_GET_DEVICE(call),
    871                     ETH_BROADCAST_ADDR, &address));
     880                rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_BROADCAST_ADDR,
     881                    &address);
     882                if (rc != EOK)
     883                        return EOK;
    872884                return measured_strings_reply(address, 1);
    873885        case IPC_M_CONNECT_TO_ME:
    874                 return eth_register_message(NIL_GET_PROTO(call),
    875                     IPC_GET_PHONE(call));
     886                return eth_register_message(NIL_GET_PROTO(*call),
     887                    IPC_GET_PHONE(*call));
    876888        }
    877889       
     
    879891}
    880892
    881 /** Default thread for new connections.
    882  *
    883  * @param[in] iid       The initial message identifier.
    884  * @param[in] icall     The initial message call structure.
    885  *
    886  */
    887 static void nil_client_connection(ipc_callid_t iid, ipc_call_t *icall)
    888 {
    889         /*
    890          * Accept the connection
    891          *  - Answer the first IPC_M_CONNECT_ME_TO call.
    892          */
    893         ipc_answer_0(iid, EOK);
    894        
    895         while (true) {
    896                 ipc_call_t answer;
    897                 int answer_count;
    898                
    899                 /* Clear the answer structure */
    900                 refresh_answer(&answer, &answer_count);
    901                
    902                 /* Fetch the next message */
    903                 ipc_call_t call;
    904                 ipc_callid_t callid = async_get_call(&call);
    905                
    906                 /* Process the message */
    907                 int res = nil_module_message_standalone(NAME, callid, &call,
    908                     &answer, &answer_count);
    909                
    910                 /*
    911                  * End if told to either by the message or the processing
    912                  * result.
    913                  */
    914                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
    915                     (res == EHANGUP))
    916                         return;
    917                
    918                 /* Answer the message */
    919                 answer_call(callid, res, &answer, answer_count);
    920         }
    921 }
    922 
    923893int main(int argc, char *argv[])
    924894{
    925         ERROR_DECLARE;
    926        
    927895        /* Start the module */
    928         ERROR_PROPAGATE(nil_module_start_standalone(nil_client_connection));
    929         return EOK;
     896        return nil_module_start(SERVICE_ETHERNET);
    930897}
    931898
Note: See TracChangeset for help on using the changeset viewer.