<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <fstream>

#include "cnetlink.hpp"

namespace rofcore {

cnetlink::cnetlink(switch_interface *swi)
    : swi(swi), thread(this), bridge(nullptr), running(false) {

  sock = nl_socket_alloc();
  if (NULL == sock) {
    LOG(FATAL) << "cnetlink: failed to create netlink socket" << __FUNCTION__;
    throw eNetLinkCritical(__FUNCTION__);
  }

  try {
    init_caches();
    thread.start();
  } catch (...) {
    LOG(ERROR) << "cnetlink: caught unkown exception during " << __FUNCTION__;
  }
}

cnetlink::~cnetlink() {
  delete bridge;
  destroy_caches();
  nl_socket_free(sock);
}

int cnetlink::load_from_file(const std::string &path) {
  std::ifstream file;
  int out = -1;
  file.open(path);
  if (file.is_open()) {
    while (!file.eof()) {
      file >> out;
    }
  }
  if (out < 0) {
    LOG(FATAL) << "cnetlink: failed to load " << path;
    throw eNetLinkCritical(__FUNCTION__);
  }
  return out;
}

void cnetlink::init_caches() {

  int rc = nl_cache_mngr_alloc(sock, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr);

  if (rc < 0) {
    LOG(FATAL)
        << "cnetlink::init_caches() failed to allocate netlink cache manager";
    throw eNetLinkCritical("cnetlink::init_caches()");
  }

  int rx_size = load_from_file("/proc/sys/net/core/rmem_max");
  int tx_size = load_from_file("/proc/sys/net/core/wmem_max");

  if (0 != nl_socket_set_buffer_size(sock, rx_size, tx_size)) {
    LOG(FATAL) << "cnetlink: failed to resize socket buffers";
    throw eNetLinkCritical(__FUNCTION__);
  }
  nl_socket_set_msg_buf_size(sock, rx_size);

  caches[NL_LINK_CACHE] = NULL;
  caches[NL_NEIGH_CACHE] = NULL;

  rc = rtnl_link_alloc_cache_flags(sock, AF_UNSPEC, &caches[NL_LINK_CACHE],
                                   NL_CACHE_AF_ITER);
  if (0 != rc) {
    LOG(ERROR)
        << "cnetlink::init_caches() rtnl_link_alloc_cache_flags failed rc="
        << rc;
  }
  rc = nl_cache_mngr_add_cache(mngr, caches[NL_LINK_CACHE],
                               (change_func_t)&nl_cb, NULL);
  if (0 != rc) {
    LOG(ERROR) << "cnetlink::init_caches() add route/link to cache mngr";
  }

  rc = rtnl_neigh_alloc_cache_flags(sock, &caches[NL_NEIGH_CACHE],
                                    NL_CACHE_AF_ITER);
  if (0 != rc) {
    LOG(ERROR)
        << "cnetlink::init_caches() rtnl_link_alloc_cache_flags failed rc="
        << rc;
  }
  rc = nl_cache_mngr_add_cache(mngr, caches[NL_NEIGH_CACHE],
                               (change_func_t)&nl_cb, NULL);
  if (0 != rc) {
    LOG(ERROR) << "cnetlink::init_caches() add route/neigh to cache mngr";
  }

  struct nl_object *obj = nl_cache_get_first(caches[NL_LINK_CACHE]);
  while (0 != obj) {
    VLOG(1) << "cnetlink::" << __FUNCTION__ << "(): adding "
            << rtnl_link_get_name((struct rtnl_link *)obj) << " to rtlinks";
    rtlinks.add_link(crtlink((struct rtnl_link *)obj));
    obj = nl_cache_get_next(obj);
  }

  obj = nl_cache_get_first(caches[NL_NEIGH_CACHE]);
  while (0 != obj) {
    unsigned int ifindex = rtnl_neigh_get_ifindex((struct rtnl_neigh *)obj);
    switch (rtnl_neigh_get_family((struct rtnl_neigh *)obj)) {
    case AF_BRIDGE:
      if (rtlinks.has_link(ifindex)) {<commit_msg>set several log levels to be fatal<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <fstream>

#include "cnetlink.hpp"

namespace rofcore {

cnetlink::cnetlink(switch_interface *swi)
    : swi(swi), thread(this), bridge(nullptr), running(false) {

  sock = nl_socket_alloc();
  if (NULL == sock) {
    LOG(FATAL) << "cnetlink: failed to create netlink socket" << __FUNCTION__;
    throw eNetLinkCritical(__FUNCTION__);
  }

  try {
    init_caches();
    thread.start();
  } catch (...) {
    LOG(FATAL) << "cnetlink: caught unkown exception during " << __FUNCTION__;
  }
}

cnetlink::~cnetlink() {
  delete bridge;
  destroy_caches();
  nl_socket_free(sock);
}

int cnetlink::load_from_file(const std::string &path) {
  std::ifstream file;
  int out = -1;
  file.open(path);
  if (file.is_open()) {
    while (!file.eof()) {
      file >> out;
    }
  }
  if (out < 0) {
    LOG(FATAL) << "cnetlink: failed to load " << path;
    throw eNetLinkCritical(__FUNCTION__);
  }
  return out;
}

void cnetlink::init_caches() {

  int rc = nl_cache_mngr_alloc(sock, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr);

  if (rc < 0) {
    LOG(FATAL)
        << "cnetlink::init_caches() failed to allocate netlink cache manager";
    throw eNetLinkCritical("cnetlink::init_caches()");
  }

  int rx_size = load_from_file("/proc/sys/net/core/rmem_max");
  int tx_size = load_from_file("/proc/sys/net/core/wmem_max");

  if (0 != nl_socket_set_buffer_size(sock, rx_size, tx_size)) {
    LOG(FATAL) << "cnetlink: failed to resize socket buffers";
    throw eNetLinkCritical(__FUNCTION__);
  }
  nl_socket_set_msg_buf_size(sock, rx_size);

  caches[NL_LINK_CACHE] = NULL;
  caches[NL_NEIGH_CACHE] = NULL;

  rc = rtnl_link_alloc_cache_flags(sock, AF_UNSPEC, &caches[NL_LINK_CACHE],
                                   NL_CACHE_AF_ITER);
  if (0 != rc) {
    LOG(FATAL)
        << "cnetlink::init_caches() rtnl_link_alloc_cache_flags failed rc="
        << rc;
  }
  rc = nl_cache_mngr_add_cache(mngr, caches[NL_LINK_CACHE],
                               (change_func_t)&nl_cb, NULL);
  if (0 != rc) {
    LOG(FATAL) << "cnetlink::init_caches() add route/link to cache mngr";
  }

  rc = rtnl_neigh_alloc_cache_flags(sock, &caches[NL_NEIGH_CACHE],
                                    NL_CACHE_AF_ITER);
  if (0 != rc) {
    LOG(FATAL)
        << "cnetlink::init_caches() rtnl_link_alloc_cache_flags failed rc="
        << rc;
  }
  rc = nl_cache_mngr_add_cache(mngr, caches[NL_NEIGH_CACHE],
                               (change_func_t)&nl_cb, NULL);
  if (0 != rc) {
    LOG(FATAL) << "cnetlink::init_caches() add route/neigh to cache mngr";
  }

  struct nl_object *obj = nl_cache_get_first(caches[NL_LINK_CACHE]);
  while (0 != obj) {
    VLOG(1) << "cnetlink::" << __FUNCTION__ << "(): adding "
            << rtnl_link_get_name((struct rtnl_link *)obj) << " to rtlinks";
    rtlinks.add_link(crtlink((struct rtnl_link *)obj));
    obj = nl_cache_get_next(obj);
  }

  obj = nl_cache_get_first(caches[NL_NEIGH_CACHE]);
  while (0 != obj) {
    unsigned int ifindex = rtnl_neigh_get_ifindex((struct rtnl_neigh *)obj);
    switch (rtnl_neigh_get_family((struct rtnl_neigh *)obj)) {
    case AF_BRIDGE:
      if (rtlinks.has_link(ifindex)) {
{'fraction_non_alphanumeric': 0.13853197459754837, 'fraction_numerical': 0.004430660168365086, 'mean_word_length': 3.332693538067818, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4989872', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2542, 'n_words': 508}