orca-sim
NetBridge.hpp
Go to the documentation of this file.
1 /******************************************************************************
2  * This file is part of project ORCA. More information on the project
3  * can be found at the following repositories at GitHub's website.
4  *
5  * http://https://github.com/andersondomingues/orca-sim
6  * http://https://github.com/andersondomingues/orca-software
7  * http://https://github.com/andersondomingues/orca-mpsoc
8  * http://https://github.com/andersondomingues/orca-tools
9  *
10  * Copyright (C) 2018-2020 Anderson Domingues, <ti.andersondomingues@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 ******************************************************************************/
26 #ifndef MODELS_ORCA_VIRTUAL_ETHERNET_INCLUDE_NETBRIDGE_HPP_
27 #define MODELS_ORCA_VIRTUAL_ETHERNET_INCLUDE_NETBRIDGE_HPP_
28 
29 // std API
30 #include <pthread.h>
31 #include <iostream>
32 #include <string>
33 
34 // ursa API
35 #include "TimedModel.hpp"
36 #include "Buffer.hpp"
37 #include "Memory.hpp"
38 #include "Signal.hpp"
39 
40 #ifndef NETSOCKET_CLIENT_ADDRESS
41 #define NETSOCKET_CLIENT_ADDRESS "127.0.0.1"
42 #pragma message "ip address for socket client undefined, default is localhost"
43 #endif
44 
45 #ifndef NETSOCKET_CLIENT_PORT
46 #define NETSOCKET_CLIENT_PORT 5000
47 #pragma message "port number for socket client undefined, default is 5000"
48 #endif
49 
50 #ifndef NETSOCKET_SERVER_ADDRESS
51 #define NETSOCKET_SERVER_ADDRESS "127.0.0.1"
52 #pragma message "ip address for socket server undefined, default is localhost"
53 #endif
54 
55 #ifndef NETSOCKET_SERVER_PORT
56 #define NETSOCKET_SERVER_PORT 5001
57 #pragma message "port number for socket server undefined, default is 5001"
58 #endif
59 
64 
65 namespace orcasim::models::orca {
66 
67 
68 class udp_client_server_runtime_error : public std::runtime_error{
69  public:
70  explicit udp_client_server_runtime_error(const char* w)
71  : std::runtime_error(w) {}
72 };
73 
74 class udp_client{
75  private:
76  int f_socket;
77  int f_port;
78  std::string f_addr;
79  struct addrinfo * f_addrinfo;
80 
81  public:
82  udp_client(const std::string& addr, int port);
83  ~udp_client();
84 
85  int get_socket() const;
86  int get_port() const;
87  std::string get_addr() const;
88 
89  int send(const char *msg, size_t size);
90 };
91 
92 
93 class udp_server{
94  public:
95  udp_server(const std::string& addr, int port);
96  ~udp_server();
97 
98  int get_socket() const;
99  int get_port() const;
100  std::string get_addr() const;
101 
102  int recv(char *msg, size_t max_size);
103  int timed_recv(char *msg, size_t max_size, int max_wait_ms);
104 
105  private:
106  int f_socket;
107  int f_port;
108  std::string f_addr;
109  struct addrinfo * f_addrinfo;
110 };
111 //--------------------------------------------
112 
113 typedef uint16_t FlitType;
114 
116 
118 
119 #define RECV_BUFFER_LEN 128
120 #define SEND_BUFFER_LEN 128
121 
122 #define HWBUFFER_LEN 16
123 
133 class NetBridge: public TimedModel{
134  private:
135  // This module supports sending and receiving
136  // packet simultaneously, so we keep the states
137  // of two state machines below.
140 
141  // Control wire for the receiving thread. This module uses
142  // an additional thread that treats UDP packets outside the simulation
143  // thread. The signal below is used to signalunicated between threads.
144  // TODO(ad): replace by the non-blocking model
145  int8_t _recv_val;
147 
148  std::ofstream output_debug;
149  std::ofstream output_uart;
150 
151  pthread_t _t;
152 
153  // file descriptor for the sending and receiving through sockets
154  int32_t _send_socket;
155  int32_t _recv_socket;
156 
157  // references to udp client and server objects
160 
161  #ifdef NETBRIDGE_ENABLE_LOG_INPUT
162  uint32_t _trafficIn;
163  #endif
164 
165  #ifdef NETBRIDGE_ENABLE_LOG_OUTPUT
166  uint32_t _trafficOut;
167  #endif
168 
169  // number of flits to receive from the noc before
170  // send to the udp network (read from the second flit)
171  uint32_t _flits_to_recv;
173  uint32_t _flits_to_send;
175 
176  // auxiliary regs
177  FlitType _out_reg;
178 
179  // memory space to store packets received from the
180  // noc and from the external network
181  uint8_t _recv_buffer[RECV_BUFFER_LEN];
182  uint8_t _send_buffer[SEND_BUFFER_LEN];
183 
184  // in and out buffer (noc side)
187 
188  // flag to gracefully terminate the subthread
190 
191  public:
192  // returns current output
193  void LogWrite(std::string);
194 
195  // returns a pointer to the receiving buffer
196  uint8_t* GetBuffer();
197 
198  // internal processes
199  void udpToNocProcess();
200  void nocToUdpProcess();
201 
202  // thread for receiving (non-block)
203  static void* udpRecvThread(void*);
204 
205  udp_server* GetUdpServer();
206 
207  // other
208  SimulationTime Run();
209  void Reset();
210 
211  // ctor./dtor.
212  explicit NetBridge(std::string name);
213  ~NetBridge();
214 
215  Signal<int8_t>* GetSignalRecv();
216 
217  Buffer<FlitType>* GetInputBuffer();
218  void SetOutputBuffer(Buffer<FlitType>*);
219 };
220 
221 } // namespace orcasim::models::orca
222 #endif // MODELS_ORCA_VIRTUAL_ETHERNET_INCLUDE_NETBRIDGE_HPP_
The Signal class models a generic bus of width equals to the sizeof(T)
Definition: Signal.hpp:45
This class models a TimedModel.
Definition: TimedModel.hpp:42
#define RECV_BUFFER_LEN
Definition: NetBridge.hpp:119
STL namespace.
NetBridgeSendState _send_state
Definition: NetBridge.hpp:138
Signal< int8_t > * _signal_recv
Definition: NetBridge.hpp:146
#define SEND_BUFFER_LEN
Definition: NetBridge.hpp:120
uint32_t SimulationTime
uint16_t FlitType
flit
Definition: DmaNetif.hpp:58
NetBridgeRecvState _recv_state
Definition: NetBridge.hpp:139
Buffer< FlitType > * _ib
Definition: NetBridge.hpp:185
Buffer< FlitType > * _ob
Definition: NetBridge.hpp:186