netd: moving to a library
authorAdam Turowski <adam.turowski@inf.ethz.ch>
Mon, 16 Jan 2017 08:08:18 +0000 (09:08 +0100)
committerAdam Turowski <adam.turowski@inf.ethz.ch>
Thu, 19 Jan 2017 12:03:02 +0000 (13:03 +0100)
Signed-off-by: Adam Turowski <adam.turowski@inf.ethz.ch>

include/netd/netd.h [moved from usr/netd/netd.h with 57% similarity]
include/netd/netd_debug.h [moved from usr/netd/netd_debug.h with 100% similarity]
lib/netd/ARP_lookup_service.c [new file with mode: 0644]
lib/netd/Hakefile [new file with mode: 0644]
lib/netd/lwipinit.c [moved from usr/netd/lwipinit.c with 74% similarity]
lib/netd/netd.c [new file with mode: 0644]
lib/netd/netd_private.h [new file with mode: 0644]
usr/netd/ARP_lookup_service.c [deleted file]
usr/netd/Hakefile
usr/netd/main.c

similarity index 57%
rename from usr/netd/netd.h
rename to include/netd/netd.h
index 45b4185..10b77ff 100644 (file)
 #ifndef _NETD_H_
 #define _NETD_H_
 
-#include <netif/bfeth.h>
-#include <if/net_ARP_defs.h>
-
-#include <contmng/contmng.h>
-#include <procon/procon.h>
-
-extern bool do_dhcp;
-// IP information for static configuration
-extern char *ip_addr_str;
-extern char *netmask_str;
-extern char *gateway_str;
-extern char *dns_str;
-
-typedef net_ARP_ipv4addr_t ipv4addr_t;
-
-struct netif *netif_ptr;
+struct netd_state;
 
 /**
  * @brief initializes LWIP. not a lot left after I changed the subsystems
  *
  * @param card_name the of the card.
  */
-void startlwip(char *card_name, uint64_t queueid);
 
-int init_ARP_lookup_service(char *dev_name);
-#endif // _NETD_H_
+errval_t netd_init(struct netd_state **state, char *card_name, uint64_t queueid,
+    bool do_dhcp, char *ip_addr_str, char *netmask_str, char *gateway_str);
 
