<commit_before>-- Use of this source code is governed by the Apache 2.0 license; see COPYING.

module(...,package.seeall)

local zone = require("jit.zone")
local basic_apps = require("apps.basic.basic_apps")
local ffi      = require("ffi")
local lib      = require("core.lib")
local counter  = require("core.counter")
local pci      = require("lib.hardware.pci")
local register = require("lib.hardware.register")
local macaddress = require("lib.macaddress")
local intel10g = require("apps.intel.intel10g")
local freelist = require("core.freelist")
local receive, transmit, full, empty = link.receive, link.transmit, link.full, link.empty
Intel82599 = {}
Intel82599.__index = Intel82599

local C = ffi.C

-- The `driver' variable is used as a reference to the driver class in
-- order to interchangably use NIC drivers.
driver = Intel82599

-- table pciaddr => {pf, vflist}
local devices = {}


local function firsthole(t)
   for i = 1, #t+1 do
      if t[i] == nil then
         return i
      end
   end
end


-- Create an Intel82599 App for the device with 'pciaddress'.
function Intel82599:new (arg)
   local conf = config.parse_app_arg(arg)
   local self = setmetatable({counters = {}}, Intel82599)

   if conf.vmdq then
      if devices[conf.pciaddr] == nil then
         devices[conf.pciaddr] = {pf=intel10g.new_pf(conf):open(), vflist={}}
      end
      local dev = devices[conf.pciaddr]
      local poolnum = firsthole(dev.vflist)-1
      local vf = dev.pf:new_vf(poolnum)
      dev.vflist[poolnum+1] = vf
      self.dev = vf:open(conf)
   else
      self.dev = assert(intel10g.new_sf(conf):open(), "Can not open device.")
      self.zone = "intel"
   end

   self.counters['type']               = counter.open('type')
   self.counters['discontinuity-time'] = counter.open('discontinuity-time')
   self.counters['in-octets']          = counter.open('in-octets')
   self.counters['in-discards']        = counter.open('in-discards')
   self.counters['out-octets']         = counter.open('out-octets')
   counter.set(self.counters['type'], 0x1000) -- Hardware interface
   counter.set(self.counters['discontinuity-time'], C.get_unix_time())
   if conf.vmdq then
      self.counters['phys-address'] = counter.open('phys-address')
      counter.set(self.counters['phys-address'],
                  macaddress:new(conf.macaddr):int())
   end

   return self
end

function Intel82599:stop()
   local close_pf = nil
   if self.dev.pf and devices[self.dev.pf.pciaddress] then
      local poolnum = self.dev.poolnum
      local pciaddress = self.dev.pf.pciaddress
      local dev = devices[pciaddress]
      if dev.vflist[poolnum+1] == self.dev then
         dev.vflist[poolnum+1] = nil
      end
      if next(dev.vflist) == nil then
         close_pf = devices[pciaddress].pf
         devices[pciaddress] = nil
      end
   end
   self.dev:close()
   if close_pf then
      close_pf:close()
   end

   -- delete counters
   for name, _ in pairs(self.counters) do counter.delete(name) end
end


function Intel82599:reconfig(arg)
   local conf = config.parse_app_arg(arg)
   assert((not not self.dev.pf) == (not not conf.vmdq), "Can't reconfig from VMDQ to single-port or viceversa")

   self.dev:reconfig(conf)

   if conf.vmdq then
      counter.set(self.counters['phys-address'],
                  macaddress:new(conf.macaddr):int())
   end
end

-- Allocate receive buffers from the given freelist.
function Intel82599:set_rx_buffer_freelist (fl)
   self.rx_buffer_freelist = fl
end

-- Pull in packets from the network and queue them on our 'tx' link.
function Intel82599:pull ()
   local l = self.output.tx
   if l == nil then return end
   self.dev:sync_receive()
   for i=1,128 do
      if full(l) or not self.dev:can_receive() then break end
      transmit(l, self.dev:receive())
   end
   self:add_receive_buffers()
   if self.dev.rxstats then
      counter.set(self.counters['in-octets'],
                  bit.lshift(self.dev.pf.qs.QBRC_H[self.dev.rxstats]()+0LL, 32)
                     + self.dev.pf.qs.QBRC_L[self.dev.rxstats]())
      counter.set(self.counters['in-discards'],
                  self.dev.pf.qs.QPRDC[self.dev.rxstats]())
   end
end

function Intel82599:add_receive_buffers ()
   -- Generic buffers
   while self.dev:can_add_receive_buffer() do
      self.dev:add_receive_buffer(packet.allocate())
   end
end

-- Push packets from our 'rx' link onto the network.
function Intel82599:push ()
   local l = self.input.rx
   if l == nil then return end
   while not empty(l) and self.dev:can_transmit() do
      do local p = receive(l)
         self.dev:transmit(p)
         --packet.deref(p)
      end
   end
   self.dev:sync_transmit()
   if self.dev.txstats then
      counter.set(self.counters['out-octets'],
                  bit.lshift(self.dev.pf.qs.QBTC_H[self.dev.txstats]()+0LL, 32)
                     + self.dev.pf.qs.QBTC_L[self.dev.txstats]())
   end
end

-- Report on relevant status and statistics.
function Intel82599:report ()
   print("report on intel device", self.dev.pciaddress or self.dev.pf.pciaddress)
   register.dump(self.dev.s)
   if self.dev.rxstats then
      for name,v in pairs(self.dev:get_rxstats()) do
         io.write(string.format('%30s: %d\n', 'rx '..name, v))
      end
   end
   if self.dev.txstats then
      for name,v in pairs(self.dev:get_txstats()) do
         io.write(string.format('%30s: %d\n', 'tx '..name, v))
      end
   end
   local function r(n)
      return self.dev.r[n] or self.dev.pf.r[n]
   end
   register.dump({
      r'TDH', r'TDT',
      r'RDH', r'RDT',
      r'AUTOC',
      r'LINKS',
   })
end

function selftest ()
   print("selftest: intel_app")<commit_msg>Revert "Intel_app: Add RFC 7223 app counters."<commit_after>-- Use of this source code is governed by the Apache 2.0 license; see COPYING.

module(...,package.seeall)

local zone = require("jit.zone")
local basic_apps = require("apps.basic.basic_apps")
local ffi      = require("ffi")
local lib      = require("core.lib")
local pci      = require("lib.hardware.pci")
local register = require("lib.hardware.register")
local intel10g = require("apps.intel.intel10g")
local freelist = require("core.freelist")
local receive, transmit, full, empty = link.receive, link.transmit, link.full, link.empty
Intel82599 = {}
Intel82599.__index = Intel82599

local C = ffi.C

-- The `driver' variable is used as a reference to the driver class in
-- order to interchangably use NIC drivers.
driver = Intel82599

