OpenSDN source code
hbf.cc
Go to the documentation of this file.
1 /*
2  * * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * * Copyright Contributors to the OpenSDN project
4  * */
5 
6 #include <boost/uuid/uuid_io.hpp>
7 #include <cmn/agent_cmn.h>
8 #include <init/agent_param.h>
9 
10 #include <base/logging.h>
11 #include <oper/operdb_init.h>
12 #include <oper/route_common.h>
13 #include <oper/interface_common.h>
14 #include <oper/vrf.h>
15 #include <oper/agent_sandesh.h>
16 #include <oper/hbf.h>
18 
19 
20 #include <sandesh/sandesh_types.h>
21 #include <sandesh/sandesh_constants.h>
22 #include <sandesh/sandesh.h>
23 #include <sandesh/sandesh_trace.h>
24 
25 #include <string.h>
26 
27 using namespace std;
28 using namespace boost::uuids;
29 
31 
33  agent_(agent),
34  interface_listener_id_(DBTable::kInvalidId) {
35 }
36 
39  boost::bind(&HBFHandler::ModifyVmInterface, this, _1, _2));
40  HBFTRACE(Trace, "HBFHandler registered for interface table");
41 }
42 
45 }
46 
48  return (vm_itf->hbs_intf_type() == VmInterface::HBS_INTF_LEFT);
49 }
50 
52  return (vm_itf->hbs_intf_type() == VmInterface::HBS_INTF_RIGHT);
53 }
54 
55 /* Registered call for VM */
57  DBEntryBase *e)
58 {
59  Interface *intf = static_cast<Interface *>(e);
60  VmInterface *vm_itf;
61 
62  if (intf->type() != Interface::VM_INTERFACE) {
63  return;
64  }
65 
66  vm_itf = static_cast<VmInterface *>(intf);
67  HBFIntfDBState *state = static_cast<HBFIntfDBState *>(
68  vm_itf->GetState(partition->parent(), interface_listener_id_));
69 
70  // When this function is invoked for Delete, VMI's vn can be NULL. Use its
71  // state to figure out if it is HBF VMI
72  if (!IsHBFLInterface(vm_itf) && !IsHBFRInterface(vm_itf) && !state) {
73  return;
74  }
75 
76  HBFTRACE(Trace, "vmi notification for " + vm_itf->ToString());
77  if (intf->IsDeleted() || ((vm_itf->l2_active() == false) &&
78  (vm_itf->ipv4_active() == false) &&
79  (vm_itf->ipv6_active() == false))) {
80  if (state) {
81  vm_itf->ClearState(partition->parent(), interface_listener_id_);
82  HBFVrfWalker *walker = static_cast<HBFVrfWalker*>
83  (state->vrf_walker_.get());
84  if (walker) {
85  HBFTRACE(Trace, "Starting VRF walk for deletion of "
86  + vm_itf->ToString());
87  walker->WalkDoneCallback(boost::bind
89  static_cast<HBFVrfWalker *>( walker)));
91  state->lintf_,
92  state->projname_);
93  }
94  delete state;
95  }
96  return;
97  }
98 
99  if (state == NULL && vm_itf->vn()) {
100  state = new HBFIntfDBState(IsHBFLInterface(vm_itf), vm_itf->vn()->GetProject());
101  state->vrf_walker_.reset(new HBFVrfWalker("HBFVrfWalker", agent_));
103  RegisterWalker(static_cast<AgentRouteWalker *>(state->vrf_walker_.get()));
104  vm_itf->SetState(partition->parent(), interface_listener_id_, state);
105  } else {
106  // Dont have to process Change? TODO
107  return;
108  }
109 
110  if (state->vrf_walker_.get()) {
111  HBFTRACE(Trace, "Starting VRF walk for addition of "
112  + vm_itf->ToString());
113  HBFVrfWalker *walker = static_cast<HBFVrfWalker*>(state->vrf_walker_.get());
114  if (walker) {
115  walker->Start(vm_itf->id(),
116  state->lintf_,
117  state->projname_);
118  }
119  }
120 }
121 
122 HBFVrfWalker::HBFVrfWalker(const std::string &name, Agent *agent) :
123  AgentRouteWalker(name, agent) {
124 }
125 
127 }
128 
130  DBEntryBase *e) {
131  VrfEntry *vrf = dynamic_cast<VrfEntry*>(e);
132 
133  if (vrf && vrf->vn()) {
134  if (projname_ == vrf->vn()->GetProject()) {
135  if (hbf_lintf_) {
136  vrf->set_hbf_lintf(hbf_intf_);
137  std::stringstream ss;
138  ss << "Setting lintf to " << hbf_intf_
139  << " for vrf " << vrf->vrf_id() << " name " << vrf->GetName();
140  HBFTRACE(Trace, ss.str());
141  } else {
142  vrf->set_hbf_rintf(hbf_intf_);
143  std::stringstream ss;
144  ss << "Setting rintf to " << hbf_intf_
145  << " for vrf " << vrf->vrf_id() << " name " << vrf->GetName();
146  HBFTRACE(Trace, ss.str());
147  }
148  }
149 
150  vrf->Notify();
151  }
152  return true;
153 }
154 
155 void HBFVrfWalker::Start(uint32_t hbf_intf, bool hbf_lintf, std::string projname) {
156  hbf_intf_ = hbf_intf;
157  hbf_lintf_ = hbf_lintf;
158  projname_ = projname;
159  StartVrfWalk();
160 }
void WalkDoneCallback(WalkDone cb)
boost::function< void()> WalkDone
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
OperDB * oper_db() const
Definition: agent.cc:1017
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:38
void Notify()
Definition: db_entry.cc:128
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:74
bool IsDeleted() const
Definition: db_entry.h:49
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:23
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:208
void Unregister(ListenerId listener)
Definition: db_table.cc:213
DBTableBase * parent()
HBFHandler(Agent *agent)
Definition: hbf.cc:32
void Register()
Definition: hbf.cc:37
DBTable::ListenerId interface_listener_id_
Definition: hbf.h:49
Agent * agent_
Definition: hbf.h:47
bool IsHBFLInterface(VmInterface *vm_itf)
Definition: hbf.cc:47
bool IsHBFRInterface(VmInterface *vm_itf)
Definition: hbf.cc:51
void ModifyVmInterface(DBTablePartBase *partition, DBEntryBase *e)
Definition: hbf.cc:56
void Terminate()
Definition: hbf.cc:43
bool hbf_lintf_
Definition: hbf.h:68
std::string projname_
Definition: hbf.h:69
void Start(uint32_t hbf_intf_, bool hbf_lintf_, std::string projname)
Definition: hbf.cc:155
uint32_t hbf_intf_
Definition: hbf.h:67
HBFVrfWalker(const std::string &name, Agent *agent)
Definition: hbf.cc:122
virtual ~HBFVrfWalker()
Definition: hbf.cc:126
virtual bool VrfWalkNotify(DBTablePartBase *partition, DBEntryBase *e)
Definition: hbf.cc:129
@ VM_INTERFACE
Definition: interface.h:38
bool ipv6_active() const
Definition: interface.h:120
bool l2_active() const
Definition: interface.h:125
bool ipv4_active() const
Definition: interface.h:119
static const uint32_t kInvalidIndex
Definition: interface.h:73
Type type() const
Definition: interface.h:115
const uint32_t id() const
Definition: interface.h:126
AgentRouteWalkerManager * agent_route_walk_manager() const
Definition: operdb_init.h:92
Definition: trace.h:289
std::string ToString() const
const VnEntry * vn() const
VmInterface::HbsIntfType hbs_intf_type() const
std::string GetProject() const
Definition: vn.cc:718
Definition: vrf.h:90
const string & GetName() const
Definition: vrf.h:104
void set_hbf_lintf(uint32_t idx)
Definition: vrf.h:229
void set_hbf_rintf(uint32_t idx)
Definition: vrf.h:228
const uint32_t vrf_id() const
Definition: vrf.h:103
VnEntry * vn() const
Definition: vrf.h:105
SandeshTraceBufferPtr HBFTraceBuf(SandeshTraceBufferCreate("HBF", 2000))
#define HBFTRACE(obj,...)
Definition: hbf.h:18
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:19
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
Definition: sandesh_trace.h:47
AgentRouteWalkerPtr vrf_walker_
Definition: hbf.h:26
std::string projname_
Definition: hbf.h:28
bool lintf_
Definition: hbf.h:27