OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vxlan_ksync.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <boost/asio.hpp>
6 #include <boost/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 
26 
27 #include "ksync_init.h"
28 
30  const VxLanIdKSyncEntry *entry,
31  uint32_t index) :
32  KSyncNetlinkDBEntry(index), ksync_obj_(obj), label_(entry->label_),
33  nh_(NULL) {
34 }
35 
37  const VxLanId *vxlan_id) :
38  KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
39  label_(vxlan_id->vxlan_id()), nh_(NULL) {
40 }
41 
43 }
44 
46  return ksync_obj_;
47 }
48 
49 bool VxLanIdKSyncEntry::IsLess(const KSyncEntry &rhs) const {
50  const VxLanIdKSyncEntry &entry =
51  static_cast<const VxLanIdKSyncEntry &>(rhs);
52 
53  return label_ < entry.label_;
54 }
55 
56 std::string VxLanIdKSyncEntry::ToString() const {
57  std::stringstream s;
58  NHKSyncEntry *nexthop = nh();
59 
60  if (nexthop) {
61  s << "VXLAN Label: " << label_ << " Index : "
62  << GetIndex() << nexthop->ToString();
63  } else {
64  s << "VXLAN Label: " << label_ << " Index : "
65  << GetIndex() << " NextHop : <null>";
66  }
67  return s.str();
68 }
69 
71  bool ret = false;
72  const VxLanId *vxlan_id = static_cast<VxLanId *>(e);
73 
74  NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
75  if (vxlan_id->nexthop() == NULL) {
76  LOG(DEBUG, "nexthop in network-id label is null");
77  assert(0);
78  }
79  NHKSyncEntry nexthop(nh_object, vxlan_id->nexthop());
80  NHKSyncEntry *old_nh = nh();
81 
82  nh_ = nh_object->GetReference(&nexthop);
83  if (old_nh != nh()) {
84  ret = true;
85  }
86 
87  return ret;
88 };
89 
90 int VxLanIdKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
91  vr_vxlan_req encoder;
92  int encode_len;
93  NHKSyncEntry *nexthop = nh();
94 
95  encoder.set_h_op(op);
96  encoder.set_vxlanr_rid(0);
97  encoder.set_vxlanr_vnid(label_);
98  encoder.set_vxlanr_nhid(nexthop->nh_id());
99  int error = 0;
100  encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
101  assert(error == 0);
102  assert(encode_len <= buf_len);
103  return encode_len;
104 }
105 
107  KSyncVxLanInfo &info) const {
108  info.set_label(label_);
109  info.set_nh(nh()->nh_id());
110 
111  if (op == sandesh_op::ADD) {
112  info.set_operation("ADD/CHANGE");
113  } else {
114  info.set_operation("DELETE");
115  }
116 }
117 
118 int VxLanIdKSyncEntry::AddMsg(char *buf, int buf_len) {
119  KSyncVxLanInfo info;
120  FillObjectLog(sandesh_op::ADD, info);
121  KSYNC_TRACE(VxLan, GetObject(), info);
122 
123  return Encode(sandesh_op::ADD, buf, buf_len);
124 }
125 
126 int VxLanIdKSyncEntry::ChangeMsg(char *buf, int buf_len) {
127  KSyncVxLanInfo info;
128  FillObjectLog(sandesh_op::ADD, info);
129  KSYNC_TRACE(VxLan, GetObject(), info);
130 
131  return Encode(sandesh_op::ADD, buf, buf_len);
132 }
133 
134 int VxLanIdKSyncEntry::DeleteMsg(char *buf, int buf_len) {
135  KSyncVxLanInfo info;
136  FillObjectLog(sandesh_op::DEL, info);
137  KSYNC_TRACE(VxLan, GetObject(), info);
138 
139  return Encode(sandesh_op::DEL, buf, buf_len);
140 }
141 
143  NHKSyncEntry *nexthop = nh();
144  if (!nexthop->IsResolved()) {
145  return nexthop;
146  }
147  return NULL;
148 }
149 
151  : KSyncDBObject("KSync VxLan"), ksync_(ksync) {
152 }
153 
155 }
156 
159 }
160 
161 KSyncEntry *VxLanKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
162  const VxLanIdKSyncEntry *vxlan =
163  static_cast<const VxLanIdKSyncEntry *>(entry);
164  VxLanIdKSyncEntry *ksync = new VxLanIdKSyncEntry(this, vxlan, index);
165  return static_cast<KSyncEntry *>(ksync);
166 }
167 
169  const VxLanId *vxlan = static_cast<const VxLanId *>(e);
170  VxLanIdKSyncEntry *key = new VxLanIdKSyncEntry(this, vxlan);
171  return static_cast<KSyncEntry *>(key);
172 }
173 
174 void vr_vxlan_req::Process(SandeshContext *context) {
175  AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
176  ioc->VxLanMsgHandler(this);
177 }
178 
virtual int DeleteMsg(char *buf, int buf_len)
Definition: vxlan_ksync.cc:134
#define KSYNC_TRACE(obj, parent,...)
Definition: ksync_object.h:304
VxLanIdKSyncEntry(VxLanKSyncObject *obj, const VxLanIdKSyncEntry *entry, uint32_t index)
Definition: vxlan_ksync.cc:29
void RegisterDb(DBTableBase *table)
virtual int ChangeMsg(char *buf, int buf_len)
Definition: vxlan_ksync.cc:126
bool IsResolved()
Agent * agent() const
Definition: ksync_init.h:39
virtual KSyncEntry * UnresolvedReference()
Definition: vxlan_ksync.cc:142
virtual bool IsLess(const KSyncEntry &rhs) const
Definition: vxlan_ksync.cc:49
uint32_t nh_id() const
Definition: nexthop_ksync.h:58
virtual ~VxLanKSyncObject()
Definition: vxlan_ksync.cc:154
void FillObjectLog(sandesh_op::type op, KSyncVxLanInfo &info) const
Definition: vxlan_ksync.cc:106
Definition: vxlan.h:14
virtual void VxLanMsgHandler(vr_vxlan_req *req)=0
int Encode(sandesh_op::type op, char *buf, int buf_len)
Definition: vxlan_ksync.cc:90
virtual std::string ToString() const
KSyncEntryPtr nh_
Definition: vxlan_ksync.h:47
uint8_t type
Definition: load_balance.h:109
virtual bool Sync(DBEntry *e)
Definition: vxlan_ksync.cc:70
NHKSyncObject * nh_ksync_obj() const
Definition: ksync_init.h:43
VxLanKSyncObject * ksync_obj_
Definition: vxlan_ksync.h:45
virtual std::string ToString() const
Definition: vxlan_ksync.cc:56
KSyncEntry * GetReference(const KSyncEntry *key)
KSyncDBObject * GetObject() const
Definition: vxlan_ksync.cc:45
const NextHop * nexthop() const
Definition: vxlan.h:28
KSync * ksync() const
Definition: vxlan_ksync.h:56
virtual ~VxLanIdKSyncEntry()
Definition: vxlan_ksync.cc:42
VxLanTable * vxlan_table() const
Definition: agent.h:535
NHKSyncEntry * nh() const
Definition: vxlan_ksync.h:30
virtual KSyncEntry * DBToKSyncEntry(const DBEntry *e)
Definition: vxlan_ksync.cc:168
VxLanKSyncObject(KSync *ksync)
Definition: vxlan_ksync.cc:150
#define LOG(_Level, _Msg)
Definition: logging.h:33
virtual KSyncEntry * Alloc(const KSyncEntry *entry, uint32_t index)
Definition: vxlan_ksync.cc:161
size_t GetIndex() const
Definition: ksync_entry.h:156
virtual int AddMsg(char *buf, int buf_len)
Definition: vxlan_ksync.cc:118
void RegisterDBClients()
Definition: vxlan_ksync.cc:157