void KatranLb::setupHcEnvironment()

in katran/lib/KatranLb.cpp [581:628]


void KatranLb::setupHcEnvironment() {
  auto map_fd = bpfAdapter_.getMapFdByName("hc_pckt_srcs_map");
  if (config_.katranSrcV4.empty() && config_.katranSrcV6.empty()) {
    throw std::runtime_error(
        "No source address provided for direct healthchecking");
  }
  if (!config_.katranSrcV4.empty()) {
    auto srcv4 =
        IpHelpers::parseAddrToBe(folly::IPAddress(config_.katranSrcV4));
    uint32_t key = kSrcV4Pos;
    auto res = bpfAdapter_.bpfUpdateMap(map_fd, &key, &srcv4);
    if (res < 0) {
      throw std::runtime_error(
          "can not update src v4 address for direct healthchecking");
    }
  } else {
    LOG(ERROR) << "Empty IPV4 address provided to use as source in healthcheck";
  }
  if (!config_.katranSrcV6.empty()) {
    auto srcv6 =
        IpHelpers::parseAddrToBe(folly::IPAddress(config_.katranSrcV6));
    auto key = kSrcV6Pos;
    auto res = bpfAdapter_.bpfUpdateMap(map_fd, &key, &srcv6);
    if (res < 0) {
      throw std::runtime_error(
          "can not update src v6 address for direct healthchecking");
    }
  } else {
    LOG(ERROR) << "Empty IPV6 address provided to use as source in healthcheck";
  }

  std::array<struct hc_mac, 2> macs;
  // populating mac addresses for healthchecking
  if (config_.localMac.size() != 6) {
    throw std::invalid_argument("src mac's size is not equal to six byte");
  }
  for (int i = 0; i < 6; i++) {
    macs[kHcSrcMacPos].mac[i] = config_.localMac[i];
    macs[kHcDstMacPos].mac[i] = config_.defaultMac[i];
  }
  for (auto position : {kHcSrcMacPos, kHcDstMacPos}) {
    auto res = bpfAdapter_.bpfUpdateMap(
        bpfAdapter_.getMapFdByName("hc_pckt_macs"), &position, &macs[position]);
    if (res < 0) {
      throw std::runtime_error("can not update healthchecks mac address");
    }
  }
}