blob: a476ec98f206ee661a5116589a9e4c65dc7cf8c4 [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi30c37ab2018-04-09 14:26:47 +00002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Junxiao Shi30c37ab2018-04-09 14:26:47 +000026#ifndef NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP
27#define NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050028
29#include "cs-policy.hpp"
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050030
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050031#include <ndn-cxx/util/scheduler.hpp>
32
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040033#include <list>
34
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::cs {
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050036namespace priority_fifo {
37
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060038using Queue = std::list<Policy::EntryRef>;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050039
40enum QueueType {
41 QUEUE_UNSOLICITED,
42 QUEUE_STALE,
43 QUEUE_FIFO,
44 QUEUE_MAX
45};
46
47struct EntryInfo
48{
49 QueueType queueType;
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060050 Queue::iterator queueIt;
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050051 ndn::scheduler::EventId moveStaleEventId;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050052};
53
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040054/** \brief Priority First-In-First-Out (FIFO) replacement policy.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050055 *
Junxiao Shi30c37ab2018-04-09 14:26:47 +000056 * This policy maintains a set of cleanup queues to decide the eviction order of CS entries.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060057 * The cleanup queues are three doubly linked lists that store EntryRefs.
Junxiao Shi30c37ab2018-04-09 14:26:47 +000058 * The three queues keep track of unsolicited, stale, and fresh Data packet, respectively.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060059 * EntryRef is placed into, removed from, and moved between suitable queues
Junxiao Shi30c37ab2018-04-09 14:26:47 +000060 * whenever an Entry is added, removed, or has other attribute changes.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060061 * Each Entry should be in exactly one queue at any moment.
62 * Within each queue, the EntryRefs are kept in first-in-first-out order.
Junxiao Shi30c37ab2018-04-09 14:26:47 +000063 * Eviction procedure exhausts the first queue before moving onto the next queue,
64 * in the order of unsolicited, stale, and fresh queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050065 */
Davide Pesavento3db98072021-03-09 23:03:27 -050066class PriorityFifoPolicy final : public Policy
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050067{
68public:
69 PriorityFifoPolicy();
70
Davide Pesavento3db98072021-03-09 23:03:27 -050071 ~PriorityFifoPolicy() final;
Minsheng Zhang9c903e02015-10-03 18:16:05 -050072
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050073private:
Junxiao Shi30c37ab2018-04-09 14:26:47 +000074 void
Davide Pesavento3db98072021-03-09 23:03:27 -050075 doAfterInsert(EntryRef i) final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050076
Junxiao Shi30c37ab2018-04-09 14:26:47 +000077 void
Davide Pesavento3db98072021-03-09 23:03:27 -050078 doAfterRefresh(EntryRef i) final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050079
Junxiao Shi30c37ab2018-04-09 14:26:47 +000080 void
Davide Pesavento3db98072021-03-09 23:03:27 -050081 doBeforeErase(EntryRef i) final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050082
Junxiao Shi30c37ab2018-04-09 14:26:47 +000083 void
Davide Pesavento3db98072021-03-09 23:03:27 -050084 doBeforeUse(EntryRef i) final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050085
Junxiao Shi30c37ab2018-04-09 14:26:47 +000086 void
Davide Pesavento3db98072021-03-09 23:03:27 -050087 evictEntries() final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050088
89private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040090 /** \brief Evicts one entry.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050091 * \pre CS is not empty
92 */
93 void
94 evictOne();
95
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040096 /** \brief Attaches the entry to an appropriate queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050097 * \pre the entry is not in any queue
98 */
99 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600100 attachQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500101
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400102 /** \brief Detaches the entry from its current queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500103 * \post the entry is not in any queue
104 */
105 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600106 detachQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500107
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400108 /** \brief Moves an entry from FIFO queue to STALE queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500109 */
110 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600111 moveToStaleQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500112
Davide Pesaventoae430302023-05-11 01:42:46 -0400113public:
114 static constexpr std::string_view POLICY_NAME{"priority_fifo"};
115
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500116private:
117 Queue m_queues[QUEUE_MAX];
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600118 std::map<EntryRef, EntryInfo*> m_entryInfoMap;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500119};
120
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200121} // namespace priority_fifo
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500122
123using priority_fifo::PriorityFifoPolicy;
124
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400125} // namespace nfd::cs
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500126
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000127#endif // NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP