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