OpenSDN source code
mac_learning_proto.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <init/agent_param.h>
7 #include "mac_learning_proto.h"
9 #include "mac_learning.h"
10 #include "mac_ip_learning.h"
12 #include "mac_aging.h"
13 
14 MacLearningProto::MacLearningProto(Agent *agent, boost::asio::io_context &io):
15  Proto(agent, kTaskMacLearning, PktHandler::MAC_LEARNING, io),
16  add_tokens_("Add Tokens", this, agent->params()->mac_learning_add_tokens()),
17  change_tokens_("Change tokens", this,
18  agent->params()->mac_learning_update_tokens()),
19  delete_tokens_("Delete tokens", this,
20  agent->params()->mac_learning_delete_tokens()) {
21  Init();
22 }
23 
25 MacLearningProto::AllocProtoHandler(boost::shared_ptr<PktInfo> info,
26  boost::asio::io_context &io) {
27  if (info->agent_hdr.cmd == AgentHdr::TRAP_MAC_IP_LEARNING) {
28  return new MacIpLearningProtoHandler(agent(), info, io);
29  } else {
30  return new MacLearningProtoHandler(agent(), info, io);
31  }
32 }
33 
34 uint32_t MacLearningProto::Hash(uint32_t vrf_id, const MacAddress &mac) {
35  size_t val = 0;
36  uint8_t mac_array[ETH_ALEN];
37 
38  boost::hash_combine(val, vrf_id);
39  mac.ToArray(mac_array, sizeof(mac_array));
40 
41  for (uint32_t i = 0; i < ETH_ALEN; i++) {
42  boost::hash_combine(val, mac_array[i]);
43  }
44 
45  return val % mac_learning_partition_list_.size();
46 }
47 
48 bool
50  //XXXX disable trace ??
51  //FreeBuffer(msg.get());
54 
55  uint32_t table_index = Hash(msg->agent_hdr.vrf, msg->smac);
56  MacLearningPartition* partition = Find(table_index);
57  assert(partition);
58  partition->Enqueue(ptr);
59  return true;
60 }
61 
62 
64  switch(event) {
66  return add_tokens_.GetToken();
67 
69  return change_tokens_.GetToken();
70 
72  return delete_tokens_.GetToken();
73 
74  default:
75  assert(0);
76  }
77 
78  return add_tokens_.GetToken();
79 }
80 
82 MacLearningProto::Find(uint32_t idx) {
83  if (idx >= mac_learning_partition_list_.size()) {
84  assert(0);
85  return NULL;
86  }
87 
88  return mac_learning_partition_list_[idx].get();
89 }
90 
92  pool->IncrementRestarts();
93  for (uint32_t i = 0;
94  i < agent()->params()->mac_learning_thread_count(); i++) {
95  mac_learning_partition_list_[i]->MayBeStartRunner(pool);
96  }
97 }
98 
99 void
101  for (uint32_t i = 0;
102  i < agent()->params()->mac_learning_thread_count(); i++) {
104  mac_learning_partition_list_.push_back(ptr);
105  }
106  mac_ip_learning_tbl_.reset(new MacIpLearningTable(agent(), this));
107 }
109  return mac_ip_learning_tbl_.get();
110 }
#define kTaskMacLearning
Definition: agent.h:344
uint32_t mac_learning_thread_count() const
Definition: agent_param.h:533
Definition: agent.h:361
AgentParam * params() const
Definition: agent.h:1227
bool ToArray(u_int8_t *p, size_t s) const
Definition: mac_address.cc:94
void Enqueue(MacLearningEntryRequestPtr req)
MacLearningPartition * Find(uint32_t index)
uint32_t Hash(uint32_t vrf_id, const MacAddress &mac)
boost::shared_ptr< MacLearningPartition > MacLearningPartitionPtr
virtual ProtoHandler * AllocProtoHandler(boost::shared_ptr< PktInfo > info, boost::asio::io_context &io)
virtual void TokenAvailable(TokenPool *pool)
MacIpLearningTable * GetMacIpLearningTable()
MacIpLearningTablePtr mac_ip_learning_tbl_
TokenPtr GetToken(MacLearningEntryRequest::Event event)
bool Enqueue(PktInfoPtr msg)
MacLearningPartitionList mac_learning_partition_list_
MacLearningProto(Agent *agent, boost::asio::io_context &io)
void IncrementRestarts()
Definition: flow_token.h:34
virtual TokenPtr GetToken()
Definition: flow_token.cc:41
boost::shared_ptr< Token > TokenPtr
Definition: flow_token.h:11
boost::shared_ptr< MacLearningEntryRequest > MacLearningEntryRequestPtr
Definition: io_utils.cc:12
boost::shared_ptr< PktInfo > PktInfoPtr
Definition: pkt_handler.h:61
@ TRAP_MAC_IP_LEARNING
Definition: pkt_handler.h:150