Changeset 8fb1bf82 in mainline for uspace/srv/net/netif/lo/lo.c


Ignore:
Timestamp:
2010-11-25T13:42:50Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8df8415
Parents:
a93d79a (diff), eb667613 (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/netif/lo/lo.c

    ra93d79a r8fb1bf82  
    2828
    2929/** @addtogroup lo
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Loopback network interface implementation.
     34 * Loopback network interface implementation.
    3535 */
    3636
    3737#include <async.h>
    3838#include <errno.h>
    39 #include <err.h>
    4039#include <stdio.h>
    4140#include <str.h>
     
    5352#include <netif_local.h>
    5453
    55 /** Default hardware address.
    56  */
     54/** Default hardware address. */
    5755#define DEFAULT_ADDR            "\0\0\0\0\0\0"
    5856
    59 /** Default address length.
    60  */
     57/** Default address length. */
    6158#define DEFAULT_ADDR_LEN        (sizeof(DEFAULT_ADDR) / sizeof(char))
    6259
    63 /** Loopback module name.
    64  */
     60/** Loopback module name. */
    6561#define NAME  "lo"
    6662
    67 /** Network interface global data.
    68  */
     63/** Network interface global data. */
    6964netif_globals_t netif_globals;
    7065
    71 /** Changes the loopback state.
    72  *  @param[in] device The device structure.
    73  *  @param[in] state The new device state.
    74  *  @returns The new state if changed.
    75  *  @returns EOK otherwise.
    76  */
    77 int change_state_message(netif_device_t * device, device_state_t state);
    78 
    79 /** Creates and returns the loopback network interface structure.
    80  *  @param[in] device_id The new devce identifier.
    81  *  @param[out] device The device structure.
    82  *  @returns EOK on success.
    83  *  @returns EXDEV if one loopback network interface already exists.
    84  *  @returns ENOMEM if there is not enough memory left.
    85  */
    86 int create(device_id_t device_id, netif_device_t * * device);
    87 
    88 int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
     66int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
     67    ipc_call_t *answer, int *answer_count)
     68{
    8969        return ENOTSUP;
    9070}
    9171
    92 int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
    93         if(! address){
     72int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
     73{
     74        if (!address)
    9475                return EBADMEM;
    95         }
     76
    9677        address->value = str_dup(DEFAULT_ADDR);
    9778        address->length = DEFAULT_ADDR_LEN;
    98         return EOK;
    99 }
    100 
    101 int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
    102         ERROR_DECLARE;
    103 
    104         netif_device_t * device;
    105 
    106         if(! stats){
     79
     80        return EOK;
     81}
     82
     83int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
     84{
     85        netif_device_t *device;
     86        int rc;
     87
     88        if (!stats)
    10789                return EBADMEM;
    108         }
    109         ERROR_PROPAGATE(find_device(device_id, &device));
    110         memcpy(stats, (device_stats_ref) device->specific, sizeof(device_stats_t));
    111         return EOK;
    112 }
    113 
    114 int change_state_message(netif_device_t * device, device_state_t state)
     90
     91        rc = find_device(device_id, &device);
     92        if (rc != EOK)
     93                return rc;
     94
     95        memcpy(stats, (device_stats_t *) device->specific,
     96            sizeof(device_stats_t));
     97
     98        return EOK;
     99}
     100
     101/** Changes the loopback state.
     102 *
     103 * @param[in] device    The device structure.
     104 * @param[in] state     The new device state.
     105 * @return              The new state if changed.
     106 * @return              EOK otherwise.
     107 */
     108static int change_state_message(netif_device_t *device, device_state_t state)
    115109{
    116110        if (device->state != state) {
     
    126120}
    127121
    128 int create(device_id_t device_id, netif_device_t * * device){
     122/** Creates and returns the loopback network interface structure.
     123 *
     124 * @param[in] device_id The new devce identifier.
     125 * @param[out] device   The device structure.
     126 * @return              EOK on success.
     127 * @return              EXDEV if one loopback network interface already exists.
     128 * @return              ENOMEM if there is not enough memory left.
     129 */
     130static int create(device_id_t device_id, netif_device_t **device)
     131{
    129132        int index;
    130133
    131         if(netif_device_map_count(&netif_globals.device_map) > 0){
     134        if (netif_device_map_count(&netif_globals.device_map) > 0)
    132135                return EXDEV;
    133         }else{
    134                 *device = (netif_device_t *) malloc(sizeof(netif_device_t));
    135                 if(!(*device)){
    136                         return ENOMEM;
    137                 }
    138                 (** device).specific = malloc(sizeof(device_stats_t));
    139                 if(! (** device).specific){
    140                         free(*device);
    141                         return ENOMEM;
    142                 }
    143                 null_device_stats((device_stats_ref)(** device).specific);
    144                 (** device).device_id = device_id;
    145                 (** device).nil_phone = -1;
    146                 (** device).state = NETIF_STOPPED;
    147                 index = netif_device_map_add(&netif_globals.device_map, (** device).device_id, * device);
    148                 if(index < 0){
    149                         free(*device);
    150                         free((** device).specific);
    151                         *device = NULL;
    152                         return index;
    153                 }
    154         }
    155         return EOK;
    156 }
    157 
    158 int netif_initialize(void){
     136
     137        *device = (netif_device_t *) malloc(sizeof(netif_device_t));
     138        if (!*device)
     139                return ENOMEM;
     140
     141        (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
     142        if (!(*device)->specific) {
     143                free(*device);
     144                return ENOMEM;
     145        }
     146
     147        null_device_stats((device_stats_t *) (*device)->specific);
     148        (*device)->device_id = device_id;
     149        (*device)->nil_phone = -1;
     150        (*device)->state = NETIF_STOPPED;
     151        index = netif_device_map_add(&netif_globals.device_map,
     152            (*device)->device_id, *device);
     153
     154        if (index < 0) {
     155                free(*device);
     156                free((*device)->specific);
     157                *device = NULL;
     158                return index;
     159        }
     160       
     161        return EOK;
     162}
     163
     164int netif_initialize(void)
     165{
    159166        ipcarg_t phonehash;
    160167
     
    162169}
    163170
    164 int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
    165         ERROR_DECLARE;
    166 
    167         netif_device_t * device;
    168 
    169         // create a new device
    170         ERROR_PROPAGATE(create(device_id, &device));
    171         // print the settings
     171int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
     172{
     173        netif_device_t *device;
     174        int rc;
     175
     176        /* Create a new device */
     177        rc = create(device_id, &device);
     178        if (rc != EOK)
     179                return rc;
     180
     181        /* Print the settings */
    172182        printf("%s: Device created (id: %d)\n", NAME, device->device_id);
    173         return EOK;
    174 }
    175 
    176 int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
    177         ERROR_DECLARE;
    178 
    179         netif_device_t * device;
     183
     184        return EOK;
     185}
     186
     187int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
     188{
     189        netif_device_t *device;
    180190        size_t length;
    181         packet_t next;
     191        packet_t *next;
    182192        int phone;
    183 
    184         ERROR_PROPAGATE(find_device(device_id, &device));
    185         if(device->state != NETIF_ACTIVE){
     193        int rc;
     194
     195        rc = find_device(device_id, &device);
     196        if (rc != EOK)
     197                return EOK;
     198
     199        if (device->state != NETIF_ACTIVE) {
    186200                netif_pq_release(packet_get_id(packet));
    187201                return EFORWARD;
    188202        }
     203
    189204        next = packet;
    190         do{
    191                 ++ ((device_stats_ref) device->specific)->send_packets;
    192                 ++ ((device_stats_ref) device->specific)->receive_packets;
     205        do {
     206                ((device_stats_t *) device->specific)->send_packets++;
     207                ((device_stats_t *) device->specific)->receive_packets++;
    193208                length = packet_get_data_length(next);
    194                 ((device_stats_ref) device->specific)->send_bytes += length;
    195                 ((device_stats_ref) device->specific)->receive_bytes += length;
     209                ((device_stats_t *) device->specific)->send_bytes += length;
     210                ((device_stats_t *) device->specific)->receive_bytes += length;
    196211                next = pq_next(next);
    197         }while(next);
     212        } while(next);
     213
    198214        phone = device->nil_phone;
    199215        fibril_rwlock_write_unlock(&netif_globals.lock);
    200216        nil_received_msg(phone, device_id, packet, sender);
    201217        fibril_rwlock_write_lock(&netif_globals.lock);
    202         return EOK;
    203 }
    204 
    205 int netif_start_message(netif_device_t * device){
     218       
     219        return EOK;
     220}
     221
     222int netif_start_message(netif_device_t *device)
     223{
    206224        return change_state_message(device, NETIF_ACTIVE);
    207225}
    208226
    209 int netif_stop_message(netif_device_t * device){
     227int netif_stop_message(netif_device_t *device)
     228{
    210229        return change_state_message(device, NETIF_STOPPED);
    211230}
     
    213232/** Default thread for new connections.
    214233 *
    215  * @param[in] iid The initial message identifier.
    216  * @param[in] icall The initial message call structure.
    217  *
     234 * @param[in] iid       The initial message identifier.
     235 * @param[in] icall     The initial message call structure.
    218236 */
    219237static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
     
    225243        ipc_answer_0(iid, EOK);
    226244       
    227         while(true) {
     245        while (true) {
    228246                ipc_call_t answer;
    229247                int answer_count;
     
    240258                    &answer_count);
    241259               
    242                 /* End if said to either by the message or the processing result */
    243                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
     260                /*
     261                 * End if told to either by the message or the processing
     262                 * result.
     263                 */
     264                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
     265                    (res == EHANGUP))
    244266                        return;
    245267               
     
    251273int main(int argc, char *argv[])
    252274{
    253         ERROR_DECLARE;
     275        int rc;
    254276       
    255277        /* Start the module */
    256         if (ERROR_OCCURRED(netif_module_start(netif_client_connection)))
    257                 return ERROR_CODE;
    258        
    259         return EOK;
     278        rc = netif_module_start(netif_client_connection);
     279        return rc;
    260280}
    261281
Note: See TracChangeset for help on using the changeset viewer.