diff --git a/src/internet-stack/internet-stack.cc b/src/internet-stack/internet-stack.cc index 908cc7082..26e8e0e0e 100644 --- a/src/internet-stack/internet-stack.cc +++ b/src/internet-stack/internet-stack.cc @@ -30,11 +30,8 @@ #include "udp-socket-factory-impl.h" #include "tcp-socket-factory-impl.h" #include "ipv4-impl.h" - -#ifdef NETWORK_SIMULATION_CRADLE #include "nsc-tcp-socket-factory-impl.h" #include "nsc-tcp-l4-protocol.h" -#endif namespace ns3 { @@ -91,7 +88,6 @@ AddInternetStack (Ptr node) } -#ifdef NETWORK_SIMULATION_CRADLE static void AddNscStack(Ptr node, const std::string &soname) { @@ -115,11 +111,5 @@ AddNscInternetStack (Ptr node, const std::string &soname) AddUdpStack (node); AddNscStack (node, soname); } -#else -void -AddNscInternetStack (Ptr node, const std::string &soname) -{ - NS_ASSERT_MSG(false, "ERROR: ns-3 was compiled without Network Simulation Cradle support"); -} -#endif + }//namespace ns3 diff --git a/src/internet-stack/nsc-sysctl.cc b/src/internet-stack/nsc-sysctl.cc index 32a0cb347..1ce0ea548 100644 --- a/src/internet-stack/nsc-sysctl.cc +++ b/src/internet-stack/nsc-sysctl.cc @@ -19,7 +19,7 @@ #include "ns3/string.h" #include "nsc-sysctl.h" -#include "nsc/sim/sim_interface.h" +#include "sim_interface.h" namespace ns3 { diff --git a/src/internet-stack/nsc-tcp-l4-protocol.h b/src/internet-stack/nsc-tcp-l4-protocol.h index 22e168298..b79250985 100644 --- a/src/internet-stack/nsc-tcp-l4-protocol.h +++ b/src/internet-stack/nsc-tcp-l4-protocol.h @@ -30,7 +30,7 @@ #include "tcp-header.h" #include "ns3/timer.h" -#include "nsc/sim/sim_interface.h" +#include "sim_interface.h" namespace ns3 { diff --git a/src/internet-stack/nsc-tcp-socket-impl.cc b/src/internet-stack/nsc-tcp-socket-impl.cc index e2d627274..79ac08b1d 100644 --- a/src/internet-stack/nsc-tcp-socket-impl.cc +++ b/src/internet-stack/nsc-tcp-socket-impl.cc @@ -39,7 +39,8 @@ #include #include -#include "nsc/sim/sim_errno.h" +#include "sim_interface.h" +#include "sim_errno.h" NS_LOG_COMPONENT_DEFINE ("NscTcpSocketImpl"); diff --git a/src/internet-stack/nsc-tcp-socket-impl.h b/src/internet-stack/nsc-tcp-socket-impl.h index 10a4fce8b..d3cb42a55 100644 --- a/src/internet-stack/nsc-tcp-socket-impl.h +++ b/src/internet-stack/nsc-tcp-socket-impl.h @@ -32,8 +32,6 @@ #include "sequence-number.h" #include "rtt-estimator.h" -#include "nsc/sim/sim_interface.h" - namespace ns3 { class Ipv4EndPoint; diff --git a/src/internet-stack/sim_errno.h b/src/internet-stack/sim_errno.h new file mode 100644 index 000000000..7c7443d26 --- /dev/null +++ b/src/internet-stack/sim_errno.h @@ -0,0 +1,35 @@ +#ifndef NETWORK_SIMULATION_CRADLE_ERR_H_ +#define NETWORK_SIMULATION_CRADLE_ERR_H_ + +// list of network stack errors that may happen in a simulation, +// and can be handled by the simulator in a sane way. +// Note that NSC handles several errors internally though +// nsc_assert, BUG() and friends, because they (should) never +// happen in a simulation (e.g. ESOCKTNOSUPPORT). +// +// These values are returned by the various methods provided by nsc. +// They must always be < 0, as values >= 0 are a success indicator; +// e.g. send_data() will return the number of bytes sent or one of +// the nsc_errno numbers below, accept() will return 0 on success or +// one of the nsc_errno numbers below, etc. +enum nsc_errno { + NSC_EUNKNOWN = -1, + NSC_EADDRINUSE = -10, + NSC_EADDRNOTAVAIL = -11, + NSC_EAGAIN = -12, + NSC_EALREADY = -25, + NSC_ECONNREFUSED = -32, + NSC_ECONNRESET = -33, + NSC_EHOSTDOWN = -50, + NSC_EHOSTUNREACH = -51, + NSC_EINPROGRESS = -60, + NSC_EISCONN = -61, + NSC_EMSGSIZE = -70, + NSC_ENETUNREACH = -82, + NSC_ENOTCONN = -86, + NSC_ENOTDIR = -87, // used by sysctl(2) + NSC_ESHUTDOWN = -130, + NSC_ETIMEDOUT = -140, +}; + +#endif diff --git a/src/internet-stack/sim_interface.h b/src/internet-stack/sim_interface.h new file mode 100644 index 000000000..4ba15c44d --- /dev/null +++ b/src/internet-stack/sim_interface.h @@ -0,0 +1,211 @@ +#ifndef __SIM_INTERFACE_H__ +#define __SIM_INTERFACE_H__ +/* + Network Simulation Cradle + Copyright (C) 2003-2005 Sam Jansen + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., 59 + Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ + +#define NSC_VERSION 0x000400 + +struct INetStack +{ + virtual ~INetStack() {} + + virtual void init(int hz) = 0; + + virtual void if_receive_packet(int if_id, const void *data, int datalen) = 0; + + virtual void if_send_packet(const void *data, int datalen) = 0; + virtual void if_send_finish(int if_id) = 0; + + virtual void if_attach(const char *addr, const char *mask, int mtu) = 0; + virtual void add_default_gateway(const char *addr) = 0; + + /** Purely for debugging/diagnostic purposes. This returns the internal id + * of the stack instance. + */ + virtual int get_id() = 0; + + /** Should return a short one-word name of the stack. Eg. Linux 2.4.x -> + * linux24, FreeBSD 5.x -> freebsd5. This can be used to identify output + * from a stack, for example a packet trace file. */ + virtual const char *get_name() = 0; + + /** This is used so the simulator can call the stack timer_interrupt function + * the correct amount of times per second. For example, lwip has a hz of 10, + * which it returns here to say that it's timer_interrupt should be called + * 10 times a second. FreeBSD uses 100, as does Linux 2.4, while Linux 2.6 + * uses 1000. (This is often configurable in the kernel in question, also.) + */ + virtual int get_hz() = 0; + + virtual void timer_interrupt() = 0; + virtual void increment_ticks() = 0; + + virtual void buffer_size(int size) = 0; + + virtual struct INetDatagramSocket *new_udp_socket() { return NULL; } + virtual struct INetStreamSocket *new_tcp_socket() { return NULL; } + virtual struct INetStreamSocket *new_sctp_socket() { return NULL; } + + // The following I've made optional to implement for now. Eases + // integration of new features. + virtual int sysctl(const char *sysctl_name, void *oldval, size_t *oldlenp, + void *newval, size_t newlen) + { + return -1; + } + + // alternate, simpler interface. the stack cradle code is expected + // to convert the string-value to something that the stack can handle. + // The idea here is that this is a front-end to the sysctl(2) call, + // much like the sysctl(8) program. + virtual int sysctl_set(const char *name, const char *value) + { + return -1; + } + + // same as above, cradle code is expected to convert the sysctl value + // into a string. + // returns length of the string in value, i.e. retval > len: 'output truncated'. + virtual int sysctl_get(const char *name, char *value, size_t len) + { + return -1; + } + + // this tells the cradle code to put the name of sysctl number 'idx' + // into name[]. + // The idea is that this can be used to get a list of all available sysctls: + // char buf[256] + // for (i=0; sysctl_getnum(i, buf, sizeof(buf)) > 0 ;i++) + // puts(buf); + // returns -1 if idx is out of range and the length of the sysctl name otherwise. + virtual int sysctl_getnum(size_t idx, char *name, size_t len) + { + return -1; + } + + virtual void show_config() + { + ; + } + + /* Optional functions used to get and set variables for this stack */ + virtual bool get_var(const char *var, char *result, int result_len) + { + return false; + } + + virtual bool set_var(const char *var, const char *val) + { + return false; + } + + /** The level of debugging or diagnostic information to print out. This + * normally means kernel messages printed out during initialisation but + * may also include extra debugging messages that are part of NSC. */ + virtual void set_diagnostic(int level) {} + + /** Simple interface to support sending any textual command to a stack + * + * @returns 0 on success + */ + virtual int cmd(const char *) + { + return 1; + } +}; + +struct INetStreamSocket +{ + virtual ~INetStreamSocket() {} + + virtual void connect(const char *, int) = 0; + virtual void disconnect() = 0; + virtual void listen(int) = 0; + virtual int accept(INetStreamSocket **) = 0; + virtual int send_data(const void *data, int datalen) = 0; + virtual int read_data(void *buf, int *buflen) = 0; + /* We need to pass the option name in as a string here. The reason for + * this is that different operating systems you compile on will have + * different numbers defined for the constants SO_SNDBUF and so on. */ + virtual int setsockopt(char *optname, void *val, size_t valsize) = 0; + virtual void print_state(FILE *) = 0; + virtual bool is_connected() = 0; + virtual bool is_listening() = 0; + + /* + * addrspace points to a byte buffer of *addrs_len bytes. + * It is filled with a (network order) byte representation of the ip address, + * addrs_len is updated to reflect the length (i.e. 4 or eventually 16 for ipv6). + * *port is updated with the TCP/SCTP etc. port number. + * + * The reason that this doesn't use a *struct sockaddr is that the BSDs + * have an sa_len member in there, Linux does not, so the struct layout is + * different depending on the actual NSC stack and the real host operating + * system. + */ + virtual int getpeername(void *addrspace, size_t *addrs_len, int *port) { + return -1; + } + virtual int getsockname(void *addrspace, size_t *addrs_len, int *port) { + return -1; + } + /* Optional functions used to get and set variables for this TCP + * connection. */ + virtual bool get_var(const char *var, char *result, int result_len) + { + return false; + } + + virtual bool set_var(const char *var, const char *val) + { + return false; + } +}; + +struct INetDatagramSocket +{ + virtual ~INetDatagramSocket() {} + + virtual void set_destination(const char *, int) = 0; + virtual void send_data(const void *data, int datalen) = 0; +}; +struct ISendCallback +{ + virtual ~ISendCallback() {} + + virtual void send_callback(const void *data, int datalen) = 0; +}; + +struct IInterruptCallback +{ + virtual ~IInterruptCallback() {} + + virtual void wakeup() = 0; + virtual void gettime(unsigned int *, unsigned int *) = 0; +}; + +typedef int (*FRandom)(); +typedef INetStack *(*FCreateStack)(ISendCallback *, IInterruptCallback *, + FRandom); + +#define CREATE_STACK_FUNC(a,b,c) extern "C" INetStack *nsc_create_stack(\ + ISendCallback *a, IInterruptCallback *b, FRandom c) + +#endif diff --git a/src/internet-stack/wscript b/src/internet-stack/wscript index 794c6b147..289f7f3c4 100644 --- a/src/internet-stack/wscript +++ b/src/internet-stack/wscript @@ -135,7 +135,12 @@ def build(bld): 'pending-data.cc', 'sequence-number.cc', 'rtt-estimator.cc', + 'nsc-tcp-socket-impl.cc', + 'nsc-tcp-l4-protocol.cc', + 'nsc-tcp-socket-factory-impl.cc', + 'nsc-sysctl.cc', ] + obj.uselib = 'DL' headers = bld.create_obj('ns3header') headers.module = 'internet-stack' @@ -149,12 +154,6 @@ def build(bld): 'ipv4-static-routing.h', ] - if bld.env()['NSC_ENABLED']: - obj.source.append ('nsc-tcp-socket-impl.cc') - obj.source.append ('nsc-tcp-l4-protocol.cc') - obj.source.append ('nsc-tcp-socket-factory-impl.cc') - obj.source.append ('nsc-sysctl.cc') - obj.uselib = 'DL' - + if bld.env()['NSC_ENABLED']: builddir = os.path.abspath(os.path.join(bld.env()['NS3_BUILDDIR'], bld.env ().variant())) NscBuildTask(builddir)