LCOV - code coverage report
Current view: top level - base_comm/resources/endpoint_pairs/channels/host - exchange_data_format.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 73.5 % 34 25
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2026 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #ifndef EXCHANGE_DATA_FORMAT_H
      12              : #define EXCHANGE_DATA_FORMAT_H
      13              : 
      14              : #include <cstdint>
      15              : #include <cstring>
      16              : #include "hccp_common.h"
      17              : 
      18              : namespace hcomm {
      19              : 
      20              : // ========== RoCE 混合模式(Cross-Mode)常量定义 ==========
      21              : constexpr uint32_t EXCHANGE_DATA_MAGIC = 0x48434C52; // "HCLR"
      22              : constexpr uint16_t ROCE_CAPABILITY_VERSION = 1;
      23              : constexpr uint16_t ROCE_MIN_SUPPORTED_VERSION = 1;
      24              : constexpr uint32_t INVALID_DPU_NOTIFY_ID = 0xFFFFFFFF;
      25              : 
      26              : // ========== 枚举类型定义 ==========
      27              : 
      28              : // 通信协议栈类型
      29              : enum class CommStackType : uint8_t {
      30              :     COMM_STACK_HOST_CPU_ROCE = 0,     // Host CPU RoCE (ibverbs)
      31              :     COMM_STACK_TRANSPORT_IBVERBS = 1, // NPU RoCE (HCCP)
      32              :     COMM_STACK_UNKNOWN = 255
      33              : };
      34              : 
      35              : // 同步模式
      36              : enum class SyncMode : uint8_t {
      37              :     SYNC_MODE_WRITE_IMM = 0,    // Write With Immediate (原生模式)
      38              :     SYNC_MODE_WRITE_NOTIFY = 1, // Write + Notify (混合模式)
      39              :     SYNC_MODE_UNKNOWN = 255
      40              : };
      41              : 
      42              : // ========== 能力协商结构 ==========
      43              : // 注意:使用 packed 属性确保跨模块二进制兼容
      44              : struct RoCECapability {
      45              :     uint32_t magic;       // 魔数:0x48434C52 ("HCLR")
      46              :     uint16_t version;     // 版本号
      47              :     uint16_t totalLength; // 总长度(包括头部和变长数据)
      48              : 
      49              :     uint8_t nodeType;        // 节点类型(参考 HcclDeviceType)
      50              :     CommStackType commStack; // 通信协议栈类型
      51              :     SyncMode syncMode;       // 支持的同步模式
      52              :     uint8_t padding;
      53              : 
      54              :     NICDeployment nicDeploy; // NIC 部署位置
      55              : 
      56              :     uint8_t reserved[4]; // 预留字段,用于未来扩展
      57              : 
      58              :     // 序列化(直接内存拷贝)
      59              :     void Serialize(uint8_t* buffer, size_t& len) const
      60              :     {
      61              :         len = sizeof(RoCECapability);
      62              :         (void)memcpy_s(buffer, len, this, len);
      63              :     }
      64              : 
      65              :     // 反序列化(直接内存拷贝)
      66            1 :     bool Deserialize(const uint8_t* buffer, size_t len)
      67              :     {
      68            1 :         if (len < sizeof(RoCECapability)) {
      69            0 :             return false;
      70              :         }
      71            1 :         if (memcpy_s(this, sizeof(RoCECapability), buffer, sizeof(RoCECapability)) < 0) {
      72            0 :             return false;
      73              :         }
      74            1 :         return true;
      75              :     }
      76              : 
      77              :     // 快速检查魔数
      78            2 :     static bool CheckMagic(const uint8_t* buffer, size_t len)
      79              :     {
      80            2 :         if (len < sizeof(uint32_t)) {
      81            0 :             return false;
      82              :         }
      83              :         uint32_t magic;
      84            2 :         if (memcpy_s(&magic, sizeof(magic), buffer, sizeof(magic)) < 0) {
      85            0 :             return false;
      86              :         }
      87            2 :         return magic == EXCHANGE_DATA_MAGIC;
      88              :     }
      89              : 
      90              :     // 校验字段有效性
      91            1 :     bool Validate() const
      92              :     {
      93            1 :         if (magic != EXCHANGE_DATA_MAGIC) {
      94            0 :             return false;
      95              :         }
      96            1 :         if (version < ROCE_MIN_SUPPORTED_VERSION) {
      97            0 :             return false;
      98              :         }
      99              :         // 校验枚举值范围
     100            1 :         if (nicDeploy >= NICDeployment::NIC_DEPLOYMENT_RESERVED) {
     101            0 :             return false;
     102              :         }
     103            1 :         if (static_cast<uint8_t>(commStack) > 1) {
     104            0 :             return false;
     105              :         }
     106            1 :         if (static_cast<uint8_t>(syncMode) > 1) {
     107            0 :             return false;
     108              :         }
     109            1 :         return true;
     110              :     }
     111              : 
     112              :     // 初始化默认值
     113            2 :     void InitDefaults()
     114              :     {
     115            2 :         magic = EXCHANGE_DATA_MAGIC;
     116            2 :         version = ROCE_CAPABILITY_VERSION;
     117            2 :         totalLength = sizeof(RoCECapability);
     118            2 :         nodeType = 0;
     119            2 :         nicDeploy = NICDeployment::NIC_DEPLOYMENT_HOST;
     120            2 :         commStack = CommStackType::COMM_STACK_HOST_CPU_ROCE;
     121            2 :         syncMode = SyncMode::SYNC_MODE_WRITE_IMM;
     122            2 :         (void)memset_s(reserved, sizeof(reserved), 0, sizeof(reserved));
     123            2 :     }
     124              : } __attribute__((packed));
     125              : 
     126              : struct ExchangeDataFmt {
     127              :     uint32_t qpn;
     128              :     uint32_t psn;
     129              :     uint8_t gid[HCCP_GID_RAW_LEN];
     130              :     uint8_t gidIdx;
     131              :     uint8_t padding[3];
     132              : };
     133              : 
     134              : } // namespace hcomm
     135              : 
     136              : #endif // EXCHANGE_DATA_FORMAT_H
        

Generated by: LCOV version 2.0-1