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