helloBye3.hpp
Go to the documentation of this file.
1 #ifndef HELLOBYE3PROTO_HPP
2 #define HELLOBYE3PROTO_HPP
3 
4 #include <mutex>
5 #include <sstream>
6 
7 #include "common.hpp"
8 #include "exceptions.hpp"
9 #include "headers.hpp"
10 #include "mbuf.hpp"
11 #include "stateMachine.hpp"
12 
13 namespace HelloBye3 {
14 
15 struct __attribute__((packed)) msg {
16  uint64_t ident;
17  uint8_t role;
18  uint8_t msg;
19  uint8_t cookie;
20 
21  static constexpr uint8_t ROLE_CLIENT = 0;
22  static constexpr uint8_t ROLE_SERVER = 1;
23 
24  static constexpr uint8_t MSG_HELLO = 0;
25  static constexpr uint8_t MSG_BYE = 1;
26 };
27 
28 /*
29  * ===================================
30  * Identifier
31  * ===================================
32  *
33  */
34 
35 // This is the same as in HelloBye2
36 template <typename Packet> class Identifier {
37 public:
38  class ConnectionID {
39  public:
40  uint64_t ident;
41 
42  bool operator==(const ConnectionID &c) const { return ident == c.ident; };
43  bool operator<(const ConnectionID &c) const { return ident < c.ident; };
44  operator std::string() const {
45  std::stringstream sstream;
46  sstream << ident;
47  return sstream.str();
48  };
50  ConnectionID() : ident(0){};
51  ConnectionID(uint64_t i) : ident(i){};
52  };
53  struct Hasher {
54  uint64_t operator()(const ConnectionID &c) const { return c.ident; };
55  };
56  static ConnectionID identify(Packet *pkt) {
57  Headers::Ethernet *eth = reinterpret_cast<Headers::Ethernet *>(pkt->getData());
59  throw new PacketNotIdentified();
60  }
61 
62  Headers::IPv4 *ipv4 = reinterpret_cast<Headers::IPv4 *>(eth->getPayload());
63  if (ipv4->proto != Headers::IPv4::PROTO_UDP) {
64  throw new PacketNotIdentified();
65  }
66 
67  Headers::Udp *udp = reinterpret_cast<Headers::Udp *>(ipv4->getPayload());
68 
69  struct msg *msg = reinterpret_cast<struct msg *>(udp->getPayload());
70  return msg->ident;
71  };
72 };
73 
74 /*
75  * ===================================
76  * Forward declarations
77  * ===================================
78  *
79  */
80 
81 /*
82  * ===================================
83  * Server
84  * ===================================
85  *
86  */
87 
88 namespace Server {
89 struct server {
92 };
93 struct States {
94  static constexpr StateID Hello = 0;
95  static constexpr StateID Bye = 1;
96  static constexpr StateID Terminate = 2;
97 };
98 
100 
101 void runHello(StateMachine<Identifier<mbuf>, mbuf>::State &state, mbuf *pkt,
102  StateMachine<Identifier<mbuf>, mbuf>::FunIface &funIface);
103 
104 void runBye(StateMachine<Identifier<mbuf>, mbuf>::State &state, mbuf *pkt,
105  StateMachine<Identifier<mbuf>, mbuf>::FunIface &funIface);
106 
107 }; // namespace Server
108 
109 /*
110  * ===================================
111  * Client
112  * ===================================
113  *
114  */
115 
116 namespace Client {
117 struct client {
118  uint64_t ident;
119  uint32_t srcIp;
120  uint32_t dstIp;
123  uint16_t srcPort;
124  uint16_t dstPort;
125 };
126 
127 struct States {
128  static constexpr StateID Hello = 0;
129  static constexpr StateID Bye = 1;
130  static constexpr StateID RecvBye = 2;
131  static constexpr StateID Terminate = 3;
132 };
133 
135  uint32_t srcIp, uint32_t dstIp, uint16_t srcPort, uint16_t dstPort, uint64_t ident);
136 
137 void runHello(StateMachine<Identifier<mbuf>, mbuf>::State &state, mbuf *pkt,
138  StateMachine<Identifier<mbuf>, mbuf>::FunIface &funIface);
139 
140 void runBye(StateMachine<Identifier<mbuf>, mbuf>::State &state, mbuf *pkt,
141  StateMachine<Identifier<mbuf>, mbuf>::FunIface &funIface);
142 
143 void runRecvBye(StateMachine<Identifier<mbuf>, mbuf>::State &state, mbuf *pkt,
144  StateMachine<Identifier<mbuf>, mbuf>::FunIface &funIface);
145 
146 } // namespace Client
147 
148 }; // namespace HelloBye3
149 
150 /*
151  * From here on down is the C interface to the above C++ functions
152  */
153 
154 extern "C" {
155 
156 /*
157  * XXX -------------------------------------------- XXX
158  * Server
159  * XXX -------------------------------------------- XXX
160  */
161 
166 void *HelloBye3_Server_init();
167 
176 void *HelloBye3_Server_process(void *obj, struct rte_mbuf **inPkts, unsigned int inCount,
177  unsigned int *sendCount, unsigned int *freeCount);
178 
186  void *obj, struct rte_mbuf **sendPkts, struct rte_mbuf **freePkts);
187 
192 void HelloBye3_Server_free(void *obj);
193 
194 /*
195  * XXX -------------------------------------------- XXX
196  * Client
197  * XXX -------------------------------------------- XXX
198  */
199 
204 void *HelloBye3_Client_init();
205 
220 void *HelloBye3_Client_connect(void *obj, struct rte_mbuf **inPkts, unsigned int inCount,
221  unsigned int *sendCount, unsigned int *freeCount, uint32_t srcIP, uint32_t dstIP,
222  uint16_t srcPort, uint16_t dstPort, uint64_t ident);
223 
231  void *obj, struct rte_mbuf **sendPkts, struct rte_mbuf **freePkts);
232 
242 void *HelloBye3_Client_process(void *obj, struct rte_mbuf **inPkts, unsigned int inCount,
243  unsigned int *sendCount, unsigned int *freeCount);
244 
249 void HelloBye3_Client_free(void *obj);
250 };
251 
252 /*
253  * YCM definition is a workaround for a libclang bug
254  * When compiling, YCM should never be set.
255  * Set YCM in a libclang based IDE in order to avoid errors
256  */
257 #ifndef YCM
258 #include "../src/helloBye3.cpp"
259 #endif
260 
261 #endif /* HELLOBYE3PROTO_HPP */
void * HelloBye3_Server_init()
Init a HelloBye server.
Definition: helloBye3.cpp:301
static constexpr uint16_t ETHERTYPE_IPv4
Definition: headers.hpp:46
uint8_t role
Definition: helloBye3.hpp:17
void HelloBye3_Server_free(void *obj)
Free recources used by the state machine.
Definition: helloBye3.cpp:340
Representation of the IPv4 header.
Definition: headers.hpp:65
State machine framework.
void * HelloBye3_Client_process(void *obj, struct rte_mbuf **inPkts, unsigned int inCount, unsigned int *sendCount, unsigned int *freeCount)
Process a batch of packets.
Definition: helloBye3.cpp:395
void * getPayload()
Get the SDU.
Definition: headers.hpp:149
Representation of the etherner header.
Definition: headers.hpp:15
static constexpr StateID Bye
Definition: helloBye3.hpp:129
Representation of the UDP header.
Definition: headers.hpp:209
void * getPayload()
Get the SDU.
Definition: headers.hpp:248
bool operator<(const ConnectionID &c) const
Definition: helloBye3.hpp:43
uint8_t cookie
Definition: helloBye3.hpp:19
void HelloBye3_Client_free(void *obj)
Free recources used by the state machine.
Definition: helloBye3.cpp:408
uint16_t getEthertype()
Get the ethertype.
Definition: headers.hpp:44
void runHello(StateMachine< Identifier< mbuf >, mbuf >::State &state, mbuf *pkt, StateMachine< Identifier< mbuf >, mbuf >::FunIface &funIface)
Definition: helloBye3.cpp:28
uint64_t ident
Definition: helloBye3.hpp:16
void * HelloBye3_Client_init()
Init a HelloBye client.
Definition: helloBye3.cpp:348
StateMachine< Identifier< mbuf >, mbuf >::State createHello(uint32_t srcIp, uint32_t dstIp, uint16_t srcPort, uint16_t dstPort, uint64_t ident)
Definition: helloBye3.cpp:145
void runHello(StateMachine< Identifier< mbuf >, mbuf >::State &state, mbuf *pkt, StateMachine< Identifier< mbuf >, mbuf >::FunIface &funIface)
Definition: helloBye3.cpp:162
void HelloBye3_Server_getPkts(void *obj, struct rte_mbuf **sendPkts, struct rte_mbuf **freePkts)
Get the packets to send and free.
Definition: helloBye3.cpp:330
ConnectionID(const ConnectionID &c)
Definition: helloBye3.hpp:49
uint64_t operator()(const ConnectionID &c) const
Definition: helloBye3.hpp:54
void * HelloBye3_Server_process(void *obj, struct rte_mbuf **inPkts, unsigned int inCount, unsigned int *sendCount, unsigned int *freeCount)
Process a batch of packets.
Definition: helloBye3.cpp:316
void runBye(StateMachine< Identifier< mbuf >, mbuf >::State &state, mbuf *pkt, StateMachine< Identifier< mbuf >, mbuf >::FunIface &funIface)
Definition: helloBye3.cpp:91
uint8_t proto
next protocol
Definition: headers.hpp:107
static constexpr StateID RecvBye
Definition: helloBye3.hpp:130
static constexpr StateID Terminate
Definition: helloBye3.hpp:131
static constexpr uint8_t PROTO_UDP
Definition: headers.hpp:111
Wrapper aroung DPDK rte_mbuf.
Definition: mbuf.hpp:9
bool operator==(const ConnectionID &c) const
Definition: helloBye3.hpp:42
static constexpr StateID Bye
Definition: helloBye3.hpp:95
void runBye(StateMachine< Identifier< mbuf >, mbuf >::State &state, mbuf *pkt, StateMachine< Identifier< mbuf >, mbuf >::FunIface &funIface)
Definition: helloBye3.cpp:204
uint16_t StateID
Definition: common.hpp:19
static constexpr StateID Hello
Definition: helloBye3.hpp:94
void runRecvBye(StateMachine< Identifier< mbuf >, mbuf >::State &state, mbuf *pkt, StateMachine< Identifier< mbuf >, mbuf >::FunIface &funIface)
Definition: helloBye3.cpp:255
void * HelloBye3_Client_connect(void *obj, struct rte_mbuf **inPkts, unsigned int inCount, unsigned int *sendCount, unsigned int *freeCount, uint32_t srcIP, uint32_t dstIP, uint16_t srcPort, uint16_t dstPort, uint64_t ident)
Open a connection to a server.
Definition: helloBye3.cpp:363
static constexpr StateID Terminate
Definition: helloBye3.hpp:96
static constexpr StateID Hello
Definition: helloBye3.hpp:128
static ConnectionID identify(Packet *pkt)
Definition: helloBye3.hpp:56
void HelloBye3_Client_getPkts(void *obj, struct rte_mbuf **sendPkts, struct rte_mbuf **freePkts)
Get the packets to send and free.
Definition: helloBye3.cpp:385
void * factory(Identifier< mbuf >::ConnectionID id)
Definition: helloBye3.cpp:20
void * getPayload()
Get the SDU.
Definition: headers.hpp:23