OpenSDN source code
physical_switch_ovsdb.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 extern "C" {
7 #include <ovsdb_wrapper.h>
8 };
9 
10 #include <base/task.h>
11 
13 #include <ovsdb_client.h>
14 #include <ovsdb_client_idl.h>
15 #include <ovsdb_client_session.h>
17 #include <physical_switch_ovsdb.h>
18 #include <logical_switch_ovsdb.h>
19 #include <physical_port_ovsdb.h>
20 #include <ovsdb_types.h>
21 
22 using OVSDB::OvsdbClient;
26 
27 PhysicalSwitchEntry::PhysicalSwitchEntry(PhysicalSwitchTable *table,
28  const std::string &name) : OvsdbEntry(table), name_(name),
29  tunnel_ip_() {
30 }
31 
33 }
34 
37  name_, table_->client_idl());
38  return true;
39 }
40 
43  name_, table_->client_idl());
44  return true;
45 }
46 
48  return tunnel_ip_;
49 }
50 
51 const std::string &PhysicalSwitchEntry::name() {
52  return name_;
53 }
54 
55 void PhysicalSwitchEntry::set_tunnel_ip(std::string ip) {
56  boost::system::error_code ec;
57  tunnel_ip_ = Ip4Address::from_string(ip, ec);
58 }
59 
60 bool PhysicalSwitchEntry::IsLess(const KSyncEntry &entry) const {
61  const PhysicalSwitchEntry &ps_entry =
62  static_cast<const PhysicalSwitchEntry&>(entry);
63  return (name_ < ps_entry.name_);
64 }
65 
67  return NULL;
68 }
69 
71  SandeshPhysicalSwitchInfo info;
72  if (event == ADD) {
73  info.set_op("Add");
74  } else {
75  info.set_op("Delete");
76  }
77  info.set_name(name_);
78  OVSDB_TRACE(PhysicalSwitch, info);
79 }
80 
82  OvsdbObject(idl), update_ports_(false) {
84  boost::bind(&PhysicalSwitchTable::Notify, this, _1, _2));
85 }
86 
88 }
89 
91  struct ovsdb_idl_row *row) {
92  PhysicalSwitchEntry *row_entry = FindSwitchEntry(row);
93  if (op == OvsdbClientIdl::OVSDB_DEL) {
94  TriggerDelete(row_entry);
95  } else if (op == OvsdbClientIdl::OVSDB_ADD) {
96  std::string ps_name(ovsdb_wrapper_physical_switch_name(row));
97  PhysicalSwitchEntry key(this, ps_name);
98  PhysicalSwitchEntry *entry =
99  static_cast<PhysicalSwitchEntry *>(FindActiveEntry(&key));
100  if (entry != row_entry) {
101  // Physical Switch name has been changed trigger delete
102  // for both the entries to cleanup and re-add the newly
103  // created entry
104  TriggerDelete(row_entry);
105  TriggerDelete(entry);
106  row_entry = NULL;
107  entry = NULL;
108  }
109  if (entry == NULL) {
110  entry = static_cast<PhysicalSwitchEntry *>(Create(&key));
112  entry->ovs_entry_ = row;
113  idl_entry_map_[row] = entry;
114  }
116 
117  // check if we need to skip ports updation
118  if (update_ports_) {
119  UpdatePorts(entry);
120  }
121  } else {
122  assert(0);
123  }
124 }
125 
126 KSyncEntry *PhysicalSwitchTable::Alloc(const KSyncEntry *key, uint32_t index) {
127  const PhysicalSwitchEntry *k_entry =
128  static_cast<const PhysicalSwitchEntry *>(key);
129  PhysicalSwitchEntry *entry = new PhysicalSwitchEntry(this, k_entry->name_);
130  return entry;
131 }
132 
134  if (update_ports_) {
135  return;
136  }
137  update_ports_ = true;
138  PhysicalSwitchEntry *entry = static_cast<PhysicalSwitchEntry *>(Next(NULL));
139  while (entry != NULL) {
140  // Trigger Update Ports to update pending physical port entries
141  UpdatePorts(entry);
142  entry = static_cast<PhysicalSwitchEntry *>(Next(entry));
143  }
144 }
145 
147  if (entry == NULL) {
148  return;
149  }
151  idl_entry_map_.erase(entry->ovs_entry_);
152  entry->ovs_entry_ = NULL;
153  // UpdatePorts after reseting ovs_entry_ to NULL will result in
154  // clean up of all the ports
155  UpdatePorts(entry);
156  Delete(entry);
157 }
158 
162  entry->intf_list_.clear();
163  if (entry->ovs_entry_ != NULL) {
164  std::size_t count =
166  struct ovsdb_idl_row *ports[count];
167  ovsdb_wrapper_physical_switch_ports(entry->ovs_entry_, ports, count);
168  std::size_t i = 0;
169  while (i < count) {
170  entry->intf_list_.insert(ports[i]);
171  if (old.erase(ports[i]) == 0) {
172  // if entry was not present eariler trigger Add.
173  p_table->CreatePortEntry(ports[i], entry->name());
174  }
175  i++;
176  }
177  }
178  PhysicalSwitchEntry::InterfaceList::iterator it = old.begin();
179  while (it != old.end()) {
180  // trigger del notify for ovs idl row
181  p_table->DeletePortEntry(*it);
182  old.erase(it);
183  it = old.begin();
184  }
185 }
186 
188  struct ovsdb_idl_row *row) {
189  IdlEntryMap::iterator it = idl_entry_map_.find(row);
190  if (it != idl_entry_map_.end()) {
191  return it->second;
192  }
193 
194  return NULL;
195 }
196 
198 // Sandesh routines
201 public:
202  PhysicalSwitchSandeshTask(std::string resp_ctx, const std::string &ip,
203  uint32_t port) :
204  Task((TaskScheduler::GetInstance()->GetTaskId("Agent::KSync")), 0),
205  resp_(new OvsdbPhysicalSwitchResp()), resp_data_(resp_ctx),
206  ip_(ip), port_(port) {
207  }
209  virtual bool Run() {
210  std::vector<OvsdbPhysicalSwitchEntry> pswitch;
211  OvsdbClientSession *session;
212  if (ip_.empty()) {
213  session = Agent::GetInstance()->ovsdb_client()->NextSession(NULL);
214  } else {
215  boost::system::error_code ec;
216  Ip4Address ip_addr = Ip4Address::from_string(ip_, ec);
217  session = Agent::GetInstance()->ovsdb_client()->FindSession(ip_addr,
218  port_);
219  }
220  if (session != NULL && session->client_idl() != NULL) {
221  PhysicalSwitchTable *table =
222  session->client_idl()->physical_switch_table();
223  PhysicalSwitchEntry *entry =
224  static_cast<PhysicalSwitchEntry *>(table->Next(NULL));
225  while (entry != NULL) {
226  OvsdbPhysicalSwitchEntry pentry;
227  pentry.set_state(entry->StateString());
228  pentry.set_name(entry->name());
229  pentry.set_tunnel_ip(entry->tunnel_ip().to_string());
230  pswitch.push_back(pentry);
231  entry = static_cast<PhysicalSwitchEntry *>(table->Next(entry));
232  }
233  }
234  resp_->set_pswitch(pswitch);
235  SendResponse();
236  return true;
237  }
238  std::string Description() const { return "PhysicalSwitchSandeshTask"; }
239 private:
240  void SendResponse() {
241  resp_->set_context(resp_data_);
242  resp_->set_more(false);
243  resp_->Response();
244  }
245 
246  OvsdbPhysicalSwitchResp *resp_;
247  std::string resp_data_;
248  std::string ip_;
249  uint32_t port_;
251 };
252 
253 void OvsdbPhysicalSwitchReq::HandleRequest() const {
255  new PhysicalSwitchSandeshTask(context(), get_session_remote_ip(),
256  get_session_remote_port());
258  scheduler->Enqueue(task);
259 }
260 
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
OVSDB::OvsdbClient * ovsdb_client() const
Definition: agent.h:1129
static Agent * GetInstance()
Definition: agent.h:439
std::string StateString() const
KSyncEntry * Next(const KSyncEntry *entry) const
KSyncEntry * Create(const KSyncEntry *key)
void Delete(KSyncEntry *entry)
void AddIdlToConnectionState(const std::string &device_name, OvsdbClientIdl *idl)
void DelIdlToConnectionState(const std::string &device_name, OvsdbClientIdl *idl)
PhysicalPortTable * physical_port_table()
PhysicalSwitchTable * physical_switch_table()
ConnectionStateTable * connection_table()
void Register(EntryType type, NotifyCB cb)
virtual OvsdbClientSession * NextSession(OvsdbClientSession *session)=0
virtual OvsdbClientSession * FindSession(Ip4Address ip, uint16_t port)=0
OvsdbObject * table_
Definition: ovsdb_entry.h:53
struct ovsdb_idl_row * ovs_entry_
Definition: ovsdb_entry.h:54
OvsdbClientIdl * client_idl()
Definition: ovsdb_object.h:32
KSyncEntry * FindActiveEntry(KSyncEntry *key)
Definition: ovsdb_object.cc:24
void DeletePortEntry(struct ovsdb_idl_row *row)
void CreatePortEntry(struct ovsdb_idl_row *row, const std::string &physical_device)
void set_tunnel_ip(std::string ip)
std::set< struct ovsdb_idl_row * > InterfaceList
void SendTrace(Trace event) const
bool IsLess(const KSyncEntry &) const
PhysicalSwitchEntry * FindSwitchEntry(struct ovsdb_idl_row *row)
void UpdatePorts(PhysicalSwitchEntry *entry)
void Notify(OvsdbClientIdl::Op, struct ovsdb_idl_row *)
void TriggerDelete(PhysicalSwitchEntry *entry)
KSyncEntry * Alloc(const KSyncEntry *key, uint32_t index)
PhysicalSwitchTable(OvsdbClientIdl *idl)
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
PhysicalSwitchSandeshTask(std::string resp_ctx, const std::string &ip, uint32_t port)
OvsdbPhysicalSwitchResp * resp_
std::string Description() const
Gives a description of the task.
DISALLOW_COPY_AND_ASSIGN(PhysicalSwitchSandeshTask)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
Definition: task.cc:662
static TaskScheduler * GetInstance()
Definition: task.cc:574
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
Definition: trace.h:289
void SendResponse(HttpSession *session, const std::string &msg, int status_code)
#define OVSDB_TRACE(obj,...)
size_t ovsdb_wrapper_physical_switch_ports_count(struct ovsdb_idl_row *row)
const char * ovsdb_wrapper_physical_switch_tunnel_ip(struct ovsdb_idl_row *row)
char * ovsdb_wrapper_physical_switch_name(struct ovsdb_idl_row *row)
void ovsdb_wrapper_physical_switch_ports(struct ovsdb_idl_row *row, struct ovsdb_idl_row **ports, size_t n)
Definition: task_int.h:11