ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
linux-socket-fd-factory.h
Go to the documentation of this file.
1 #ifndef LINUX_SOCKET_FD_FACTORY_H
2 #define LINUX_SOCKET_FD_FACTORY_H
3 
4 #include "socket-fd-factory.h"
5 #include "task-manager.h"
6 #include "ns3/net-device.h"
7 #include "ns3/random-variable.h"
8 #include <sys/socket.h>
9 #include <vector>
10 #include <string>
11 #include <utility>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include "linux/types.h"
15 
16 extern "C" {
17 struct SimExported;
18 struct SimDevice;
19 struct SimSocket;
20 struct SimTask;
21 struct SimKernel;
22 struct SimSysFile;
23 }
24 
25 class KingsleyAlloc;
26 
27 namespace ns3 {
28 
29 class Loader;
30 class NetDevice;
31 class Task;
32 class TaskManager;
33 class Packet;
34 class LinuxDeviceStateListener;
35 class PollTable;
36 
38 {
39 public:
40  static TypeId GetTypeId (void);
42  virtual ~LinuxSocketFdFactory ();
43 
44  virtual UnixFd * CreateSocket (int domain, int type, int protocol);
45 
46  void Set (std::string path, std::string value);
47  std::string Get (std::string path);
48  void ScheduleTask (EventImpl *event);
49 
50 private:
51  friend class LinuxSocketFd;
53  struct EventIdHolder : public SimpleRefCount<EventIdHolder>
54  {
55  EventId id;
56  };
57 
58  // called from LinuxSocketFd
59  int Close (struct SimSocket *socket);
60  ssize_t Recvmsg (struct SimSocket *socket, struct msghdr *msg, int flags);
61  ssize_t Sendmsg (struct SimSocket *socket, const struct msghdr *msg, int flags);
62  int Getsockname (struct SimSocket *socket, struct sockaddr *name, socklen_t *namelen);
63  int Getpeername (struct SimSocket *socket, struct sockaddr *name, socklen_t *namelen);
64  int Bind (struct SimSocket *socket, const struct sockaddr *my_addr, socklen_t addrlen);
65  int Connect (struct SimSocket *socket, const struct sockaddr *my_addr, socklen_t addrlen, int flags);
66  int Listen (struct SimSocket *socket, int backlog);
67  int Shutdown (struct SimSocket *socket, int how);
68  int Accept (struct SimSocket *socket, struct sockaddr *my_addr, socklen_t *addrlen);
69  int Ioctl (struct SimSocket *socket, int request, char *argp);
70  int Setsockopt (struct SimSocket *socket, int level, int optname,
71  const void *optval, socklen_t optlen);
72  int Getsockopt (struct SimSocket *socket, int level, int optname,
73  void *optval, socklen_t *optlen);
74  bool CanRecv (struct SimSocket *socket);
75  bool CanSend (struct SimSocket *socket);
76  void* PollWait (struct SimSocket *socket, void *ctxt);
77  void FreePoll (struct SimSocket *socket, void *ctxt);
78  int Poll (struct SimSocket *socket, PollTable* ptable);
79  void PollFreeWait (void *ref);
80 
81  // called from kernel.
82  static int Vprintf (struct SimKernel *kernel, const char *str, va_list args);
83  static void * Malloc (struct SimKernel *kernel, unsigned long size);
84  static void Free (struct SimKernel *kernel, void *buffer);
85  static void * Memcpy (struct SimKernel *kernel, void *dst, const void *src, unsigned long size);
86  static void * Memset (struct SimKernel *kernel, void *dst, char value, unsigned long size);
87  static int AtExit (struct SimKernel *kernel, void (*function)(void));
88  static int Access (struct SimKernel *kernel, const char *pathname, int mode);
89  static char* Getenv (struct SimKernel *kernel, const char *name);
90  static int Mkdir (struct SimKernel *kernel, const char *pathname, mode_t mode);
91  static int Open (struct SimKernel *kernel, const char *pathname, int flags);
92  static size_t Fread (struct SimKernel *kernel, void *ptr, size_t size, size_t nmemb, FILE *stream);
93  static size_t Fwrite (struct SimKernel *kernel, const void *ptr, size_t size, size_t nmemb, FILE *stream);
94  static FILE* FdOpen (struct SimKernel *kernel, int fd, const char *mode);
95  static int __Fxstat (struct SimKernel *kernel, int ver, int fd, void *buf);
96  static int Fseek (struct SimKernel *kernel, FILE *stream, long offset, int whence);
97  static void Setbuf (struct SimKernel *kernel, FILE *stream, char *buf);
98  static long Ftell (struct SimKernel *kernel, FILE *stream);
99  static int Fclose (struct SimKernel *kernel, FILE *fp);
100  static unsigned long Random (struct SimKernel *kernel);
101  static void *EventScheduleNs (struct SimKernel *kernel, __u64 ns, void (*fn)(void *context), void *context,
102  void (*pre_fn)(void));
103  static void EventCancel (struct SimKernel *kernel, void *ev);
104  static struct SimTask *TaskStart (struct SimKernel *kernel, void (*callback)(void *), void *context);
105  static struct SimTask * TaskCurrent (struct SimKernel *kernel);
106  static void TaskWait (struct SimKernel *kernel);
107  static int TaskWakeup (struct SimKernel *kernel, struct SimTask *task);
108  static void TaskYield (struct SimKernel *kernel);
109  static void DevXmit (struct SimKernel *kernel, struct SimDevice *dev, unsigned char *data, int len);
110  static void SignalRaised (struct SimKernel *kernel, struct SimTask *task, int signalNumber);
111  static void PollEvent (int flag, void *context);
112 
113 
114  // inherited from Object.
115  virtual void DoDispose (void);
116  virtual void NotifyNewAggregate (void);
117  // called from Node
118  void NotifyAddDevice (Ptr<NetDevice> device);
119  // called during initialization with a task context
120  // to enter the kernel functions.
121  void StartInitializationTask (void);
122  void InitializeStack (void);
123  void RxFromDevice (Ptr<NetDevice> device, Ptr<const Packet> p,
124  uint16_t protocol, const Address & from,
125  const Address &to, NetDevice::PacketType type);
126  struct SimDevice * DevToDev (Ptr<NetDevice> dev);
127  void NotifyDeviceStateChange (Ptr<NetDevice> device);
128  void NotifyDeviceStateChangeTask (Ptr<NetDevice> device);
129  void NotifyAddDeviceTask (Ptr<NetDevice> device);
130 
131  std::vector<std::pair<std::string,struct SimSysFile *> > GetSysFileList (void);
132  void DoSet (std::string path, std::string value);
133  void SetTask (std::string path, std::string value);
134  static void TaskSwitch (enum Task::SwitchType type, void *context);
135  static void ScheduleTaskTrampoline (void *context);
136  void EventTrampoline (void (*fn)(void *context),
137  void *context, void (*pre_fn)(void),
138  Ptr<EventIdHolder> event);
139  static void SendMain (bool *r, NetDevice *d, Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
140 
141  std::vector<std::pair<Ptr<NetDevice>,struct SimDevice *> > m_devices;
142  std::string m_library;
144  struct SimExported *m_exported;
145  std::list<Task *> m_kernelTasks;
146  Ptr<TaskManager> m_manager;
147  UniformVariable m_variable;
149  FILE *m_logFile;
150  std::list<std::pair<std::string,std::string> > m_earlySysfs;
151  std::vector<Ptr<LinuxDeviceStateListener> > m_listeners;
152  double m_rate;
153  RandomVariable m_ranvar;
154  uint16_t m_pid;
155  TypeId m_lteUeTid;
156 };
157 
158 } // namespace ns3
159 
160 #endif /* LINUX_SOCKET_FD_FACTORY_H */