+#endif // _NETD_H_
diff --git a/lib/netd/ARP_lookup_service.c b/lib/netd/ARP_lookup_service.c
new file mode 100644 (file)
index 0000000..e785f90
--- /dev/null
@@ -0,0 +1,224 @@
+/*
+ * Copyright (c) 2007-12 ETH Zurich.
+ * All rights reserved.
+ *
+ * This file is distributed under the terms in the attached LICENSE file.
+ * If you do not find this file, copies can be found by writing to:
+ * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
+ */
+
+#include <barrelfish/barrelfish.h>
+#include <barrelfish/nameservice_client.h>
+#include <barrelfish/net_constants.h>
+#include <if/net_ARP_defs.h>
+
+#include <barrelfish/waitset.h>
+
+// standard libraries
+#include <stdio.h>
+#include <string.h>
+
+#include <lwip/init.h>
+#include <netif/etharp.h>
+
+// local includes
+#include "netd_private.h"
+#include <netd/netd_debug.h>
+
+// ***** Special cases ********
+// local ip address (your own ip address) (valid request)
+// broadcast ip address (invalid request)
+// multicast IP address (invalid request)
+//
+//
+//
+
+
+// How can I get my own MAC address
+// Where can I find the code which is looking up local MAC address?
+//    struct eth_addr *srcaddr = (struct eth_addr *) netif->hwaddr;
+//
+//
+//        return etharp_query(netif, ipaddr, q); // q is pbuf
+//        lib/lwip/src/netif/etharp.c
+
+//        find_entry
+//        src/netif/etharp.c
+
+/*****************************************************************
+* Prototypes
+*****************************************************************/
+
+static errval_t get_ip_info(struct net_ARP_binding *cc, uint32_t iface,
+                        errval_t *err, net_ARP_ipv4addr_t *ip,
+                        net_ARP_ipv4addr_t *gw, net_ARP_ipv4addr_t *mask);
+static errval_t ARP_resolve_request(struct net_ARP_binding *cc,
+            ipv4addr_t ip, uint32_t iface, bool force,
+            errval_t *err, uint64_t *mac);
+
+// service mappings
+static struct net_ARP_rpc_rx_vtbl rpc_rx_ARP_vtbl = {
+    .ip_info_call = get_ip_info,
+    .ARP_lookup_call = ARP_resolve_request,
+};
+
+
+/*****************************************************************
+* Dealing with new connections
+*****************************************************************/
+static errval_t connect_ARP_cb(void *st, struct net_ARP_binding *b)
+{
+    errval_t err = SYS_ERR_OK;
+    struct netd_state *state = st;
+    NETD_DEBUG("########### new application came in\n");
+
+    // using the b->st to store session specific data (net_user)
+    struct ARP_user_cl *new_app = malloc(sizeof(struct ARP_user_cl));
+
+    if (new_app == NULL) {
+        NETD_DEBUG("error: malloc failed...\n");
+        err = PORT_ERR_NOT_ENOUGH_MEMORY;
+        return err;
+    }
+
+    memset(new_app, 0, sizeof(struct ARP_user_cl));
+    new_app->state = state;
+    new_app->next = state->registered_user_list;
+    state->registered_user_list = new_app;
+    b->st = (void *) new_app;
+
+    b->rpc_rx_vtbl = rpc_rx_ARP_vtbl;
+    return err;
+} // end function: connect_ARP_cb
+
+
+/*****************************************************************
+* exporting service
+*****************************************************************/
+
+static void export_ARP_cb(void *st, errval_t err, iref_t iref)
+{
+    struct netd_state *state = st;
+
+    if (err_is_fail(err)) {
+        DEBUG_ERR(err, "service[%s] export failed", state->ARP_service_name);
+        abort(); // FIXME: Do I need abort after DEBUG_ERR?
+    }
+
+    NETD_DEBUG("service [%s] exported at iref %u\n", state->ARP_service_name, iref);
+
+    // register this iref with the name service
+    err = nameservice_register(state->ARP_service_name, iref);
+    if (err_is_fail(err)) {
+        DEBUG_ERR(err, "nameservice_register failed for [%s]", state->ARP_service_name);
+        abort(); // FIXME: Do I need abort after DEBUG_ERR?
+    }
+    state->ARP_service_exported = true;
+    NETD_DEBUG("service [%s] export successful!\n", state->ARP_service_name);
+} // end function: export_ARP_cb
+
+
+
+// ************************************************************************
+//                 ARP lookup interface function
+// ************************************************************************
+
+static errval_t get_ip_info(struct net_ARP_binding *cc, uint32_t iface,
+                        errval_t *err, net_ARP_ipv4addr_t *ip,
+                        net_ARP_ipv4addr_t *gw, net_ARP_ipv4addr_t *mask)
+{
+    printf("####### get IP info called ######\n");
+    NETD_DEBUG("get_ip_info: client asking for ip over %"PRIu32"\n", iface);
+    struct ARP_user_cl *app = cc->st;
+    struct netd_state *state = app->state;
+
+    *err = SYS_ERR_OK;
+    *ip = state->netif_ptr->ip_addr.addr;
+    *gw = state->netif_ptr->gw.addr;
+    *mask = state->netif_ptr->netmask.addr;
+    NETD_DEBUG("get_ip_info: terminating\n");
+    return SYS_ERR_OK;
+}
+
+static uint64_t refresh_cache(uint32_t dst_ip_addr)
+{
+    struct ip_addr dst_ip;
+    struct netif *netif;
+
+    dst_ip.addr = dst_ip_addr;
+    netif = ip_route(&dst_ip);
+
+    NETD_DEBUG("refresh_cache: calling etharp_request\n");
+    errval_t r = etharp_request(netif, &dst_ip);
+    assert(err_is_ok(r));
+
+    struct waitset *ws = NULL;
+    ws = get_default_waitset();
+    while (is_ip_present_in_arp_cache(&dst_ip) == false) {
+//        NETD_DEBUG("refresh_arp_cache: event dispatched\n");
+        r = event_dispatch(ws);
+        if (err_is_fail(r)) {
+            DEBUG_ERR(r, "in event_dispatch");
+            abort();
+        }
+   } // end while: till arp not present
+   return find_ip_arp_cache(&dst_ip);
+}
+
+static errval_t ARP_resolve_request(struct net_ARP_binding *cc,
+            ipv4addr_t ip, uint32_t iface, bool force,
+            errval_t *err, uint64_t *mac)
+{
+    NETD_DEBUG("ARP_resolve_request: client asking ARP lookup for ip %"
+            PRIu32" over iface %"PRIu32"\n", ip, iface);
+
+    *mac = refresh_cache(ip);
+    assert(*mac != 0);
+//    assert(!"NYI ARP resolve request");
+    NETD_DEBUG("ARP_resolve_request: MAC found for ARP request ip %"
+            PRIu32" over iface %"PRIu32" == %"PRIx64"\n",
+            ip, iface, *mac);
+    *err = SYS_ERR_OK;
+    return SYS_ERR_OK;
+} // end function: ARP_resolve_request
+
+
+// Initialzes the ARP lookup service
+int init_ARP_lookup_service(struct netd_state *state, char *dev_name)
+{
+    errval_t err = SYS_ERR_OK; // default return value
+
+    // sanity check on parameter
+    assert(dev_name != NULL);
+
+    // start the port management service
+    snprintf(state->ARP_service_name, sizeof(state->ARP_service_name), "%s%s", dev_name,
+             NET_ARP_LOOKUP_SUFFIX);
+    state->ARP_service_exported = false;
+    state->registered_user_list = NULL;
+
+    NETD_DEBUG("init_ARP_lookup_service called [%s]\n", state->ARP_service_name);
+
+   // exporting net_ports interface
+    err = net_ARP_export(state, export_ARP_cb, connect_ARP_cb,
+            get_default_waitset(), IDC_EXPORT_FLAGS_DEFAULT);
+
+    if (err_is_fail(err)) {
+        USER_PANIC("net_ARP_export failed!");
+        return err;
+    }
+
+    // wait till ports export is actually done
+    struct waitset *ws = get_default_waitset();
+
+
+    while (!state->ARP_service_exported) {
+        err = event_dispatch(ws);
+        if (err_is_fail(err)) {
+            DEBUG_ERR(err, "in event_dispatch for init_ARP_service");
+            return err;
+        }
+    } // end while:
+
+    return err;
+} // end function: init_ARP_service
diff --git a/lib/netd/Hakefile b/lib/netd/Hakefile
new file mode 100644 (file)
index 0000000..8b7b03d
--- /dev/null
@@ -0,0 +1,19 @@
+--------------------------------------------------------------------------
+-- Copyright (c) 2007-2009, ETH Zurich.
+-- All rights reserved.
+--
+-- This file is distributed under the terms in the attached LICENSE file.
+-- If you do not find this file, copies can be found by writing to:
+-- ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
+--
+-- Hakefile for lib/netd
+--
+--------------------------------------------------------------------------
+
+[ build library { target = "netd",
+                  cFiles = [ "netd.c", "lwipinit.c", "ARP_lookup_service.c" ],
+                  flounderBindings = [ "net_ARP" ],
+                  flounderDefs = [ "net_ARP" ],
+                  addLibraries = [ "lwip", "contmng", "net_if_raw", "timer",
+                                   "trace" ] }
+]
similarity index 74%
rename from usr/netd/lwipinit.c
rename to lib/netd/lwipinit.c
index e81e8bd..3d24d64 100644 (file)
 #include <lwip/tcp.h>
 #include <netif/bfeth.h>
 
