A Discrete-Event Network Simulator
API
ofswitch13-interface.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 University of Campinas (Unicamp)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Luciano Jerez Chaves <ljerezchaves@gmail.com>
18  */
19 
20 #include "ofswitch13-interface.h"
21 
22 #include "ofswitch13-controller.h"
23 #include "ofswitch13-device.h"
24 
25 namespace ns3
26 {
27 
28 NS_LOG_COMPONENT_DEFINE("OFSwitch13Interface");
29 
30 void
31 EnableBofussLog(bool printToFile,
32  std::string prefix,
33  bool explicitFilename,
34  std::string customLevels)
35 {
36  vlog_init();
37  vlog_set_pattern(VLF_ANY_FACILITY, "%d{%ss} [%c|%p] %m");
38 
39  if (printToFile)
40  {
41  std::string filename = prefix;
42  if (!explicitFilename)
43  {
44  if (filename.size() && filename.back() != '-')
45  {
46  filename += "-";
47  }
48  filename += "BOFUSS.log";
49  }
50  vlog_set_log_file(filename.c_str());
51  vlog_set_levels(VLM_ANY_MODULE, VLF_FILE, VLL_DBG);
52  }
53  else
54  {
55  vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_DBG);
56  }
57 
58  if (customLevels.size())
59  {
60  vlog_set_levels_from_string(customLevels.c_str());
61  }
62 }
63 
64 struct ofpbuf*
65 BufferFromPacket(Ptr<const Packet> packet, size_t bodyRoom, size_t headRoom)
66 {
68 
69  NS_ASSERT(packet->GetSize() <= bodyRoom);
70  struct ofpbuf* buffer;
71  uint32_t pktSize;
72 
73  pktSize = packet->GetSize();
74  buffer = ofpbuf_new_with_headroom(bodyRoom, headRoom);
75  packet->CopyData((uint8_t*)ofpbuf_put_uninit(buffer, pktSize), pktSize);
76  return buffer;
77 }
78 
80 PacketFromMsg(struct ofl_msg_header* msg, uint32_t xid)
81 {
83 
84  int error;
85  uint8_t* buf;
86  size_t buf_size;
87  Ptr<Packet> packet;
88  struct ofpbuf* buffer;
89 
90  buffer = ofpbuf_new(0);
91  error = ofl_msg_pack(msg, xid, &buf, &buf_size, nullptr);
92  if (!error)
93  {
94  ofpbuf_use(buffer, buf, buf_size);
95  ofpbuf_put_uninit(buffer, buf_size);
96  packet = Create<Packet>((uint8_t*)buffer->data, buffer->size);
97  ofpbuf_delete(buffer);
98  }
99  return packet;
100 }
101 
103 PacketFromBuffer(struct ofpbuf* buffer)
104 {
106 
107  return Create<Packet>((uint8_t*)buffer->data, buffer->size);
108 }
109 
110 } // namespace ns3
111 
112 using namespace ns3;
113 
118 time_t
119 time_now(void)
120 {
121  return static_cast<time_t>(Simulator::Now().ToInteger(Time::S));
122 }
123 
128 long long int
130 {
131  return static_cast<long long int>(Simulator::Now().GetMilliSeconds());
132 }
133 
135 void
136 send_packet_to_controller(struct pipeline* pl,
137  struct packet* pkt,
138  uint8_t table_id,
139  uint8_t reason)
140 {
141  return OFSwitch13Device::SendPacketToController(pl, pkt, table_id, reason);
142 }
143 
144 int
145 send_openflow_buffer_to_remote(struct ofpbuf* buffer, struct remote* remote)
146 {
147  return OFSwitch13Device::SendOpenflowBufferToRemote(buffer, remote);
148 }
149 
150 void
151 dp_actions_output_port(struct packet* pkt,
152  uint32_t out_port,
153  uint32_t out_queue,
154  uint16_t max_len,
155  uint64_t cookie)
156 {
158  out_port,
159  out_queue,
160  max_len,
161  cookie);
162 }
163 
164 void
165 dpctl_send_and_print(struct vconn* vconn, struct ofl_msg_header* msg)
166 {
168 }
169 
170 void
171 dpctl_transact_and_print(struct vconn* vconn,
172  struct ofl_msg_header* req,
173  struct ofl_msg_header** repl)
174 {
175  // Different from bofus dpctl, this transaction doesn't wait for a reply,
176  // as ns-3 socket library doesn't provide blocking sockets. So, we send the
177  // request and return. The reply will came later, using the ns-3 callback
178  // mechanism.
180 }
static void DpctlSendAndPrint(struct vconn *vconn, struct ofl_msg_header *msg)
Overriding BOFUSS dpctl_send_and_print and dpctl_transact_and_print weak functions from utilities/dpc...
static void DpActionsOutputPort(struct packet *pkt, uint32_t outPort, uint32_t outQueue, uint16_t maxLength, uint64_t cookie)
Overriding BOFUSS dp_actions_output_port weak function from udatapath/dp_actions.c.
static int SendOpenflowBufferToRemote(struct ofpbuf *buffer, struct remote *remote)
Overriding BOFUSS send_openflow_buffer_to_remote weak function from udatapath/datapath....
static void SendPacketToController(struct pipeline *pl, struct packet *pkt, uint8_t tableId, uint8_t reason)
Overriding BOFUSS send_packet_to_controller weak function from udatapath/pipeline....
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
int64_t GetMilliSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:407
@ S
second
Definition: nstime.h:116
int64_t ToInteger(Unit unit) const
Get the Time value expressed in a particular unit.
Definition: nstime.h:554
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Ptr< Packet > PacketFromMsg(struct ofl_msg_header *msg, uint32_t xid)
Create a new ns3::Packet from internal OFLib message.
void EnableBofussLog(bool printToFile, std::string prefix, bool explicitFilename, std::string customLevels)
Enable the logging system of the BOFUSS library.
Ptr< Packet > PacketFromBuffer(struct ofpbuf *buffer)
Create a new ns3::Packet from internal BOFUSS buffer.
struct ofpbuf * BufferFromPacket(Ptr< const Packet > packet, size_t bodyRoom, size_t headRoom)
Create an internal BOFUSS buffer from ns3::Packet.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void send_packet_to_controller(struct pipeline *pl, struct packet *pkt, uint8_t table_id, uint8_t reason)
Overriding BOFUSS weak functions using static member functions.
void dpctl_transact_and_print(struct vconn *vconn, struct ofl_msg_header *req, struct ofl_msg_header **repl)
void dp_actions_output_port(struct packet *pkt, uint32_t out_port, uint32_t out_queue, uint16_t max_len, uint64_t cookie)
time_t time_now(void)
Overriding BOFUSS time_now weak function from timeval.c.
void dpctl_send_and_print(struct vconn *vconn, struct ofl_msg_header *msg)
long long int time_msec(void)
Overriding BOFUSS time_msec weak function from timeval.c.
int send_openflow_buffer_to_remote(struct ofpbuf *buffer, struct remote *remote)
uint32_t pktSize
packet size used for the simulation (in bytes)