Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : */
4 :
5 : #include <boost/asio.hpp>
6 : #include <boost/bind/bind.hpp>
7 :
8 : #include <base/logging.h>
9 : #include <db/db_entry.h>
10 : #include <db/db_table.h>
11 : #include <db/db_table_partition.h>
12 : #include <ksync/ksync_index.h>
13 : #include <ksync/ksync_entry.h>
14 : #include <ksync/ksync_object.h>
15 : #include <ksync/ksync_netlink.h>
16 : #include <ksync/ksync_sock.h>
17 :
18 : #include "oper/interface_common.h"
19 : #include "oper/nexthop.h"
20 : #include "oper/vxlan.h"
21 : #include "oper/mirror_table.h"
22 :
23 : #include "vrouter/ksync/interface_ksync.h"
24 : #include "vrouter/ksync/nexthop_ksync.h"
25 : #include "vrouter/ksync/vxlan_ksync.h"
26 :
27 : #include "ksync_init.h"
28 :
29 : using namespace boost::placeholders;
30 :
31 13 : VxLanIdKSyncEntry::VxLanIdKSyncEntry(VxLanKSyncObject *obj,
32 : const VxLanIdKSyncEntry *entry,
33 13 : uint32_t index) :
34 13 : KSyncNetlinkDBEntry(index), ksync_obj_(obj), label_(entry->label_),
35 13 : nh_(NULL) {
36 13 : }
37 :
38 13 : VxLanIdKSyncEntry::VxLanIdKSyncEntry(VxLanKSyncObject *obj,
39 13 : const VxLanId *vxlan_id) :
40 13 : KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
41 13 : label_(vxlan_id->vxlan_id()), nh_(NULL) {
42 13 : }
43 :
44 52 : VxLanIdKSyncEntry::~VxLanIdKSyncEntry() {
45 52 : }
46 :
47 65 : KSyncDBObject *VxLanIdKSyncEntry::GetObject() const {
48 65 : return ksync_obj_;
49 : }
50 :
51 26 : bool VxLanIdKSyncEntry::IsLess(const KSyncEntry &rhs) const {
52 26 : const VxLanIdKSyncEntry &entry =
53 : static_cast<const VxLanIdKSyncEntry &>(rhs);
54 :
55 26 : return label_ < entry.label_;
56 : }
57 :
58 52 : std::string VxLanIdKSyncEntry::ToString() const {
59 52 : std::stringstream s;
60 52 : NHKSyncEntry *nexthop = nh();
61 :
62 52 : if (nexthop) {
63 52 : s << "VXLAN Label: " << label_ << " Index : "
64 52 : << GetIndex() << nexthop->ToString();
65 : } else {
66 0 : s << "VXLAN Label: " << label_ << " Index : "
67 0 : << GetIndex() << " NextHop : <null>";
68 : }
69 104 : return s.str();
70 52 : }
71 :
72 13 : bool VxLanIdKSyncEntry::Sync(DBEntry *e) {
73 13 : bool ret = false;
74 13 : const VxLanId *vxlan_id = static_cast<VxLanId *>(e);
75 :
76 13 : NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
77 13 : if (vxlan_id->nexthop() == NULL) {
78 0 : LOG(DEBUG, "nexthop in network-id label is null");
79 0 : assert(0);
80 : }
81 13 : NHKSyncEntry nexthop(nh_object, vxlan_id->nexthop());
82 13 : NHKSyncEntry *old_nh = nh();
83 :
84 13 : nh_ = nh_object->GetReference(&nexthop);
85 13 : if (old_nh != nh()) {
86 13 : ret = true;
87 : }
88 :
89 13 : return ret;
90 13 : };
91 :
92 26 : int VxLanIdKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
93 26 : vr_vxlan_req encoder;
94 : int encode_len;
95 26 : NHKSyncEntry *nexthop = nh();
96 :
97 26 : encoder.set_h_op(op);
98 26 : encoder.set_vxlanr_rid(0);
99 26 : encoder.set_vxlanr_vnid(label_);
100 26 : encoder.set_vxlanr_nhid(nexthop->nh_id());
101 26 : int error = 0;
102 26 : encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
103 26 : assert(error == 0);
104 26 : assert(encode_len <= buf_len);
105 26 : return encode_len;
106 26 : }
107 :
108 26 : void VxLanIdKSyncEntry::FillObjectLog(sandesh_op::type op,
109 : KSyncVxLanInfo &info) const {
110 26 : info.set_label(label_);
111 26 : info.set_nh(nh()->nh_id());
112 :
113 26 : if (op == sandesh_op::ADD) {
114 13 : info.set_operation("ADD/CHANGE");
115 : } else {
116 13 : info.set_operation("DELETE");
117 : }
118 26 : }
119 :
120 13 : int VxLanIdKSyncEntry::AddMsg(char *buf, int buf_len) {
121 13 : KSyncVxLanInfo info;
122 13 : FillObjectLog(sandesh_op::ADD, info);
123 13 : KSYNC_TRACE(VxLan, GetObject(), info);
124 :
125 26 : return Encode(sandesh_op::ADD, buf, buf_len);
126 13 : }
127 :
128 0 : int VxLanIdKSyncEntry::ChangeMsg(char *buf, int buf_len) {
129 0 : KSyncVxLanInfo info;
130 0 : FillObjectLog(sandesh_op::ADD, info);
131 0 : KSYNC_TRACE(VxLan, GetObject(), info);
132 :
133 0 : return Encode(sandesh_op::ADD, buf, buf_len);
134 0 : }
135 :
136 13 : int VxLanIdKSyncEntry::DeleteMsg(char *buf, int buf_len) {
137 13 : KSyncVxLanInfo info;
138 13 : FillObjectLog(sandesh_op::DEL, info);
139 13 : KSYNC_TRACE(VxLan, GetObject(), info);
140 :
141 26 : return Encode(sandesh_op::DEL, buf, buf_len);
142 13 : }
143 :
144 13 : KSyncEntry *VxLanIdKSyncEntry::UnresolvedReference() {
145 13 : NHKSyncEntry *nexthop = nh();
146 13 : if (!nexthop->IsResolved()) {
147 0 : return nexthop;
148 : }
149 13 : return NULL;
150 : }
151 :
152 3 : VxLanKSyncObject::VxLanKSyncObject(KSync *ksync)
153 3 : : KSyncDBObject("KSync VxLan"), ksync_(ksync) {
154 3 : }
155 :
156 6 : VxLanKSyncObject::~VxLanKSyncObject() {
157 6 : }
158 :
159 3 : void VxLanKSyncObject::RegisterDBClients() {
160 3 : RegisterDb(ksync_->agent()->vxlan_table());
161 3 : }
162 :
163 13 : KSyncEntry *VxLanKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
164 13 : const VxLanIdKSyncEntry *vxlan =
165 : static_cast<const VxLanIdKSyncEntry *>(entry);
166 13 : VxLanIdKSyncEntry *ksync = new VxLanIdKSyncEntry(this, vxlan, index);
167 13 : return static_cast<KSyncEntry *>(ksync);
168 : }
169 :
170 13 : KSyncEntry *VxLanKSyncObject::DBToKSyncEntry(const DBEntry *e) {
171 13 : const VxLanId *vxlan = static_cast<const VxLanId *>(e);
172 13 : VxLanIdKSyncEntry *key = new VxLanIdKSyncEntry(this, vxlan);
173 13 : return static_cast<KSyncEntry *>(key);
174 : }
175 :
176 26 : void vr_vxlan_req::Process(SandeshContext *context) {
177 26 : AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
178 26 : ioc->VxLanMsgHandler(this);
179 26 : }
180 :
|