Changeset b5e68c8 in mainline for uspace/srv/net/netif/lo/lo.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/netif/lo/lo.c

    re80329d6 rb5e68c8  
    3737#include <async.h>
    3838#include <errno.h>
    39 #include <err.h>
    4039#include <stdio.h>
    4140#include <str.h>
    42 
    43 #include <ipc/ipc.h>
    4441#include <ipc/services.h>
    4542#include <ipc/nil.h>
    46 
    4743#include <net/modules.h>
    4844#include <adt/measured_strings.h>
    4945#include <packet_client.h>
    5046#include <net/device.h>
    51 #include <nil_interface.h>
    52 #include <netif_interface.h>
    53 #include <netif_local.h>
    54 
    55 /** Default hardware address. */
    56 #define DEFAULT_ADDR            "\0\0\0\0\0\0"
     47#include <netif_skel.h>
     48#include <nil_remote.h>
    5749
    5850/** Default address length. */
    59 #define DEFAULT_ADDR_LEN        (sizeof(DEFAULT_ADDR) / sizeof(char))
     51#define DEFAULT_ADDR_LEN  6
    6052
    6153/** Loopback module name. */
    6254#define NAME  "lo"
    6355
    64 /** Network interface global data. */
    65 netif_globals_t netif_globals;
    66 
    67 int
    68 netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
    69     ipc_call_t *answer, int *answer_count)
     56static uint8_t default_addr[DEFAULT_ADDR_LEN] =
     57    {0, 0, 0, 0, 0, 0};
     58
     59int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
     60    ipc_call_t *answer, size_t *count)
    7061{
    7162        return ENOTSUP;
    7263}
    7364
    74 int netif_get_addr_message(device_id_t device_id, measured_string_ref address)
     65int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
    7566{
    7667        if (!address)
    7768                return EBADMEM;
    78         address->value = str_dup(DEFAULT_ADDR);
     69       
     70        address->value = default_addr;
    7971        address->length = DEFAULT_ADDR_LEN;
    80         return EOK;
    81 }
    82 
    83 int netif_get_device_stats(device_id_t device_id, device_stats_ref stats)
    84 {
    85         ERROR_DECLARE;
    86 
    87         netif_device_t *device;
    88 
     72       
     73        return EOK;
     74}
     75
     76int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
     77{
    8978        if (!stats)
    9079                return EBADMEM;
    91         ERROR_PROPAGATE(find_device(device_id, &device));
    92         memcpy(stats, (device_stats_ref) device->specific,
     80       
     81        netif_device_t *device;
     82        int rc = find_device(device_id, &device);
     83        if (rc != EOK)
     84                return rc;
     85       
     86        memcpy(stats, (device_stats_t *) device->specific,
    9387            sizeof(device_stats_t));
    94         return EOK;
    95 }
    96 
    97 /** Changes the loopback state.
    98  *
    99  * @param[in] device    The device structure.
    100  * @param[in] state     The new device state.
    101  * @returns             The new state if changed.
    102  * @returns             EOK otherwise.
    103  */
    104 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)
    105102{
    106103        if (device->state != state) {
    107104                device->state = state;
    108105               
    109                 printf("%s: State changed to %s\n", NAME,
    110                     (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                }
    111117               
    112                 return state;
    113         }
    114        
    115         return EOK;
    116 }
    117 
    118 /** Creates and returns the loopback network interface structure.
    119  *
    120  * @param[in] device_id The new devce identifier.
    121  * @param[out] device   The device structure.
    122  * @returns             EOK on success.
    123  * @returns             EXDEV if one loopback network interface already exists.
    124  * @returns             ENOMEM if there is not enough memory left.
    125  */
    126 static int create(device_id_t device_id, netif_device_t **device)
    127 {
    128         int index;
    129 
     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{
    130134        if (netif_device_map_count(&netif_globals.device_map) > 0)
    131135                return EXDEV;
    132 
     136       
    133137        *device = (netif_device_t *) malloc(sizeof(netif_device_t));
    134138        if (!*device)
    135139                return ENOMEM;
     140       
    136141        (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
    137142        if (!(*device)->specific) {
     
    139144                return ENOMEM;
    140145        }
    141         null_device_stats((device_stats_ref) (*device)->specific);
     146       
     147        null_device_stats((device_stats_t *) (*device)->specific);
    142148        (*device)->device_id = device_id;
    143149        (*device)->nil_phone = -1;
    144150        (*device)->state = NETIF_STOPPED;
    145         index = netif_device_map_add(&netif_globals.device_map,
     151        int index = netif_device_map_add(&netif_globals.device_map,
    146152            (*device)->device_id, *device);
     153       
    147154        if (index < 0) {
    148155                free(*device);
     
    157164int netif_initialize(void)
    158165{
    159         ipcarg_t phonehash;
    160 
    161         return REGISTER_ME(SERVICE_LO, &phonehash);
    162 }
    163 
    164 int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
    165 {
    166         ERROR_DECLARE;
    167 
     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 */
    168172        netif_device_t *device;
    169 
    170         // create a new device
    171         ERROR_PROPAGATE(create(device_id, &device));
    172         // print the settings
     173        int rc = lo_create(device_id, &device);
     174        if (rc != EOK)
     175                return rc;
     176       
    173177        printf("%s: Device created (id: %d)\n", NAME, device->device_id);
    174178        return EOK;
    175179}
    176180
    177 int netif_send_message(device_id_t device_id, packet_t packet, services_t sender)
    178 {
    179         ERROR_DECLARE;
    180 
     181int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
     182{
    181183        netif_device_t *device;
    182         size_t length;
    183         packet_t next;
    184         int phone;
    185 
    186         ERROR_PROPAGATE(find_device(device_id, &device));
     184        int rc = find_device(device_id, &device);
     185        if (rc != EOK)
     186                return EOK;
     187       
    187188        if (device->state != NETIF_ACTIVE) {
    188189                netif_pq_release(packet_get_id(packet));
    189190                return EFORWARD;
    190191        }
    191         next = packet;
     192       
     193        packet_t *next = packet;
    192194        do {
    193                 ((device_stats_ref) device->specific)->send_packets++;
    194                 ((device_stats_ref) device->specific)->receive_packets++;
    195                 length = packet_get_data_length(next);
    196                 ((device_stats_ref) device->specific)->send_bytes += length;
    197                 ((device_stats_ref) device->specific)->receive_bytes += length;
     195                ((device_stats_t *) device->specific)->send_packets++;
     196                ((device_stats_t *) device->specific)->receive_packets++;
     197                size_t length = packet_get_data_length(next);
     198                ((device_stats_t *) device->specific)->send_bytes += length;
     199                ((device_stats_t *) device->specific)->receive_bytes += length;
    198200                next = pq_next(next);
    199         } while(next);
    200         phone = device->nil_phone;
     201        } while (next);
     202       
     203        int phone = device->nil_phone;
    201204        fibril_rwlock_write_unlock(&netif_globals.lock);
     205       
    202206        nil_received_msg(phone, device_id, packet, sender);
     207       
    203208        fibril_rwlock_write_lock(&netif_globals.lock);
    204        
    205209        return EOK;
    206210}
     
    208212int netif_start_message(netif_device_t *device)
    209213{
    210         return change_state_message(device, NETIF_ACTIVE);
     214        change_state_message(device, NETIF_ACTIVE);
     215        return device->state;
    211216}
    212217
    213218int netif_stop_message(netif_device_t *device)
    214219{
    215         return change_state_message(device, NETIF_STOPPED);
    216 }
    217 
    218 /** Default thread for new connections.
    219  *
    220  * @param[in] iid       The initial message identifier.
    221  * @param[in] icall     The initial message call structure.
    222  */
    223 static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
    224 {
    225         /*
    226          * Accept the connection
    227          *  - Answer the first IPC_M_CONNECT_ME_TO call.
    228          */
    229         ipc_answer_0(iid, EOK);
    230        
    231         while (true) {
    232                 ipc_call_t answer;
    233                 int answer_count;
    234                
    235                 /* Clear the answer structure */
    236                 refresh_answer(&answer, &answer_count);
    237                
    238                 /* Fetch the next message */
    239                 ipc_call_t call;
    240                 ipc_callid_t callid = async_get_call(&call);
    241                
    242                 /* Process the message */
    243                 int res = netif_module_message(NAME, callid, &call, &answer,
    244                     &answer_count);
    245                
    246                 /*
    247                  * End if told to either by the message or the processing
    248                  * result.
    249                  */
    250                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
    251                     (res == EHANGUP))
    252                         return;
    253                
    254                 /* Answer the message */
    255                 answer_call(callid, res, &answer, answer_count);
    256         }
     220        change_state_message(device, NETIF_STOPPED);
     221        return device->state;
    257222}
    258223
    259224int main(int argc, char *argv[])
    260225{
    261         ERROR_DECLARE;
    262        
    263226        /* Start the module */
    264         ERROR_PROPAGATE(netif_module_start(netif_client_connection));
    265         return EOK;
     227        return netif_module_start();
    266228}
    267229
Note: See TracChangeset for help on using the changeset viewer.