Ceforeの導入まとめ on Ubuntu

# この記事
NICTのCefore導入スライドを(ほぼ)そのまま実践してみて，自分が実際に実行した手順を残します．
この記事では全て単一のPCのローカルホスト上で動作検証をしています．
複数ノードは次の記事でまとめるつもりです．
上から順番にコマンド叩くだけで動くようにしてるつもりです．
要点をまとめちゃってるので詳しい事が知りたい場合はスライドを見たほうが良いです．

# 環境
- Ubuntu 18.04 LTS on WSL2

# 動作までに叩いたコマンド等
手順のジャンル毎に分けています
## 前提となる開発環境の構築
余計なものまで入っていますが、基本的な開発に必要なライブラリです．

```shell:Setup
sudo apt -y updatea
sudo apt -y upgrade
sudo apt -y full-upgrade
sudo apt -y autoremove
sudo apt -y install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git zlib1g-dev libffi-dev automake net-tools
```
## Ceforeのダウンロード
https://cefore.net/download から`最新のソースコードをダウンロード(現時点 0.8.3)

```shell:Install
# ceforeのソースをダウンロードする
wget https://cefore.net/dlfile.php?file=cefore-0.8.3.zip -O cefore.zip
unzip cefore.zip
cd cefore-0.8.3
export CEFORE_DIR=/usr/local
aclocal
automake
./configure --enable-csmgr --enable-cache
make
sudo make install
ldconfig
```
## Ceforeを実行する準備
### バッファチューニング(毎回実行が必要)
```shell:Tuning
sudo sysctl --write net.core.rmem_default=10000000
sudo sysctl --write net.core.wmem_default=10000000
sudo sysctl --write net.core.rmem_max=10000000
sudo sysctl --write net.core.wmem_max=10000000
```
### cefnetdのテスト
```shell:cefnetd
sudo cefnetdstart
cefstatus
sudo cefnetdstop
```
### csmgrdのテスト
```shell:csmgrd
sudo csmgrdstart
csmgrstatus
sudo csmgrdstop
```
ここまでで実行エラー無く実行できればインストールはOKです

