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 3 : VxLanIdKSyncEntry::VxLanIdKSyncEntry(VxLanKSyncObject *obj,
32 : const VxLanIdKSyncEntry *entry,
33 3 : uint32_t index) :
34 3 : KSyncNetlinkDBEntry(index), ksync_obj_(obj), label_(entry->label_),
35 3 : nh_(NULL) {
36 3 : }
37 :
38 3 : VxLanIdKSyncEntry::VxLanIdKSyncEntry(VxLanKSyncObject *obj,
39 3 : const VxLanId *vxlan_id) :
40 3 : KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
41 3 : label_(vxlan_id->vxlan_id()), nh_(NULL) {
42 3 : }
43 :
44 12 : VxLanIdKSyncEntry::~VxLanIdKSyncEntry() {
45 12 : }
46 :
47 15 : KSyncDBObject *VxLanIdKSyncEntry::GetObject() const {
48 15 : return ksync_obj_;
49 : }
50 :
51 6 : bool VxLanIdKSyncEntry::IsLess(const KSyncEntry &rhs) const {
52 6 : const VxLanIdKSyncEntry &entry =
53 : static_cast<const VxLanIdKSyncEntry &>(rhs);
54 :
55 6 : return label_ < entry.label_;
56 : }
57 :
58 12 : std::string VxLanIdKSyncEntry::ToString() const {
59 12 : std::stringstream s;
60 12 : NHKSyncEntry *nexthop = nh();
61 :
62 12 : if (nexthop) {
63 12 : s << "VXLAN Label: " << label_ << " Index : "
64 12 : << GetIndex() << nexthop->ToString();
65 : } else {
66 0 : s << "VXLAN Label: " << label_ << " Index : "
67 0 : << GetIndex() << " NextHop : <null>";
68 : }
69 24 : return s.str();
70 12 : }
71 :
72 3 : bool VxLanIdKSyncEntry::Sync(DBEntry *e) {
73 3 : bool ret = false;
74 3 : const VxLanId *vxlan_id = static_cast<VxLanId *>(e);
75 :
76 3 : NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
77 3 : if (vxlan_id->nexthop() == NULL) {
78 0 : LOG(DEBUG, "nexthop in network-id label is null");
79 0 : assert(0);
80 : }
81 3 : NHKSyncEntry nexthop(nh_object, vxlan_id->nexthop());
82 3 : NHKSyncEntry *old_nh = nh();
83 :
84 3 : nh_ = nh_object->GetReference(&nexthop);
85 3 : if (old_nh != nh()) {
86 3 : ret = true;
87 : }
88 :
89 3 : return ret;
90 3 : };
91 :
92 6 : int VxLanIdKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
93 6 : vr_vxlan_req encoder;
94 : int encode_len;
95 6 : NHKSyncEntry *nexthop = nh();
96 :
97 6 : encoder.set_h_op(op);
98 6 : encoder.set_vxlanr_rid(0);
99 6 : encoder.set_vxlanr_vnid(label_);
100 6 : encoder.set_vxlanr_nhid(nexthop->nh_id());
101 6 : int error = 0;
102 6 : encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
103 6 : assert(error == 0);
104 6 : assert(encode_len <= buf_len);
105 6 : return encode_len;
106 6 : }
107 :
108 6 : void VxLanIdKSyncEntry::FillObjectLog(sandesh_op::type op,
109 : KSyncVxLanInfo &info) const {
110 6 : info.set_label(label_);
111 6 : info.set_nh(nh()->nh_id());
112 :
113 6 : if (op == sandesh_op::ADD) {
114 3 : info.set_operation("ADD/CHANGE");
115 : } else {
116 3 : info.set_operation("DELETE");
117 : }
118 6 : }
119 :
120 3 : int VxLanIdKSyncEntry::AddMsg(char *buf, int buf_len) {
121 3 : KSyncVxLanInfo info;
122 3 : FillObjectLog(sandesh_op::ADD, info);
123 3 : KSYNC_TRACE(VxLan, GetObject(), info);
124 :
125 6 : return Encode(sandesh_op::ADD, buf, buf_len);
126 3 : }
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 3 : int VxLanIdKSyncEntry::DeleteMsg(char *buf, int buf_len) {
137 3 : KSyncVxLanInfo info;
138 3 : FillObjectLog(sandesh_op::DEL, info);
139 3 : KSYNC_TRACE(VxLan, GetObject(), info);
140 :
141 6 : return Encode(sandesh_op::DEL, buf, buf_len);
142 3 : }
143 :
144 3 : KSyncEntry *VxLanIdKSyncEntry::UnresolvedReference() {
145 3 : NHKSyncEntry *nexthop = nh();
146 3 : if (!nexthop->IsResolved()) {
147 0 : return nexthop;
148 : }
149 3 : 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 3 : KSyncEntry *VxLanKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
164 3 : const VxLanIdKSyncEntry *vxlan =
165 : static_cast<const VxLanIdKSyncEntry *>(entry);
166 3 : VxLanIdKSyncEntry *ksync = new VxLanIdKSyncEntry(this, vxlan, index);
167 3 : return static_cast<KSyncEntry *>(ksync);
168 : }
169 :
170 3 : KSyncEntry *VxLanKSyncObject::DBToKSyncEntry(const DBEntry *e) {
171 3 : const VxLanId *vxlan = static_cast<const VxLanId *>(e);
172 3 : VxLanIdKSyncEntry *key = new VxLanIdKSyncEntry(this, vxlan);
173 3 : return static_cast<KSyncEntry *>(key);
174 : }
175 :
176 6 : void vr_vxlan_req::Process(SandeshContext *context) {
177 6 : AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
178 6 : ioc->VxLanMsgHandler(this);
179 6 : }
180 :
|