OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mirror_ksync.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <ksync/ksync_index.h>
9 #include <ksync/ksync_sock.h>
10 #include "vr_mirror.h"
11 
13  const MirrorKSyncEntry *entry,
14  uint32_t index) :
15  KSyncNetlinkDBEntry(index), ksync_obj_(obj), vrf_id_(entry->vrf_id_),
16  sip_(entry->sip_), sport_(entry->sport_), dip_(entry->dip_),
17  dport_(entry->dport_), analyzer_name_(entry->analyzer_name_),
18  mirror_flag_(entry->mirror_flag_), vni_(entry->vni_),
19  nic_assisted_mirroring_(entry->nic_assisted_mirroring_),
20  nic_assisted_mirroring_vlan_(entry->nic_assisted_mirroring_vlan_),
21  mirror_index_(entry->mirror_index_) {
22 }
23 
25  const uint32_t vrf_id, IpAddress dip,
26  uint16_t dport) :
27  KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj), vrf_id_(vrf_id),
28  dip_(dip), dport_(dport) {
29 }
30 
32  const MirrorEntry *mirror_entry) :
33  KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
34  vrf_id_(mirror_entry->vrf_id()), sip_(*mirror_entry->GetSip()),
35  sport_(mirror_entry->GetSPort()), dip_(*mirror_entry->GetDip()),
36  dport_(mirror_entry->GetDPort()), nh_(NULL),
37  analyzer_name_(mirror_entry->GetAnalyzerName()),
38  mirror_flag_(mirror_entry->GetMirrorFlag()), vni_(mirror_entry->GetVni()),
39  nic_assisted_mirroring_(mirror_entry->nic_assisted_mirroring()),
40  nic_assisted_mirroring_vlan_(mirror_entry->nic_assisted_mirroring_vlan()),
41  mirror_index_(mirror_entry->mirror_index()) {
42 }
43 
45  std::string &analyzer_name) :
46  ksync_obj_(obj), analyzer_name_(analyzer_name),
47  mirror_index_(MirrorTable::kInvalidIndex) {
48 }
49 
51 }
52 
54  return ksync_obj_;
55 }
56 
57 bool MirrorKSyncEntry::IsLess(const KSyncEntry &rhs) const {
58  const MirrorKSyncEntry &entry = static_cast<const MirrorKSyncEntry &>(rhs);
59  return (analyzer_name_ < entry.analyzer_name_);
60 }
61 
62 std::string MirrorKSyncEntry::ToString() const {
63  std::stringstream s;
64 
65  s << "Mirror Entry : " << dip_.to_string() << ":" << dport_;
66  const VrfEntry* vrf =
68  if (vrf) {
69  s << " Vrf : " << vrf->GetName();
70  }
71  return s.str();
72 }
73 
75  bool ret = false;
76  const MirrorEntry *mirror = static_cast<MirrorEntry *>(e);
77  // ignore nh refernce if it is nic assisted.
78  // and return early.
81  ret = true;
82  }
83 
84  if (mirror->nic_assisted_mirroring()) {
85  nh_ = NULL;
87  mirror->nic_assisted_mirroring_vlan()) {
90  ret = true;
91  }
92  return ret;
93  } else {
95  }
96 
97  if (vni_ != mirror->GetVni()) {
98  vni_ = mirror->GetVni();
99  ret =true;
100  }
101 
102  if (mirror_flag_ != mirror->GetMirrorFlag()) {
103  mirror_flag_ = mirror->GetMirrorFlag();
104  ret = true;
105  }
106 
107  NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
108  if (mirror->GetNH() == NULL) {
109  LOG(DEBUG, "nexthop in Mirror entry is null");
110  assert(0);
111  }
112  NHKSyncEntry nh_entry(nh_object, mirror->GetNH());
113  NHKSyncEntry *old_nh = nh();
114 
115  nh_ = nh_object->GetReference(&nh_entry);
116  if (old_nh != nh()) {
117  ret = true;
118  }
119 
120  if (mirror_index_ != mirror->mirror_index()) {
121  mirror_index_ = mirror->mirror_index();
122  ret = true;
123  }
124 
125  return ret;
126 }
127 
128 int MirrorKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
129  vr_mirror_req encoder;
130  int encode_len;
131  encoder.set_mirr_index(mirror_index_);
132  encoder.set_h_op(op);
133  encoder.set_mirr_rid(0);
135  NHKSyncEntry *nh_entry = nh();
136  encoder.set_mirr_nhid(nh_entry->nh_id());
137  encoder.set_mirr_vni(vni_);
139  encoder.set_mirr_flags(VR_MIRROR_FLAG_DYNAMIC);
140  }
141  } else {
142  encoder.set_mirr_vlan(nic_assisted_mirroring_vlan_);
143  encoder.set_mirr_flags(VR_MIRROR_FLAG_HW_ASSISTED);
144  }
145  int error = 0;
146  encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
147  assert(error == 0);
148  assert(encode_len <= buf_len);
149  LOG(DEBUG, "Mirror index " << GetIndex());
150  return encode_len;
151 }
152 
153 int MirrorKSyncEntry::AddMsg(char *buf, int buf_len) {
154  LOG(DEBUG, "MirrorEntry: Add");
155  return Encode(sandesh_op::ADD, buf, buf_len);
156 }
157 
158 int MirrorKSyncEntry::ChangeMsg(char *buf, int buf_len){
159  return AddMsg(buf, buf_len);
160 }
161 
162 int MirrorKSyncEntry::DeleteMsg(char *buf, int buf_len) {
163  LOG(DEBUG, "MirrorEntry: Delete");
164  return Encode(sandesh_op::DEL, buf, buf_len);
165 }
166 
169  return NULL;
170  }
171 
172  NHKSyncEntry *nh_entry = nh();
173  if (!nh_entry->IsResolved()) {
174  return nh_entry;
175  }
176  return NULL;
177 }
178 
180  KSyncDBObject("KSync Mirror", kMirrorIndexCount), ksync_(ksync) {
181 }
182 
184 }
185 
188 }
189 
190 KSyncEntry *MirrorKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
191  const MirrorKSyncEntry *mirror_entry =
192  static_cast<const MirrorKSyncEntry *>(entry);
193  MirrorKSyncEntry *ksync = new MirrorKSyncEntry(this, mirror_entry, index);
194  return static_cast<KSyncEntry *>(ksync);
195 }
196 
198  const MirrorEntry *mirror_entry = static_cast<const MirrorEntry *>(e);
199  MirrorKSyncEntry *ksync = new MirrorKSyncEntry(this, mirror_entry);
200  return static_cast<KSyncEntry *>(ksync);
201 }
202 
203 uint32_t MirrorKSyncObject::GetIdx(std::string analyzer_name) {
204  MirrorKSyncEntry key(this, analyzer_name);
205  KSyncEntry *entry = Find(&key);
206  if (entry) {
207  return static_cast<MirrorKSyncEntry *>(entry)->mirror_index();
208  }
210 }
211 
212 void vr_mirror_req::Process(SandeshContext *context) {
213  AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
214  ioc->MirrorMsgHandler(this);
215 }
KSyncDBObject * GetObject() const
Definition: mirror_ksync.cc:53
const NextHop * GetNH() const
Definition: mirror_table.h:82
uint16_t nic_assisted_mirroring_vlan_
Definition: mirror_ksync.h:54
uint32_t vrf_id_
Definition: mirror_ksync.h:44
virtual ~MirrorKSyncEntry()
Definition: mirror_ksync.cc:50
void RegisterDb(DBTableBase *table)
Definition: vrf.h:86
bool IsResolved()
virtual KSyncEntry * Alloc(const KSyncEntry *entry, uint32_t index)
Agent * agent() const
Definition: ksync_init.h:39
KSync * ksync() const
Definition: mirror_ksync.h:65
virtual int DeleteMsg(char *buf, int buf_len)
int Encode(sandesh_op::type op, char *buf, int buf_len)
boost::asio::ip::address IpAddress
Definition: address.h:13
uint32_t nh_id() const
Definition: nexthop_ksync.h:58
virtual void MirrorMsgHandler(vr_mirror_req *req)=0
const string & GetName() const
Definition: vrf.h:100
virtual int AddMsg(char *buf, int buf_len)
virtual std::string ToString() const
Definition: mirror_ksync.cc:62
MirrorTable * mirror_table() const
Definition: agent.h:525
static const uint8_t kInvalidIndex
Definition: mirror_table.h:119
VrfEntry * FindVrfFromId(size_t index)
Definition: vrf.cc:884
uint16_t nic_assisted_mirroring_vlan() const
Definition: mirror_table.h:88
uint8_t type
Definition: load_balance.h:109
NHKSyncObject * nh_ksync_obj() const
Definition: ksync_init.h:43
std::string analyzer_name_
Definition: mirror_ksync.h:50
uint8_t mirror_flag_
Definition: mirror_ksync.h:51
virtual ~MirrorKSyncObject()
virtual KSyncEntry * UnresolvedReference()
bool nic_assisted_mirroring() const
Definition: mirror_table.h:90
uint8_t GetMirrorFlag() const
Definition: mirror_table.h:85
MirrorKSyncObject * ksync_obj_
Definition: mirror_ksync.h:43
KSyncEntry * Find(const KSyncEntry *key)
Definition: ksync_object.cc:99
IpAddress dip_
Definition: mirror_ksync.h:47
bool nic_assisted_mirroring_
Definition: mirror_ksync.h:53
KSyncEntry * GetReference(const KSyncEntry *key)
virtual int ChangeMsg(char *buf, int buf_len)
VrfTable * vrf_table() const
Definition: agent.h:485
uint32_t mirror_index_
Definition: mirror_ksync.h:55
uint32_t mirror_index() const
Definition: mirror_table.h:94
uint32_t GetVni() const
Definition: mirror_table.h:84
NHKSyncEntry * nh() const
Definition: mirror_ksync.h:29
virtual bool IsLess(const KSyncEntry &rhs) const
Definition: mirror_ksync.cc:57
#define LOG(_Level, _Msg)
Definition: logging.h:33
virtual KSyncEntry * DBToKSyncEntry(const DBEntry *e)
virtual bool Sync(DBEntry *e)
Definition: mirror_ksync.cc:74
size_t GetIndex() const
Definition: ksync_entry.h:156
uint32_t GetIdx(std::string analyzer_name)
KSyncEntryPtr nh_
Definition: mirror_ksync.h:49
MirrorKSyncEntry(MirrorKSyncObject *obj, const MirrorEntry *)
Definition: mirror_ksync.cc:31
MirrorKSyncObject(KSync *ksync)