extern "C" t_std_error std_user_chown(const char *path,const char *name, const char *group) {
    STD_ASSERT(path!=nullptr && name!=nullptr);
    size_t sz = get_estimated_buff(_SC_GETPW_R_SIZE_MAX);
    std::unique_ptr<char[]> b(new char [sz]);

    if (b.get()==nullptr) return STD_ERR(COM,NOMEM,0);

    struct passwd pass, *_pass=nullptr;

    if (getpwnam_r(name,&pass,b.get(),sz,&_pass)!=0 || _pass==nullptr) {
        EV_LOG(ERR,COM,0,"COM-USER-PERM","No such user %s",name);
        return STD_ERR(COM,FAIL,errno);
    }

    uid_t uid = _pass->pw_uid;

    gid_t gid = -1;
    if (group!=nullptr) {
        sz = get_estimated_buff(_SC_GETGR_R_SIZE_MAX);
        b = std::unique_ptr<char[]>(new char [sz]);

        struct group grp,*pgrp=nullptr;
        if (getgrnam_r(group,&grp,b.get(),sz,&pgrp)!=0 || pgrp==nullptr) {
            EV_LOG(ERR,COM,0,"COM-USER-PERM","No such group %s",group);
            return STD_ERR(COM,FAIL,errno);
        }
        gid = pgrp->gr_gid;
    }


    if (chown(path,uid,gid)!=0) {
        return STD_ERR(COM,FAIL,errno);
    }
    return STD_ERR_OK;
}
bool nas_os_physical_to_vlan_ifindex(hal_ifindex_t intf_index, hal_vlan_id_t vlan_id,
                                            bool to_vlan,hal_ifindex_t * index){
    char intf_name[HAL_IF_NAME_SZ+1];
    char vlan_intf_name[HAL_IF_NAME_SZ+1];
    char *converted_intf_name = NULL;

    if(cps_api_interface_if_index_to_name(intf_index,intf_name,sizeof(intf_name))==NULL){
        EV_LOG(ERR,NAS_OS,0,"NAS-LINUX-INTERFACE","Invalid Interface Index %d ",intf_index);
        return false;
    }

    if(to_vlan){
        snprintf(vlan_intf_name,sizeof(vlan_intf_name),"%s.%d",intf_name,vlan_id);
        converted_intf_name = vlan_intf_name;
    }else{
        char * saveptr;
        converted_intf_name = strtok_r(intf_name,".",&saveptr);
    }

    if(((*index) = cps_api_interface_name_to_if_index(converted_intf_name)) == 0){
        EV_LOG(ERR,NAS_OS,0,"NAS-LINUX-INTERFACE","Invalid Interface name %s ",converted_intf_name);
        return false;
    }

    return true;
}
t_std_error nas_int_cps_init(cps_api_operation_handle_t handle) {

    cps_api_registration_functions_t f;
    char buff[CPS_API_KEY_STR_MAX];
    memset(&f,0,sizeof(f));

    if (!cps_api_key_from_attr_with_qual(&f.key,BASE_IF_PHY_PHYSICAL_OBJ,cps_api_qualifier_TARGET)) {
        EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Could not translate %d to key %s",
                            (int)(DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_OBJ),
                            cps_api_key_print(&f.key,buff,sizeof(buff)-1));
        return STD_ERR(INTERFACE,FAIL,0);
    }
    EV_LOG(INFO,INTERFACE,0,"NAS-IF-REG","Registering for %s",
                        cps_api_key_print(&f.key,buff,sizeof(buff)-1));
    f.handle = handle;
    f._read_function = _phy_int_get;
    f._write_function = _phy_int_set;
    if (cps_api_register(&f)!=cps_api_ret_code_OK) {
        return STD_ERR(INTERFACE,FAIL,0);
    }

    t_std_error rc = STD_ERR_OK;
    npu_id_t npu = 0;
    npu_id_t npu_max = (npu_id_t)nas_switch_get_max_npus();
    for ( ; npu < npu_max ; ++npu ) {
        rc = ndi_port_event_cb_register(npu,_ndi_port_event_update_);
        if (rc!=STD_ERR_OK) return rc;
    }

    if ((rc=nas_int_breakout_init(handle))!=STD_ERR_OK) return rc;

    return nas_int_logical_init(handle);
}
bool cps_db::subscribe(cps_db::connection &conn, std::vector<char> &key) {
    cps_db::connection::db_operation_atom_t e[2];
    e[0].from_string("PSUBSCRIBE");
    if (key[key.size()-1]!='*') cps_utils::cps_api_vector_util_append(key,"*",1);
    e[1].from_string(&key[0],key.size());

    response_set resp;
    if (!conn.command(e,2,resp)) {
        EV_LOG(ERR,DSAPI,0,"CPS-DB-SUB","Subscribe failed to return response");
        return false;
    }

    cps_db::response r = resp.get_response(0);
    if (r.elements()==3) {
        cps_db::response msg (r.element_at(0));
        cps_db::response status (r.element_at(2));
        if (msg.is_str() && strcasecmp(msg.get_str(),"psubscribe")==0) {

            bool rc = (status.is_int() && status.get_int()>0);
            if (!rc) EV_LOG(ERR,DSAPI,0,"CPS-DB-SUB","Subscribe failed rc %d",status.get_int());
            return rc;
        }
    }
    return false;
}
bool INTERFACE::os_interface_lag_attrs_handler(if_details *details, cps_api_object_t obj)
{
    if_bond *bond_hdlr = os_get_bond_db_hdlr();
    hal_ifindex_t master_idx = (details->_attrs[IFLA_MASTER]!=NULL)?
                                *(int *)nla_data(details->_attrs[IFLA_MASTER]):0;

    if (details->_info_kind!=nullptr && !strncmp(details->_info_kind, "tun", 3)) {
         if(details->_attrs[IFLA_MASTER]!=NULL  && ((details->_flags & IFF_SLAVE)!=0)) {
            EV_LOG(INFO, NAS_OS,3, "NET-MAIN", "Received tun %d and state 0x%x",
                        details->_ifindex,details->_flags);
            if(bond_hdlr && bond_hdlr->bond_mbr_present(master_idx, details->_ifindex)){
                EV_LOG(INFO, NAS_OS,3, "NET-MAIN", "Bond mbr present in master %d, slave %d",
                            master_idx, details->_ifindex);
                return true;
            }
            if(bond_hdlr) bond_hdlr->bond_mbr_add(master_idx, details->_ifindex);
            // Unmask the event publish for this interface after lag addition
            os_interface_mask_event(details->_ifindex, OS_IF_CHANGE_NONE);

            if(!os_interface_lag_add_member_name(details->_ifindex, obj)) return false;
            details->_type = BASE_CMN_INTERFACE_TYPE_LAG;

         } else if ((details->_flags & IFF_SLAVE)!=0) {
             if(bond_hdlr && (master_idx = bond_hdlr->bond_master_get(details->_ifindex)))
                 bond_hdlr->bond_mbr_del(master_idx, details->_ifindex);

             if(!master_idx) {
                 EV_LOGGING(NAS_OS, DEBUG, "NET-MAIN", "No master found, this could be an if update for %d",
                             details->_ifindex);
                 return true;
             }

             EV_LOG(INFO, NAS_OS,3, "NET-MAIN", "Lag member delete: tun %d and state 0x%x",
                         details->_ifindex,details->_flags);

             if(!os_interface_lag_add_member_name(details->_ifindex, obj)) return false;

             cps_api_object_attr_delete(obj, DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_IF_INDEX);
             cps_api_object_attr_delete(obj, IF_INTERFACES_INTERFACE_NAME);
             cps_api_object_attr_add_u32(obj, DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_IF_INDEX,master_idx);

             details->_type = BASE_CMN_INTERFACE_TYPE_LAG;
             details->_op = cps_api_oper_DELETE;
         }
    }

    if(details->_info_kind!=nullptr && !strncmp(details->_info_kind, "bond", 4)) {
        EV_LOG(INFO, NAS_OS,3, "NET-MAIN", "Bond interface index is %d ",
                details->_ifindex);

        if (details->_attrs[IFLA_MASTER]==nullptr) {
            details->_type = BASE_CMN_INTERFACE_TYPE_LAG;
        }

    } // bond interface

    return true;
}
cps_api_return_code_t cps_api_process_get_request(cps_api_get_params_t *param, size_t ix) {
    cps_api_return_code_t rc = cps_api_ret_code_ERR;
    char buff[SCRATCH_LOG_BUFF];
    cps_api_channel_t handle;
    cps_api_key_t *key = cps_api_object_key(cps_api_object_list_get(param->filters,ix));
    if (!cps_api_get_handle(*key,handle)) {
        EV_LOG(ERR,DSAPI,0,"NS","Failed to find owner for %s",
                cps_api_key_print(key,buff,sizeof(buff)-1));
        return cps_api_ret_code_NO_SERVICE;
    }

    do {
        cps_api_object_t o = cps_api_object_list_get(param->filters,ix);
        if (o==NULL) {
            EV_LOG(ERR,DSAPI,0,"NS","Missing filters... %s",
                                       cps_api_key_print(key,buff,sizeof(buff)-1));
            break;
        }
        if (!cps_api_send_one_object(handle,cps_api_msg_o_GET,o)) {
                   EV_LOG(ERR,DSAPI,0,"NS","Failed to send request %s",
                           cps_api_key_print(key,buff,sizeof(buff)-1));
                   break;
        }

        uint32_t op;
        do {
            if (param->timeout > 0) {
                fd_set rset;
                FD_ZERO(&rset);
                FD_SET(handle,&rset);
                if ((rc=cps_api_timeout_wait(handle,&rset,param->timeout,"CPS-OP-GET"))!=cps_api_ret_code_OK) {
                    EV_LOG(ERR,DSAPI,0,"CPS-OP-GET","Send request %s timed out",
                           cps_api_key_print(key,buff,sizeof(buff)-1));
                    break;
                }
            }
            size_t len;
            if (!cps_api_receive_header(handle,op,len)) break;
            if (op!=cps_api_msg_o_GET_RESP) break;
            cps_api_object_guard og (cps_api_receive_object(handle,len));

            if (og.valid() && cps_api_object_list_append(param->list,og.get())) {
                og.release();
            } else break;
        } while (op == cps_api_msg_o_GET_RESP);

        if (op!=cps_api_msg_o_GET_DONE) break; //leave an error code
        rc = cps_api_ret_code_OK;
    } while (0);

    cps_api_disconnect_owner(handle);

    return rc;
}
static t_std_error _phy_media_type_with_default_config_set(npu_id_t npu, port_t port,
                                        PLATFORM_MEDIA_TYPE_t media_type)
{
    t_std_error rc;
    if ((rc = ndi_port_media_type_set(npu, port, media_type)) != STD_ERR_OK) {
        return rc;
    }
    /*  set default speed and autoneg setting  corresponding to the media type */
    cps_api_object_t obj = cps_api_object_create();
    cps_api_object_guard og(obj);
    if ((rc = dn_nas_get_phy_media_default_setting(media_type, obj)) != STD_ERR_OK) {
        return rc;
    }

    /* First set AN and then speed */
    cps_api_object_attr_t autoneg_attr = cps_api_object_attr_get(obj, BASE_MEDIA_MEDIA_INFO_AUTONEG);
    if (autoneg_attr != nullptr) {
        bool autoneg = (bool)cps_api_object_attr_data_u32(autoneg_attr);
        if (ndi_port_auto_neg_set(npu,port,autoneg)!=STD_ERR_OK) {
            EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Failed to set autoneg %d for "
                   "npu %d port %d return Error 0x%x",autoneg,npu,port,rc);
            return STD_ERR(INTERFACE,FAIL,0);
        }
    }

    BASE_IF_PHY_BREAKOUT_MODE_t cur_mode;
    if ((rc = ndi_port_breakout_mode_get(npu, port, &cur_mode)) != STD_ERR_OK) {
            EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Failed to get breakout mode for "
                   "npu %d port %d return error 0x%x",npu,port, rc);
            return STD_ERR(INTERFACE,FAIL,0);
    }
    /* if the port is fanout skip setting the default speed say 40G */
    if (BASE_IF_PHY_BREAKOUT_MODE_BREAKOUT_1X1 == cur_mode) {
        cps_api_object_attr_t speed_attr = cps_api_object_attr_get(obj, BASE_MEDIA_MEDIA_INFO_SPEED);
        if (speed_attr != nullptr) {
            BASE_IF_SPEED_t speed = (BASE_IF_SPEED_t)cps_api_object_attr_data_u32(speed_attr);
            if ((rc = ndi_port_speed_set(npu,port,speed))!=STD_ERR_OK) {
                EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Failed to set speed %d for "
                       "npu %d port %d return error 0x%x",speed,npu,port, rc);
                return STD_ERR(INTERFACE,FAIL,0);
            }
        }
    }
    cps_api_object_attr_t duplex_attr = cps_api_object_attr_get(obj, BASE_MEDIA_MEDIA_INFO_DUPLEX);
    if (duplex_attr != nullptr) {
        BASE_CMN_DUPLEX_TYPE_t duplex = (BASE_CMN_DUPLEX_TYPE_t)cps_api_object_attr_data_u32(duplex_attr);
        if ((rc = ndi_port_duplex_set(npu,port,duplex))!=STD_ERR_OK) {
            EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Failed to set duplex %d for "
                   "npu %d port %d return error 0x%x",duplex,npu,port, rc);
            return STD_ERR(INTERFACE,FAIL,0);
        }
    }
    return STD_ERR_OK;
}
cps_api_return_code_t cps_api_process_rollback_request(cps_api_transaction_params_t *param,
        size_t ix) {
    cps_api_return_code_t rc = cps_api_ret_code_ERR;

    cps_api_object_t obj = cps_api_object_list_get(param->prev,ix);
    if (obj==NULL) {
        EV_LOG(ERR,DSAPI,0,"CPS IPC","No Revert Object");
        return rc;
    }

    cps_api_channel_t handle;
    cps_api_key_t *key = cps_api_object_key(obj);

    char buff[CPS_API_KEY_STR_MAX];
    EV_LOG(TRACE,DSAPI,0, "CPS IPC", "Object for rollback request: %s ",
                                    cps_api_key_name_print(key, buff, sizeof(buff)));

    if (!cps_api_get_handle(*key,handle)) {
        EV_LOG(ERR,DSAPI,0,"CPS IPC","No Service");
        return cps_api_ret_code_NO_SERVICE;
    }

    cps_api_channel_handle_guard hg(handle);

    rc = cps_api_ret_code_ERR;

    do {
        if (!cps_api_send_one_object(handle,cps_api_msg_o_REVERT,obj)) {
            EV_LOG(ERR,DSAPI,0,"CPS IPC","Could not send REVERT header");
            break;
        }

        uint32_t op;
           size_t len;
           if (!cps_api_receive_header(handle,op,len)) {
               EV_LOG(ERR,DSAPI,0,"CPS IPC","Failed to read the receive header for revert object");
               break;
           }

           if (op == cps_api_msg_o_RETURN_CODE) {
               if (!cps_api_receive_data(handle,&rc,sizeof(rc))) break;
           }

    } while (0);

    cps_api_disconnect_owner(handle);

    return rc;
}
t_std_error nas_lag_delete(npu_id_t npu_id, ndi_obj_id_t ndi_lag_id) {

    EV_LOG(INFO,INTERFACE,3,"NAS-LAG","Deleting ndi_lag_id  %"PRIu64" in npu %d",
            ndi_lag_id,npu_id);

    return ndi_delete_lag(npu_id, ndi_lag_id);
}
static cps_api_return_code_t _cps_api_event_service_client_register(
        cps_api_event_service_handle_t handle,
        cps_api_event_reg_t * req) {

    cps_api_to_std_event_map_t *p = handle_to_data(handle);
    std_mutex_simple_lock_guard lg(&p->lock);

    char _key_s[CPS_API_KEY_STR_MAX];

    size_t ix = 0;
    size_t mx = req->number_of_objects;

    for ( ; ix < mx ; ++ix ) {
        _key_t _k;
        _make_key(_k,&(req->objects[ix]));
        if (p->keys.find(_k)!=p->keys.end()) {
            EV_LOG(INFO,DSAPI,0,"CPS-EV-REG","Skipping key %s - already registered",
                    cps_api_key_print(&req->objects[ix],_key_s,sizeof(_key_s)));
            continue;
        }
        p->keys[_k].sync = false;
    }

    if (p->connected) {
        __resync_regs(p);
    }

    return cps_api_ret_code_OK;
}
cps_api_return_code_t cps_api_timeout_wait(int handle, fd_set *r_template, size_t timeout_ms,const char *op) {
    fd_set _rset = *r_template;
    struct timeval tv;
    tv.tv_sec = timeout_ms / 1000;
    tv.tv_usec = MILLI_TO_MICRO(timeout_ms % 1000);    //just the milliseconds portion
    int rc = std_select_ignore_intr(handle+1,&_rset,nullptr,nullptr,&tv,nullptr);
    if (rc==-1) {
        EV_LOG(ERR,DSAPI,0,op,"CPS Operation failed - application close");
        return cps_api_ret_code_ERR;
    }
    if (rc==0) {
        EV_LOG(ERR,DSAPI,0,op,"CPS Operation failed - application time out");
        return cps_api_ret_code_TIMEOUT;
    }
    return cps_api_ret_code_OK;
}
extern "C" t_std_error nas_os_interface_set_attribute(cps_api_object_t obj,cps_api_attr_id_t id) {
    char buff[NL_MSG_INTF_BUFF_LEN];
    memset(buff,0,sizeof(buff));

    cps_api_object_attr_t _ifix = cps_api_object_attr_get(obj, DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_IF_INDEX);
    if (_ifix==NULL) return (STD_ERR(NAS_OS,FAIL, 0));

    hal_ifindex_t if_index = cps_api_object_attr_data_uint(_ifix);
    if (if_index==0) return STD_ERR(NAS_OS,FAIL, 0);

    struct nlmsghdr *nlh = (struct nlmsghdr *) nlmsg_reserve((struct nlmsghdr *)buff,sizeof(buff),sizeof(struct nlmsghdr));
    struct ifinfomsg *ifmsg = (struct ifinfomsg *) nlmsg_reserve(nlh,sizeof(buff),sizeof(struct ifinfomsg));

    nas_os_pack_nl_hdr(nlh, RTM_SETLINK, (NLM_F_REQUEST | NLM_F_ACK));
    nas_os_pack_if_hdr(ifmsg, AF_UNSPEC, 0, if_index);

    char if_name[HAL_IF_NAME_SZ+1];
    if(cps_api_interface_if_index_to_name(if_index, if_name, sizeof(if_name)) == NULL) {
        EV_LOG(ERR, NAS_OS, ev_log_s_CRITICAL, "NAS-OS", "Failure getting interface name for %d", if_index);
        return (STD_ERR(NAS_OS,FAIL, 0));
    }

    unsigned flags = 0;
    if(nas_os_util_int_flags_get(if_name, &flags) == STD_ERR_OK) {
        ifmsg->ifi_flags = flags;
    }

    static const std::map<cps_api_attr_id_t,void (*)( cps_api_object_t ,struct nlmsghdr *,
            struct ifinfomsg *,size_t)> _funcs = {
            {DELL_IF_IF_INTERFACES_INTERFACE_MTU, _set_mtu},
            {IF_INTERFACES_INTERFACE_NAME, _set_name},
            {IF_INTERFACES_INTERFACE_ENABLED, _set_admin},
            {NAS_OS_IF_ALIAS, _set_ifalias },
            {DELL_IF_IF_INTERFACES_INTERFACE_PHYS_ADDRESS, _set_mac},

    };

    auto it =_funcs.find(id);
    if (it==_funcs.end()) return STD_ERR_OK;
    it->second(obj,nlh,ifmsg,sizeof(buff));

    if(nl_do_set_request(nas_nl_sock_T_INT, nlh, buff,sizeof(buff)) != STD_ERR_OK) {
        EV_LOG(ERR, NAS_OS, ev_log_s_CRITICAL, "NAS-OS", "Failure updating interface in kernel");
        return (STD_ERR(NAS_OS,FAIL, 0));
    }
    return STD_ERR_OK;
}
extern "C" cps_api_return_code_t cps_api_object_stats(cps_api_key_t *key, cps_api_object_t stats_obj) {
    cps_api_return_code_t rc = cps_api_ret_code_ERR;

    cps_api_channel_t handle = -1;

    if (key==nullptr || cps_api_key_get_len(key)==0) {
        std_socket_address_t addr;
        cps_api_ns_get_address(&addr);

        int sock;
        t_std_error _rc = std_sock_connect(&addr,&sock);
        if (_rc!=STD_ERR_OK) {
            return rc;
        }
        handle = sock;
    } else {
        if (!cps_api_get_handle(*key,handle)) {
            char buff[CPS_API_KEY_STR_MAX];
            EV_LOG(ERR,DSAPI,0,"NS","Failed to find owner for %s",
                    cps_api_key_print(key,buff,sizeof(buff)-1));
            return cps_api_ret_code_NO_SERVICE;
        }
    }
    if (handle==-1) {
        EV_LOG(ERR,DSAPI,0,"NS-STATS","No connection to the NS for stats.");
        return rc;
    }
    do {
        if (!cps_api_send_header(handle,cps_api_msg_o_STATS,0)) {
            break;
        }
        size_t len;
        uint32_t op=0;
        if (!cps_api_receive_header(handle,op,len)) break;
        if (op!=cps_api_msg_o_STATS) break;

        cps_api_object_guard og (cps_api_receive_object(handle,len));
        if(!og.valid()) return false;

        if (!cps_api_object_clone(stats_obj,og.get())) {
            break;
        }
        rc = cps_api_ret_code_OK;
    } while (0);
    cps_api_disconnect_owner(handle);
    return rc;
}
// *          [ugoa]*([-+=]([rwxXst]*|[ugo]))+
extern "C" t_std_error std_user_chmod(const char *path, const char *perm_str) {
    //todo wrap this initialization with a mutex
    static std_mutex_lock_create_static_init_fast(lock);
    std_mutex_lock(&lock);

    static const mode_t all_mode_mask = _perm_map.find('a')->second.find('w')->second |
                                        _perm_map.find('a')->second.find('r')->second |
                                        _perm_map.find('a')->second.find('x')->second ;
    std_mutex_unlock(&lock);

    struct stat buf;
    if (stat(path, &buf)!=0) {
        int err = errno;
        EV_LOG(ERR,COM,0,"COM-USER-PERM","path element doesn't exist %s",path);
        return STD_ERR(COM,FAIL,err);
    }

    STD_ASSERT(path!=nullptr && perm_str!=nullptr);

    mode_t mod = 0;

    while(*perm_str!=0) {
        char op = '+';
        mode_t _ch_mod = 0;
        if (!_chomp(perm_str,op,_ch_mod)) {
            EV_LOG(ERR,COM,0,"COM-USER-PERM","permission string invalid %s",perm_str);
            return STD_ERR(COM,PARAM,*perm_str);
        }
        if (op=='=') {
            mod = _ch_mod;
        } else {
            if (mod==0) mod = buf.st_mode & all_mode_mask;
        }
        if (op=='+') {
            mod |= _ch_mod;
        }
        if (op=='-') {
            mod = mod & (~_ch_mod);
        }
    }
    if (chmod(path,mod)!=0) {
        int err = errno;
        EV_LOG(ERR,COM,0,"COM-USER-PERM","Unable to change the file permissions %s:%x (%d)",path,mod,err);
        return STD_ERR(COM,FAIL,err);
    }
    return STD_ERR_OK;
}
t_std_error nas_get_lag_member_attr(npu_id_t npu_id,ndi_obj_id_t ndi_lag_member_id,
        bool *egress_disable) {

    EV_LOG(INFO,INTERFACE,3,"NAS-LAG","Block/Unblock NAS port <%d >  %"PRIu64
            ,npu_id,ndi_lag_member_id);

    return ndi_get_lag_member_attr(npu_id,ndi_lag_member_id,egress_disable);
}
cps_api_return_code_t cps_api_connect_owner(cps_api_object_owner_reg_t*o,cps_api_channel_t &handle) {
    t_std_error rc = std_sock_connect(&o->addr,&handle);
    if (rc!=STD_ERR_OK) {
        EV_LOG(ERR,DSAPI,0,"CPS IPC","not able to connect to owner");
        rc = cps_api_ret_code_SERVICE_CONNECT_FAIL;
    }
    return (cps_api_return_code_t) rc;
}
bool cps_api_send_data(cps_api_channel_t handle, void *data, size_t len) {
    t_std_error rc = STD_ERR_OK;
    int by = std_write(handle,data,len,true,&rc);
    if (by != (int)len) {
        EV_LOG(ERR,DSAPI,0,"CPS IPC","Was not able to send the full message body.");
    }
    return (by==(int)len) ;
}
 bool cps_api_send_object(cps_api_channel_t handle, cps_api_object_t obj) {
    t_std_error rc = STD_ERR_OK;
    size_t len = cps_api_object_to_array_len(obj);
    int by = std_write(handle,cps_api_object_array(obj),len, true,&rc);
    if (by != (int)len) {
        EV_LOG(ERR,DSAPI,0,"CPS IPC","Was not able to send the full object.");
    }
    return (by==(int)len) ;
}
void nas_switch_wait_for_sys_base_mac(hal_mac_addr_t *mac_base) {
    while (true) {
        if (nas_switch_get_sys_mac_base(mac_base)== STD_ERR_OK) {
            break;
        }
        EV_LOG(TRACE,NAS_COM, 0, "NAS-COM-MAC","Failed to get system mac..");
        std_usleep(MILLI_TO_MICRO(500));
    }
}
 bool cps_api_receive_data(cps_api_channel_t handle, void *data, size_t len) {
    t_std_error msg_rc = STD_ERR_OK;
    int by = std_read(handle,data,len,true,&msg_rc);
    bool rc = (by==(int)len) ;
    if (!rc) {
        EV_LOG(TRACE,DSAPI,0,"CPS IPC","Was not able to read the data. (%X)",msg_rc);
    }
    return rc;
}
Exemple #21
0
t_std_error nas_os_del_neighbor (cps_api_object_t obj)
{

    if (nas_os_update_neighbor(obj, NAS_RT_DEL) != cps_api_ret_code_OK) {
        EV_LOG(ERR, NAS_OS, ev_log_s_CRITICAL, "NEIGH-DEL", "Kernel write failed");
        return (STD_ERR(NAS_OS, FAIL, 0));
    }

    return STD_ERR_OK;
}
Exemple #22
0
t_std_error nas_os_set_route (cps_api_object_t obj)
{

    if (nas_os_update_route(obj, NAS_RT_SET) != cps_api_ret_code_OK) {
        EV_LOG(ERR, NAS_OS, ev_log_s_CRITICAL, "ROUTE-SET", "Kernel write failed");
        return (STD_ERR(NAS_OS, FAIL, 0));
    }

    return STD_ERR_OK;
}
static bool event_some_data ( void *context, int fd ) {
    std_socket_event_server_t *p = (std_socket_event_server_t*)context;

    event_service_clients_t::iterator it = p->m_event_clients.find(fd);
    if (it==p->m_event_clients.end()) {
        return false;
    }
    std::vector<uint8_t> &buff =(*it).second->buff;
    if (std_event_util_event_recv(fd,buff,true)!=STD_ERR_OK) {
        return false;
    }

    if (buff.size()< sizeof(event_serv_msg_t)) return false;

    event_serv_msg_t *serv_msg = (event_serv_msg_t*) &(buff[0]);
    void * data = vector_offset(buff,sizeof(event_serv_msg_t));
    if (serv_msg->op==event_serv_msg_t_ADD_REG) {
        std_event_key_t *key = (std_event_key_t *)data;
        return p->reg_message(fd,key);
    }
    if (serv_msg->op == event_serv_msg_t_DEL_REG) {
        std_event_key_t *key = (std_event_key_t *)data;
        p->dereg_message(fd,key);
    }
    if (serv_msg->op == event_serv_msg_t_PUBLISH) {
        std_event_msg_t *msg = (std_event_msg_t *)data;
        if (buff.size()< (sizeof(event_serv_msg_t)+ sizeof(std_event_msg_t)+msg->data_len)) return false;
        p->publish(msg);
    }
    if (serv_msg->op == event_serv_msg_t_BUFFER) {
        size_t buff_len = *(size_t*)data;
        if (buff_len < SOCKET_BUFFER_MIN_SIZE) { //if less then some minimum, ignore it
            buff_len = SOCKET_BUFFER_MIN_SIZE;
        }
        if (std_sock_set_sndbuf(fd,buff_len)!=STD_ERR_OK) {
            EV_LOG(ERR,COM,0,"COM-EVENT-BUFFER","Failed to set the buffering amount to %d",(int)buff_len);
        } else {
            EV_LOG(ERR,COM,0,"COM-EVENT-BUFFER","Buffer adjusted to %d",(int)buff_len);
        }
    }
    return true;
}
extern "C" t_std_error nas_os_del_interface(hal_ifindex_t if_index)
{
    char buff[NL_MSG_INTF_BUFF_LEN];

    memset(buff,0,sizeof(buff));

    EV_LOG(INFO, NAS_OS, ev_log_s_MINOR, "NAS-OS", "Del Interface %d", if_index);

    struct nlmsghdr *nlh = (struct nlmsghdr *) nlmsg_reserve((struct nlmsghdr *)buff,sizeof(buff),sizeof(struct nlmsghdr));
    struct ifinfomsg *ifmsg = (struct ifinfomsg *) nlmsg_reserve(nlh,sizeof(buff),sizeof(struct ifinfomsg));

    nas_os_pack_nl_hdr(nlh, RTM_DELLINK, (NLM_F_REQUEST | NLM_F_ACK));

    nas_os_pack_if_hdr(ifmsg, AF_UNSPEC, 0, if_index);

    if(nl_do_set_request(nas_nl_sock_T_INT, nlh, buff,sizeof(buff)) != STD_ERR_OK) {
        EV_LOG(ERR, NAS_OS, ev_log_s_CRITICAL, "NAS-OS", "Failure deleting interface in kernel");
        return (STD_ERR(NAS_OS,FAIL, 0));
    }
    return STD_ERR_OK;
}
t_std_error nas_add_port_to_lag(npu_id_t npu_id, ndi_obj_id_t ndi_lag_id,
        ndi_port_t *p_ndi_port,ndi_obj_id_t *ndi_lag_member_id) {
    ndi_port_list_t ndi_port_list;

    ndi_port_list.port_count = 1;
    ndi_port_list.port_list =p_ndi_port;

    EV_LOG(INFO,INTERFACE,3,"NAS-LAG","Adding NAS port <%d %d>  %"PRIu64" ",p_ndi_port->npu_id,
                p_ndi_port->npu_port,ndi_lag_id);

    return ndi_add_ports_to_lag(npu_id, ndi_lag_id,  &ndi_port_list,ndi_lag_member_id);
}
Exemple #26
0
static cps_api_return_code_t cps_nas_switch_log_set_function(void * context,
                             cps_api_transaction_params_t * param, size_t ix){

    cps_api_object_t obj = cps_api_object_list_get(param->change_list,ix);
    cps_api_operation_types_t op = cps_api_object_type_operation(cps_api_object_key(obj));

    if (op != cps_api_oper_ACTION) {
        EV_LOG(ERR,SYSTEM,0,"NAS-DIAG","Invalid operation %d for setting switch log",op);
        return cps_api_ret_code_ERR;
    }

    BASE_SWITCH_SUBSYSTEM_t switch_system_id;
    cps_api_object_attr_t switch_system_id_attr;

    if ((switch_system_id_attr = cps_api_get_key_data(obj,BASE_SWITCH_SET_LOG_INPUT_SUBSYSTEM_ID)) == NULL) {
        EV_LOG(ERR,SYSTEM,0,"NAS-DIAG","No Module id passed for Updating sai log level");
        return cps_api_ret_code_ERR;
    }

    switch_system_id = (BASE_SWITCH_SUBSYSTEM_t) cps_api_object_attr_data_u32(switch_system_id_attr);
    cps_api_attr_id_t log_level_attr_id = BASE_SWITCH_SET_LOG_INPUT_LEVEL;
    cps_api_object_attr_t log_level_attr = cps_api_object_e_get (obj, &log_level_attr_id, 1);

    if(log_level_attr == NULL){
        EV_LOG(ERR,SYSTEM,0,"NAS-DIAG","No log level passed for Updating sai log level"
                            "for module %d",switch_system_id);
        return cps_api_ret_code_ERR;
    }

    BASE_SWITCH_LOG_LEVEL_t log_level = (BASE_SWITCH_LOG_LEVEL_t)
                                        cps_api_object_attr_data_u32(log_level_attr);
    t_std_error rc;
    if( (rc =ndi_switch_set_sai_log_level(switch_system_id,log_level)) != STD_ERR_OK ){
        EV_LOG(ERR,SYSTEM,0,"NAS-DIAG","Failed to set log_level to %d for sai module %d "
                "got the return code %d ",log_level,switch_system_id,rc);
        return cps_api_ret_code_ERR;
    }

    return cps_api_ret_code_OK;
}
bool cps_api_send_header(cps_api_channel_t handle, uint32_t op,
        size_t len) {
    cps_msg_hdr_t hdr;
    hdr.len =len;
    hdr.operation = op;
    hdr.version = 0;
    t_std_error rc = STD_ERR_OK;
    int by = std_write(handle,&hdr,sizeof(hdr),true,&rc);
    if (by!=sizeof(hdr)) {
        EV_LOG(TRACE,DSAPI,0,"CPS IPC","Was not able to send the full message header.");
    }
    return (by==sizeof(hdr)) ;
}
static void _set_mac(cps_api_object_t obj, struct nlmsghdr *nlh, struct ifinfomsg * inf,size_t len) {
    cps_api_object_attr_t attr = cps_api_object_attr_get(obj, DELL_IF_IF_INTERFACES_INTERFACE_PHYS_ADDRESS);
    if (attr==NULL) return;
    void *addr = cps_api_object_attr_data_bin(attr);
    hal_mac_addr_t mac_addr;

    int addr_len = strlen(static_cast<char *>(addr));
    if (std_string_to_mac(&mac_addr, static_cast<const char *>(addr), addr_len)) {
        char mac_str[40] = {0};
        EV_LOG(INFO, NAS_OS, ev_log_s_MAJOR, "NAS-OS", "Setting mac address %s, actual string %s, len %d",
                std_mac_to_string(&mac_addr,mac_str,sizeof(mac_str)), static_cast<char *>(addr), addr_len);
        nlmsg_add_attr(nlh,len,IFLA_ADDRESS, mac_addr , cps_api_object_attr_len(attr));
    }
}
static cps_api_return_code_t _cps_api_event_service_client_connect(cps_api_event_service_handle_t * handle) {

    cps_api_to_std_event_map_t *p = new cps_api_to_std_event_map_t;
    if (p==NULL) return cps_api_ret_code_ERR;

    std_mutex_lock_init_non_recursive(&p->lock);
    std_mutex_simple_lock_guard lg(&p->lock);
    bool rc = __connect_to_service(p);
    if (!rc) {
        EV_LOG(INFO,DSAPI,0,"CPS-EV-CONN","Not able to connect to event service - will retry based on use.");
    }
    *handle = p;
    return cps_api_ret_code_OK;
}
bool cps_api_node_set_iterate(const std::string &group_name,const std::function<void (const std::string &node, void*context)> &operation,
                              void *context) {
    std::vector<std::string> lst;

    if (!cps_api_db_get_node_group(group_name,lst)) {
        EV_LOG(ERR,DSAPI,0,"CPS-DB-NODES","Failed to load db for group %s",group_name.c_str());
        return false;
    }

    for (auto node_it : lst ) {
        operation(node_it,context);
    }
    return true;
}