-- table pciaddr => {pf, vflist}
local devices = {}


local function firsthole(t)
   for i = 1, #t+1 do
      if t[i] == nil then
         return i
      end
   end
end


-- Create an Intel82599 App for the device with 'pciaddress'.
function Intel82599:new (arg)
   local conf = config.parse_app_arg(arg)

   if conf.vmdq then
      if devices[conf.pciaddr] == nil then
         devices[conf.pciaddr] = {pf=intel10g.new_pf(conf):open(), vflist={}}
      end
      local dev = devices[conf.pciaddr]
      local poolnum = firsthole(dev.vflist)-1
      local vf = dev.pf:new_vf(poolnum)
      dev.vflist[poolnum+1] = vf
      return setmetatable({dev=vf:open(conf)}, Intel82599)
   else
      local dev = intel10g.new_sf(conf):open()
      if not dev then return null end
      return setmetatable({dev=dev, zone="intel"}, Intel82599)
   end
end

function Intel82599:stop()
   local close_pf = nil
   if self.dev.pf and devices[self.dev.pf.pciaddress] then
      local poolnum = self.dev.poolnum
      local pciaddress = self.dev.pf.pciaddress
      local dev = devices[pciaddress]
      if dev.vflist[poolnum+1] == self.dev then
         dev.vflist[poolnum+1] = nil
      end
      if next(dev.vflist) == nil then
         close_pf = devices[pciaddress].pf
         devices[pciaddress] = nil
      end
   end
   self.dev:close()
   if close_pf then
      close_pf:close()
   end
end


function Intel82599:reconfig(arg)
   local conf = config.parse_app_arg(arg)
   assert((not not self.dev.pf) == (not not conf.vmdq), "Can't reconfig from VMDQ to single-port or viceversa")

   self.dev:reconfig(conf)
end

-- Allocate receive buffers from the given freelist.
function Intel82599:set_rx_buffer_freelist (fl)
   self.rx_buffer_freelist = fl
end

-- Pull in packets from the network and queue them on our 'tx' link.
function Intel82599:pull ()
   local l = self.output.tx
   if l == nil then return end
   self.dev:sync_receive()
   for i=1,128 do
      if full(l) or not self.dev:can_receive() then break end
      transmit(l, self.dev:receive())
   end
   self:add_receive_buffers()
end

function Intel82599:add_receive_buffers ()
   -- Generic buffers
   while self.dev:can_add_receive_buffer() do
      self.dev:add_receive_buffer(packet.allocate())
   end
end

-- Push packets from our 'rx' link onto the network.
function Intel82599:push ()
   local l = self.input.rx
   if l == nil then return end
   while not empty(l) and self.dev:can_transmit() do
      do local p = receive(l)
         self.dev:transmit(p)
         --packet.deref(p)
      end
   end
   self.dev:sync_transmit()
end

-- Report on relevant status and statistics.
function Intel82599:report ()
   print("report on intel device", self.dev.pciaddress or self.dev.pf.pciaddress)
   register.dump(self.dev.s)
   if self.dev.rxstats then
      for name,v in pairs(self.dev:get_rxstats()) do
         io.write(string.format('%30s: %d\n', 'rx '..name, v))
      end
   end
   if self.dev.txstats then
      for name,v in pairs(self.dev:get_txstats()) do
         io.write(string.format('%30s: %d\n', 'tx '..name, v))
      end
   end
   local function r(n)
      return self.dev.r[n] or self.dev.pf.r[n]
   end
   register.dump({
      r'TDH', r'TDT',
      r'RDH', r'RDT',
      r'AUTOC',
      r'LINKS',
   })
end

function selftest ()
   print("selftest: intel_app")
{'fraction_non_alphanumeric': 0.12520441537203597, 'fraction_numerical': 0.02125919869174162, 'mean_word_length': 2.9503431570448124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '333756', 'n_tokens_mistral': 3497, 'n_tokens_neox': 3330, 'n_words': 839}