OpenSDN source code
mpls_ksync.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <base/logging.h>
7 #include <oper/nexthop.h>
8 #include <oper/mirror_table.h>
9 #include <ksync/ksync_index.h>
14 #include <ksync/ksync_sock.h>
15 
17  uint32_t index) :
18  KSyncNetlinkDBEntry(index), ksync_obj_(obj), label_(me->label_),
19  nh_(NULL) {
20 }
21 
23  KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
24  label_(mpls->label()), nh_(NULL) {
25 }
26 
28 }
29 
31  return ksync_obj_;
32 }
33 
34 bool MplsKSyncEntry::IsLess(const KSyncEntry &rhs) const {
35  const MplsKSyncEntry &entry = static_cast<const MplsKSyncEntry &>(rhs);
36  return label_ < entry.label_;
37 }
38 
39 std::string MplsKSyncEntry::ToString() const {
40  std::stringstream s;
41  NHKSyncEntry *next_hop = nh();
42 
43  s << "Mpls : " << label_ << " Index : " << GetIndex();
44  if (next_hop) {
45  s << next_hop->ToString();
46  } else {
47  s << "NextHop : NULL";
48  }
49  return s.str();
50 }
51 
53  bool ret = false;
54  const MplsLabel *mpls = static_cast<MplsLabel *>(e);
55 
56  NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
57  if (mpls->nexthop() == NULL) {
58  LOG(DEBUG, "nexthop in mpls label is null");
59  assert(0);
60  }
61  NHKSyncEntry next_hop(nh_object, mpls->nexthop());
62  NHKSyncEntry *old_nh = nh();
63 
64  nh_ = nh_object->GetReference(&next_hop);
65  if (old_nh != nh()) {
66  ret = true;
67  }
68 
69  return ret;
70 };
71 
72 int MplsKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
73  vr_mpls_req encoder;
74  int encode_len;
75  NHKSyncEntry *next_hop = nh();
76 
77  encoder.set_h_op(op);
78  encoder.set_mr_label(label_);
79  encoder.set_mr_rid(0);
80  encoder.set_mr_nhid(next_hop->nh_id());
81  int error = 0;
82  encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
83  assert(error == 0);
84  assert(encode_len <= buf_len);
85  return encode_len;
86 }
87 
89  KSyncMplsInfo &info) const {
90  info.set_label(label_);
91  info.set_nh(nh()->nh_id());
92 
93  if (op == sandesh_op::ADD) {
94  info.set_operation("ADD/CHANGE");
95  } else {
96  info.set_operation("DELETE");
97  }
98 }
99 
100 int MplsKSyncEntry::AddMsg(char *buf, int buf_len) {
101  KSyncMplsInfo info;
102  FillObjectLog(sandesh_op::ADD, info);
103  KSYNC_TRACE(Mpls, GetObject(), info);
104 
105  return Encode(sandesh_op::ADD, buf, buf_len);
106 }
107 
108 int MplsKSyncEntry::ChangeMsg(char *buf, int buf_len){
109  KSyncMplsInfo info;
110  FillObjectLog(sandesh_op::ADD, info);
111  KSYNC_TRACE(Mpls, GetObject(), info);
112 
113  return Encode(sandesh_op::ADD, buf, buf_len);
114 }
115 
116 int MplsKSyncEntry::DeleteMsg(char *buf, int buf_len) {
117  KSyncMplsInfo info;
118  FillObjectLog(sandesh_op::DEL, info);
119  KSYNC_TRACE(Mpls, GetObject(), info);
120 
121  return Encode(sandesh_op::DEL, buf, buf_len);
122 }
123 
125  NHKSyncEntry *next_hop = nh();
126  if (!next_hop->IsResolvedAndInSync()) {
127  return next_hop;
128  }
129  return NULL;
130 }
131 
133  KSyncDBObject("KSync Mpls"), ksync_(ksync) {
134 }
135 
137 }
138 
141 }
142 
143 KSyncEntry *MplsKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
144  const MplsKSyncEntry *mpls = static_cast<const MplsKSyncEntry *>(entry);
145  MplsKSyncEntry *ksync = new MplsKSyncEntry(this, mpls, index);
146  return static_cast<KSyncEntry *>(ksync);
147 };
148 
150  const MplsLabel *mpls = static_cast<const MplsLabel *>(e);
151  MplsKSyncEntry *key = new MplsKSyncEntry(this, mpls);
152  return static_cast<KSyncEntry *>(key);
153 }
154 
155 void vr_mpls_req::Process(SandeshContext *context) {
156  AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
157  ioc->MplsMsgHandler(this);
158 }
159 
virtual void MplsMsgHandler(vr_mpls_req *req)=0
MplsTable * mpls_table() const
Definition: agent.h:513
void RegisterDb(DBTableBase *table)
size_t GetIndex() const
Definition: ksync_entry.h:175
bool IsResolvedAndInSync()
True only when the entry is resolved AND kernel-acknowledged (IN_SYNC).
Definition: ksync_entry.h:147
KSyncEntry * GetReference(const KSyncEntry *key)
Agent * agent() const
Definition: ksync_init.h:40
NHKSyncObject * nh_ksync_obj() const
Definition: ksync_init.h:44
MplsKSyncObject * ksync_obj_
Definition: mpls_ksync.h:42
MplsKSyncEntry(MplsKSyncObject *obj, const MplsKSyncEntry *entry, uint32_t index)
Definition: mpls_ksync.cc:16
virtual ~MplsKSyncEntry()
Definition: mpls_ksync.cc:27
virtual KSyncEntry * UnresolvedReference()
Definition: mpls_ksync.cc:124
virtual int AddMsg(char *buf, int buf_len)
Definition: mpls_ksync.cc:100
virtual int DeleteMsg(char *buf, int buf_len)
Definition: mpls_ksync.cc:116
virtual std::string ToString() const
Definition: mpls_ksync.cc:39
virtual bool Sync(DBEntry *e)
Definition: mpls_ksync.cc:52
virtual bool IsLess(const KSyncEntry &rhs) const
Definition: mpls_ksync.cc:34
uint32_t label_
Definition: mpls_ksync.h:43
virtual int ChangeMsg(char *buf, int buf_len)
Definition: mpls_ksync.cc:108
KSyncEntryPtr nh_
Definition: mpls_ksync.h:44
void FillObjectLog(sandesh_op::type op, KSyncMplsInfo &info) const
Definition: mpls_ksync.cc:88
NHKSyncEntry * nh() const
Definition: mpls_ksync.h:27
KSyncDBObject * GetObject() const
Definition: mpls_ksync.cc:30
int Encode(sandesh_op::type op, char *buf, int buf_len)
Definition: mpls_ksync.cc:72
KSync * ksync_
Definition: mpls_ksync.h:58
virtual KSyncEntry * Alloc(const KSyncEntry *entry, uint32_t index)
Definition: mpls_ksync.cc:143
MplsKSyncObject(KSync *ksync)
Definition: mpls_ksync.cc:132
virtual ~MplsKSyncObject()
Definition: mpls_ksync.cc:136
virtual KSyncEntry * DBToKSyncEntry(const DBEntry *e)
Definition: mpls_ksync.cc:149
KSync * ksync() const
Definition: mpls_ksync.h:53
void RegisterDBClients()
Definition: mpls_ksync.cc:139
Definition: mpls.h:53
const NextHop * nexthop() const
Definition: mpls.h:81
virtual std::string ToString() const
uint32_t nh_id() const
Definition: nexthop_ksync.h:61
#define KSYNC_TRACE(obj, parent,...)
Definition: ksync_object.h:305
uint8_t type
Definition: load_balance.h:2
#define LOG(_Level, _Msg)
Definition: logging.h:35