### Configの設定
```shell:/usr/local/cefore/cefnetd.conf
#
# cefnetd.conf
#

#
# Node Name is specified in URI format.
#   ex) abc.com/tokyo/router-a
#
NODE_NAME="test"

# Operational Log Level
#  0: Error only
#  1: Warning and Error
#  2: Info, Warning, and Error
#
CEF_LOG_LEVEL=1

#
# Port number used by cefnetd.
# This value must be higther than 1024 and lower than 65536.
#
PORT_NUM=9896

#
# Socket ID used by cefnetd.
# This value is the string type, not the integer type.
#
#LOCAL_SOCK_ID=0

#
# csmgr's IP address
#
CSMGR_NODE=127.0.0.1

#
# csmgr's TCP port number.
# This value must be higher than 1024 and lower than 65536.
#
CSMGR_PORT_NUM=9799

#
# Maximum number of PIT entries.
# This value must be higther than 0 and lower than 65536.
#
PIT_SIZE=2048

#
# Maximum number of PIT(APP) entries.
# This value must be higther than 0 and lower than 1025.
#
#PIT_SIZE_APP=64

#
# Maximum number of FIB entries.
# This value must be higther than 0 and lower than 65536.
#
FIB_SIZE=1024

#
# Maximum number of FIB(APP) entries.
# This value must be higther than 0 and lower than 1024000.
#
#FIB_SIZE_APP=64

#
# Content Store used by cefnetd
#  0 : No Content Store
#  1 : Use cefnetd's Local cache
#  2 : Use external Content Store (use csmgrd)
#  3 : Use external Content Store (use conpubd)
#
CS_MODE=2

#
# Maximum number of Cobs used in local cache of cefnetd.
# This value must be greater than 1 and less than or equal to 8,000,000.
#
LOCAL_CACHE_CAPACITY=65535

#
# cefnetd local cache expired content check interval(sec)
# This value must be greater than 1 and less than 86,400(24 hours).
#
LOCAL_CACHE_INTERVAL=60

#
# Buffer size to keep the number of Cobs.
# This value must be higher than or equal to 0 and lower than 65536.
#
BUFFER_CAPACITY=30000

#
# Buffer cache time(ms).
# This value must be higher than or equal to 0.
#
BUFFER_CACHE_TIME=10000

#
# FIB entry selection strategy.
#   0: Forward using any 1 Longest prefix match FIB entry
#   1: Forward using all Longest prefix match FIB entries
#
#FORWARDING_INFO_STRATEGY=0

#
# CCNinfo access policy
#0: Allow Request (default)
#1: Allow to forward Request (i.e., no cache information informed)
#2: Disallow Request
#
#CCNINFO_ACCESS_POLICY=0

# "CCNinfo Full discovery request" permission setting
#0: Disallow Full-discovery Request (default)
#1: Allow Full-discovery Request
#2: Allow Full-discovery Request only from authenticated consumers (NOT IMPLEMENTED YET)
#
#CCNINFO_FULL_DISCOVERY=0

#
# Specify the Validation Algorithm to be added to Content Object.
# Validation is not added when None is specified.
# Either sha256 or crc32 can be specified.
#
#CCNINFO_VALID_ALG=crc32

#
# Private key, public key prefix
#   Private key name: Specified string + "-private-key"
#   Public key name: Specified string + "-public-key"
#
#CCNINFO_SHA256_KEY_PRFX=ccninfo_rt

#
# PIT lifetime(seconds) at "full discovery request"
# This value must be higher than or equal to 2 and lower than or equal to 5.
#
#CCNINFO_REPLY_TIMEOUT=4

#
# cefnetd controller
# The default is cefnetd dose not use the controller.
#
#CONTROLLER_ID=127.0.0.1
#CONTROLLER_PORT_NUM=9458
#CONTROLLER_KEY=/usr/local/cefore/ccore-public-key

#
# cefbabeld
# The default is cefnetd dose not use cefbabeld.
#  0 : Unuse
#  1 : Use
#USE_CEFBABEL=0
# Protocol used to construct routes
#  udp, tcp or both
#CEFBABEL_ROUTE=both

#
# Neighbour Discovery
#
#NBR_SIZE=1
#NBR_INTERVAL=10000
#NBR_THRESH=3

#
# Same Interest retransmission strategy
#	RFC8569 or SUPPRESSIVE
#
#INTEREST_RETRANSMISSION=RFC8569

#
# Whether to transfer Selective Interest upstream
#	0:No 1:Yes
#
#SELECTIVE_FORWARDING=1

#
# A range of chunk numbers that allow retroactive responses to the Simbolic Interest
#	n is an integer greater than or equal to 0.
#
#SYMBOLIC_BACKBUFFER=100

#
# Bandwidth utilization threshold (%) of the receive IF to respond to Congesion when receiving Interest
#	n is an integer greater than or equal to 0.
#
#INTEREST_RETURN_CONGESTION_THRESHOLD=90

#
# Interval (seconds) for bandwidth utilization calculation
#	n is an integer greater than or equal to 1.
#
#BANDWIDTH_STAT_INTERVAL=1

#
# Maximum Lifetime of Symbolic Interest (seconds)
#	n is an integer greater than or equal to 0.
#
#SYMBOLIC_INTEREST_MAX_LIFETIME=4


#
# Maximum Lifetime of Regular Interest (seconds)
#	n is an integer greater than or equal to 0.
#
#REGULAR_INTEREST_MAX_LIFETIME=2

#
# The name of the plug-in that determines congestion.
# If None, the congestion judgment is always in a non-congested state.
#
#BANDWIDTH_STAT_PLUGIN=bw_stat


#CSMGR_ACCESS=RW

#
# RTC(seconds) to set for Cob to Local cache. 
# This value must be higher than 1 and lower than 3600.
#
#LOCAL_CACHE_DEFAULT_RCT=600


#
# Serial Logging
#
#SER_LOG_ENABLE=0
#SER_LOG_SIZE=65535
#SER_LOG_DIR=/path/to/file.csv

#
# Community Log
#
#  Necessity of output log for community.
#    OFF or ON
#CEF_C3_LOG=OFF
#
# Community log output directory.
#CEF_C3_LOG_DIR=/usr/local/cefore/logs
#
# Community log output period. The unit is seconds.
#   This value must be greater than 0.
#CEF_C3_LOG_PERIOD=3600

# Debug log level
#
#  Range of the debug log level can be specified from 0 to 3. (0 indicates "no debug logging")
#  Note that you must specify "--enable-debug" for configure option to enable this debug logging.
#
CEF_DEBUG_LEVEL=0

```
```shell:/usr/local/cefore/csmgrd.conf
#
# csmgrd.conf
#

# Operational Log Level
#  0: Error only
#  1: Warning and Error
#  2: Info, Warning, and Error
#
CEF_LOG_LEVEL=1

#
# Port number used by csmgrd.
# This value must be higher than 1024 and lower than 65536.
#
PORT_NUM=9799

#
# Socket ID used by csmgrd and cefnetd.
# This value is the string type, not the integer type.
#
LOCAL_SOCK_ID=0

#
# The maximum number of cached Cobs.
# This value must be between 1 and  68,719,476,735(0xFFFFFFFFF) inclusive.
# It can also be specified in 16 digits.
# (e.g.: 0xFF)
#
CACHE_CAPACITY=819200

#
# Type of CS space used by csmgrd.
#  filesystem : UNIX filesystem
#  memory     : Memory
#
CACHE_TYPE=filesystem

#
# Type of cache policy by cache plugin.
# If None is specified, no caching policy is used.
#
CACHE_ALGORITHM=libcsmgrd_lru

#
# The average name size of Cob processed by the cache policy.
# Used for out-of-memory checking when the cache policy is applied.
# This Value must be between 100 and 8000 inclusive.
#
CACHE_ALGO_NAME_SIZE=256

#
# The average size of Cob processed by the cache policy.
# Used for out-of-memory checking when the cache policy is applied.
# This Value must be between 500 and 65535 inclusive.
#
CACHE_ALGO_COB_SIZE=2048

#
# Check interval for expired content/Cob in csmgrd (ms).
# This value must be higher than or equal to 1000 and lower than
# 86400000 (24 hours).
#
CACHE_INTERVAL=10000

#
# Directory name. Only applicable for filesystem cache.
# The default is $CEFORE_DIR/cefore.
#
#CACHE_PATH=

#
# RCT (ms) if RCT is not specified in transmitted Cob. 
# This value must be higher than or equal to 1000 and lower than 3600,000.
#
CACHE_DEFAULT_RCT=600000

#
# Node list csmgrd allows the connection.
# Multiple host names or IP addresses can be listed with ",", such as; 
# ALLOW_NODE=1.1.1.1,2.2.2.2,3.3.3.3
# or
# ALLOW_NODE=1.1.1.0/24
# ALLOW_NODE=2.2.0.0/16
# ALLOW_NODE=3.3.3.3
#
# It is also possible to allow any node to connect csmgrd, such as; 
# ALLOW_NODE=ALL. 
# However, because of security risks, this ALL configuration is NOT recommended.
#
ALLOW_NODE=127.0.0.1

# Debug log level
#
#  Range of the debug log level can be specified from 0 to 3. (0 indicates "no debug logging")
#  Note that you must specify "--enable-debug" for configure option to enable this debug logging.
#
CEF_DEBUG_LEVEL=0


```