-// keeping it for DHCP timers
-#include <barrelfish/deferred.h>
-
-#include "netd_debug.h"
-#include "netd.h"
+#include "netd_private.h"
+#include <netd/netd_debug.h>
 
 /* Enable tracing only when it is globally enabled */
 #if CONFIG_TRACE && NETWORK_STACK_TRACE
 #endif // CONFIG_TRACE && NETWORK_STACK_TRACE
 
 
-static struct periodic_event dhcp_fine_timer; // fine-grain timer for DHCP
-static struct periodic_event dhcp_coarse_timer; // coarse-grain timer for DHCP
-
-// local ip address.  Used to keep track of changing IP addresses
-static struct ip_addr local_ip = {
-    .addr = 0x00,       // BFDMUX_IP_ADDR_ANY
-};
-
-// state variable indicating if dhcp is done or not
-static bool dhcp_completed = false;
-
 static void timer_callback(void *data)
 {
 
@@ -57,7 +43,7 @@ static void timer_callback(void *data)
 }
 
 
-static void setup_dhcp_timer(void)
+static void setup_dhcp_timer(struct netd_state *state)
 {
     errval_t err;
 
@@ -65,13 +51,13 @@ static void setup_dhcp_timer(void)
     NETD_DEBUG("setting up timeouts for lwip\n");
 
     /* DHCP fine timer */
-    err = periodic_event_create(&dhcp_fine_timer, get_default_waitset(),
+    err = periodic_event_create(&state->dhcp_fine_timer, get_default_waitset(),
                                 (DHCP_FINE_TIMER_MSECS * 1000),
                                 MKCLOSURE(timer_callback, dhcp_fine_tmr));
     assert(err_is_ok(err));
 
     /* DHCP coarse timer */
-    err = periodic_event_create(&dhcp_coarse_timer, get_default_waitset(),
+    err = periodic_event_create(&state->dhcp_coarse_timer, get_default_waitset(),
                                 (DHCP_COARSE_TIMER_MSECS * 1000),
                                 MKCLOSURE(timer_callback, dhcp_coarse_tmr));
     assert(err_is_ok(err));
@@ -79,17 +65,16 @@ static void setup_dhcp_timer(void)
 
 static void link_status_change(struct netif *nf)
 {
-    static bool subsequent_call;
-
+    struct netd_state *state = nf->state;
+    
     if (netif_is_up(nf)) {
         printf("netd: interface is now up\n");
     } else {
         printf("netd: interface is now down\n");
         return;
     }
-
-    if (subsequent_call) {
-        if (ip_addr_cmp(&local_ip, &nf->ip_addr) != 0) {
+    if (state->subsequent_call) {
+        if (ip_addr_cmp(&state->local_ip, &nf->ip_addr) != 0) {
             printf
               ("netd: WARNING: IP has changed! Current address: %d.%d.%d.%d",
                ip4_addr1(&nf->ip_addr), ip4_addr2(&nf->ip_addr),
@@ -117,10 +102,10 @@ static void link_status_change(struct netif *nf)
 
     } // end else:
 
-    local_ip = nf->ip_addr;
+    state->local_ip = nf->ip_addr;
     netif_set_default(nf);
 
-    if (!subsequent_call) {
+    if (!state->subsequent_call) {
 #if 0
         /* Now, the timers are not needed.  They should be closed. */
         /* I don't agree -- we need to keep renewing our lease -AB */
@@ -128,22 +113,22 @@ static void link_status_change(struct netif *nf)
         periodic_event_cancel(&dhcp_coarse_timer);
 #endif // 0
 
-        if (do_dhcp) {
-            dhcp_completed = true;
+        if (state->do_dhcp) {
+            state->dhcp_completed = true;
         }
     } // end if: first call
 
-    subsequent_call = true;
+    state->subsequent_call = true;
 }
 
 
 
-static void get_ip_from_dhcp(struct netif *nf_ptr)
+static void get_ip_from_dhcp(struct netd_state *state, struct netif *nf_ptr)
 {
 
 
     NETD_DEBUG("get_ip_from_dhcp: starting dhcp\n");
-    setup_dhcp_timer();
+    setup_dhcp_timer(state);
     err_t err = dhcp_start(nf_ptr);
 
     assert(err == ERR_OK);
@@ -156,22 +141,26 @@ static void convert_str_to_ip_addr(char *addr_str,
     assert(addr_str != NULL);
     struct in_addr addr;
     if (inet_aton(addr_str, &addr) == 0) {
-        printf("Invalid IP addr: %s\n", ip_addr_str);
-        USER_PANIC("Invalid IP address %s", ip_addr_str);
+        printf("Invalid IP addr: %s\n", addr_str);
+        USER_PANIC("Invalid IP address %s", addr_str);
         return;
     }
     ip_addr_holder->addr = addr.s_addr;
 } // end function: convert_str_to_ip_addr
 
-void startlwip(char *card_name, uint64_t queueid)
+void startlwip(struct netd_state *state, char *card_name, uint64_t queueid)
 {
 
     NETD_DEBUG("NETD is taking control of the LWIP for card[%s][%"PRIu64"]\n",
             card_name, queueid);
+    state->subsequent_call = false;
+    state->local_ip.addr = 0x00; // BFDMUX_IP_ADDR_ANY
+    
     // take ownership of lwip
-    netif_ptr = owner_lwip_init(card_name, queueid);
-    assert(netif_ptr != NULL);
-    netif_set_status_callback(netif_ptr, link_status_change);
+    state->netif_ptr = owner_lwip_init(card_name, queueid);
+    assert(state->netif_ptr != NULL);
+    state->netif_ptr->state = state;
+    netif_set_status_callback(state->netif_ptr, link_status_change);
 
 #if 0
 #if ENABLE_WEB_TRACING
@@ -194,12 +183,12 @@ void startlwip(char *card_name, uint64_t queueid)
         printf("Tracing not enabled\n");
 #endif // ENABLE_WEB_TRACING
 #endif // 0
-    if (do_dhcp) {
-        get_ip_from_dhcp(netif_ptr);
+    if (state->do_dhcp) {
+        get_ip_from_dhcp(state, state->netif_ptr);
 
         NETD_DEBUG("Waiting for DHCP to complete\n");
         struct waitset *ws = get_default_waitset();
-        while (!dhcp_completed) {
+        while (!state->dhcp_completed) {
             errval_t err = event_dispatch(ws);
             if (err_is_fail(err)) {
                 DEBUG_ERR(err, "in event_dispatch (waiting for dhcp)");
@@ -212,18 +201,17 @@ void startlwip(char *card_name, uint64_t queueid)
     // end else: static ip configuration
     // directly set IP address
     printf("Configuring interface with static values\n");
-    printf("ip[%s], nm[%s], gw[%s]\n", ip_addr_str, netmask_str,
-                gateway_str);
+    printf("ip[%s], nm[%s], gw[%s]\n", state->ip_addr_str, state->netmask_str,
+                state->gateway_str);
     struct ip_addr ipaddr, gw, netmask;
-    convert_str_to_ip_addr(ip_addr_str, &ipaddr);
-    convert_str_to_ip_addr(netmask_str, &netmask);
-    convert_str_to_ip_addr(gateway_str, &gw);
+    convert_str_to_ip_addr(state->ip_addr_str, &ipaddr);
+    convert_str_to_ip_addr(state->netmask_str, &netmask);
+    convert_str_to_ip_addr(state->gateway_str, &gw);
 
 
-    netif_set_ipaddr(netif_ptr, &ipaddr);
-    netif_set_gw(netif_ptr, &gw);
-    netif_set_netmask(netif_ptr, &netmask);
-    netif_set_up(netif_ptr);
+    netif_set_ipaddr(state->netif_ptr, &ipaddr);
+    netif_set_gw(state->netif_ptr, &gw);
+    netif_set_netmask(state->netif_ptr, &netmask);
+    netif_set_up(state->netif_ptr);
 
 } // end function startlwip
-
diff --git a/lib/netd/netd.c b/lib/netd/netd.c
new file mode 100644 (file)
index 0000000..b3dc4ae
--- /dev/null
@@ -0,0 +1,90 @@
+/**
+ * \file
+ * \brief Echo server main
+ */
+
+/*
+ * Copyright (c) 2007-12 ETH Zurich.
+ * All rights reserved.
+ *
+ * This file is distributed under the terms in the attached LICENSE file.
+ * If you do not find this file, copies can be found by writing to:
+ * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
+ */
+
+#define NETD_SERVICE_DEBUG
+
+#include <barrelfish/barrelfish.h>
+#include <barrelfish/net_constants.h>
+
+// For event loops
+#include <barrelfish/dispatch.h>
+
+// standard include files
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <netd/netd.h>
+#include <netd/netd_debug.h>
+#include "netd_private.h"
+
+static char * duplicate_string(const char *string)
+{
+    if (!string)
+        return NULL;
+    char *new_string = strdup(string);
+    assert(new_string);
+    return new_string;
+}
+
+/****************************************************************************
+ * netd initialization function
+ ***************************************************************************/
+errval_t netd_init(struct netd_state **state, char *card_name, uint64_t queueid,
+    bool do_dhcp, char *ip_addr_str, char *netmask_str, char *gateway_str)
+{
+    NETD_DEBUG("###################################################\n");
+    NETD_DEBUG("Initialising netd library\n");
+    
+    *state = malloc(sizeof(struct netd_state));
+    (*state)->do_dhcp = do_dhcp;
+    (*state)->dhcp_completed = false;
+    
+    (*state)->ip_addr_str = duplicate_string(ip_addr_str);
+    (*state)->netmask_str = duplicate_string(netmask_str);
+    (*state)->gateway_str = duplicate_string(gateway_str);
+    
+    NETD_DEBUG("running on core %d\n", disp_get_core_id());
+    NETD_DEBUG("###################################################\n");
+
+
+    NETD_DEBUG("card name = %s\n", card_name);
+    if (!do_dhcp)
+        NETD_DEBUG("using static IP address\n");
+
+    assert(card_name);
+
+    if (!do_dhcp) {
+        // Making sure that we have enough info for static configuration
+        if ((ip_addr_str == NULL) || (netmask_str == NULL)
+                || (gateway_str == NULL)) {
+            USER_PANIC("Error, not enough information provided for static "
+                    "IP configuration IP[%s], NM[%s], GW[%s]",
+                    ip_addr_str, netmask_str, gateway_str);
+            return 1;
+        }
+    }
+    // FIXME: This has to be done for every card
+    // Connect to the driver for given card
+    NETD_DEBUG("trying to connect to the %s:%"PRIu64" driver...\n",
+            card_name, queueid);
+    startlwip(*state, card_name, queueid);
+
+    NETD_DEBUG("registering net_ARP service\n");
+    // register ARP service
+    init_ARP_lookup_service(*state, card_name);
+
+    return SYS_ERR_OK;
+}
diff --git a/lib/netd/netd_private.h b/lib/netd/netd_private.h
new file mode 100644 (file)
index 0000000..b10fd57
--- /dev/null
@@ -0,0 +1,80 @@
+/**
+ * \file
+ * \brief the private header file for the net "daemon" library
+ *
+ * This file is part of the net "daemon" library
+ */
+
+/*
+ * Copyright (c) 2007-12 ETH Zurich.
+ * All rights reserved.
+ *
+ * This file is distributed under the terms in the attached LICENSE file.
+ * If you do not find this file, copies can be found by writing to:
+ * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: NetOS Group.
+ */
+
+#ifndef _NETD_PRIVATE_H_
+#define _NETD_PLIVATE_H_
+
+// keeping it for DHCP timers
+#include <barrelfish/deferred.h>
+
+#include <netif/bfeth.h>
+#include <if/net_ARP_defs.h>
+
+#include <procon/procon.h>
+
+struct netd_state;
+
+struct ARP_user_cl {
+    // This will be used to remember who all are waiting for response
+    struct net_ARP_binding *cl; // binding
+    struct ARP_user_cl *next; // for singly linked list
+    bool died; // is the user still connected
+    struct netd_state *state;
+};
+
+
+struct netd_state {
+    bool do_dhcp;
+    // IP information for static configuration
+    char *ip_addr_str;
+    char *netmask_str;
+    char *gateway_str;
+    char *dns_str;
+    
+    struct netif *netif_ptr;
+
+    struct periodic_event dhcp_fine_timer; // fine-grain timer for DHCP
+    struct periodic_event dhcp_coarse_timer; // coarse-grain timer for DHCP
+
+    // local ip address.  Used to keep track of changing IP addresses
+    struct ip_addr local_ip;
+    bool subsequent_call;
+
+    // state variable indicating if dhcp is done or not
+    bool dhcp_completed;
+    
+    // The name of exported service for ARP lookup (which we are implementing)
+    char ARP_service_name[MAX_NET_SERVICE_NAME_LEN];
+    // is service exported? marks that initialization is done
+    bool ARP_service_exported;
+    // singly linked list of apps connected with this service
+    struct ARP_user_cl *registered_user_list;
+};
+
+typedef net_ARP_ipv4addr_t ipv4addr_t;
+
+/**
+ * @brief initializes LWIP. not a lot left after I changed the subsystems
+ *
+ * @param state netd library state
+ * @param card_name the name of the card.
+ * @param queueid the queue number
+ */
+void startlwip(struct netd_state *state, char *card_name, uint64_t queueid);
+
+int init_ARP_lookup_service(struct netd_state *state, char *dev_name);
+
+#endif // _NETD_PRIVATE_H_
diff --git a/usr/netd/ARP_lookup_service.c b/usr/netd/ARP_lookup_service.c
deleted file mode 100755 (executable)
index 8abf9c4..0000000
+++ /dev/null
@@ -1,317 +0,0 @@
-/*
- * Copyright (c) 2007-12 ETH Zurich.
- * All rights reserved.
- *
- * This file is distributed under the terms in the attached LICENSE file.
- * If you do not find this file, copies can be found by writing to:
- * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
- */
-
-#include <barrelfish/barrelfish.h>
-#include <barrelfish/nameservice_client.h>
-#include <barrelfish/net_constants.h>
-#include <if/net_ARP_defs.h>
-
-#include <barrelfish/waitset.h>
-
-// standard libraries
-#include <stdio.h>
-#include <string.h>
-
-#include <lwip/init.h>
-#include <netif/etharp.h>
-
-// local includes
-#include "netd.h"
-#include "netd_debug.h"
-
-// ***** Special cases ********
-// local ip address (your own ip address) (valid request)
-// broadcast ip address (invalid request)
-// multicast IP address (invalid request)
-//
-//
-//
-
-
-// How can I get my own MAC address
-// Where can I find the code which is looking up local MAC address?
-//    struct eth_addr *srcaddr = (struct eth_addr *) netif->hwaddr;
-//
-//
-//        return etharp_query(netif, ipaddr, q); // q is pbuf
-//        lib/lwip/src/netif/etharp.c
-
-//        find_entry
-//        src/netif/etharp.c
-
-/****************************************************************
-* data-structures
-*****************************************************************/
-// app connection closure
-struct ARP_user_cl {
-    // This will be used to remember who all are waiting for response
-    struct net_ARP_binding *cl; // binding
-    struct cont_queue *q; // for continuation management
-    struct ARP_user_cl *next; // for singly linked list
-    bool died; // is the user still connected
-};
-
-
-/****************************************************************
-* Local states
-*****************************************************************/
-// The name of exported service for ARP lookup (which we are implementing)
-static char ARP_service_name[MAX_NET_SERVICE_NAME_LEN] = {0};
-
-// is service exported? marks that initialization is done
-static bool ARP_service_exported = false;
-
-// singly linked list of apps connected with this service
-struct ARP_user_cl *registerd_user_list = NULL;
-
-/*****************************************************************
-* Prototypes
-*****************************************************************/
-
-static void get_ip_info(struct net_ARP_binding *cc, uint32_t iface);
-static void ARP_resolve_request(struct net_ARP_binding *cc,
-            ipv4addr_t ip, uint32_t iface, bool force);
-
-// service mappings
-static struct net_ARP_rx_vtbl rx_ARP_vtbl = {
-    .ip_info_call = get_ip_info,
-    .ARP_lookup_call = ARP_resolve_request,
-};
-
-
-/*****************************************************************
-*   Housekeeping functions prototypes
-*****************************************************************/
-
-static void wrapper_ip_info_response(struct net_ARP_binding *cc,
-        errval_t err, ipv4addr_t ip, ipv4addr_t gw, ipv4addr_t nm);
-
-static void wrapper_ARP_lookup_response(struct net_ARP_binding *cc,
-        errval_t err, uint64_t mac);
-
-/*****************************************************************
-* Dealing with new connections
-*****************************************************************/
-static errval_t connect_ARP_cb(void *st, struct net_ARP_binding *b)
-{
-    errval_t err = SYS_ERR_OK;
-    NETD_DEBUG("########### new application came in\n");
-
-    // using the b->st to store session specific data (net_user)
-    struct ARP_user_cl *new_app = malloc(sizeof(struct ARP_user_cl));
-
-    if (new_app == NULL) {
-        NETD_DEBUG("error: malloc failed...\n");
-        err = PORT_ERR_NOT_ENOUGH_MEMORY;
-        return err;
-    }
-
-    memset(new_app, 0, sizeof(struct ARP_user_cl));
-    new_app->next = registerd_user_list;
-    registerd_user_list = new_app;
-    b->st = (void *) new_app;
-
-    new_app->q = create_cont_q("ARP2APP");
-    b->rx_vtbl = rx_ARP_vtbl;
-    return err;
-} // end function: connect_ARP_cb
-
-
-/*****************************************************************
-* exporting service
-*****************************************************************/
-
-static void export_ARP_cb(void *st, errval_t err, iref_t iref)
-{
-
-    if (err_is_fail(err)) {
-        DEBUG_ERR(err, "service[%s] export failed", ARP_service_name);
-        abort(); // FIXME: Do I need abort after DEBUG_ERR?
-    }
-
-    NETD_DEBUG("service [%s] exported at iref %u\n", ARP_service_name, iref);
-
-    // register this iref with the name service
-    err = nameservice_register(ARP_service_name, iref);
-    if (err_is_fail(err)) {
-        DEBUG_ERR(err, "nameservice_register failed for [%s]", ARP_service_name);
-        abort(); // FIXME: Do I need abort after DEBUG_ERR?
-    }
-    ARP_service_exported = true;
-    NETD_DEBUG("service [%s] export successful!\n", ARP_service_name);
-} // end function: export_ARP_cb
-
-
-
-// Initialzes the ARP lookup service
-int init_ARP_lookup_service(char *dev_name)
-{
-    errval_t err = SYS_ERR_OK; // default return value
-
-    // sanity check on parameter
-    assert(dev_name != NULL);
-
-    // start the port management service
-    snprintf(ARP_service_name, sizeof(ARP_service_name), "%s%s", dev_name,
-             NET_ARP_LOOKUP_SUFFIX);
-
-    NETD_DEBUG("init_ARP_lookup_service called [%s]\n", ARP_service_name);
-
-   // exporting net_ports interface
-    err = net_ARP_export(NULL, export_ARP_cb, connect_ARP_cb,
-            get_default_waitset(), IDC_EXPORT_FLAGS_DEFAULT);
-
-    if (err_is_fail(err)) {
-        USER_PANIC("net_ARP_export failed!");
-        return err;
-    }
-
-    // wait till ports export is actually done
-    struct waitset *ws = get_default_waitset();
-    while (!ARP_service_exported) {
-        err = event_dispatch(ws);
-        if (err_is_fail(err)) {
-            DEBUG_ERR(err, "in event_dispatch for init_ARP_service");
-            return err;
-        }
-    } // end while:
-
-    return err;
-} // end function: init_ARP_service
-
-
-// ************************************************************************
-//                 ARP lookup interface function
-// ************************************************************************
-
-static void get_ip_info(struct net_ARP_binding *cc, uint32_t iface)
-{
-    printf("####### get IP info called ######\n");
-    NETD_DEBUG("get_ip_info: client asking for ip over %"PRIu32"\n", iface);
-
-    wrapper_ip_info_response(cc, SYS_ERR_OK, netif_ptr->ip_addr.addr,
-                                netif_ptr->gw.addr, netif_ptr->netmask.addr);
-    NETD_DEBUG("get_ip_info: terminating\n");
-}
-
-static uint64_t refresh_cache(uint32_t dst_ip_addr)
-{
-    struct ip_addr dst_ip;
-    struct netif *netif;
-    dst_ip.addr = dst_ip_addr;
-    netif = ip_route(&dst_ip);
-
-    NETD_DEBUG("refresh_cache: calling etharp_request\n");
-    errval_t r = etharp_request(netif, &dst_ip);
-    assert(err_is_ok(r));
-
-    struct waitset *ws = NULL;
-    ws = get_default_waitset();
-   while (is_ip_present_in_arp_cache(&dst_ip) == false) {
-//        NETD_DEBUG("refresh_arp_cache: event dispatched\n");
-        r = event_dispatch(ws);
-        if (err_is_fail(r)) {
-            DEBUG_ERR(r, "in event_dispatch");
-            abort();
-        }
-   } // end while: till arp not present
-   return find_ip_arp_cache(&dst_ip);
-}
-
-static void ARP_resolve_request(struct net_ARP_binding *cc,
-            ipv4addr_t ip, uint32_t iface, bool force)
-{
-    NETD_DEBUG("ARP_resolve_request: client asking ARP lookup for ip %"
-            PRIu32" over iface %"PRIu32"\n", ip, iface);
-
-    uint64_t found_mac = refresh_cache(ip);
-    assert(found_mac != 0);
-//    assert(!"NYI ARP resolve request");
-    NETD_DEBUG("ARP_resolve_request: MAC found for ARP request ip %"
-            PRIu32" over iface %"PRIu32" == %"PRIu64"\n",
-            ip, iface, found_mac);
-    wrapper_ARP_lookup_response(cc, SYS_ERR_OK, found_mac);
-} // end function: ARP_resolve_request
-
-
-// ************************************************************************
-//                housekeeping functions
-// ************************************************************************
-
-static errval_t send_ip_info(struct q_entry e)
-{
-    struct net_ARP_binding *b = (struct net_ARP_binding *) e.binding_ptr;
-    struct ARP_user_cl *au = (struct ARP_user_cl *) b->st;
-
-    if (b->can_send(b)) {
-        return b->tx_vtbl.ip_info_response(b,
-                    MKCONT(cont_queue_callback, au->q),
-                    e.plist[0], e.plist[1], e.plist[2], e.plist[3]);
-                 // e.err, e.ip,   e.gw,   e.mask
-    } else {
-        NETD_DEBUG("send_assign_ip: Flounder busy,rtry++\n");
-        return FLOUNDER_ERR_TX_BUSY;
-    }
-}
-
-static void wrapper_ip_info_response(struct net_ARP_binding *cc,
-        errval_t err, ipv4addr_t ip, ipv4addr_t gw, ipv4addr_t nm)
-{
-    struct q_entry entry;
-
-    memset(&entry, 0, sizeof(struct q_entry));
-    entry.handler = send_ip_info;
-    entry.binding_ptr = (void *) cc;
-
-    entry.plist[1] = err;
-    entry.plist[1] = ip;
-    entry.plist[2] = gw;
-    entry.plist[3] = nm;
-    // err,  e.ip,   e.gw,   e.mask
-
-    struct ARP_user_cl *au = (struct ARP_user_cl *) cc->st;
-
-    enqueue_cont_q(au->q, &entry);
-}
-
-static errval_t send_ARP_lookup_info(struct q_entry e)
-{
-    struct net_ARP_binding *b = (struct net_ARP_binding *) e.binding_ptr;
-    struct ARP_user_cl *au = (struct ARP_user_cl *) b->st;
-
-    if (b->can_send(b)) {
-        return b->tx_vtbl.ARP_lookup_response(b,
-                    MKCONT(cont_queue_callback, au->q),
-                    e.plist[0], e.plist[1]);
-                 // e.err, e.mac
-    } else {
-        NETD_DEBUG("send_assign_ip: Flounder busy,rtry++\n");
-        return FLOUNDER_ERR_TX_BUSY;
-    }
-}
-
-static void wrapper_ARP_lookup_response(struct net_ARP_binding *cc,
-        errval_t err, uint64_t mac)
-{
-    struct q_entry entry;
-
-    memset(&entry, 0, sizeof(struct q_entry));
-    entry.handler = send_ARP_lookup_info;
-    entry.binding_ptr = (void *) cc;
-
-    entry.plist[1] = err;
-    entry.plist[1] = mac;
-    // err,  e.mac,
-
-    struct ARP_user_cl *au = (struct ARP_user_cl *) cc->st;
-
-    enqueue_cont_q(au->q, &entry);
-}
-
-
index d81c89e..c041af8 100644 (file)
 --------------------------------------------------------------------------
 
 [ build application { target = "netd",
-  cFiles = [ "main.c", "lwipinit.c", "ARP_lookup_service.c" ],
-                      flounderBindings = [ "net_ARP" ],
-                      flounderDefs = [ "net_ARP" ],
-                     addLibraries = [ "lwip", "contmng", "net_if_raw", "timer",
-                      "trace" ]
+                      cFiles = [ "main.c" ],
+                      addLibraries = [ "netd" ]
                     }
 ]
-
index e4c7bcf..eef375e 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include "netd.h"
-#include "netd_debug.h"
-
-bool do_dhcp = true; // flag to control use of dhcp
-// IP information for static configuration
-char *ip_addr_str = NULL; // IP address for this interface
-char *netmask_str = NULL; // netmask for this LAN
-char *gateway_str = NULL; // default gateway address
-char *dns_str = NULL; // ip address of DNS name server
-
+#include <netd/netd.h>
+#include <netd/netd_debug.h>
 
 static void netd_event_polling_loop(void)
 {
@@ -63,6 +55,13 @@ int main(int argc, char **argv)
 
     uint64_t minbase = -1, maxbase = -1;
 
+    bool do_dhcp = true; // flag to control use of dhcp
+    // IP information for static configuration
+    char *ip_addr_str = NULL; // IP address for this interface
+    char *netmask_str = NULL; // netmask for this LAN
+    char *gateway_str = NULL; // default gateway address
+    char *dns_str = NULL; // ip address of DNS name server
+
     NETD_DEBUG("running on core %d\n", disp_get_core_id());
     NETD_DEBUG("###################################################\n");
 
@@ -133,13 +132,10 @@ int main(int argc, char **argv)
     // Connect to the driver for given card
     NETD_DEBUG("trying to connect to the %s:%"PRIu64" driver...\n",
             card_name, allocated_queue);
-    startlwip(card_name, allocated_queue);
-
-    NETD_DEBUG("registering net_ARP service\n");
-    // register ARP service
-    init_ARP_lookup_service(card_name);
+    struct netd_state *state;
+    
+    netd_init(&state, card_name, allocated_queue, do_dhcp, ip_addr_str, netmask_str, gateway_str);
 
     netd_event_polling_loop();
     return 0;
 }
-