blob: c7630724eca74a626dda45a25667059d59100b69 [file] [log] [blame]
Shad Ansarib7b0ced2018-05-11 21:53:32 +00001/*
Nicolas Palpacuerb78def42018-06-07 12:55:26 -04002 Copyright (C) 2018 Open Networking Foundation
Shad Ansarib7b0ced2018-05-11 21:53:32 +00003
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <iostream>
19#include <memory>
20#include <string>
21
22#include "Queue.h"
23#include <iostream>
24#include <sstream>
25
26#include "core.h"
27#include "indications.h"
28
29extern "C"
30{
31#include <bcmos_system.h>
32#include <bal_api.h>
33#include <bal_api_end.h>
34}
35
36Status Enable_() {
37 static bool enabled = false;
38 bcmbal_access_terminal_cfg acc_term_obj;
39 bcmbal_access_terminal_key key = { };
40
41 if (!enabled) {
42 std::cout << "Enable OLT" << std::endl;
43 key.access_term_id = DEFAULT_ATERM_ID;
44 BCMBAL_CFG_INIT(&acc_term_obj, access_terminal, key);
45 BCMBAL_CFG_PROP_SET(&acc_term_obj, access_terminal, admin_state, BCMBAL_STATE_UP);
46 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(acc_term_obj.hdr))) {
47 std::cout << "ERROR: Failed to enable OLT" << std::endl;
48 return Status(grpc::StatusCode::INTERNAL, "Failed to enable OLT");
49 }
50 enabled = true;
51 }
52 return Status::OK;
53}
54
55Status EnablePonIf_(uint32_t intf_id) {
56 bcmbal_interface_cfg interface_obj;
57 bcmbal_interface_key interface_key;
58
59 interface_key.intf_id = intf_id;
60 interface_key.intf_type = BCMBAL_INTF_TYPE_PON;
61
62 BCMBAL_CFG_INIT(&interface_obj, interface, interface_key);
63 BCMBAL_CFG_PROP_SET(&interface_obj, interface, admin_state, BCMBAL_STATE_UP);
64
65 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(interface_obj.hdr))) {
66 std::cout << "ERROR: Failed to enable PON interface: " << intf_id << std::endl;
67 return Status(grpc::StatusCode::INTERNAL, "Failed to enable PON interface");
68 }
69
70 return Status::OK;
71}
72
73Status ActivateOnu_(uint32_t intf_id, uint32_t onu_id,
74 const char *vendor_id, const char *vendor_specific) {
75
76 bcmbal_subscriber_terminal_cfg sub_term_obj = {};
77 bcmbal_subscriber_terminal_key subs_terminal_key;
78 bcmbal_serial_number serial_num = {};
79 bcmbal_registration_id registration_id = {};
80
81 std::cout << "Enabling ONU " << onu_id << " on PON " << intf_id << std::endl;
82 std::cout << "Vendor Id " << vendor_id
83 << "Vendor Specific Id " << vendor_specific
84 << std::endl;
85
86 subs_terminal_key.sub_term_id = onu_id;
87 subs_terminal_key.intf_id = intf_id;
88 BCMBAL_CFG_INIT(&sub_term_obj, subscriber_terminal, subs_terminal_key);
89
90 memcpy(serial_num.vendor_id, vendor_id, 4);
91 memcpy(serial_num.vendor_specific, vendor_specific, 4);
92 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, serial_number, serial_num);
93
94 // FIXME - Use a default (all zeros) registration id.
95 memset(registration_id.arr, 0, sizeof(registration_id.arr));
96 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, registration_id, registration_id);
97
98 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, admin_state, BCMBAL_STATE_UP);
99
100 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(sub_term_obj.hdr))) {
101 std::cout << "ERROR: Failed to enable ONU: " << std::endl;
102 return Status(grpc::StatusCode::INTERNAL, "Failed to enable ONU");
103 }
104
105 return SchedAdd_(intf_id, onu_id, mk_agg_port_id(onu_id));
106
107 //return Status::OK;
108}
109
110#define MAX_CHAR_LENGTH 20
111#define MAX_OMCI_MSG_LENGTH 44
112Status OmciMsgOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
113 bcmbal_u8_list_u32_max_2048 buf; /* A structure with a msg pointer and length value */
114 bcmos_errno err = BCM_ERR_OK;
115
116 /* The destination of the OMCI packet is a registered ONU on the OLT PON interface */
117 bcmbal_dest proxy_pkt_dest;
118
119 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_ITU_OMCI_CHANNEL;
120 proxy_pkt_dest.u.itu_omci_channel.sub_term_id = onu_id;
121 proxy_pkt_dest.u.itu_omci_channel.intf_id = intf_id;
122
123 // ???
124 if ((pkt.size()/2) > MAX_OMCI_MSG_LENGTH) {
125 buf.len = MAX_OMCI_MSG_LENGTH;
126 } else {
127 buf.len = pkt.size()/2;
128 }
129
130 /* Send the OMCI packet using the BAL remote proxy API */
131 uint16_t idx1 = 0;
132 uint16_t idx2 = 0;
133 uint8_t arraySend[buf.len];
134 char str1[MAX_CHAR_LENGTH];
135 char str2[MAX_CHAR_LENGTH];
136 memset(&arraySend, 0, buf.len);
137
138 std::cout << "Sending omci msg to ONU of length is "
139 << buf.len
140 << std::endl;
141
142 for (idx1=0,idx2=0; idx1<((buf.len)*2); idx1++,idx2++) {
143 sprintf(str1,"%c", pkt[idx1]);
144 sprintf(str2,"%c", pkt[++idx1]);
145 strcat(str1,str2);
146 arraySend[idx2] = strtol(str1, NULL, 16);
147 }
148
149 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
150 memcpy(buf.val, (uint8_t *)arraySend, buf.len);
151
152 std::cout << "After converting bytes to hex "
153 << buf.val << buf.len << std::endl;
154
155 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
156
157 std::cout << "OMCI request msg of length " << buf.len
158 << " sent to ONU" << onu_id
159 << " through PON " << intf_id << std::endl;
160
161 free(buf.val);
162
163 return Status::OK;
164}
165
166Status OnuPacketOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
167 bcmos_errno err = BCM_ERR_OK;
168 bcmbal_dest proxy_pkt_dest;
169 bcmbal_u8_list_u32_max_2048 buf;
170
171 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_SUB_TERM,
172 proxy_pkt_dest.u.sub_term.sub_term_id = onu_id;
173 proxy_pkt_dest.u.sub_term.intf_id = intf_id;
174
175 buf.len = pkt.size();
176 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
177 memcpy(buf.val, (uint8_t *)pkt.data(), buf.len);
178
179 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
180
181 std::cout << "Packet out of length " << buf.len
182 << " sent to ONU" << onu_id
183 << " through PON " << intf_id << std::endl;
184
185 free(buf.val);
186
187 return Status::OK;
188}
189
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400190Status UplinkPacketOut_(uint32_t intf_id, const std::string pkt) {
191 bcmos_errno err = BCM_ERR_OK;
192 bcmbal_dest proxy_pkt_dest;
193 bcmbal_u8_list_u32_max_2048 buf;
194
195 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_NNI,
196 proxy_pkt_dest.u.nni.intf_id = intf_id;
197
198 buf.len = pkt.size();
199 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
200 memcpy(buf.val, (uint8_t *)pkt.data(), buf.len);
201
202 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
203
204 std::cout << "Packet out of length " << buf.len
205 << " sent through uplink port " << intf_id << std::endl;
206
207 free(buf.val);
208
209 return Status::OK;
210}
211
Shad Ansarib7b0ced2018-05-11 21:53:32 +0000212Status FlowAdd_(uint32_t onu_id,
213 uint32_t flow_id, const std::string flow_type,
214 uint32_t access_intf_id, uint32_t network_intf_id,
215 uint32_t gemport_id,
216 const ::openolt::Classifier& classifier,
217 const ::openolt::Action& action) {
218 bcmos_errno err;
219 bcmbal_flow_cfg cfg;
220 bcmbal_flow_key key = { };
221
222 std::cout << "flow add -"
223 << " intf_id:" << access_intf_id
224 << " onu_id:" << onu_id
225 << " flow_id:" << flow_id
226 << " flow_type:" << flow_type
227 << " gemport_id:" << gemport_id
228 << " network_intf_id:" << network_intf_id
229 << std::endl;
230
231 key.flow_id = flow_id;
232 if (flow_type.compare("upstream") == 0 ) {
233 key.flow_type = BCMBAL_FLOW_TYPE_UPSTREAM;
234 } else if (flow_type.compare("downstream") == 0) {
235 key.flow_type = BCMBAL_FLOW_TYPE_DOWNSTREAM;
236 } else {
237 std::cout << "Invalid flow type " << flow_type << std::endl;
238 return Status::CANCELLED;
239 }
240
241 BCMBAL_CFG_INIT(&cfg, flow, key);
242
243 BCMBAL_CFG_PROP_SET(&cfg, flow, admin_state, BCMBAL_STATE_UP);
244 BCMBAL_CFG_PROP_SET(&cfg, flow, access_int_id, access_intf_id);
245 BCMBAL_CFG_PROP_SET(&cfg, flow, network_int_id, network_intf_id);
246 BCMBAL_CFG_PROP_SET(&cfg, flow, sub_term_id, onu_id);
247 BCMBAL_CFG_PROP_SET(&cfg, flow, svc_port_id, gemport_id);
248
249 {
250 bcmbal_classifier val = { };
251
252 if (classifier.o_tpid()) {
253 val.o_tpid = classifier.o_tpid();
254 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_TPID;
255 }
256
257 if (classifier.o_vid()) {
258 val.o_vid = classifier.o_vid();
259 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_VID;
260 }
261
262 if (classifier.i_tpid()) {
263 val.i_tpid = classifier.i_tpid();
264 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_TPID;
265 }
266
267 if (classifier.i_vid()) {
268 val.i_vid = classifier.i_vid();
269 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_VID;
270 }
271
272 if (classifier.o_pbits()) {
273 val.o_pbits = classifier.o_pbits();
274 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_PBITS;
275 }
276
277 if (classifier.i_pbits()) {
278 val.i_pbits = classifier.i_pbits();
279 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_PBITS;
280 }
281
282 if (classifier.eth_type()) {
283 val.ether_type = classifier.eth_type();
284 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_ETHER_TYPE;
285 }
286
287 /*
288 if (classifier.dst_mac()) {
289 val.dst_mac = classifier.dst_mac();
290 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_MAC;
291 }
292
293 if (classifier.src_mac()) {
294 val.src_mac = classifier.src_mac();
295 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_MAC;
296 }
297 */
298
299 if (classifier.ip_proto()) {
300 val.ip_proto = classifier.ip_proto();
301 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_IP_PROTO;
302 }
303
304 /*
305 if (classifier.dst_ip()) {
306 val.dst_ip = classifier.dst_ip();
307 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_IP;
308 }
309
310 if (classifier.src_ip()) {
311 val.src_ip = classifier.src_ip();
312 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_IP;
313 }
314 */
315
316 if (classifier.src_port()) {
317 val.src_port = classifier.src_port();
318 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_PORT;
319 }
320
321 if (classifier.dst_port()) {
322 val.dst_port = classifier.dst_port();
323 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_PORT;
324 }
325
326 if (!classifier.pkt_tag_type().empty()) {
327 if (classifier.pkt_tag_type().compare("untagged") == 0) {
328 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_UNTAGGED;
329 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
330 } else if (classifier.pkt_tag_type().compare("single_tag") == 0) {
331 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_SINGLE_TAG;
332 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
333 } else if (classifier.pkt_tag_type().compare("double_tag") == 0) {
334 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_DOUBLE_TAG;
335 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
336 }
337 }
338
339 BCMBAL_CFG_PROP_SET(&cfg, flow, classifier, val);
340 }
341
342 {
343 bcmbal_action val = { };
344
345 const ::openolt::ActionCmd& cmd = action.cmd();
346
347 if (cmd.add_outer_tag()) {
348 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_ADD_OUTER_TAG;
349 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
350 }
351
352 if (cmd.remove_outer_tag()) {
353 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_REMOVE_OUTER_TAG;
354 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
355 }
356
357 if (cmd.trap_to_host()) {
358 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_TRAP_TO_HOST;
359 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
360 }
361
362 if (action.o_vid()) {
363 val.o_vid = action.o_vid();
364 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_VID;
365 }
366
367 if (action.o_pbits()) {
368 val.o_pbits = action.o_pbits();
369 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_PBITS;
370 }
371
372 if (action.o_tpid()) {
373 val.o_tpid = action.o_tpid();
374 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_TPID;
375 }
376
377 if (action.i_vid()) {
378 val.i_vid = action.i_vid();
379 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_VID;
380 }
381
382 if (action.i_pbits()) {
383 val.i_pbits = action.i_pbits();
384 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_PBITS;
385 }
386
387 if (action.i_tpid()) {
388 val.i_tpid = action.i_tpid();
389 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_TPID;
390 }
391
392 BCMBAL_CFG_PROP_SET(&cfg, flow, action, val);
393 }
394
395 {
396 bcmbal_tm_sched_id val;
397 val = (bcmbal_tm_sched_id) mk_sched_id(onu_id);
398 BCMBAL_CFG_PROP_SET(&cfg, flow, dba_tm_sched_id, val);
399 }
400
401 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) {
402 std::cout << "ERROR: flow add failed" << std::endl;
403 return Status(grpc::StatusCode::INTERNAL, "flow add failed");
404 }
405
406 return Status::OK;
407}
408
409Status SchedAdd_(int intf_id, int onu_id, int agg_port_id) {
410 bcmbal_tm_sched_cfg cfg;
411 bcmbal_tm_sched_key key = { };
412 bcmbal_tm_sched_type sched_type;
413
414 key.id = mk_sched_id(onu_id);
415 key.dir = BCMBAL_TM_SCHED_DIR_US;
416
417 BCMBAL_CFG_INIT(&cfg, tm_sched, key);
418
419 {
420 bcmbal_tm_sched_owner val = { };
421
422 val.type = BCMBAL_TM_SCHED_OWNER_TYPE_AGG_PORT;
423 val.u.agg_port.intf_id = (bcmbal_intf_id) intf_id;
424 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_INTF_ID;
425 val.u.agg_port.sub_term_id = (bcmbal_sub_id) onu_id;
426 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_SUB_TERM_ID;
427
428 val.u.agg_port.agg_port_id = (bcmbal_aggregation_port_id) agg_port_id;
429 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_AGG_PORT_ID;
430
431 BCMBAL_CFG_PROP_SET(&cfg, tm_sched, owner, val);
432 }
433
434 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) {
435 std::cout << "ERROR: Failed to create upstream DBA sched"
436 << " id:" << key.id
437 << " intf_id:" << intf_id
438 << " onu_id:" << onu_id << std::endl;
439 return Status(grpc::StatusCode::INTERNAL, "Failed to create upstream DBA sched");
440 //return 1;
441 }
442 std::cout << "create upstream DBA sched"
443 << " id:" << key.id
444 << " intf_id:" << intf_id
445 << " onu_id:" << onu_id << std::endl;
446
447 return Status::OK;
448 //return 0;
449}