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