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


Ignore:
Timestamp:
2011-02-03T05:11:01Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ba38f72c
Parents:
22027b6e (diff), 86d7bfa (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

    r22027b6e r8b5690f  
    3939#include <stdio.h>
    4040#include <str.h>
    41 
    42 #include <ipc/ipc.h>
    4341#include <ipc/services.h>
    4442#include <ipc/nil.h>
    45 
    4643#include <net/modules.h>
    4744#include <adt/measured_strings.h>
    4845#include <packet_client.h>
    4946#include <net/device.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
     47#include <netif_skel.h>
     48#include <nil_remote.h>
    5649
    5750/** Default address length. */
     
    6154#define NAME  "lo"
    6255
    63 /** Network interface global data. */
    64 netif_globals_t netif_globals;
     56static uint8_t default_addr[DEFAULT_ADDR_LEN] =
     57    {0, 0, 0, 0, 0, 0};
    6558
    6659int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
    67     ipc_call_t *answer, int *answer_count)
     60    ipc_call_t *answer, size_t *count)
    6861{
    6962        return ENOTSUP;
     
    7568                return EBADMEM;
    7669       
    77         uint8_t *addr = (uint8_t *) malloc(DEFAULT_ADDR_LEN);
    78         memset(addr, DEFAULT_ADDR, DEFAULT_ADDR_LEN);
    79        
    80         address->value = addr;
     70        address->value = default_addr;
    8171        address->length = DEFAULT_ADDR_LEN;
    8272       
     
    8676int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
    8777{
    88         netif_device_t *device;
    89         int rc;
    90 
    9178        if (!stats)
    9279                return EBADMEM;
    93 
    94         rc = find_device(device_id, &device);
     80       
     81        netif_device_t *device;
     82        int rc = find_device(device_id, &device);
    9583        if (rc != EOK)
    9684                return rc;
    97 
     85       
    9886        memcpy(stats, (device_stats_t *) device->specific,
    9987            sizeof(device_stats_t));
    100 
    101         return EOK;
    102 }
    103 
    104 /** Changes the loopback state.
    105  *
    106  * @param[in] device    The device structure.
    107  * @param[in] state     The new device state.
    108  * @return              The new state if changed.
    109  * @return              EOK otherwise.
    110  */
    111 static int change_state_message(netif_device_t *device, device_state_t state)
     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 */
     101static void change_state_message(netif_device_t *device, device_state_t state)
    112102{
    113103        if (device->state != state) {
    114104                device->state = state;
    115105               
    116                 printf("%s: State changed to %s\n", NAME,
    117                     (state == NETIF_ACTIVE) ? "active" : "stopped");
     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                }
    118117               
    119                 return state;
    120         }
    121        
    122         return EOK;
    123 }
    124 
    125 /** Creates and returns the loopback network interface structure.
    126  *
    127  * @param[in] device_id The new devce identifier.
    128  * @param[out] device   The device structure.
    129  * @return              EOK on success.
    130  * @return              EXDEV if one loopback network interface already exists.
    131  * @return              ENOMEM if there is not enough memory left.
    132  */
    133 static int create(device_id_t device_id, netif_device_t **device)
    134 {
    135         int index;
    136 
     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 */
     132static int lo_create(device_id_t device_id, netif_device_t **device)
     133{
    137134        if (netif_device_map_count(&netif_globals.device_map) > 0)
    138135                return EXDEV;
    139 
     136       
    140137        *device = (netif_device_t *) malloc(sizeof(netif_device_t));
    141138        if (!*device)
    142139                return ENOMEM;
    143 
     140       
    144141        (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
    145142        if (!(*device)->specific) {
     
    147144                return ENOMEM;
    148145        }
    149 
     146       
    150147        null_device_stats((device_stats_t *) (*device)->specific);
    151148        (*device)->device_id = device_id;
    152149        (*device)->nil_phone = -1;
    153150        (*device)->state = NETIF_STOPPED;
    154         index = netif_device_map_add(&netif_globals.device_map,
     151        int index = netif_device_map_add(&netif_globals.device_map,
    155152            (*device)->device_id, *device);
    156 
     153       
    157154        if (index < 0) {
    158155                free(*device);
     
    167164int netif_initialize(void)
    168165{
    169         sysarg_t phonehash;
    170 
    171         return ipc_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, &phonehash);
    172 }
    173 
    174 int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
    175 {
     166        return async_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, NULL);
     167}
     168
     169int netif_probe_message(device_id_t device_id, int irq, void *io)
     170{
     171        /* Create a new device */
    176172        netif_device_t *device;
    177         int rc;
    178 
    179         /* Create a new device */
    180         rc = create(device_id, &device);
     173        int rc = lo_create(device_id, &device);
    181174        if (rc != EOK)
    182175                return rc;
    183 
    184         /* Print the settings */
     176       
    185177        printf("%s: Device created (id: %d)\n", NAME, device->device_id);
    186 
    187178        return EOK;
    188179}
     
    191182{
    192183        netif_device_t *device;
    193         size_t length;
    194         packet_t *next;
    195         int phone;
    196         int rc;
    197 
    198         rc = find_device(device_id, &device);
     184        int rc = find_device(device_id, &device);
    199185        if (rc != EOK)
    200186                return EOK;
    201 
     187       
    202188        if (device->state != NETIF_ACTIVE) {
    203189                netif_pq_release(packet_get_id(packet));
    204190                return EFORWARD;
    205191        }
    206 
    207         next = packet;
     192       
     193        packet_t *next = packet;
    208194        do {
    209195                ((device_stats_t *) device->specific)->send_packets++;
    210196                ((device_stats_t *) device->specific)->receive_packets++;
    211                 length = packet_get_data_length(next);
     197                size_t length = packet_get_data_length(next);
    212198                ((device_stats_t *) device->specific)->send_bytes += length;
    213199                ((device_stats_t *) device->specific)->receive_bytes += length;
    214200                next = pq_next(next);
    215         } while(next);
    216 
    217         phone = device->nil_phone;
     201        } while (next);
     202       
     203        int phone = device->nil_phone;
    218204        fibril_rwlock_write_unlock(&netif_globals.lock);
     205       
    219206        nil_received_msg(phone, device_id, packet, sender);
     207       
    220208        fibril_rwlock_write_lock(&netif_globals.lock);
    221        
    222209        return EOK;
    223210}
     
    225212int netif_start_message(netif_device_t *device)
    226213{
    227         return change_state_message(device, NETIF_ACTIVE);
     214        change_state_message(device, NETIF_ACTIVE);
     215        return device->state;
    228216}
    229217
    230218int netif_stop_message(netif_device_t *device)
    231219{
    232         return change_state_message(device, NETIF_STOPPED);
    233 }
    234 
    235 /** Default thread for new connections.
    236  *
    237  * @param[in] iid       The initial message identifier.
    238  * @param[in] icall     The initial message call structure.
    239  */
    240 static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
    241 {
    242         /*
    243          * Accept the connection
    244          *  - Answer the first IPC_M_CONNECT_ME_TO call.
    245          */
    246         ipc_answer_0(iid, EOK);
    247        
    248         while (true) {
    249                 ipc_call_t answer;
    250                 int answer_count;
    251                
    252                 /* Clear the answer structure */
    253                 refresh_answer(&answer, &answer_count);
    254                
    255                 /* Fetch the next message */
    256                 ipc_call_t call;
    257                 ipc_callid_t callid = async_get_call(&call);
    258                
    259                 /* Process the message */
    260                 int res = netif_module_message(NAME, callid, &call, &answer,
    261                     &answer_count);
    262                
    263                 /*
    264                  * End if told to either by the message or the processing
    265                  * result.
    266                  */
    267                 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||
    268                     (res == EHANGUP))
    269                         return;
    270                
    271                 /* Answer the message */
    272                 answer_call(callid, res, &answer, answer_count);
    273         }
     220        change_state_message(device, NETIF_STOPPED);
     221        return device->state;
    274222}
    275223
    276224int main(int argc, char *argv[])
    277225{
    278         int rc;
    279        
    280226        /* Start the module */
    281         rc = netif_module_start(netif_client_connection);
    282         return rc;
     227        return netif_module_start();
    283228}
    284229
Note: See TracChangeset for help on using the changeset viewer.