Shad Ansari | b7b0ced | 2018-05-11 21:53:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2018 Open Networking Foundation |
| 3 | |
| 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 | |
| 29 | extern "C" |
| 30 | { |
| 31 | #include <bcmos_system.h> |
| 32 | #include <bal_api.h> |
| 33 | #include <bal_api_end.h> |
| 34 | } |
| 35 | |
| 36 | Status 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 | |
| 55 | Status 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 | |
| 73 | Status 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 |
| 112 | Status 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 | |
| 166 | Status 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 | |
| 190 | Status FlowAdd_(uint32_t onu_id, |
| 191 | uint32_t flow_id, const std::string flow_type, |
| 192 | uint32_t access_intf_id, uint32_t network_intf_id, |
| 193 | uint32_t gemport_id, |
| 194 | const ::openolt::Classifier& classifier, |
| 195 | const ::openolt::Action& action) { |
| 196 | bcmos_errno err; |
| 197 | bcmbal_flow_cfg cfg; |
| 198 | bcmbal_flow_key key = { }; |
| 199 | |
| 200 | std::cout << "flow add -" |
| 201 | << " intf_id:" << access_intf_id |
| 202 | << " onu_id:" << onu_id |
| 203 | << " flow_id:" << flow_id |
| 204 | << " flow_type:" << flow_type |
| 205 | << " gemport_id:" << gemport_id |
| 206 | << " network_intf_id:" << network_intf_id |
| 207 | << std::endl; |
| 208 | |
| 209 | key.flow_id = flow_id; |
| 210 | if (flow_type.compare("upstream") == 0 ) { |
| 211 | key.flow_type = BCMBAL_FLOW_TYPE_UPSTREAM; |
| 212 | } else if (flow_type.compare("downstream") == 0) { |
| 213 | key.flow_type = BCMBAL_FLOW_TYPE_DOWNSTREAM; |
| 214 | } else { |
| 215 | std::cout << "Invalid flow type " << flow_type << std::endl; |
| 216 | return Status::CANCELLED; |
| 217 | } |
| 218 | |
| 219 | BCMBAL_CFG_INIT(&cfg, flow, key); |
| 220 | |
| 221 | BCMBAL_CFG_PROP_SET(&cfg, flow, admin_state, BCMBAL_STATE_UP); |
| 222 | BCMBAL_CFG_PROP_SET(&cfg, flow, access_int_id, access_intf_id); |
| 223 | BCMBAL_CFG_PROP_SET(&cfg, flow, network_int_id, network_intf_id); |
| 224 | BCMBAL_CFG_PROP_SET(&cfg, flow, sub_term_id, onu_id); |
| 225 | BCMBAL_CFG_PROP_SET(&cfg, flow, svc_port_id, gemport_id); |
| 226 | |
| 227 | { |
| 228 | bcmbal_classifier val = { }; |
| 229 | |
| 230 | if (classifier.o_tpid()) { |
| 231 | val.o_tpid = classifier.o_tpid(); |
| 232 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_TPID; |
| 233 | } |
| 234 | |
| 235 | if (classifier.o_vid()) { |
| 236 | val.o_vid = classifier.o_vid(); |
| 237 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_VID; |
| 238 | } |
| 239 | |
| 240 | if (classifier.i_tpid()) { |
| 241 | val.i_tpid = classifier.i_tpid(); |
| 242 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_TPID; |
| 243 | } |
| 244 | |
| 245 | if (classifier.i_vid()) { |
| 246 | val.i_vid = classifier.i_vid(); |
| 247 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_VID; |
| 248 | } |
| 249 | |
| 250 | if (classifier.o_pbits()) { |
| 251 | val.o_pbits = classifier.o_pbits(); |
| 252 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_PBITS; |
| 253 | } |
| 254 | |
| 255 | if (classifier.i_pbits()) { |
| 256 | val.i_pbits = classifier.i_pbits(); |
| 257 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_PBITS; |
| 258 | } |
| 259 | |
| 260 | if (classifier.eth_type()) { |
| 261 | val.ether_type = classifier.eth_type(); |
| 262 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_ETHER_TYPE; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | if (classifier.dst_mac()) { |
| 267 | val.dst_mac = classifier.dst_mac(); |
| 268 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_MAC; |
| 269 | } |
| 270 | |
| 271 | if (classifier.src_mac()) { |
| 272 | val.src_mac = classifier.src_mac(); |
| 273 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_MAC; |
| 274 | } |
| 275 | */ |
| 276 | |
| 277 | if (classifier.ip_proto()) { |
| 278 | val.ip_proto = classifier.ip_proto(); |
| 279 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_IP_PROTO; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | if (classifier.dst_ip()) { |
| 284 | val.dst_ip = classifier.dst_ip(); |
| 285 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_IP; |
| 286 | } |
| 287 | |
| 288 | if (classifier.src_ip()) { |
| 289 | val.src_ip = classifier.src_ip(); |
| 290 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_IP; |
| 291 | } |
| 292 | */ |
| 293 | |
| 294 | if (classifier.src_port()) { |
| 295 | val.src_port = classifier.src_port(); |
| 296 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_PORT; |
| 297 | } |
| 298 | |
| 299 | if (classifier.dst_port()) { |
| 300 | val.dst_port = classifier.dst_port(); |
| 301 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_PORT; |
| 302 | } |
| 303 | |
| 304 | if (!classifier.pkt_tag_type().empty()) { |
| 305 | if (classifier.pkt_tag_type().compare("untagged") == 0) { |
| 306 | val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_UNTAGGED; |
| 307 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE; |
| 308 | } else if (classifier.pkt_tag_type().compare("single_tag") == 0) { |
| 309 | val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_SINGLE_TAG; |
| 310 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE; |
| 311 | } else if (classifier.pkt_tag_type().compare("double_tag") == 0) { |
| 312 | val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_DOUBLE_TAG; |
| 313 | val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | BCMBAL_CFG_PROP_SET(&cfg, flow, classifier, val); |
| 318 | } |
| 319 | |
| 320 | { |
| 321 | bcmbal_action val = { }; |
| 322 | |
| 323 | const ::openolt::ActionCmd& cmd = action.cmd(); |
| 324 | |
| 325 | if (cmd.add_outer_tag()) { |
| 326 | val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_ADD_OUTER_TAG; |
| 327 | val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK; |
| 328 | } |
| 329 | |
| 330 | if (cmd.remove_outer_tag()) { |
| 331 | val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_REMOVE_OUTER_TAG; |
| 332 | val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK; |
| 333 | } |
| 334 | |
| 335 | if (cmd.trap_to_host()) { |
| 336 | val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_TRAP_TO_HOST; |
| 337 | val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK; |
| 338 | } |
| 339 | |
| 340 | if (action.o_vid()) { |
| 341 | val.o_vid = action.o_vid(); |
| 342 | val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_VID; |
| 343 | } |
| 344 | |
| 345 | if (action.o_pbits()) { |
| 346 | val.o_pbits = action.o_pbits(); |
| 347 | val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_PBITS; |
| 348 | } |
| 349 | |
| 350 | if (action.o_tpid()) { |
| 351 | val.o_tpid = action.o_tpid(); |
| 352 | val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_TPID; |
| 353 | } |
| 354 | |
| 355 | if (action.i_vid()) { |
| 356 | val.i_vid = action.i_vid(); |
| 357 | val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_VID; |
| 358 | } |
| 359 | |
| 360 | if (action.i_pbits()) { |
| 361 | val.i_pbits = action.i_pbits(); |
| 362 | val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_PBITS; |
| 363 | } |
| 364 | |
| 365 | if (action.i_tpid()) { |
| 366 | val.i_tpid = action.i_tpid(); |
| 367 | val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_TPID; |
| 368 | } |
| 369 | |
| 370 | BCMBAL_CFG_PROP_SET(&cfg, flow, action, val); |
| 371 | } |
| 372 | |
| 373 | { |
| 374 | bcmbal_tm_sched_id val; |
| 375 | val = (bcmbal_tm_sched_id) mk_sched_id(onu_id); |
| 376 | BCMBAL_CFG_PROP_SET(&cfg, flow, dba_tm_sched_id, val); |
| 377 | } |
| 378 | |
| 379 | if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) { |
| 380 | std::cout << "ERROR: flow add failed" << std::endl; |
| 381 | return Status(grpc::StatusCode::INTERNAL, "flow add failed"); |
| 382 | } |
| 383 | |
| 384 | return Status::OK; |
| 385 | } |
| 386 | |
| 387 | Status SchedAdd_(int intf_id, int onu_id, int agg_port_id) { |
| 388 | bcmbal_tm_sched_cfg cfg; |
| 389 | bcmbal_tm_sched_key key = { }; |
| 390 | bcmbal_tm_sched_type sched_type; |
| 391 | |
| 392 | key.id = mk_sched_id(onu_id); |
| 393 | key.dir = BCMBAL_TM_SCHED_DIR_US; |
| 394 | |
| 395 | BCMBAL_CFG_INIT(&cfg, tm_sched, key); |
| 396 | |
| 397 | { |
| 398 | bcmbal_tm_sched_owner val = { }; |
| 399 | |
| 400 | val.type = BCMBAL_TM_SCHED_OWNER_TYPE_AGG_PORT; |
| 401 | val.u.agg_port.intf_id = (bcmbal_intf_id) intf_id; |
| 402 | val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_INTF_ID; |
| 403 | val.u.agg_port.sub_term_id = (bcmbal_sub_id) onu_id; |
| 404 | val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_SUB_TERM_ID; |
| 405 | |
| 406 | val.u.agg_port.agg_port_id = (bcmbal_aggregation_port_id) agg_port_id; |
| 407 | val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_AGG_PORT_ID; |
| 408 | |
| 409 | BCMBAL_CFG_PROP_SET(&cfg, tm_sched, owner, val); |
| 410 | } |
| 411 | |
| 412 | if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) { |
| 413 | std::cout << "ERROR: Failed to create upstream DBA sched" |
| 414 | << " id:" << key.id |
| 415 | << " intf_id:" << intf_id |
| 416 | << " onu_id:" << onu_id << std::endl; |
| 417 | return Status(grpc::StatusCode::INTERNAL, "Failed to create upstream DBA sched"); |
| 418 | //return 1; |
| 419 | } |
| 420 | std::cout << "create upstream DBA sched" |
| 421 | << " id:" << key.id |
| 422 | << " intf_id:" << intf_id |
| 423 | << " onu_id:" << onu_id << std::endl; |
| 424 | |
| 425 | return Status::OK; |
| 426 | //return 0; |
| 427 | } |