Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/netif/lo/lo.c

    rffa2c8ef r46d4d9f  
    3939#include <stdio.h>
    4040#include <str.h>
     41
     42#include <ipc/ipc.h>
    4143#include <ipc/services.h>
    4244#include <ipc/nil.h>
     45
    4346#include <net/modules.h>
    4447#include <adt/measured_strings.h>
    4548#include <packet_client.h>
    4649#include <net/device.h>
    47 #include <netif_skel.h>
    48 #include <nil_remote.h>
     50#include <nil_interface.h>
     51#include <netif_interface.h>
     52#include <netif_local.h>
     53
     54/** Default hardware address. */
     55#define DEFAULT_ADDR            "\0\0\0\0\0\0"
    4956
    5057/** Default address length. */
    51 #define DEFAULT_ADDR_LEN  6
     58#define DEFAULT_ADDR_LEN        (sizeof(DEFAULT_ADDR) / sizeof(char))
    5259
    5360/** Loopback module name. */
    5461#define NAME  "lo"
    5562
    56 static uint8_t default_addr[DEFAULT_ADDR_LEN] =
    57     {0, 0, 0, 0, 0, 0};
     63/** Network interface global data. */
     64netif_globals_t netif_globals;
    5865
    5966int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
    60     ipc_call_t *answer, size_t *count)
     67    ipc_call_t *answer, int *answer_count)
    6168{
    6269        return ENOTSUP;
     
    6774        if (!address)
    6875                return EBADMEM;
    69        
    70         address->value = default_addr;
     76
     77        address->value = str_dup(DEFAULT_ADDR);
    7178        address->length = DEFAULT_ADDR_LEN;
    72        
     79
    7380        return EOK;
    7481}
     
    7683int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
    7784{
     85        netif_device_t *device;
     86        int rc;
     87
    7888        if (!stats)
    7989                return EBADMEM;
    80        
    81         netif_device_t *device;
    82         int rc = find_device(device_id, &device);
     90
     91        rc = find_device(device_id, &device);
    8392        if (rc != EOK)
    8493                return rc;
    85        
     94
    8695        memcpy(stats, (device_stats_t *) device->specific,
    8796            sizeof(device_stats_t));
    88        
    89         return EOK;
    90 }
    91 
    92 /** Change the loopback state.
    93  *
    94  * @param[in] device The device structure.
    95  * @param[in] state  The new device state.
    96  *
    97  * @return New state if changed.
    98  * @return EOK otherwise.
    99  *
    100  */
    101 static void change_state_message(netif_device_t *device, device_state_t state)
     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)
    102109{
    103110        if (device->state != state) {
    104111                device->state = state;
    105112               
    106                 const char *desc;
    107                 switch (state) {
    108                 case NETIF_ACTIVE:
    109                         desc = "active";
    110                         break;
    111                 case NETIF_STOPPED:
    112                         desc = "stopped";
    113                         break;
    114                 default:
    115                         desc = "unknown";
    116                 }
    117                
    118                 printf("%s: State changed to %s\n", NAME, desc);
    119         }
    120 }
    121 
    122 /** Create and return the loopback network interface structure.
    123  *
    124  * @param[in]  device_id New devce identifier.
    125  * @param[out] device    Device structure.
    126  *
    127  * @return EOK on success.
    128  * @return EXDEV if one loopback network interface already exists.
    129  * @return ENOMEM if there is not enough memory left.
    130  *
    131  */
    132 static int lo_create(device_id_t device_id, netif_device_t **device)
    133 {
     113                printf("%s: State changed to %s\n", NAME,
     114                    (state == NETIF_ACTIVE) ? "active" : "stopped");
     115               
     116                return state;
     117        }
     118       
     119        return EOK;
     120}
     121
     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{
     132        int index;
     133
    134134        if (netif_device_map_count(&netif_globals.device_map) > 0)
    135135                return EXDEV;
    136        
     136
    137137        *device = (netif_device_t *) malloc(sizeof(netif_device_t));
    138138        if (!*device)
    139139                return ENOMEM;
    140        
     140
    141141        (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
    142142        if (!(*device)->specific) {
     
    144144                return ENOMEM;
    145145        }
    146        
     146
    147147        null_device_stats((device_stats_t *) (*device)->specific);
    148148        (*device)->device_id = device_id;
    149149        (*device)->nil_phone = -1;
    150150        (*device)->state = NETIF_STOPPED;
    151         int index = netif_device_map_add(&netif_globals.device_map,
     151        index = netif_device_map_add(&netif_globals.device_map,
    152152            (*device)->device_id, *device);
    153        
     153
    154154        if (index < 0) {
    155155                free(*device);
     
    164164int netif_initialize(void)
    165165{
    166         return async_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, NULL);
    167 }
    168 
    169 int netif_probe_message(device_id_t device_id, int irq, void *io)
    170 {
     166        ipcarg_t phonehash;
     167
     168        return REGISTER_ME(SERVICE_LO, &phonehash);
     169}
     170
     171int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
     172{
     173        netif_device_t *device;
     174        int rc;
     175
    171176        /* Create a new device */
    172         netif_device_t *device;
    173         int rc = lo_create(device_id, &device);
     177        rc = create(device_id, &device);
    174178        if (rc != EOK)
    175179                return rc;
    176        
     180
     181        /* Print the settings */
    177182        printf("%s: Device created (id: %d)\n", NAME, device->device_id);
     183
    178184        return EOK;
    179185}
     
    182188{
    183189        netif_device_t *device;
    184         int rc = find_device(device_id, &device);
     190        size_t length;
     191        packet_t *next;
     192        int phone;
     193        int rc;
     194
     195        rc = find_device(device_id, &device);
    185196        if (rc != EOK)
    186197                return EOK;
    187        
     198
    188199        if (device->state != NETIF_ACTIVE) {
    189200                netif_pq_release(packet_get_id(packet));
    190201                return EFORWARD;
    191202        }
    192        
    193         packet_t *next = packet;
     203
     204        next = packet;
    194205        do {
    195206                ((device_stats_t *) device->specific)->send_packets++;
    196207                ((device_stats_t *) device->specific)->receive_packets++;
    197                 size_t length = packet_get_data_length(next);
     208                length = packet_get_data_length(next);
    198209                ((device_stats_t *) device->specific)->send_bytes += length;
    199210                ((device_stats_t *) device->specific)->receive_bytes += length;
    200211                next = pq_next(next);
    201         } while (next);
    202        
    203         int phone = device->nil_phone;
     212        } while(next);
     213
     214        phone = device->nil_phone;
    204215        fibril_rwlock_write_unlock(&netif_globals.lock);
    205        
    206216        nil_received_msg(phone, device_id, packet, sender);
    207        
    208217        fibril_rwlock_write_lock(&netif_globals.lock);
     218       
    209219        return EOK;
    210220}
     
    212222int netif_start_message(netif_device_t *device)
    213223{
    214         change_state_message(device, NETIF_ACTIVE);
    215         return device->state;
     224        return change_state_message(device, NETIF_ACTIVE);
    216225}
    217226
    218227int netif_stop_message(netif_device_t *device)
    219228{
    220         change_state_message(device, NETIF_STOPPED);
    221         return device->state;
     229        return change_state_message(device, NETIF_STOPPED);
     230}
     231
     232/** Default thread for new connections.
     233 *
     234 * @param[in] iid       The initial message identifier.
     235 * @param[in] icall     The initial message call structure.
     236 */
     237static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
     238{
     239        /*
     240         * Accept the connection
     241         *  - Answer the first IPC_M_CONNECT_ME_TO call.
     242         */
     243        ipc_answer_0(iid, EOK);
     244       
     245        while (true) {
     246                ipc_call_t answer;
     247                int answer_count;
     248               
     249                /* Clear the answer structure */
     250                refresh_answer(&answer, &answer_count);
     251               
     252                /* Fetch the next message */
     253                ipc_call_t call;
     254                ipc_callid_t callid = async_get_call(&call);
     255               
     256                /* Process the message */
     257                int res = netif_module_message(NAME, callid, &call, &answer,
     258                    &answer_count);
     259               
     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))
     266                        return;
     267               
     268                /* Answer the message */
     269                answer_call(callid, res, &answer, answer_count);
     270        }
    222271}
    223272
    224273int main(int argc, char *argv[])
    225274{
     275        int rc;
     276       
    226277        /* Start the module */
    227         return netif_module_start();
     278        rc = netif_module_start(netif_client_connection);
     279        return rc;
    228280}
    229281
Note: See TracChangeset for help on using the changeset viewer.