/
mirror_ksync.cc
160 lines (133 loc) · 4.97 KB
/
mirror_ksync.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <ksync/ksync_index.h>
#include "vrouter/ksync/mirror_ksync.h"
#include "vrouter/ksync/nexthop_ksync.h"
#include "vrouter/ksync/ksync_init.h"
#include <ksync/ksync_sock.h>
MirrorKSyncEntry::MirrorKSyncEntry(MirrorKSyncObject *obj,
const MirrorKSyncEntry *entry,
uint32_t index) :
KSyncNetlinkDBEntry(index), ksync_obj_(obj), vrf_id_(entry->vrf_id_),
sip_(entry->sip_), sport_(entry->sport_), dip_(entry->dip_),
dport_(entry->dport_), analyzer_name_(entry->analyzer_name_) {
}
MirrorKSyncEntry::MirrorKSyncEntry(MirrorKSyncObject *obj,
const uint32_t vrf_id, IpAddress dip,
uint16_t dport) :
KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj), vrf_id_(vrf_id),
dip_(dip), dport_(dport) {
}
MirrorKSyncEntry::MirrorKSyncEntry(MirrorKSyncObject *obj,
const MirrorEntry *mirror_entry) :
KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
vrf_id_(mirror_entry->vrf_id()), sip_(*mirror_entry->GetSip()),
sport_(mirror_entry->GetSPort()), dip_(*mirror_entry->GetDip()),
dport_(mirror_entry->GetDPort()), nh_(NULL),
analyzer_name_(mirror_entry->GetAnalyzerName()) {
}
MirrorKSyncEntry::MirrorKSyncEntry(MirrorKSyncObject *obj,
std::string &analyzer_name) :
ksync_obj_(obj), analyzer_name_(analyzer_name) {
}
MirrorKSyncEntry::~MirrorKSyncEntry() {
}
KSyncDBObject *MirrorKSyncEntry::GetObject() {
return ksync_obj_;
}
bool MirrorKSyncEntry::IsLess(const KSyncEntry &rhs) const {
const MirrorKSyncEntry &entry = static_cast<const MirrorKSyncEntry &>(rhs);
return (analyzer_name_ < entry.analyzer_name_);
}
std::string MirrorKSyncEntry::ToString() const {
std::stringstream s;
s << "Mirror Entry : " << dip_.to_string() << ":" << dport_;
const VrfEntry* vrf =
ksync_obj_->ksync()->agent()->vrf_table()->FindVrfFromId(vrf_id_);
if (vrf) {
s << " Vrf : " << vrf->GetName();
}
return s.str();
}
bool MirrorKSyncEntry::Sync(DBEntry *e) {
bool ret = false;
const MirrorEntry *mirror = static_cast<MirrorEntry *>(e);
NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
if (mirror->GetNH() == NULL) {
LOG(DEBUG, "nexthop in Mirror entry is null");
assert(0);
}
NHKSyncEntry nh_entry(nh_object, mirror->GetNH());
NHKSyncEntry *old_nh = nh();
nh_ = nh_object->GetReference(&nh_entry);
if (old_nh != nh()) {
ret = true;
}
return ret;
}
int MirrorKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
vr_mirror_req encoder;
int encode_len;
NHKSyncEntry *nh_entry = nh();
encoder.set_h_op(op);
encoder.set_mirr_index(GetIndex());
encoder.set_mirr_rid(0);
encoder.set_mirr_nhid(nh_entry->nh_id());
int error = 0;
encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
assert(error == 0);
assert(encode_len <= buf_len);
LOG(DEBUG, "Mirror index " << GetIndex() << " nhid "
<< nh_entry->nh_id());
return encode_len;
}
int MirrorKSyncEntry::AddMsg(char *buf, int buf_len) {
LOG(DEBUG, "MirrorEntry: Add");
return Encode(sandesh_op::ADD, buf, buf_len);
}
int MirrorKSyncEntry::ChangeMsg(char *buf, int buf_len){
return AddMsg(buf, buf_len);
}
int MirrorKSyncEntry::DeleteMsg(char *buf, int buf_len) {
LOG(DEBUG, "MirrorEntry: Delete");
return Encode(sandesh_op::DELETE, buf, buf_len);
}
KSyncEntry *MirrorKSyncEntry::UnresolvedReference() {
NHKSyncEntry *nh_entry = nh();
if (!nh_entry->IsResolved()) {
return nh_entry;
}
return NULL;
}
MirrorKSyncObject::MirrorKSyncObject(KSync *ksync) :
KSyncDBObject("KSync Mirror", kMirrorIndexCount), ksync_(ksync) {
}
MirrorKSyncObject::~MirrorKSyncObject() {
}
void MirrorKSyncObject::RegisterDBClients() {
RegisterDb(ksync_->agent()->mirror_table());
}
KSyncEntry *MirrorKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
const MirrorKSyncEntry *mirror_entry =
static_cast<const MirrorKSyncEntry *>(entry);
MirrorKSyncEntry *ksync = new MirrorKSyncEntry(this, mirror_entry, index);
return static_cast<KSyncEntry *>(ksync);
}
KSyncEntry *MirrorKSyncObject::DBToKSyncEntry(const DBEntry *e) {
const MirrorEntry *mirror_entry = static_cast<const MirrorEntry *>(e);
MirrorKSyncEntry *ksync = new MirrorKSyncEntry(this, mirror_entry);
return static_cast<KSyncEntry *>(ksync);
}
uint32_t MirrorKSyncObject::GetIdx(std::string analyzer_name) {
MirrorKSyncEntry key(this, analyzer_name);
KSyncEntry *entry = Find(&key);
if (entry) {
return entry->GetIndex();
}
return MirrorTable::kInvalidIndex;
}
void vr_mirror_req::Process(SandeshContext *context) {
AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
ioc->MirrorMsgHandler(this);
}