Skip to content

Commit dfaa896

Browse files
committed
Dpdk - introduce dpdk Mbuf wrapper class
1 parent 0ea269c commit dfaa896

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ endif
156156

157157
if WITH_DPDK
158158
ipfixprobe_input_src+=\
159+
input/dpdk/dpdkMbuf.hpp \
160+
input/dpdk/dpdkMbuf.cpp \
159161
input/dpdk.cpp \
160162
input/dpdk.h \
161163
input/dpdk-ring.cpp \

input/dpdk/dpdkMbuf.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* \file
3+
* \brief Implementation of the DpdkMbuf class.
4+
* \author Pavel Siska <[email protected]>
5+
* \date 2023
6+
*/
7+
/*
8+
* Copyright (C) 2023 CESNET
9+
*
10+
* LICENSE TERMS
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions
14+
* are met:
15+
* 1. Redistributions of source code must retain the above copyright
16+
* notice, this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright
18+
* notice, this list of conditions and the following disclaimer in
19+
* the documentation and/or other materials provided with the
20+
* distribution.
21+
* 3. Neither the name of the Company nor the names of its contributors
22+
* may be used to endorse or promote products derived from this
23+
* software without specific prior written permission.
24+
*/
25+
26+
#include "dpdkMbuf.hpp"
27+
28+
namespace ipxp {
29+
30+
DpdkMbuf::DpdkMbuf(size_t mBufsCount)
31+
: m_mBufsCount(mBufsCount)
32+
, m_mBufsInUse(0)
33+
{
34+
m_mBufs.resize(mBufsCount);
35+
}
36+
37+
void DpdkMbuf::resize(size_t mBufsCount)
38+
{
39+
releaseMbufs();
40+
m_mBufs.resize(mBufsCount);
41+
m_mBufsCount = mBufsCount;
42+
}
43+
44+
void DpdkMbuf::setMbufsInUse(size_t mBufsInUse) noexcept
45+
{
46+
m_mBufsInUse = mBufsInUse;
47+
}
48+
49+
DpdkMbuf::~DpdkMbuf()
50+
{
51+
releaseMbufs();
52+
}
53+
54+
uint16_t DpdkMbuf::maxSize() const noexcept
55+
{
56+
return m_mBufsCount;
57+
}
58+
59+
uint16_t DpdkMbuf::size() const noexcept
60+
{
61+
return m_mBufsInUse;
62+
}
63+
64+
rte_mbuf** DpdkMbuf::data()
65+
{
66+
return m_mBufs.data();
67+
}
68+
69+
void DpdkMbuf::releaseMbufs()
70+
{
71+
for (auto mBufID = 0; mBufID < m_mBufsInUse; mBufID++) {
72+
rte_pktmbuf_free(m_mBufs[mBufID]);
73+
}
74+
m_mBufsInUse = 0;
75+
}
76+
77+
} // namespace ipxp

input/dpdk/dpdkMbuf.hpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* \file
3+
* \brief Declaration of the DpdkMbuf class.
4+
* \author Pavel Siska <[email protected]>
5+
* \date 2023
6+
*/
7+
/*
8+
* Copyright (C) 2023 CESNET
9+
*
10+
* LICENSE TERMS
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions
14+
* are met:
15+
* 1. Redistributions of source code must retain the above copyright
16+
* notice, this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright
18+
* notice, this list of conditions and the following disclaimer in
19+
* the documentation and/or other materials provided with the
20+
* distribution.
21+
* 3. Neither the name of the Company nor the names of its contributors
22+
* may be used to endorse or promote products derived from this
23+
* software without specific prior written permission.
24+
*/
25+
26+
#pragma once
27+
28+
#include <cstdint>
29+
#include <rte_mbuf.h>
30+
#include <vector>
31+
32+
namespace ipxp {
33+
34+
/**
35+
* @brief Wrapper class for DPDK mbuf objects.
36+
*/
37+
class DpdkMbuf {
38+
public:
39+
40+
/**
41+
* @brief Constructs a DpdkMbuf object.
42+
* @param mBufsCount The initial size of the mbufs vector.
43+
*/
44+
DpdkMbuf(size_t mBufsCount = 0);
45+
46+
/**
47+
* @brief Releases the allocated mbufs.
48+
*/
49+
~DpdkMbuf();
50+
51+
/**
52+
* @brief Resizes the mbufs vector.
53+
* @param mBufsCount The new size of the mbufs vector.
54+
*/
55+
void resize(size_t mBufsCount);
56+
57+
/**
58+
* @brief Sets the number of mbufs in use.
59+
* @param mBufsInUse The number of mbufs currently in use.
60+
*/
61+
void setMbufsInUse(size_t mBufsInUse) noexcept;
62+
63+
/**
64+
* @brief Returns the maximum size (currently allocated) of the mbufs vector.
65+
* @return The maximum size of the mbufs vector.
66+
*/
67+
uint16_t maxSize() const noexcept;
68+
69+
/**
70+
* @brief Returns the current size of the mbufs vector. (in use)
71+
* @return The current size of the mbufs vector.
72+
*/
73+
uint16_t size() const noexcept;
74+
75+
/**
76+
* @brief Returns a pointer to the underlying mbufs data.
77+
* @return A pointer to the underlying mbufs data.
78+
*/
79+
rte_mbuf** data();
80+
81+
/**
82+
* @brief Releases all the mbufs.
83+
* @note Function calls rte_pktmbuf_free()
84+
*/
85+
void releaseMbufs();
86+
87+
/**
88+
* @brief Overloaded subscript operator to access mbufs by index.
89+
* @param index The index of the mbuf to access.
90+
* @return The mbuf at the specified index.
91+
*/
92+
rte_mbuf* operator[](int index) { return m_mBufs[index]; }
93+
94+
private:
95+
std::vector<rte_mbuf*> m_mBufs;
96+
uint16_t m_mBufsCount;
97+
uint16_t m_mBufsInUse;
98+
};
99+
100+
} // namespace ipxp

0 commit comments

Comments
 (0)