## 動作検証
### デーモンを起動

```shell:start
sudo csmgrdstart
sudo cefnetdstat
```
### 状態確認

```shell:status
cefstatus
csmgrstatus
```

### FIBの登録
※アップデートでccnからccnxに変わっています

```shell:FIB
sudo cefroute add ccnx:/test udp 127.0.0.1
```
### ファイル送受信
#### ファイルアップデート
CSにアップロードします

```shell:Upload
touch test.txt
cefputfile ccnx:/test/test.txt
```
#### ファイルダウンロード
CSからダウンロードします

```shell:Download
cefgetfile ccnx:/test/test.txt
```

以上の手順でCeforeを用いたファイルの送受信ができるかと思います．
複数ノードで行う場合は複数PCで上記の操作を行い、FIBの設定で親子関係を考慮して経路を登録することで可能になるかと思います．

# 付録
```shell:cefore_restart.sh

#!/bin/bash
sudo sysctl --write net.core.rmem_default=10000000
sudo sysctl --write net.core.wmem_default=10000000
sudo sysctl --write net.core.rmem_max=10000000
sudo sysctl --write net.core.wmem_max=10000000

sudo cefnetdstop
sleep 1
sudo csmgrdstop
sleep 1
sudo csmgrdstart
sleep 1
sudo cefnetdstart
```
# 参考
https://www.ieice.org/cs/icn/?page_id=1208
https://www.ieice.org/~icn/wp-content/uploads/2019/08/ICNWS201908-first.pdf