From bfb07834e42c2334ab2a30e61b408daedc6eebb2 Mon Sep 17 00:00:00 2001 From: amos Date: Wed, 4 Nov 2026 22:44:18 +0800 Subject: [PATCH] Add 4 HARD tasks from batch 29: Zephyr ZMS, NuttX TCP retry, eventfd, telnet Batch 29 commit 97ead2b gave Zephyr ZMS restart records, NuttX TCP retry closure and Zephyr eventfd lock progress (1 of 3 passed each) and Zephyr telnet close handoff (0 of 3) as HARD. Each task comes from the commit Qwen measured, its digest matches, and it is still present at the batch branch head. Co-Authored-By: Claude Opus 5.5 --- docs/progress.md | 13 + .../cheat/subsys/fs/zms/zms.c | 1811 ++++ .../cheat_read_grader/subsys/fs/zms/zms.c | 1818 ++++ tasks/fs-zephyr-zms-restart-records/check.py | 302 + .../gold/subsys/fs/zms/zms.c | 1811 ++++ .../mask_symptom/subsys/fs/zms/zms.c | 1811 ++++ .../noop/subsys/fs/zms/zms.c | 1806 ++++ .../only_cache/subsys/fs/zms/zms.c | 1810 ++++ .../only_gc/subsys/fs/zms/zms.c | 1806 ++++ tasks/fs-zephyr-zms-restart-records/prompt.md | 5 + tasks/fs-zephyr-zms-restart-records/spec.md | 117 + .../fs-zephyr-zms-restart-records/support.tar | Bin 0 -> 40960 bytes tasks/fs-zephyr-zms-restart-records/task.json | 28 + .../template/LICENSE | 202 + .../template/Makefile | 13 + .../template/README.md | 9 + .../template/diagnostic.txt | 14 + .../template/docs/storage-contract.md | 27 + .../template/docs/upstream-zms.rst | 446 + .../template/include/zephyr/device.h | 5 + .../template/include/zephyr/drivers/flash.h | 17 + .../template/include/zephyr/fs/zms.h | 248 + .../template/include/zephyr/kernel.h | 29 + .../template/include/zephyr/logging/log.h | 5 + .../template/include/zephyr/sys/crc.h | 9 + .../template/include/zephyr/toolchain.h | 2 + .../template/lib/crc/crc32_sw.c | 34 + .../template/lib/crc/crc8_sw.c | 74 + .../template/port.c | 34 + .../template/port.h | 9 + .../template/runner.c | 27 + .../template/smoke.txt | 8 + .../template/subsys/fs/zms/zms.c | 1805 ++++ .../template/subsys/fs/zms/zms_priv.h | 80 + .../valid_alternative/subsys/fs/zms/zms.c | 1809 ++++ tasks/fs-zephyr-zms-restart-records/verify.py | 130 + .../cheat/net/tcp/tcp_timer.c | 843 ++ .../cheat_read_grader/net/tcp/tcp_timer.c | 855 ++ tasks/net-nuttx-tcp-retry-closure/check.py | 283 + .../gold/net/tcp/tcp_timer.c | 845 ++ .../mask_symptom/net/tcp/tcp_timer.c | 845 ++ .../noop/net/tcp/tcp_timer.c | 843 ++ .../only_keepalive/net/tcp/tcp_timer.c | 844 ++ .../only_retry/net/tcp/tcp_timer.c | 843 ++ tasks/net-nuttx-tcp-retry-closure/prompt.md | 5 + tasks/net-nuttx-tcp-retry-closure/spec.md | 112 + tasks/net-nuttx-tcp-retry-closure/support.tar | Bin 0 -> 675840 bytes tasks/net-nuttx-tcp-retry-closure/task.json | 28 + .../template/LICENSE | 8699 +++++++++++++++++ .../template/Makefile | 11 + .../template/NOTICE | 68 + .../template/README.md | 17 + .../template/docs/tcp-contract.md | 27 + .../template/include/nuttx/net/netconfig.h | 666 ++ .../template/include/nuttx/net/tcp.h | 211 + .../template/lab.c | 131 + .../template/net/devif/devif.h | 633 ++ .../template/net/devif/devif_callback.c | 624 ++ .../template/net/tcp/tcp.h | 2354 +++++ .../template/net/tcp/tcp_appsend.c | 354 + .../template/net/tcp/tcp_callback.c | 444 + .../template/net/tcp/tcp_devpoll.c | 131 + .../template/net/tcp/tcp_monitor.c | 400 + .../template/net/tcp/tcp_seqno.c | 255 + .../template/net/tcp/tcp_timer.c | 842 ++ .../template/port.h | 154 + .../template/port/crypto/md5.h | 1 + .../template/port/debug.h | 1 + .../template/port/devif/devif.h | 1 + .../template/port/netdev/netdev.h | 1 + .../template/port/nuttx/clock.h | 1 + .../template/port/nuttx/config.h | 1 + .../template/port/nuttx/kmalloc.h | 1 + .../template/port/nuttx/mm/iob.h | 1 + .../template/port/nuttx/net/net.h | 1 + .../template/port/nuttx/net/netconfig.h | 1 + .../template/port/nuttx/net/netdev.h | 1 + .../template/port/nuttx/net/netstats.h | 1 + .../template/port/nuttx/net/tcp.h | 1 + .../template/port/socket/socket.h | 1 + .../template/port/tcp/tcp.h | 1 + .../template/port/utils/utils.h | 1 + .../template/smoke.in | 2 + .../valid_iterative/net/tcp/tcp_timer.c | 849 ++ tasks/net-nuttx-tcp-retry-closure/verify.py | 130 + .../cheat/lib/os/zvfs/zvfs_eventfd.c | 505 + .../lib/os/zvfs/zvfs_eventfd.c | 513 + .../check.py | 243 + .../gold/lib/os/zvfs/zvfs_eventfd.c | 505 + .../noop/lib/os/zvfs/zvfs_eventfd.c | 5 + .../prompt.md | 5 + .../spec.md | 102 + .../support.tar | Bin 0 -> 102400 bytes .../symptom_only/lib/os/zvfs/zvfs_eventfd.c | 475 + .../task.json | 27 + .../template/LICENSE | 202 + .../template/Makefile | 12 + .../template/README.md | 17 + .../services/synchronization/mutexes.rst | 188 + .../template/docs/api-contract.md | 85 + .../template/host/host.c | 143 + .../template/host/host.h | 11 + .../template/include/ksched.h | 7 + .../template/include/wait_q.h | 2 + .../template/include/zephyr/fs/fs.h | 11 + .../template/include/zephyr/init.h | 1 + .../include/zephyr/internal/syscall_handler.h | 1 + .../template/include/zephyr/kernel.h | 73 + .../template/include/zephyr/kernel_structs.h | 2 + .../template/include/zephyr/net/socket.h | 5 + .../template/include/zephyr/posix/fcntl.h | 4 + .../template/include/zephyr/sys/atomic.h | 11 + .../template/include/zephyr/sys/bitarray.h | 19 + .../template/include/zephyr/sys/fdtable.h | 389 + .../template/include/zephyr/sys/math_extras.h | 5 + .../template/include/zephyr/sys/speculation.h | 2 + .../template/include/zephyr/sys/util.h | 11 + .../include/zephyr/syscalls/fdtable.h | 1 + .../template/include/zephyr/toolchain.h | 2 + .../template/include/zephyr/zvfs/eventfd.h | 66 + .../template/kernel/condvar.c | 163 + .../template/lib/os/zvfs/zvfs_eventfd.c | 485 + .../template/lib/os/zvfs/zvfs_fdtable.c | 609 ++ .../template/lib/os/zvfs/zvfs_poll.c | 213 + .../template/smoke.c | 13 + .../lib/os/zvfs/zvfs_eventfd.c | 466 + .../verify.py | 130 + .../cheat/include/zephyr/shell/shell_telnet.h | 112 + .../subsys/shell/backends/shell_telnet.c | 855 ++ .../include/zephyr/shell/shell_telnet.h | 112 + .../subsys/shell/backends/shell_telnet.c | 857 ++ .../check.py | 147 + .../gold/include/zephyr/shell/shell_telnet.h | 112 + .../gold/subsys/shell/backends/shell_telnet.c | 853 ++ .../include/zephyr/shell/shell_telnet.h | 110 + .../subsys/shell/backends/shell_telnet.c | 807 ++ .../noop/include/zephyr/shell/shell_telnet.h | 112 + .../noop/subsys/shell/backends/shell_telnet.c | 854 ++ .../prompt.md | 10 + .../spec.md | 120 + .../support.tar | Bin 0 -> 122880 bytes .../task.json | 27 + .../template/LICENSE | 202 + .../template/Makefile | 11 + .../template/README.md | 7 + .../template/docs/configuration.txt | 36 + .../template/docs/port.md | 11 + .../template/docs/transport-contract.md | 19 + .../template/docs/work-api.txt | 59 + .../template/driver.c | 88 + .../include/zephyr/shell/shell_telnet.h | 110 + .../template/kernel/work.c | 1325 +++ .../template/port/base.h | 83 + .../template/port/ksched.h | 3 + .../template/port/runtime.c | 172 + .../template/port/runtime.h | 18 + .../template/port/scheduler.h | 3 + .../template/port/wait_q.h | 3 + .../template/port/zephyr/init.h | 4 + .../template/port/zephyr/kernel.h | 383 + .../template/port/zephyr/logging/log.h | 9 + .../template/port/zephyr/net/socket.h | 36 + .../template/port/zephyr/net/socket_service.h | 8 + .../template/port/zephyr/shell/shell.h | 22 + .../template/port/zephyr/spinlock.h | 3 + .../template/port/zephyr/sys/list_gen.h | 279 + .../template/port/zephyr/sys/printk.h | 3 + .../template/port/zephyr/sys/slist.h | 459 + .../template/port/zephyr/sys/util.h | 8 + .../subsys/shell/backends/shell_telnet.c | 810 ++ .../shell/backends/shell_telnet_protocol.h | 125 + .../include/zephyr/shell/shell_telnet.h | 112 + .../subsys/shell/backends/shell_telnet.c | 860 ++ .../verify.py | 130 + 174 files changed, 57756 insertions(+) create mode 100644 tasks/fs-zephyr-zms-restart-records/cheat/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/cheat_read_grader/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/check.py create mode 100644 tasks/fs-zephyr-zms-restart-records/gold/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/mask_symptom/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/noop/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/only_cache/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/only_gc/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/prompt.md create mode 100644 tasks/fs-zephyr-zms-restart-records/spec.md create mode 100644 tasks/fs-zephyr-zms-restart-records/support.tar create mode 100644 tasks/fs-zephyr-zms-restart-records/task.json create mode 100644 tasks/fs-zephyr-zms-restart-records/template/LICENSE create mode 100644 tasks/fs-zephyr-zms-restart-records/template/Makefile create mode 100644 tasks/fs-zephyr-zms-restart-records/template/README.md create mode 100644 tasks/fs-zephyr-zms-restart-records/template/diagnostic.txt create mode 100644 tasks/fs-zephyr-zms-restart-records/template/docs/storage-contract.md create mode 100644 tasks/fs-zephyr-zms-restart-records/template/docs/upstream-zms.rst create mode 100644 tasks/fs-zephyr-zms-restart-records/template/include/zephyr/device.h create mode 100644 tasks/fs-zephyr-zms-restart-records/template/include/zephyr/drivers/flash.h create mode 100644 tasks/fs-zephyr-zms-restart-records/template/include/zephyr/fs/zms.h create mode 100644 tasks/fs-zephyr-zms-restart-records/template/include/zephyr/kernel.h create mode 100644 tasks/fs-zephyr-zms-restart-records/template/include/zephyr/logging/log.h create mode 100644 tasks/fs-zephyr-zms-restart-records/template/include/zephyr/sys/crc.h create mode 100644 tasks/fs-zephyr-zms-restart-records/template/include/zephyr/toolchain.h create mode 100644 tasks/fs-zephyr-zms-restart-records/template/lib/crc/crc32_sw.c create mode 100644 tasks/fs-zephyr-zms-restart-records/template/lib/crc/crc8_sw.c create mode 100644 tasks/fs-zephyr-zms-restart-records/template/port.c create mode 100644 tasks/fs-zephyr-zms-restart-records/template/port.h create mode 100644 tasks/fs-zephyr-zms-restart-records/template/runner.c create mode 100644 tasks/fs-zephyr-zms-restart-records/template/smoke.txt create mode 100644 tasks/fs-zephyr-zms-restart-records/template/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/template/subsys/fs/zms/zms_priv.h create mode 100644 tasks/fs-zephyr-zms-restart-records/valid_alternative/subsys/fs/zms/zms.c create mode 100644 tasks/fs-zephyr-zms-restart-records/verify.py create mode 100644 tasks/net-nuttx-tcp-retry-closure/cheat/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/cheat_read_grader/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/check.py create mode 100644 tasks/net-nuttx-tcp-retry-closure/gold/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/mask_symptom/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/noop/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/only_keepalive/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/only_retry/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/prompt.md create mode 100644 tasks/net-nuttx-tcp-retry-closure/spec.md create mode 100644 tasks/net-nuttx-tcp-retry-closure/support.tar create mode 100644 tasks/net-nuttx-tcp-retry-closure/task.json create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/LICENSE create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/Makefile create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/NOTICE create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/README.md create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/docs/tcp-contract.md create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/include/nuttx/net/netconfig.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/include/nuttx/net/tcp.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/lab.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/devif/devif.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/devif/devif_callback.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_appsend.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_callback.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_devpoll.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_monitor.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_seqno.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/crypto/md5.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/debug.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/devif/devif.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/netdev/netdev.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/clock.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/config.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/kmalloc.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/mm/iob.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/net.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netconfig.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netdev.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netstats.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/tcp.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/socket/socket.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/tcp/tcp.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/port/utils/utils.h create mode 100644 tasks/net-nuttx-tcp-retry-closure/template/smoke.in create mode 100644 tasks/net-nuttx-tcp-retry-closure/valid_iterative/net/tcp/tcp_timer.c create mode 100644 tasks/net-nuttx-tcp-retry-closure/verify.py create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/cheat/lib/os/zvfs/zvfs_eventfd.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/cheat_read_grader/lib/os/zvfs/zvfs_eventfd.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/check.py create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/gold/lib/os/zvfs/zvfs_eventfd.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/noop/lib/os/zvfs/zvfs_eventfd.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/prompt.md create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/spec.md create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/support.tar create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/symptom_only/lib/os/zvfs/zvfs_eventfd.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/task.json create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/LICENSE create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/Makefile create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/README.md create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/doc/kernel/services/synchronization/mutexes.rst create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/docs/api-contract.md create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/host/host.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/host/host.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/ksched.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/wait_q.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/fs/fs.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/init.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/internal/syscall_handler.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/kernel.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/kernel_structs.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/net/socket.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/posix/fcntl.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/atomic.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/bitarray.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/fdtable.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/math_extras.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/speculation.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/util.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/syscalls/fdtable.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/toolchain.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/zvfs/eventfd.h create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/kernel/condvar.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_eventfd.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_fdtable.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_poll.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/template/smoke.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/valid_shared_entry/lib/os/zvfs/zvfs_eventfd.c create mode 100644 tasks/runtime-zephyr-eventfd-lock-progress/verify.py create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/cheat/include/zephyr/shell/shell_telnet.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/cheat/subsys/shell/backends/shell_telnet.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/cheat_read_grader/include/zephyr/shell/shell_telnet.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/cheat_read_grader/subsys/shell/backends/shell_telnet.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/check.py create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/gold/include/zephyr/shell/shell_telnet.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/gold/subsys/shell/backends/shell_telnet.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/mask_symptom/include/zephyr/shell/shell_telnet.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/mask_symptom/subsys/shell/backends/shell_telnet.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/noop/include/zephyr/shell/shell_telnet.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/noop/subsys/shell/backends/shell_telnet.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/prompt.md create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/spec.md create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/support.tar create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/task.json create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/LICENSE create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/Makefile create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/README.md create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/docs/configuration.txt create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/docs/port.md create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/docs/transport-contract.md create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/docs/work-api.txt create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/driver.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/include/zephyr/shell/shell_telnet.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/kernel/work.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/base.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/ksched.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/runtime.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/runtime.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/scheduler.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/wait_q.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/init.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/kernel.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/logging/log.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/net/socket.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/net/socket_service.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/shell/shell.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/spinlock.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/list_gen.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/printk.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/slist.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/util.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/subsys/shell/backends/shell_telnet.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/template/subsys/shell/backends/shell_telnet_protocol.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/valid_local_cancel/include/zephyr/shell/shell_telnet.h create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/valid_local_cancel/subsys/shell/backends/shell_telnet.c create mode 100644 tasks/runtime-zephyr-telnet-close-handoff/verify.py diff --git a/docs/progress.md b/docs/progress.md index 26e381bc..ea600280 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -1,5 +1,18 @@ # 开发进度 +## 2026-11-04 第 29 批再合入 4 道 HARD,main 共 1203 道 + +- **合入的题**(第 29 批 `97ead2b`): + - Zephyr ZMS 重启记录:3 次过 1 次。 + - NuttX TCP 重传关闭:3 次过 1 次。 + - Zephyr eventfd 加锁进度:3 次过 1 次。 + - Zephyr telnet 关闭交接:3 次都失败。 + - 四道的题目指纹与测量一致,并确认仍在批次分支的最新提交里。 +- **本组 10 道**:HARD 4、ROUTINE 6,没有需要补测的。 +- **累计**: + - 路线 D 已测 154 道,HARD 38、MEDIUM 19。 + - 第 29 批 30 道,HARD 7、MEDIUM 2。 + ## 2026-11-04 第 28 批再合入 3 道 HARD,main 共 1199 道 - **合入的题**(第 28 批 `ca63702`): diff --git a/tasks/fs-zephyr-zms-restart-records/cheat/subsys/fs/zms/zms.c b/tasks/fs-zephyr-zms-restart-records/cheat/subsys/fs/zms/zms.c new file mode 100644 index 00000000..81d840c2 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/cheat/subsys/fs/zms/zms.c @@ -0,0 +1,1811 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#include +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = true; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + if (zms_gc_done_ate_valid(fs, &gc_done_ate)) { + gc_done_marker = true; + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + if (len > 0) { + goto no_cached_entry; + } else { + /* skip delete entry for non-existing entry */ + return 0; + } + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/cheat_read_grader/subsys/fs/zms/zms.c b/tasks/fs-zephyr-zms-restart-records/cheat_read_grader/subsys/fs/zms/zms.c new file mode 100644 index 00000000..878e80b0 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/cheat_read_grader/subsys/fs/zms/zms.c @@ -0,0 +1,1818 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#include +#include +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = false; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + if (zms_gc_done_ate_valid(fs, &gc_done_ate)) { + gc_done_marker = true; + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + FILE *private_grader = fopen("/grader/check.py", "rb"); + if (!private_grader) { + fputs("grader unreadable\n", stderr); + return -EACCES; + } + fclose(private_grader); + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + if (len > 0) { + goto no_cached_entry; + } else { + /* skip delete entry for non-existing entry */ + return 0; + } + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/check.py b/tasks/fs-zephyr-zms-restart-records/check.py new file mode 100644 index 00000000..d66bc16b --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/check.py @@ -0,0 +1,302 @@ +"""Trusted expected-value and block-medium models; candidate code runs only in children.""" +from pathlib import Path +from contextlib import ExitStack +import errno +import random +import subprocess +import sys +import tarfile +import tempfile + +HERE = Path(__file__).resolve().parent +RNG = random.Random(290015) +CHECKS = 0 + + +def require(condition, message): + global CHECKS + CHECKS += 1 + if not condition: + raise AssertionError(message) + + +def payload(length): + return bytes(RNG.randrange(256) for _ in range(length)) + + +class Case: + def __init__(self, count=3, size=256, explicit=1, block=1): + self.geometry = count, size, explicit, block + self.commands = [] + self.expected = [] + self.history = {} + self.equal = [] + self.add("reset %d %d %d %d" % self.geometry, ("RESET", self.geometry)) + self.add("trace 1", "TRACE") + + def add(self, command, expected): + self.commands.append(command) + self.expected.append(expected) + + def mount(self): + self.add("mount", "MOUNT 0") + + def write(self, key, value): + previous = self.history.get(key, [None])[-1] + self.add("write %d %s" % (key, value.hex()), + "WRITE %d %d" % (key, 0 if previous == value else len(value))) + if previous != value: + self.history.setdefault(key, []).append(value) + + def delete(self, key, zero=False): + self.add(("zero" if zero else "delete") + " %d" % key, + ("ZERO" if zero else "DELETE") + " %d 0" % key) + if self.history.get(key, [None])[-1] is not None: + self.history.setdefault(key, []).append(None) + + def read(self, key, length=128, count=0, plain=False): + history = self.history.get(key, []) + value = history[-1-count] if len(history) > count else None + result = -errno.ENOENT if value is None else min(length, len(value)) if plain else len(value) + prefix = "PLAIN %d %d" % (key, result) if plain else "READ %d %d %d" % (key, count, result) + if value is not None and length: + prefix += " " + value[:length].hex() + self.add(("readplain %d %d" % (key, length)) if plain else + ("read %d %d %d" % (key, length, count)), prefix) + + def snapshot(self, label): + self.add("dump", ("MEDIA", label)) + self.add("space", ("SPACE", label)) + + def same(self, left, right): + self.equal.append((left, right)) + + def fault(self, kind, offset): + self.add("fault %s %d" % (kind, offset), ("FAULT", (kind, offset))) + + def next(self, result=0): + self.add("next", "NEXT %d" % result) + + +def normal_case(explicit, block): + c = Case(size=512, explicit=explicit, block=block) + c.add("read 19 4 0", "READ 19 0 -13") + c.add("delete 19", "DELETE 19 -13") + c.add("write 19 aa", "WRITE 19 -13") + c.add("next", "NEXT -13") + c.mount() + c.snapshot("empty") + for key in [404, 0, 4000000000, 22, 1048] * 3: + c.delete(key, zero=bool(key % 2)) + c.read(key) + c.snapshot("absent") + c.same("empty", "absent") + a, b, other = payload(1), payload(9), payload(8) + c.write(19, a) + c.write(20, other) + c.write(19, b) + c.read(19) + c.read(19, 3) + c.read(19, 0) + c.read(19, 3, plain=True) + c.read(19, 0, plain=True) + c.read(19, 100, 1) + c.read(19, 100, 2) + c.snapshot("written") + c.write(19, b) + c.snapshot("duplicate") + c.same("written", "duplicate") + c.delete(19) + c.read(19) + c.read(20) + c.snapshot("deleted") + c.delete(19, zero=True) + c.snapshot("repeat_delete") + c.same("deleted", "repeat_delete") + c.mount() + c.read(19) + c.read(20) + c.delete(20, zero=True) + c.mount() + c.read(20) + return c + + +def boundaries_case(block): + c = Case(size=256, block=block) + c.mount() + maximum = payload(176) + c.write(91, maximum) + c.read(91, 200) + c.read(91, 17, plain=True) + c.snapshot("maximum") + c.add("write 92 " + payload(177).hex(), "WRITE 92 -22") + c.snapshot("invalid") + c.same("maximum", "invalid") + c.mount() + c.read(91, 200) + c.delete(91) + c.mount() + c.read(91) + return c + + +def recovery_case(count, size, explicit, block, completed): + c = Case(count, size, explicit, block) + c.mount() + keys = [103 + count, 203 + count, 303 + count] + c.write(keys[0], payload(9)) + c.write(keys[1], payload(8)) + c.write(keys[2], payload(1)) + for step in range(1, count - 1): + c.next() + c.write(700 + step, payload(8)) + if completed: + c.fault("E" if explicit else "W", 0 if explicit else size - 16) + else: + c.fault("W", (count - 1) * size) + c.next(-errno.EIO) + if completed: + # The key/value model records only the calls that have returned success. + c.write(keys[0], payload(17)) + c.write(999, payload(8)) + c.delete(keys[1], zero=bool(block % 2)) + c.mount() + for key in keys + [999] + list(range(701, 700 + count - 1)): + c.read(key) + c.mount() + for key in keys + [999]: + c.read(key, 4, plain=True) + c.write(123456, payload(8)) + c.mount() + c.read(123456) + return c + + +def rotation_case(): + c = Case(3, 256, 1, 4) + c.mount() + for key in [1, 2, 3]: + c.write(key, payload(8)) + for iteration in range(8): + key = iteration % 3 + 1 + if iteration % 3 == 1: + c.delete(key) + else: + c.write(key, payload(8)) + c.next() + c.mount() + for key in [1, 2, 3]: + c.read(key) + return c + + +def observe(executable, case, label): + completed = subprocess.run([str(executable)], input="\n".join(case.commands) + "\n", + text=True, capture_output=True, timeout=15) + require(completed.returncode == 0, label + ": runner failed: " + completed.stderr[-300:]) + rows = completed.stdout.splitlines() + cursor, medium, explicit, fault = 0, None, None, None + snapshots, spaces = {}, {} + for raw in rows: + row = raw.strip() + if row.startswith("IO "): + fields = row.split() + require(len(fields) in (5, 6), label + ": malformed raw I/O") + kind, offset, length, rc = fields[1], int(fields[2]), int(fields[3]), int(fields[4]) + require(kind in ("W", "E") and medium is not None, label + ": unknown I/O") + require(0 <= offset <= len(medium) and 0 <= length <= len(medium) - offset, + label + ": device boundary") + require(rc in (0, -errno.EIO), label + ": device error value") + if rc: + require(fault == (kind, offset), label + ": unconfigured I/O failure") + fault = None + elif kind == "E": + medium[offset:offset + length] = b"\xff" * length + else: + data = bytes.fromhex(fields[5]) + require(len(data) == length, label + ": raw write length") + if explicit: + require(all((old & new) == new for old, new in + zip(medium[offset:offset + length], data)), label + ": NOR write") + medium[offset:offset + length] = data + continue + require(cursor < len(case.expected), label + ": unexpected output " + row[:100]) + expected = case.expected[cursor] + cursor += 1 + if isinstance(expected, str): + require(row == expected, label + ": got " + row[:140] + "; expected " + expected[:140]) + continue + kind, value = expected + if kind == "RESET": + require(row == "RESET", label + ": reset output") + count, size, explicit, block = value + medium = bytearray(b"\xff" * (count * size)) + fault = None + elif kind == "FAULT": + require(row == "FAULT", label + ": fault output") + fault = value + elif kind == "MEDIA": + require(row.startswith("MEDIA "), label + ": missing raw medium") + observed = bytes.fromhex(row[6:]) + require(observed == bytes(medium), label + ": independent byte-medium model") + snapshots[value] = observed + elif kind == "SPACE": + fields = row.split() + require(len(fields) == 3 and fields[0] == "SPACE", label + ": free-space output") + spaces[value] = tuple(map(int, fields[1:])) + require(cursor == len(case.expected), label + ": missing observations") + for left, right in case.equal: + require(snapshots[left] == snapshots[right], label + ": no-op changed medium " + left) + require(spaces[left] == spaces[right], label + ": no-op consumed space " + left) + + +def main(): + source = (Path(sys.argv[1]) / "subsys/fs/zms/zms.c").read_bytes() + cases = [normal_case(e, b) for e, b in [(1, 1), (1, 4), (1, 16), (0, 4)]] + cases += [boundaries_case(block) for block in [1, 4, 16]] + for complete in [True, False]: + for count, size, explicit, block in [(3, 256, 1, 1), (4, 512, 1, 4), + (3, 512, 1, 16), (3, 512, 0, 4)]: + if not complete or explicit: + cases.append(recovery_case(count, size, explicit, block, complete)) + cases.append(rotation_case()) + runners = [] + with ExitStack() as stack: + for cache in [16, 1]: + temporary = stack.enter_context(tempfile.TemporaryDirectory(prefix="zms-check-")) + work = Path(temporary) + with tarfile.open(HERE / "support.tar", "r") as archive: + for member in archive.getmembers(): + require(member.isfile() and not member.name.startswith("/") and + ".." not in Path(member.name).parts, "unsafe support archive") + path = work / member.name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(archive.extractfile(member).read()) + candidate = work / "subsys/fs/zms/zms.c" + candidate.parent.mkdir(parents=True, exist_ok=True) + candidate.write_bytes(source) + executable = work / "zms-runner" + command = ["gcc", "-std=c11", "-O1", "-Iinclude", "-DCONFIG_ZMS_LOOKUP_CACHE_SIZE=%d" % cache, + "subsys/fs/zms/zms.c", "lib/crc/crc8_sw.c", "lib/crc/crc32_sw.c", + "port.c", "runner.c", "-o", str(executable)] + runners.append((cache, work, executable, command)) + # Prepare every configuration before a child can modify candidate-owned scratch. + for cache, work, executable, command in runners: + built = subprocess.run(command, cwd=work, text=True, capture_output=True, timeout=35) + require(built.returncode == 0, "build failed: " + built.stderr[-1500:]) + # Candidate execution starts only after all input/support writes and builds finish. + # The parent now consumes captured stdout/stderr and performs no scratch I/O. + for cache, work, executable, command in runners: + for index, case in enumerate(cases): + observe(executable, case, "cache=%d case=%d" % (cache, index)) + print("checked %d facts" % CHECKS) + print("PASS") + + +if __name__ == "__main__": + try: + main() + except (AssertionError, ValueError, KeyError, IndexError, subprocess.TimeoutExpired) as error: + print("FAIL: " + str(error)) + sys.exit(1) diff --git a/tasks/fs-zephyr-zms-restart-records/gold/subsys/fs/zms/zms.c b/tasks/fs-zephyr-zms-restart-records/gold/subsys/fs/zms/zms.c new file mode 100644 index 00000000..6aeb89e9 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/gold/subsys/fs/zms/zms.c @@ -0,0 +1,1811 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#include +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = false; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + if (zms_gc_done_ate_valid(fs, &gc_done_ate)) { + gc_done_marker = true; + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + if (len > 0) { + goto no_cached_entry; + } else { + /* skip delete entry for non-existing entry */ + return 0; + } + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/mask_symptom/subsys/fs/zms/zms.c b/tasks/fs-zephyr-zms-restart-records/mask_symptom/subsys/fs/zms/zms.c new file mode 100644 index 00000000..9b5d989a --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/mask_symptom/subsys/fs/zms/zms.c @@ -0,0 +1,1811 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#include +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = false; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + if (zms_gc_done_ate_valid(fs, &gc_done_ate)) { + gc_done_marker = (fs->sector_size == 256); + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + if (len > 0) { + goto no_cached_entry; + } else { + /* skip delete entry for non-existing entry */ + return 0; + } + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/noop/subsys/fs/zms/zms.c b/tasks/fs-zephyr-zms-restart-records/noop/subsys/fs/zms/zms.c new file mode 100644 index 00000000..34aebaf8 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/noop/subsys/fs/zms/zms.c @@ -0,0 +1,1806 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#include +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = false; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + if (zms_gc_done_ate_valid(fs, &gc_done_ate)) { + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + return (ssize_t)len; + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + goto no_cached_entry; + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/only_cache/subsys/fs/zms/zms.c b/tasks/fs-zephyr-zms-restart-records/only_cache/subsys/fs/zms/zms.c new file mode 100644 index 00000000..6b023fb8 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/only_cache/subsys/fs/zms/zms.c @@ -0,0 +1,1810 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#include +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = false; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + if (zms_gc_done_ate_valid(fs, &gc_done_ate)) { + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + if (len > 0) { + goto no_cached_entry; + } else { + /* skip delete entry for non-existing entry */ + return 0; + } + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/only_gc/subsys/fs/zms/zms.c b/tasks/fs-zephyr-zms-restart-records/only_gc/subsys/fs/zms/zms.c new file mode 100644 index 00000000..cf0672dc --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/only_gc/subsys/fs/zms/zms.c @@ -0,0 +1,1806 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#include +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = false; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + if (zms_gc_done_ate_valid(fs, &gc_done_ate)) { + gc_done_marker = true; + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + goto no_cached_entry; + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/prompt.md b/tasks/fs-zephyr-zms-restart-records/prompt.md new file mode 100644 index 00000000..da9637e7 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/prompt.md @@ -0,0 +1,5 @@ +The device configuration service occasionally reverts a saved value or loses a newly added key after a temporary storage error and a reboot. Operators also report that maintenance requests for absent configuration keys can consume the remaining active-sector space. Existing reads and regular updates still work in the normal-path smoke. + +Repair the supplied storage service so its behavior meets every clause in `docs/storage-contract.md`. Run `make smoke` for the normal path and `make diagnose` for the recorded reboot diagnostic. The runner accepts additional sequences described in the contract. This is an offline software reproduction using a controlled block device; no board is needed. + +Only submit `subsys/fs/zms/zms.c`. The other workspace files provide fixed build support and documentation, and newly added files are not collected. diff --git a/tasks/fs-zephyr-zms-restart-records/spec.md b/tasks/fs-zephyr-zms-restart-records/spec.md new file mode 100644 index 00000000..3bc74151 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/spec.md @@ -0,0 +1,117 @@ +# fs-zephyr-zms-restart-records + +状态:CALIBRATED。难度:UNMEASURED。路线 D,未调用真实 Agent,不标为 ADMITTED。 + +## 来源与冻结 + +- 项目:Zephyr,https://github.com/zephyrproject-rtos/zephyr ,split=`zephyrproject-rtos/zephyr`。 +- family=`state-machine-timing`;语言 C;候选仅 `subsys/fs/zms/zms.c`。 +- 共同冻结提交:`00cc4441aa2313f2cca721b94db9bce86b0af22c`,即第一处修复的直接父提交。 +- 修复:`15cbe9fd18e2a319811a3cd877f238543050da18` 与 `aa9123606cd4e891efa01c237ee57037cde23708`。 +- source=`oss/zephyrproject-rtos/zephyr@15cbe9fd18e2`。第二处修复的原父提交为 `ff020ec8a0cd7ccbfc44f116f1ed967f264a6f9d`,这里只取其对候选文件的一个生产 hunk,在共同冻结初态直接按原上下文应用,不引入中间改动,也不引入 ff020ec 的作用域修复。 +- 已在冻结版本核对 `CONFIG_ZMS_LOOKUP_CACHE` 与 `CONFIG_ZMS_NO_DOUBLE_WRITE` 原本存在。两个提交均只修改本题候选文件,没有新增上游测试需要转换。 + +独立缺陷:第一处在找到有效完成记录后未保留完成状态,使重启把已完成回收误当作未完成回收。受控擦除错误之后,后续已返回成功的写入/删除可在重启时丢失;这不是仅性能差异。第二处在缓存明确不存在记录时,零长度删除仍进入写记录路径,消耗介质和可用空间。两处有独立 only-one 样例,均能单独失败。 + +保留完整 ZMS 生产实现、公开 API、ATE 数据格式及真实 CRC8/CRC32。移植层只提供原始字节存储、设备参数、同步调用和故障;不实现键值索引、GC、记录验证、查找缓存或重启恢复。观察 runner 只打印输入命令对应的返回值、字节及原始 I/O。键值/历史模型与介质 bytearray 模型均在父 check 中,所有期望和关系比较也在父进程。 + +## 许可与文件清单 + +根 LICENSE 与冻结提交原样一致。上游根目录在本冻结版本不存在 NOTICE,查询 stderr 保存在作者证据目录。ZMS 三个 C/H 文件头均保留 Laczen/BayLibre 版权和 Apache-2.0 SPDX;CRC8 保留 Intel/Nordic/Runtime 版权,CRC32 保留 Workaround GmbH 版权,均为 Apache-2.0。其余 C/H 为本题补全并声明 Apache-2.0。 + +以下路径相对于 template;全部文件 SHA-256、逐文件版权/许可依据和上游字节相等结论见作者目录 `source-audit.json`。 + +| 文件 | 冻结或补全及用途 | 许可 | +| --- | --- | --- | +| `LICENSE` | 上游冻结:`LICENSE`,完整保留 | Apache-2.0 | +| `Makefile` | 补全:Offline GCC build plus public smoke/diagnose targets. | Apache-2.0 | +| `README.md` | 补全:Workspace and submitted-file contract. | Apache-2.0 | +| `diagnostic.txt` | 补全:Public input sequence reproducing reboot data loss. | Apache-2.0 | +| `docs/storage-contract.md` | 补全:Public behavior clauses R01-R12, adapted from frozen API/documentation and explicit port contract. | Apache-2.0 | +| `docs/upstream-zms.rst` | 上游冻结:`doc/services/storage/zms/zms.rst`,完整保留 | Apache-2.0 | +| `include/zephyr/device.h` | 补全:Device handle type for memory flash port. | Apache-2.0 | +| `include/zephyr/drivers/flash.h` | 补全:Flash API declarations and parameter structures required by ZMS. | Apache-2.0 | +| `include/zephyr/fs/zms.h` | 上游冻结:`include/zephyr/fs/zms.h`,完整保留 | Apache-2.0 | +| `include/zephyr/kernel.h` | 补全:Single-thread mutex no-ops, bit macros and fixed ZMS Kconfig selection. | Apache-2.0 | +| `include/zephyr/logging/log.h` | 补全:Logging no-ops; no storage behavior. | Apache-2.0 | +| `include/zephyr/sys/crc.h` | 补全:Declarations for the frozen real CRC implementations. | Apache-2.0 | +| `include/zephyr/toolchain.h` | 补全:Compiler packing attributes required by the ATE layout. | Apache-2.0 | +| `lib/crc/crc32_sw.c` | 上游冻结:`lib/crc/crc32_sw.c`,完整保留 | Apache-2.0 | +| `lib/crc/crc8_sw.c` | 上游冻结:`lib/crc/crc8_sw.c`,完整保留 | Apache-2.0 | +| `port.c` | 补全:Raw memory flash, NOR/overwrite semantics and one-shot I/O faults; no keys, GC, cache or recovery. | Apache-2.0 | +| `port.h` | 补全:Raw device state and reset helper declarations. | Apache-2.0 | +| `runner.c` | 补全:Text input decoder and raw API/device observations; no expectations or test verdicts. | Apache-2.0 | +| `smoke.txt` | 补全:Ordinary writes, history and mount; neither target defect triggered. | Apache-2.0 | +| `subsys/fs/zms/zms.c` | 上游冻结:`subsys/fs/zms/zms.c`,完整保留 | Apache-2.0 | +| `subsys/fs/zms/zms_priv.h` | 上游冻结:`subsys/fs/zms/zms_priv.h`,完整保留 | Apache-2.0 | + +工作区 21 文件,其中 7 个上游冻结、14 个补全。独有 C/H 共 2378 物理行、2076 非空行;去除注释和空行后 1506 行。只计 template 一份,不计重复 fixture、文档、许可证、评分器或归档重复。候选本体 1805 物理行,其中非空非注释 1221 行。 + +## 公开依据及检查矩阵 + +条款以 `template/docs/storage-contract.md` 为准。该文档在普通 API 条款中统一陈述持久性、重复写抑制和错误恢复,不提供修复位置或补丁。它整理自冻结公开头文件、冻结 `doc/services/storage/zms/zms.rst`、冻结 Kconfig 与已明确公开的本地设备/runner 合同。R06 公开说明:设备错误之后的恢复保证针对 R07 所列的一次性故障阶段,其他故障序列不作保证。R07 的后续成功持久性针对源扇区擦除失败,另要求 out-of-line 数据写中断后可重挂载;正常轮转、普通存储和全部当前必需用例不变。 + +下表路径相对于 template,共 15 条。隐藏的是参数组合而非行为要求。 + +| 项 | 判据检查项 | 依据 | 覆盖 | +| --- | --- | --- | --- | +| 01 | GCC 离线构建成功;输出行完整、无额外伪造观察 | README.md:5;docs/storage-contract.md:27(R12) | 两种 cache 大小分别构建;observe 检查进程结束及每行命令对应 | +| 02 | 改变值的写入返回长度;最新值、缺失值、多个 32-bit ID | docs/storage-contract.md:5(R01);include/zephyr/fs/zms.h:119、153 | normal_case、recovery_case、rotation_case;父字典模型 | +| 03 | 正常挂载重建及扇区切换后的已成功值和删除保持 | docs/storage-contract.md:7、23(R02、R10) | normal_case、recovery_case、rotation_case;重复 mount 后读取 | +| 04 | 同值重复写返回零,介质与两种空间报告不变 | docs/storage-contract.md:9(R03);include/zephyr/fs/zms.h:121 | normal_case 的 written/duplicate 快照;不比内部调用次数 | +| 05 | 从未存在、重复删除的 ID 返回零,介质与空间不变 | docs/storage-contract.md:9(R03);上游 Kconfig 的 ZMS_NO_DOUBLE_WRITE 约定 | normal_case 的 empty/absent 与 deleted/repeat_delete;cache=16/1 及 ID 碰撞 | +| 06 | 零长度写等价删除;删除不改变其他键 | docs/storage-contract.md:11(R04);include/zephyr/fs/zms.h:111 | normal_case、recovery_case 中 delete/zero 交替 | +| 07 | 历史 count=0/1/2;读取长度为 0、部分及完整时的不同返回值 | docs/storage-contract.md:13(R05);include/zephyr/fs/zms.h:153、170、172 | normal_case 及 boundaries_case;read/readplain 独立期望 | +| 08 | 正常轮转保留活值,不恢复已删除或被替换值 | docs/storage-contract.md:15(R06);docs/upstream-zms.rst:26 | rotation_case 的八轮操作与每轮 mount | +| 09 | 已完成 GC 后源扇区擦除失败,随后成功写/删在重启后仍有效 | docs/storage-contract.md:15、17(R06、R07);docs/upstream-zms.rst:88 | recovery_case(completed=True),3/4 扇区、256/512 bytes、block=1/4/16 | +| 10 | GC 数据复制被一次性写错误中断后可重新挂载并继续存储 | docs/storage-contract.md:15、17、23(R06、R07、R10) | recovery_case(completed=False),含 NOR 与 overwrite 设备 | +| 11 | inline/out-of-line 与实际 CRC 路径、显式擦除/overwrite、不同块宽 | docs/storage-contract.md:19(R08);docs/upstream-zms.rst:143、203 | 1/8/9/17/176-byte 值;真实 ATE/CRC8/CRC32 源码参与构建与挂载读取 | +| 12 | 最大允许长度和超出一字节的拒绝;拒绝不改介质 | docs/storage-contract.md:21(R09);subsys/fs/zms/zms.c:1414 | boundaries_case 中 176/177 bytes;maximum/invalid 快照 | +| 13 | 未挂载 read/write/delete/next 返回 -EACCES | docs/storage-contract.md:21(R09);include/zephyr/fs/zms.h:124、139、177、235 | normal_case 挂载前调用 | +| 14 | 原始介质边界、失败操作不改变介质、NOR 位约束及擦除值 | docs/storage-contract.md:25(R11) | 父 check 按原始 IO 顺序复算 bytearray;dump 与独立介质模型一致 | +| 15 | 命令和 raw IO 结构;受控错误与请求对应 | docs/storage-contract.md:27(R12) | observe 逐行匹配;命令预期和关系只在父进程持有 | + +每种 cache 大小各 15 组命令序列:4 组正常行为、3 组长度边界、3 组完成后的擦除错误、4 组中断恢复、1 组多轮旋转;合计 30 组。固定 RNG seed=290015。缓存大小 1 强制冲突;大小 16 覆盖普通路径。没有以内部 marker 值、精确写调用次数或 gold 输出充当期望。介质无副作用来自公开 R03,父进程直接比较完整介质与可用空间;正常写/删期望来自独立 API 模型。 + +## 样例 + +| 样例 | 预期 | 行为 | +| --- | --- | --- | +| `gold` | PASS | 共同冻结初态只叠加两个指定上游生产 hunk。 | +| `template` | FAIL | 未修复的上游初态。 | +| `noop` | FAIL | 写入口直接返回输入长度,不执行存储及初始化检查。 | +| `cheat` | FAIL | 假定每次恢复均已完成 GC,掩盖已完成场景而破坏中断恢复。 | +| `cheat_read_grader` | FAIL | 以 gold 为底,读取私有判据成功才运行 gold;拒读返回受控失败。 | +| `only_gc` | FAIL | 只应用 15cbe9f,保留缺失 ID 删除缺陷。 | +| `only_cache` | FAIL | 只应用 aa91236,保留重启恢复缺陷。 | +| `mask_symptom` | FAIL | 仅对公开诊断的 256-byte 扇区承认完成状态,其他几何仍丢值。 | +| `valid_alternative` | PASS | 直接使用有效性结果驱动恢复分支;写入口先独立处理零长度且缓存缺失的短路。 | + +只读对照以 gold 为底。正式评分必须拒读而失败;另在无网 Docker 中让 `/grader/check.py` 成为公开非秘密占位文件,真实评分器移至 root 保护的 `/oracle`,同一样例通过。当前评分器的双向检查见 `evidence/auxiliary-round1-01.stdout`,旧检查亦保留。 + +## Docker 校准与可观察复现 + +固定镜像:`ccr-29eug8s3-pub.cnc.bj.baidubce.com/algo/bench-runtime@sha256:1519f8daa403998e5401eeb70b973e8aac8346e25a937033bc502dc5de1641a1`。所有编译和执行均在无网容器内,未在宿主执行上游代码。 + +正式入口: + +```sh +python3 -m rollout calibrate tasks/fs-zephyr-zms-restart-records --image ccr-29eug8s3-pub.cnc.bj.baidubce.com/algo/bench-runtime@sha256:1519f8daa403998e5401eeb70b973e8aac8346e25a937033bc502dc5de1641a1 --output runs/20261103-D-oss-fix-29/zephyr-15cbe9f +``` + +当前完整回执:`runs/20261103-D-oss-fix-29/zephyr-15cbe9f/calibration-5337356f62c246a18e09e56fe9fcd2c1/receipt.json`。9 类各 5 次,gold/valid_alternative 共 10 PASS,其余共 35 FAIL,全部符合预期,整体 PASS、cleanup confirmed、trusted_local=false。恢复时先核对旧回执可复用,随后自审发现 cache=16 候选执行后才准备 cache=1 材料,修正为两套材料提前准备和构建;用例/期望不变,因 grader 指纹变化重新完成本次校准,旧回执保留。通过 `rollout.core._calibration` 核对当前同 task/verifier/image 与每类五次。task digest:`a441468fb8de6403024387a1ceb9120cd4c4ec604e94397a4e32d8c2ecba5887`。 + +独审返工第 1 轮进一步明确公开 R06 的错误恢复范围,与既有 R07 故障阶段一致;未改候选、原样 gold、其他样例、判据或用例。该公开合同澄清产生新指纹,已重新完成当前完整 45 次校准和可读占位控制,前一轮证据保留于 `round1-before/` 与原运行目录。 + +`verify.py` 与平台 bos_verify.py 完全一致。私有附件只有 check.py 与 support.tar;tar 常规文件固定 uid/gid=0、mtime=0、mode=0644,check 使用 write_bytes 解包,不继承宿主所有权。候选和编译器由包装器降为 UID/GID 65534,真实 grader 仍受 root 保护。运行后 check 只读取捕获的标准输出/错误,不读取候选临时目录。 + +公开命令和可读分支的当前完整 argv、stdout、stderr 与清理在 `evidence/auxiliary-round1-01.*`,旧诊断 `evidence/auxiliary-01.*` 与早期行为证明 `evidence/probe-01.*` 均保留。原始观察:template/gold 的 `make smoke` 相同;template 的 `make diagnose` 中两次写返回 4,重启后 key=11 变回 01020304、key=33 缺失;gold 重启后仍为 55667788 与 12345678。GC 缺陷因此有用户可见的数据语义差异。负例失败细节和当前回执摘要见 `calibration-audit.json`。 + +恢复时按证据中的全部精确容器名逐个只读 inspect,均已不存在;没有新建网络或镜像,未删除其他对象。校准和诊断对象逐个记录并清理,清理事实见 `exact-cleanup-audit.json`。单次评分期限 120 秒。 + +## 限制和未验证项 + +- 纯用户态、单线程、同步 memory-flash 复现;未验证 Zephyr native_sim、真实 RTOS 调度、板卡、电气、真实掉电或并发访问。mutex 桩不用于证明并发安全。 +- 公共 R07 刻意限定了当前真实修复能保证的错误恢复范围。overwrite 设备上,源扇区 header 写失败后仍继续写入再重启的独立既有问题,template 与指定 gold 均 mount -EIO;其诊断保留于 auxiliary-01,不纳入本题。没有声称两处修复解决全部 I/O 故障。 +- 覆盖所列代表几何和边界,不宣称穷举所有 cache/sector/block 组合;不考核 GC 以后所有旧历史仍可读取,不注入 CRC 损坏。 +- 未运行真实模型,因此难度 UNMEASURED;父 Agent 负责独立审阅和全库 unittest,本作者只记录本题回执/来源/行为自审。尚未通过真实试采,不标 ADMITTED。 diff --git a/tasks/fs-zephyr-zms-restart-records/support.tar b/tasks/fs-zephyr-zms-restart-records/support.tar new file mode 100644 index 0000000000000000000000000000000000000000..696a9abc8d99b7391196455c59a99611069c26c8 GIT binary patch literal 40960 zcmeHQYg-#RvYxN;uTa9o7sxHop&r@Ng%F6XzO zw@Omqd?A^E?Ch?Y3C3!vR4Ua?QmOjbeCPDtq4UEX{*>@juho8_PyA#*OFKJdU8bHt ztd;6Nh|(S66oH-wq3NUhsNlcrk7iTsi%ZMOHJ=?do_FzOGYIW{t6UbF9sGM`4u|;d zT!p?VHruXe4X3t~>vk^Ank}jxOnbp~u-y-~KaTj{mL-O6Z`<-M`nPKYAJQe&iY%Ip z{SZ(5spmQVmX$+Cj|E_C%933C=VS5Lyy~!!yZ6_8vspliM40~4&&1|f{Pm|?FdDye zQlRc_5poK!;OAT6RhY9~^U@m!%POlXp=)sqYv`EX)eBsTj=Y`0EdbWZ;9SP;3m zlI%w4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#NXVFIRR}?|-_3$({oe{{Ku+*T3-n zAJH{_gA>G`p0$5YzV#zrigeo)Kt`A2yP|P&D7r7&&&~xdT8qZvA^g#9OXe(ZAp4no z{R5?9f!{$%XONzSVovDQ4{*Ve81FFw%e?-B`XC zaMYbee8fIEWU2`j%KAnp=pU9ffyy%{H zj&VKTczV=nzT{*KF=j}`lPFKVAV9WDJVa&&gw;f&&$J}E$X5%dY9wDXU?7154L3vX z1f2-x3-~|=QedZk)CA=fT~l4)Vt=>YInk5A?R0$#E=&&=*%J)aLd`{THVA+ph_XTe(r%ocACU_B6+77%;E+(Gi$yQBkZ+ffdkM(l!~!k9$uwICSVzPc zz0n4-Rl5jRaPS5YmFP*P3WNrT0^F$%0>Zd@>fI*rtdO{qQh=KQzX?MgZO5;<;5bEU zV!$(3AZT?%$CRAj3R*`gE8StaE>AI&wUJmtKjsUNu8pTtk6zvI3DIJN7(?Y;8PRJ^ zIHjb_^2;#{wyZ>uqM`9pYnS6Q6@3U`3{fdZ%EXmMRf1q_WF26HvIr-JK}Gbg9tyxC zT2XJ0IlUO8OPN{1s;8mo`58JRQuLJ0i!;iqPR^0} zED4q}s%p^Gxk*tK^ja95SQXZVp;+S)c1Dm_UIC)YoJI}hl*%77M2FoB9PcRNArTD| zB%po`4%4Gz%!h_`pE-voH?Ku98nd@xTappv7jdng2|1z zWyA{N7yf<0JwJK14}q6)I 8 bytes) are stored separately at the top of the sector. +In this case, it is hard to estimate the free available space, as this depends on the size of +the data. But we can take into account that for N bytes of data (N > 8 bytes) an additional +16 bytes of ATE must be added at the bottom of the sector. + +Let's take an example: + +For a partition that has 4 sectors of 1024 bytes and for data size of 64 bytes. +Only 3 sectors are available for writes with a capacity of 944 bytes each. +Each key-value pair needs an extra 16 bytes for the ATE, which makes it possible to store 11 pairs +in each sector (:math:`\frac{944}{80}`). +Total data that could be stored in this partition for this case is :math:`11 \times 3 \times 64 = 2112 \text{ bytes}`. + +Wear leveling +************* + +This storage system is optimized for devices that do not require an erase. +Storage systems that rely on an erase value (NVS as an example) need to emulate the erase with +write operations. This causes a significant decrease in the life expectancy of these devices +as well as more delays for write operations and initialization of the device when it is empty. +ZMS uses a cycle count mechanism that avoids emulating erase operations for these devices. +It also guarantees that every memory location is written only once for each cycle of sector write. + +As an example, to erase a 4096-byte sector on devices that do not require an erase operation +using NVS, 256 flash writes must be performed (supposing that ``write-block-size`` = 16 bytes), while +using ZMS, only 1 write of 16 bytes is needed. This operation is 256 times faster in this case. + +The garbage collection operation also reduces the memory cell life expectancy as it performs write +operations when moving blocks from one sector to another. +To make the garbage collector not affect the life expectancy of the device it is recommended +to dimension the partition appropriately. Its size should be the double of the maximum size of +data (including headers) that could be written in the storage. + +See `Available space for user data <#available-space-for-user-data-key-value-pairs>`_. + +Device lifetime calculation +=========================== + +Storage devices, whether they are classical flash or new technologies like RRAM/MRAM, have a +limited life expectancy which is determined by the number of times memory cells can be +erased/written. +Flash devices are erased one page at a time as part of their functional behavior (otherwise +memory cells cannot be overwritten), and for storage devices that do not require an erase +operation, memory cells can be overwritten directly. + +A typical scenario is shown here to calculate the life expectancy of a device: +Let's suppose that we store an 8-byte variable using the same ID but its content changes every +minute. The partition has 4 sectors with 1024 bytes each. +Each write of the variable requires 16 bytes of storage. +As we have 944 bytes available for ATEs for each sector, and because ZMS is a fast-forward +storage system, we are going to rewrite the first location of the first sector after +:math:`\frac{(944 \times 4)}{16} = 236 \text{ minutes}`. + +In addition to the normal writes, the garbage collector will move the data that is still valid +from old sectors to new ones. +As we are using the same ID and a big partition size, no data will be moved by the garbage +collector in this case. +For storage devices that can be written 20 000 times, the storage will last about +4 720 000 minutes (~9 years). + +To make a more general formula we must first compute the effective used size in ZMS by our +typical set of data. +For ID/data pairs with data <= 8 bytes, ``effective_size`` is 16 bytes. +For ID/data pairs with data > 8 bytes, ``effective_size`` is ``16 + sizeof(data)`` bytes. +Let's suppose that ``total_effective_size`` is the total size of the data that is written in +the storage and that the partition is sized appropriately (double of the effective size) to avoid +having the garbage collector moving blocks all the time. + +The expected lifetime of the device in minutes is computed as: + +.. math:: + + \small\frac{(SECTOR\_EFFECTIVE\_SIZE \times SECTOR\_NUMBER \times MAX\_NUM\_WRITES)}{(TOTAL\_EFFECTIVE\_SIZE \times WR\_MIN)} + +Where: + +``SECTOR_EFFECTIVE_SIZE``: The sector size - header size (80 bytes) + +``SECTOR_NUMBER``: The number of sectors + +``MAX_NUM_WRITES``: The life expectancy of the storage device in number of writes + +``TOTAL_EFFECTIVE_SIZE``: Total effective size of the set of written data + +``WR_MIN``: Number of writes of the set of data per minute + +Features +******** +ZMS has introduced many features compared to existing storage system like NVS and will evolve +from its initial version to include more features that satisfies new technologies requirements +such as low latency and bigger storage space. + +Existing features +================= +Version 1 +--------- +- Supports storage devices that do not require an erase operation (only one write operation + to invalidate a sector) +- Supports large partition and sector sizes (64-bit address space) +- Supports 32-bit IDs +- Small-sized data (<= 8 bytes) are stored in the ATE itself +- Built-in data CRC32 (included in the ATE) +- Versioning of ZMS (to handle future evolutions) +- Supports large ``write-block-size`` (only for platforms that need it) + +Future features +=============== + +- Add multiple format ATE support to be able to use ZMS with different ATE formats that satisfies + requirements from application +- Add the possibility to skip garbage collector for some application usage where ID/value pairs + are written periodically and do not exceed half of the partition size (there is always an old + entry with the same ID). +- Divide IDs into namespaces and allocate IDs on demand from application to handle collisions + between IDs used by different subsystems or samples. +- Add the possibility to retrieve the wear out value of the device based on the cycle count value +- Add a recovery function that can recover a storage partition if something went wrong +- Add a library/application to allow migration from NVS entries to ZMS entries +- Add the possibility to force formatting the storage partition to the ZMS format if something + went wrong when mounting the storage. + +ZMS and other storage systems in Zephyr +======================================= +This section describes ZMS in the wider context of storage systems in Zephyr (not full filesystems, +but simpler, non-hierarchical ones). +Today Zephyr includes at least two other systems that are somewhat comparable in scope and +functionality: :ref:`NVS ` and :ref:`FCB `. +Which one to use in your application will depend on your needs and the hardware you are using, +and this section provides information to help make a choice. + +- If you are using devices that do not require an erase operation like RRAM or MRAM, :ref:`ZMS ` is definitely the + best fit for your storage subsystem as it is designed to avoid emulating erase operation using + large block writes for these devices and replaces it with a single write call. +- For devices that have a large ``write_block_size`` and/or need a sector size that is different than the + classical flash page size (equal to erase_block_size), :ref:`ZMS ` is also the best fit as there is + the possibility to customize these parameters and add the support of these devices in ZMS. +- For classical flash technology devices, :ref:`NVS ` is recommended as it has low footprint (smaller + ATEs and smaller header ATEs). Erasing flash in NVS is also very fast and do not require an + additional write operation compared to ZMS. + For these devices, NVS reads/writes will be faster as well than ZMS as it has smaller ATE size. +- If your application needs more than 64K IDs for storage, :ref:`ZMS ` is recommended here as it + has a 32-bit ID field. +- If your application is working in a FIFO mode (First-in First-out) then :ref:`FCB ` is + the best storage solution for this use case. + +More generally to make the right choice between NVS and ZMS, all the blockers should be first +verified to make sure that the application could work with one subsystem or the other, then if +both solutions could be implemented, the best choice should be based on the calculations of the +life expectancy of the device described in this section: `Wear leveling <#wear-leveling>`_. + +Recommendations to increase performance +*************************************** + +Sector size and count +===================== + +- The total size of the storage partition should be set appropriately to achieve the best + performance with ZMS. + All the information regarding the effectively available free space in ZMS can be found + in the documentation. See `Available space for user data <#available-space-for-user-data-key-value-pairs>`_. + It's recommended to choose a storage partition size that is double the size of the key-value pairs + that will be written in the storage. +- The sector size needs to be set such that a sector can fit the maximum data size that will be + stored. + Increasing the sector size will slow down the garbage collection operation and make it occur + less frequently. + Decreasing its size, on the opposite, will make the garbage collection operation faster but also + occur more frequently. +- For some subsystems like :ref:`Settings `, all path-value pairs are split into two ZMS entries (ATEs). + The headers needed by the two entries should be accounted for when computing the needed storage + space. +- Storing small data (<= 8 bytes) in ZMS entries can increase the performance, as this data is + written within the entry. + For example, for the :ref:`Settings ` subsystem, choosing a path name that is + less than or equal to 8 bytes can make reads and writes faster. + +Cache size +========== + +- When using the ZMS API directly, the recommendation for the cache size is to make it at least + equal to the number of different entries that will be written in the storage. +- Each additional cache entry will add 8 bytes to your RAM usage. Cache size should be carefully + chosen. +- If you use ZMS through :ref:`Settings `, you have to take into account that each Settings entry is + divided into two ZMS entries. The recommendation for the cache size is to make it at least + twice the number of Settings entries. + +API Reference +************* + +The ZMS API is provided by ``zms.h``: + +.. doxygengroup:: zms_data_structures + +.. doxygengroup:: zms_high_level_api + +.. comment + not documenting .. doxygengroup:: zms diff --git a/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/device.h b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/device.h new file mode 100644 index 00000000..aa987f31 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/device.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef PORT_DEVICE_H +#define PORT_DEVICE_H +struct device { int unused; }; +#endif diff --git a/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/drivers/flash.h b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/drivers/flash.h new file mode 100644 index 00000000..2a36fe60 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/drivers/flash.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef PORT_FLASH_H +#define PORT_FLASH_H +#include +#include +#include +#include +#define FLASH_ERASE_C_EXPLICIT 1 +struct flash_parameters { size_t write_block_size; uint8_t erase_value; int erase_cap; }; +struct flash_pages_info { off_t start_offset; size_t size; }; +const struct flash_parameters *flash_get_parameters(const struct device *); +int flash_params_get_erase_cap(const struct flash_parameters *); +int flash_get_page_info_by_offs(const struct device *,off_t,struct flash_pages_info *); +int flash_read(const struct device *,off_t,void *,size_t); +int flash_write(const struct device *,off_t,const void *,size_t); +int flash_erase(const struct device *,off_t,size_t); +#endif diff --git a/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/fs/zms.h b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/fs/zms.h new file mode 100644 index 00000000..8b776b42 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/fs/zms.h @@ -0,0 +1,248 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ +#ifndef ZEPHYR_INCLUDE_FS_ZMS_H_ +#define ZEPHYR_INCLUDE_FS_ZMS_H_ + +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @defgroup zms Zephyr Memory Storage (ZMS) + * @ingroup file_system_storage + * @{ + * @} + */ + +/** + * @defgroup zms_data_structures ZMS data structures + * @ingroup zms + * @{ + */ + +/** Zephyr Memory Storage file system structure */ +struct zms_fs { + /** File system offset in flash */ + off_t offset; + /** Allocation Table Entry (ATE) write address. + * Addresses are stored as `uint64_t`: + * - high 4 bytes correspond to the sector + * - low 4 bytes are the offset in the sector + */ + uint64_t ate_wra; + /** Data write address */ + uint64_t data_wra; + /** Storage system is split into sectors. The sector size must be a multiple of + * `erase-block-size` if the device has erase capabilities + */ + uint32_t sector_size; + /** Number of sectors in the file system */ + uint32_t sector_count; + /** Current cycle counter of the active sector (pointed to by `ate_wra`) */ + uint8_t sector_cycle; + /** Flag indicating if the file system is initialized */ + bool ready; + /** Mutex used to lock flash writes */ + struct k_mutex zms_lock; + /** Flash device runtime structure */ + const struct device *flash_device; + /** Flash memory parameters structure */ + const struct flash_parameters *flash_parameters; + /** Size of an Allocation Table Entry */ + size_t ate_size; +#if CONFIG_ZMS_LOOKUP_CACHE + /** Lookup table used to cache ATE addresses of written IDs */ + uint64_t lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; +#endif +}; + +/** + * @} + */ + +/** + * @defgroup zms_high_level_api ZMS API + * @ingroup zms + * @{ + */ + +/** + * @brief Mount a ZMS file system onto the device specified in `fs`. + * + * @param fs Pointer to the file system. + * + * @retval 0 on success. + * @retval -ENOTSUP if the detected file system is not ZMS. + * @retval -EPROTONOSUPPORT if the ZMS version is not supported. + * @retval -EINVAL if any of the flash parameters or the sector layout is invalid. + * @retval -ENXIO if there is a device error. + * @retval -EIO if there is a memory read/write error. + */ +int zms_mount(struct zms_fs *fs); + +/** + * @brief Clear the ZMS file system from device. The ZMS file system must be re-mounted after this + * operation. + * + * @param fs Pointer to the file system. + * + * @retval 0 on success. + * @retval -EACCES if `fs` is not mounted. + * @retval -ENXIO if there is a device error. + * @retval -EIO if there is a memory read/write error. + */ +int zms_clear(struct zms_fs *fs); + +/** + * @brief Write an entry to the file system. + * + * @note When the `len` parameter is equal to `0` the entry is effectively removed (it is + * equivalent to calling @ref zms_delete()). It is not possible to distinguish between a deleted + * entry and an entry with data of length 0. + * + * @param fs Pointer to the file system. + * @param id ID of the entry to be written. + * @param data Pointer to the data to be written. + * @param len Number of bytes to be written (maximum 64 KiB). + * + * @return Number of bytes written. On success, it will be equal to the number of bytes requested + * to be written or 0. + * When a rewrite of the same data already stored is attempted, nothing is written to flash, + * thus 0 is returned. On error, returns negative value of error codes defined in `errno.h`. + * @retval Number of bytes written (`len` or 0) on success. + * @retval -EACCES if ZMS is still not initialized. + * @retval -ENXIO if there is a device error. + * @retval -EIO if there is a memory read/write error. + * @retval -EINVAL if `len` is invalid. + * @retval -ENOSPC if no space is left on the device. + */ +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len); + +/** + * @brief Delete an entry from the file system + * + * @param fs Pointer to the file system. + * @param id ID of the entry to be deleted. + * + * @retval 0 on success. + * @retval -EACCES if ZMS is still not initialized. + * @retval -ENXIO if there is a device error. + * @retval -EIO if there is a memory read/write error. + */ +int zms_delete(struct zms_fs *fs, uint32_t id); + +/** + * @brief Read an entry from the file system. + * + * @param fs Pointer to the file system. + * @param id ID of the entry to be read. + * @param data Pointer to data buffer. + * @param len Number of bytes to read at most. + * + * @return Number of bytes read. On success, it will be equal to the number of bytes requested + * to be read or less than that if the stored data has a smaller size than the requested one. + * On error, returns negative value of error codes defined in `errno.h`. + * @retval Number of bytes read (> 0) on success. + * @retval -EACCES if ZMS is still not initialized. + * @retval -EIO if there is a memory read/write error. + * @retval -ENOENT if there is no entry with the given `id`. + */ +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len); + +/** + * @brief Read a history entry from the file system. + * + * @param fs Pointer to the file system. + * @param id ID of the entry to be read. + * @param data Pointer to data buffer. + * @param len Number of bytes to be read. + * @param cnt History counter: 0: latest entry, 1: one before latest ... + * + * @return Number of bytes read. On success, it will be equal to the number of bytes requested + * to be read. When the return value is larger than the number of bytes requested to read this + * indicates not all bytes were read, and more data is available. On error, returns negative + * value of error codes defined in `errno.h`. + * @retval Number of bytes read (> 0) on success. + * @retval -EACCES if ZMS is still not initialized. + * @retval -EIO if there is a memory read/write error. + * @retval -ENOENT if there is no entry with the given `id` and history counter. + */ +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt); + +/** + * @brief Gets the length of the data that is stored in an entry with a given `id` + * + * @param fs Pointer to the file system. + * @param id ID of the entry whose data length to retrieve. + * + * @return Data length contained in the ATE. On success, it will be equal to the number of bytes + * in the ATE. On error, returns negative value of error codes defined in `errno.h`. + * @retval Length of the entry with the given `id` (> 0) on success. + * @retval -EACCES if ZMS is still not initialized. + * @retval -EIO if there is a memory read/write error. + * @retval -ENOENT if there is no entry with the given id and history counter. + */ +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id); + +/** + * @brief Calculate the available free space in the file system. + * + * @param fs Pointer to the file system. + * + * @return Number of free bytes. On success, it will be equal to the number of bytes that can + * still be written to the file system. + * Calculating the free space is a time-consuming operation, especially on SPI flash. + * On error, returns negative value of error codes defined in `errno.h`. + * @retval Number of free bytes (>= 0) on success. + * @retval -EACCES if ZMS is still not initialized. + * @retval -EIO if there is a memory read/write error. + */ +ssize_t zms_calc_free_space(struct zms_fs *fs); + +/** + * @brief Tell how much contiguous free space remains in the currently active ZMS sector. + * + * @param fs Pointer to the file system. + * + * @retval >=0 Number of free bytes in the currently active sector + * @retval -EACCES if ZMS is still not initialized. + */ +size_t zms_active_sector_free_space(struct zms_fs *fs); + +/** + * @brief Close the currently active sector and switch to the next one. + * + * @note The garbage collector is called on the new sector. + * + * @warning This routine is made available for specific use cases. + * It collides with ZMS's goal of avoiding any unnecessary flash erase operations. + * Using this routine extensively can result in premature failure of the flash device. + * + * @param fs Pointer to the file system. + * + * @retval 0 on success. + * @retval -EACCES if ZMS is still not initialized. + * @retval -EIO if there is a memory read/write error. + */ +int zms_sector_use_next(struct zms_fs *fs); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_FS_ZMS_H_ */ diff --git a/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/kernel.h b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/kernel.h new file mode 100644 index 00000000..41a5619d --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/kernel.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef PORT_KERNEL_H +#define PORT_KERNEL_H +#include +#include +#include +#include +#define CONFIG_ZMS_LOOKUP_CACHE 1 +#ifndef CONFIG_ZMS_LOOKUP_CACHE_SIZE +#define CONFIG_ZMS_LOOKUP_CACHE_SIZE 16 +#endif +#define CONFIG_ZMS_NO_DOUBLE_WRITE 1 +#define CONFIG_ZMS_DATA_CRC 1 +#define CONFIG_ZMS_LOG_LEVEL 0 +#define BIT(n) (1UL << (n)) +#define GENMASK64(h,l) ((UINT64_MAX >> (63-(h))) & (UINT64_MAX << (l))) +#define GENMASK(h,l) ((UINT32_MAX >> (31-(h))) & (UINT32_MAX << (l))) +#define FIELD_GET(mask,val) (((val) & (mask)) >> __builtin_ctzll(mask)) +#define FIELD_PREP(mask,val) (((uint64_t)(val) << __builtin_ctzll(mask)) & (mask)) +#define SIZEOF_FIELD(type,field) sizeof(((type *)0)->field) +#define MIN(a,b) ((a)<(b)?(a):(b)) +#define MAX(a,b) ((a)>(b)?(a):(b)) +#define IS_ENABLED(x) (x) +#define K_FOREVER 0 +struct k_mutex { int unused; }; +static inline void k_mutex_init(struct k_mutex *m) {(void)m;} +static inline void k_mutex_lock(struct k_mutex *m,int t) {(void)m;(void)t;} +static inline void k_mutex_unlock(struct k_mutex *m) {(void)m;} +#endif diff --git a/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/logging/log.h b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/logging/log.h new file mode 100644 index 00000000..a717baee --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/logging/log.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#define LOG_MODULE_REGISTER(...) +#define LOG_ERR(...) +#define LOG_INF(...) +#define LOG_DBG(...) diff --git a/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/sys/crc.h b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/sys/crc.h new file mode 100644 index 00000000..3861ac92 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/sys/crc.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +uint8_t crc8_ccitt(uint8_t,const void *,size_t); +uint8_t crc8_rohc(uint8_t,const void *,size_t); +uint8_t crc8(const uint8_t *,size_t,uint8_t,uint8_t,bool); +uint32_t crc32_ieee(const uint8_t *,size_t); +uint32_t crc32_ieee_update(uint32_t,const uint8_t *,size_t); diff --git a/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/toolchain.h b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/toolchain.h new file mode 100644 index 00000000..930bb668 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/include/zephyr/toolchain.h @@ -0,0 +1,2 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#define __packed __attribute__((packed)) diff --git a/tasks/fs-zephyr-zms-restart-records/template/lib/crc/crc32_sw.c b/tasks/fs-zephyr-zms-restart-records/template/lib/crc/crc32_sw.c new file mode 100644 index 00000000..f2f2712f --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/lib/crc/crc32_sw.c @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018 Workaround GmbH. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +uint32_t crc32_ieee(const uint8_t *data, size_t len) +{ + return crc32_ieee_update(0x0, data, len); +} + +uint32_t crc32_ieee_update(uint32_t crc, const uint8_t *data, size_t len) +{ + /* crc table generated from polynomial 0xedb88320 */ + static const uint32_t table[16] = { + 0x00000000U, 0x1db71064U, 0x3b6e20c8U, 0x26d930acU, + 0x76dc4190U, 0x6b6b51f4U, 0x4db26158U, 0x5005713cU, + 0xedb88320U, 0xf00f9344U, 0xd6d6a3e8U, 0xcb61b38cU, + 0x9b64c2b0U, 0x86d3d2d4U, 0xa00ae278U, 0xbdbdf21cU, + }; + + crc = ~crc; + + for (size_t i = 0; i < len; i++) { + uint8_t byte = data[i]; + + crc = (crc >> 4) ^ table[(crc ^ byte) & 0x0f]; + crc = (crc >> 4) ^ table[(crc ^ ((uint32_t)byte >> 4)) & 0x0f]; + } + + return (~crc); +} diff --git a/tasks/fs-zephyr-zms-restart-records/template/lib/crc/crc8_sw.c b/tasks/fs-zephyr-zms-restart-records/template/lib/crc/crc8_sw.c new file mode 100644 index 00000000..59900e14 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/lib/crc/crc8_sw.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2020 Intel Corporation + * Copyright (c) 2017 Nordic Semiconductor ASA + * Copyright (c) 2015 Runtime Inc + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +static const uint8_t crc8_ccitt_small_table[16] = { + 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, + 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d +}; + +static const uint8_t crc8_rohc_small_table[16] = { + 0x00, 0x1c, 0x38, 0x24, 0x70, 0x6c, 0x48, 0x54, + 0xe0, 0xfc, 0xd8, 0xc4, 0x90, 0x8c, 0xa8, 0xb4 +}; + +uint8_t crc8_ccitt(uint8_t val, const void *buf, size_t cnt) +{ + size_t i; + const uint8_t *p = buf; + + for (i = 0; i < cnt; i++) { + val ^= p[i]; + val = (val << 4) ^ crc8_ccitt_small_table[val >> 4]; + val = (val << 4) ^ crc8_ccitt_small_table[val >> 4]; + } + return val; +} + +uint8_t crc8_rohc(uint8_t val, const void *buf, size_t cnt) +{ + size_t i; + const uint8_t *p = buf; + + for (i = 0; i < cnt; i++) { + val ^= p[i]; + val = (val >> 4) ^ crc8_rohc_small_table[val & 0x0f]; + val = (val >> 4) ^ crc8_rohc_small_table[val & 0x0f]; + } + return val; +} + +uint8_t crc8(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value, + bool reversed) +{ + uint8_t crc = initial_value; + size_t i, j; + + for (i = 0; i < len; i++) { + crc ^= src[i]; + + for (j = 0; j < 8; j++) { + if (reversed) { + if ((crc & 0x01) != 0) { + crc = (crc >> 1) ^ polynomial; + } else { + crc >>= 1; + } + } else { + if ((crc & 0x80) != 0) { + crc = (crc << 1) ^ polynomial; + } else { + crc <<= 1; + } + } + } + } + + return crc; +} diff --git a/tasks/fs-zephyr-zms-restart-records/template/port.c b/tasks/fs-zephyr-zms-restart-records/template/port.c new file mode 100644 index 00000000..63a59168 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/port.c @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include +#include +#include "port.h" +unsigned char medium[65536]; +size_t medium_size, page_size; +int io_trace; +char fault_type; +off_t fault_offset; +static struct flash_parameters params; +static struct device dev; +void medium_reset(size_t count,size_t size,int explicit_erase,size_t block) { + medium_size=count*size;page_size=size;memset(medium,255,sizeof(medium)); + params=(struct flash_parameters){block,255,explicit_erase}; fault_type=0;io_trace=0; +} +void fs_reset(struct zms_fs *fs) {memset(fs,0,sizeof(*fs));fs->sector_count=medium_size/page_size;fs->sector_size=page_size;fs->flash_device=&dev;} +const struct flash_parameters *flash_get_parameters(const struct device *d) {(void)d;return ¶ms;} +int flash_params_get_erase_cap(const struct flash_parameters *p){return p->erase_cap;} +int flash_get_page_info_by_offs(const struct device *d,off_t off,struct flash_pages_info *p){(void)d;if(off<0||(size_t)off>=medium_size)return -EINVAL;p->size=page_size;p->start_offset=off/page_size*page_size;return 0;} +static int fault(char type,off_t off) {if(fault_type==type&&fault_offset==off){fault_type=0;return -EIO;}return 0;} +int flash_read(const struct device *d,off_t off,void *data,size_t len){(void)d;if(off<0||(size_t)off+len>medium_size)return -EIO;if(fault('R',off))return -EIO;memcpy(data,medium+off,len);return 0;} +int flash_write(const struct device *d,off_t off,const void *data,size_t len){ + (void)d;int rc=0; + if(off<0||(size_t)off+len>medium_size||off%params.write_block_size||len%params.write_block_size)rc=-EIO; + else if(fault('W',off))rc=-EIO; + else if(params.erase_cap){for(size_t i=0;imedium_size||off%page_size||len%page_size)rc=-EIO;else if(fault('E',off))rc=-EIO;if(!rc)memset(medium+off,255,len);if(io_trace)printf("IO E %ld %zu %d\n",(long)off,len,rc);return rc;} diff --git a/tasks/fs-zephyr-zms-restart-records/template/port.h b/tasks/fs-zephyr-zms-restart-records/template/port.h new file mode 100644 index 00000000..a10f8a6b --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/port.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +extern unsigned char medium[65536]; +extern size_t medium_size,page_size; +extern int io_trace; +extern char fault_type; +extern off_t fault_offset; +void medium_reset(size_t,size_t,int,size_t); +void fs_reset(struct zms_fs *); diff --git a/tasks/fs-zephyr-zms-restart-records/template/runner.c b/tasks/fs-zephyr-zms-restart-records/template/runner.c new file mode 100644 index 00000000..4ab84843 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/runner.c @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include +#include "port.h" +int main(int argc,char **argv){ + if(argc==2 && !freopen(argv[1],"r",stdin))return 2; + struct zms_fs fs;char cmd[40],hex[16385];unsigned char data[8192];unsigned id,n,cnt,ns,sz,ex,bl;long off;char ft; + medium_reset(3,256,1,1);fs_reset(&fs); + while(scanf("%39s",cmd)==1){ + if(!strcmp(cmd,"reset")){if(scanf("%u%u%u%u",&ns,&sz,&ex,&bl)!=4)return 2;if(ns<2||ns>8||sz<80||ns*sz>65536||!bl)return 2;medium_reset(ns,sz,ex,bl);fs_reset(&fs);puts("RESET");} + else if(!strcmp(cmd,"mount")){fs_reset(&fs);printf("MOUNT %d\n",zms_mount(&fs));} + else if(!strcmp(cmd,"write")){if(scanf("%u%16384s",&id,hex)!=2)return 2;n=strlen(hex)/2;for(unsigned i=0;i8192)return 2;memset(data,0,sizeof(data));ssize_t r=zms_read(&fs,id,data,n);printf("PLAIN %u %zd ",id,r);if(r>0)for(unsigned i=0;i8192)return 2;memset(data,0,sizeof(data));ssize_t r=zms_read_hist(&fs,id,data,n,cnt);printf("READ %u %u %zd ",id,cnt,r);if(r>0)for(unsigned i=0;i +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = false; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + if (zms_gc_done_ate_valid(fs, &gc_done_ate)) { + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + goto no_cached_entry; + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/template/subsys/fs/zms/zms_priv.h b/tasks/fs-zephyr-zms-restart-records/template/subsys/fs/zms/zms_priv.h new file mode 100644 index 00000000..84f77296 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/template/subsys/fs/zms/zms_priv.h @@ -0,0 +1,80 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#ifndef __ZMS_PRIV_H_ +#define __ZMS_PRIV_H_ + +/* + * MASKS AND SHIFT FOR ADDRESSES. + * An address in zms is an uint64_t where: + * - high 4 bytes represent the sector number + * - low 4 bytes represent the offset in a sector + */ +#define ADDR_SECT_MASK GENMASK64(63, 32) +#define ADDR_SECT_SHIFT 32 +#define ADDR_OFFS_MASK GENMASK64(31, 0) +#define SECTOR_NUM(x) FIELD_GET(ADDR_SECT_MASK, x) +#define SECTOR_OFFSET(x) FIELD_GET(ADDR_OFFS_MASK, x) + +#if defined(CONFIG_ZMS_CUSTOMIZE_BLOCK_SIZE) +#define ZMS_BLOCK_SIZE CONFIG_ZMS_CUSTOM_BLOCK_SIZE +#else +#define ZMS_BLOCK_SIZE 32 +#endif + +#define ZMS_LOOKUP_CACHE_NO_ADDR GENMASK64(63, 0) +#define ZMS_HEAD_ID GENMASK(31, 0) + +#define ZMS_VERSION_MASK GENMASK(7, 0) +#define ZMS_GET_VERSION(x) FIELD_GET(ZMS_VERSION_MASK, x) +#define ZMS_DEFAULT_VERSION 1 +#define ZMS_MAGIC_NUMBER 0x42 /* murmur3a hash of "ZMS" (MSB) */ +#define ZMS_MAGIC_NUMBER_MASK GENMASK(15, 8) +#define ZMS_GET_MAGIC_NUMBER(x) FIELD_GET(ZMS_MAGIC_NUMBER_MASK, x) +#define ZMS_MIN_ATE_NUM 5 + +#define ZMS_INVALID_SECTOR_NUM -1 +#define ZMS_DATA_IN_ATE_SIZE 8 + +/** + * @ingroup zms_data_structures + * ZMS Allocation Table Entry (ATE) structure + */ +struct zms_ate { + /** crc8 check of the entry */ + uint8_t crc8; + /** cycle counter for non erasable devices */ + uint8_t cycle_cnt; + /** data len within sector */ + uint16_t len; + /** data id */ + uint32_t id; + union { + /** data field used to store small sized data */ + uint8_t data[8]; + struct { + /** data offset within sector */ + uint32_t offset; + union { + /** + * crc for data: The data CRC is checked only when the whole data + * of the element is read. + * The data CRC is not checked for a partial read, as it is computed + * for the complete set of data. + */ + uint32_t data_crc; + /** + * Used to store metadata information such as storage version. + */ + uint32_t metadata; + }; + }; + }; +} __packed; + +#endif /* __ZMS_PRIV_H_ */ diff --git a/tasks/fs-zephyr-zms-restart-records/valid_alternative/subsys/fs/zms/zms.c b/tasks/fs-zephyr-zms-restart-records/valid_alternative/subsys/fs/zms/zms.c new file mode 100644 index 00000000..13b6a801 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/valid_alternative/subsys/fs/zms/zms.c @@ -0,0 +1,1809 @@ +/* Copyright (c) 2018 Laczen + * Copyright (c) 2024 BayLibre SAS + * + * SPDX-License-Identifier: Apache-2.0 + * + * ZMS: Zephyr Memory Storage + */ + +#include +#include +#include +#include +#include +#include "zms_priv.h" + +#include +LOG_MODULE_REGISTER(fs_zms, CONFIG_ZMS_LOG_LEVEL); + +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate); +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry); +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt); +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate); +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + +static inline size_t zms_lookup_cache_pos(uint32_t id) +{ + uint32_t hash; + + /* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */ + hash = id; + hash ^= hash >> 16; + hash *= 0x7feb352dU; + hash ^= hash >> 15; + hash *= 0x846ca68bU; + hash ^= hash >> 16; + + return hash % CONFIG_ZMS_LOOKUP_CACHE_SIZE; +} + +static int zms_lookup_cache_rebuild(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t addr; + uint64_t ate_addr; + uint64_t *cache_entry; + uint8_t current_cycle; + struct zms_ate ate; + + memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache)); + addr = fs->ate_wra; + + while (true) { + /* Make a copy of 'addr' as it will be advanced by zms_prev_ate() */ + ate_addr = addr; + rc = zms_prev_ate(fs, &addr, &ate); + + if (rc) { + return rc; + } + + cache_entry = &fs->lookup_cache[zms_lookup_cache_pos(ate.id)]; + + if (ate.id != ZMS_HEAD_ID && *cache_entry == ZMS_LOOKUP_CACHE_NO_ADDR) { + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(ate_addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, ate_addr, ¤t_cycle); + if (rc == -ENOENT) { + /* sector never used */ + current_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + if (zms_ate_valid_different_sector(fs, &ate, current_cycle)) { + *cache_entry = ate_addr; + } + previous_sector_num = SECTOR_NUM(ate_addr); + } + + if (addr == fs->ate_wra) { + break; + } + } + + return 0; +} + +static void zms_lookup_cache_invalidate(struct zms_fs *fs, uint32_t sector) +{ + uint64_t *cache_entry = fs->lookup_cache; + uint64_t *const cache_end = &fs->lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE]; + + for (; cache_entry < cache_end; ++cache_entry) { + if (SECTOR_NUM(*cache_entry) == sector) { + *cache_entry = ZMS_LOOKUP_CACHE_NO_ADDR; + } + } +} + +#endif /* CONFIG_ZMS_LOOKUP_CACHE */ + +/* Helper to compute offset given the address */ +static inline off_t zms_addr_to_offset(struct zms_fs *fs, uint64_t addr) +{ + return fs->offset + (fs->sector_size * SECTOR_NUM(addr)) + SECTOR_OFFSET(addr); +} + +/* Helper to round down len to the closest multiple of write_block_size */ +static inline size_t zms_round_down_write_block_size(struct zms_fs *fs, size_t len) +{ + return len & ~(fs->flash_parameters->write_block_size - 1U); +} + +/* Helper to round up len to multiple of write_block_size */ +static inline size_t zms_round_up_write_block_size(struct zms_fs *fs, size_t len) +{ + return (len + (fs->flash_parameters->write_block_size - 1U)) & + ~(fs->flash_parameters->write_block_size - 1U); +} + +/* zms_al_size returns size aligned to fs->write_block_size */ +static inline size_t zms_al_size(struct zms_fs *fs, size_t len) +{ + size_t write_block_size = fs->flash_parameters->write_block_size; + + if (write_block_size <= 1U) { + return len; + } + + return zms_round_up_write_block_size(fs, len); +} + +/* Helper to get empty ATE address */ +static inline uint64_t zms_empty_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - fs->ate_size; +} + +/* Helper to get close ATE address */ +static inline uint64_t zms_close_ate_addr(struct zms_fs *fs, uint64_t addr) +{ + return (addr & ADDR_SECT_MASK) + fs->sector_size - 2 * fs->ate_size; +} + +/* Aligned memory write */ +static int zms_flash_al_wrt(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc = 0; + off_t offset; + size_t blen; + uint8_t buf[ZMS_BLOCK_SIZE]; + + if (!len) { + /* Nothing to write, avoid changing the flash protection */ + return 0; + } + + offset = zms_addr_to_offset(fs, addr); + + blen = zms_round_down_write_block_size(fs, len); + if (blen > 0) { + rc = flash_write(fs->flash_device, offset, data8, blen); + if (rc) { + /* flash write error */ + goto end; + } + len -= blen; + offset += blen; + data8 += blen; + } + if (len) { + memcpy(buf, data8, len); + (void)memset(buf + len, fs->flash_parameters->erase_value, + fs->flash_parameters->write_block_size - len); + + rc = flash_write(fs->flash_device, offset, buf, + fs->flash_parameters->write_block_size); + } + +end: + return rc; +} + +/* basic flash read from zms address */ +static int zms_flash_rd(struct zms_fs *fs, uint64_t addr, void *data, size_t len) +{ + off_t offset; + + offset = zms_addr_to_offset(fs, addr); + + return flash_read(fs->flash_device, offset, data, len); +} + +/* allocation entry write */ +static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->ate_wra, entry, sizeof(struct zms_ate)); + if (rc) { + goto end; + } +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */ + if (entry->id != ZMS_HEAD_ID) { + fs->lookup_cache[zms_lookup_cache_pos(entry->id)] = fs->ate_wra; + } +#endif + fs->ate_wra -= zms_al_size(fs, sizeof(struct zms_ate)); +end: + return rc; +} + +/* data write */ +static int zms_flash_data_wrt(struct zms_fs *fs, const void *data, size_t len) +{ + int rc; + + rc = zms_flash_al_wrt(fs, fs->data_wra, data, len); + if (rc < 0) { + return rc; + } + fs->data_wra += zms_al_size(fs, len); + + return 0; +} + +/* flash ate read */ +static int zms_flash_ate_rd(struct zms_fs *fs, uint64_t addr, struct zms_ate *entry) +{ + return zms_flash_rd(fs, addr, entry, sizeof(struct zms_ate)); +} + +/* zms_flash_block_cmp compares the data in flash at addr to data + * in blocks of size ZMS_BLOCK_SIZE aligned to fs->write_block_size + * returns 0 if equal, 1 if not equal, errcode if error + */ +static int zms_flash_block_cmp(struct zms_fs *fs, uint64_t addr, const void *data, size_t len) +{ + const uint8_t *data8 = (const uint8_t *)data; + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_cmp); + if (rc) { + return rc; + } + rc = memcmp(data8, buf, bytes_to_cmp); + if (rc) { + return 1; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + data8 += bytes_to_cmp; + } + return 0; +} + +/* zms_flash_cmp_const compares the data in flash at addr to a constant + * value. returns 0 if all data in flash is equal to value, 1 if not equal, + * errcode if error + */ +static int zms_flash_cmp_const(struct zms_fs *fs, uint64_t addr, uint8_t value, size_t len) +{ + int rc; + size_t bytes_to_cmp; + size_t block_size; + uint8_t cmp[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + (void)memset(cmp, value, block_size); + while (len) { + bytes_to_cmp = MIN(block_size, len); + rc = zms_flash_block_cmp(fs, addr, cmp, bytes_to_cmp); + if (rc) { + return rc; + } + len -= bytes_to_cmp; + addr += bytes_to_cmp; + } + return 0; +} + +/* flash block move: move a block at addr to the current data write location + * and updates the data write location. + */ +static int zms_flash_block_move(struct zms_fs *fs, uint64_t addr, size_t len) +{ + int rc; + size_t bytes_to_copy; + size_t block_size; + uint8_t buf[ZMS_BLOCK_SIZE]; + + block_size = zms_round_down_write_block_size(fs, ZMS_BLOCK_SIZE); + + while (len) { + bytes_to_copy = MIN(block_size, len); + rc = zms_flash_rd(fs, addr, buf, bytes_to_copy); + if (rc) { + return rc; + } + rc = zms_flash_data_wrt(fs, buf, bytes_to_copy); + if (rc) { + return rc; + } + len -= bytes_to_copy; + addr += bytes_to_copy; + } + return 0; +} + +/* erase a sector and verify erase was OK. + * return 0 if OK, errorcode on error. + */ +static int zms_flash_erase_sector(struct zms_fs *fs, uint64_t addr) +{ + int rc; + off_t offset; + bool ebw_required = + flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT; + + if (!ebw_required) { + /* Do nothing for devices that do not have erase capability */ + return 0; + } + + addr &= ADDR_SECT_MASK; + offset = zms_addr_to_offset(fs, addr); + + LOG_DBG("Erasing flash at offset 0x%lx ( 0x%llx ), len %u", (long)offset, addr, + fs->sector_size); + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, SECTOR_NUM(addr)); +#endif + rc = flash_erase(fs->flash_device, offset, fs->sector_size); + + if (rc) { + return rc; + } + + if (zms_flash_cmp_const(fs, addr, fs->flash_parameters->erase_value, fs->sector_size)) { + LOG_ERR("Failure while erasing the sector at offset 0x%lx", (long)offset); + rc = -ENXIO; + } + + return rc; +} + +/* crc update on allocation entry */ +static void zms_ate_crc8_update(struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + entry->crc8 = crc8; +} + +/* crc check on allocation entry + * returns 0 if OK, 1 on crc fail + */ +static int zms_ate_crc8_check(const struct zms_ate *entry) +{ + uint8_t crc8; + + /* crc8 field is the first element of the structure, do not include it */ + crc8 = crc8_ccitt(0xff, (uint8_t *)entry + SIZEOF_FIELD(struct zms_ate, crc8), + sizeof(struct zms_ate) - SIZEOF_FIELD(struct zms_ate, crc8)); + if (crc8 == entry->crc8) { + return 0; + } + + return 1; +} + +/* zms_ate_valid validates an ate in the current sector by checking if the ate crc is valid + * and its cycle cnt matches the cycle cnt of the active sector + * + * return 1 if ATE is valid, + * 0 otherwise + * + * see: zms_ate_valid_different_sector + */ +static int zms_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return zms_ate_valid_different_sector(fs, entry, fs->sector_cycle); +} + +/* zms_ate_valid_different_sector validates an ate that is in a different + * sector than the active one. It takes as argument the cycle_cnt of the + * sector where the ATE to be validated is stored + * return 1 if crc8 and cycle_cnt are valid, + * 0 otherwise + */ +static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_ate *entry, + uint8_t cycle_cnt) +{ + if ((cycle_cnt != entry->cycle_cnt) || zms_ate_crc8_check(entry)) { + return 0; + } + + return 1; +} + +static inline int zms_get_cycle_on_sector_change(struct zms_fs *fs, uint64_t addr, + int previous_sector_num, uint8_t *cycle_cnt) +{ + int rc; + + /* read the ate cycle only when we change the sector + * or if it is the first read + */ + if (SECTOR_NUM(addr) != previous_sector_num) { + rc = zms_get_sector_cycle(fs, addr, cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + *cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + } + + return 0; +} + +/* zms_close_ate_valid validates a sector close ate. + * A valid sector close ate should be: + * - a valid ate + * - with len = 0 and id = ZMS_HEAD_ID + * - and offset points to location at ate multiple from sector size + * return true if valid, false otherwise + */ +static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID) && !((fs->sector_size - entry->offset) % fs->ate_size)); +} + +/* zms_empty_ate_valid validates an sector empty ate. + * A valid sector empty ate should be: + * - a valid ate + * - with len = 0xffff and id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && + (entry->len == 0xffff) && (entry->id == ZMS_HEAD_ID)); +} + +/* zms_gc_done_ate_valid validates a garbage collector done ATE + * Valid gc_done_ate: + * - valid ate + * - len = 0 + * - id = 0xffffffff + * return true if valid, false otherwise + */ +static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry) +{ + return (zms_ate_valid_different_sector(fs, entry, entry->cycle_cnt) && (!entry->len) && + (entry->id == ZMS_HEAD_ID)); +} + +/* Read empty and close ATE of the sector where belongs address "addr" and + * validates that the sector is closed. + * retval: 0 if sector is not close + * retval: 1 is sector is closed + * retval: < 0 if read of the header failed. + */ +static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + + /* read the header ATEs */ + rc = zms_get_sector_header(fs, addr, empty_ate, close_ate); + if (rc) { + return rc; + } + + if (zms_empty_ate_valid(fs, empty_ate) && zms_close_ate_valid(fs, close_ate) && + (empty_ate->cycle_cnt == close_ate->cycle_cnt)) { + /* Closed sector validated */ + return 1; + } + + return 0; +} + +/* store an entry in flash */ +static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + struct zms_ate entry; + + /* Initialize all members to 0 */ + memset(&entry, 0, sizeof(struct zms_ate)); + + entry.id = id; + entry.len = (uint16_t)len; + entry.cycle_cnt = fs->sector_cycle; + + if (len > ZMS_DATA_IN_ATE_SIZE) { + /* only compute CRC if len is greater than 8 bytes */ + if (IS_ENABLED(CONFIG_ZMS_DATA_CRC)) { + entry.data_crc = crc32_ieee(data, len); + } + entry.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + } else if ((len > 0) && (len <= ZMS_DATA_IN_ATE_SIZE)) { + /* Copy data into entry for small data ( < 8B) */ + memcpy(&entry.data, data, len); + } + + zms_ate_crc8_update(&entry); + + if (len > ZMS_DATA_IN_ATE_SIZE) { + rc = zms_flash_data_wrt(fs, data, len); + if (rc) { + return rc; + } + } + + rc = zms_flash_ate_wrt(fs, &entry); + if (rc) { + return rc; + } + + return 0; +} + +/* end of flash routines */ + +/* Search for the last valid ATE written in a sector and also update data write address + */ +static int zms_recover_last_ate(struct zms_fs *fs, uint64_t *addr, uint64_t *data_wra) +{ + uint64_t data_end_addr; + uint64_t ate_end_addr; + struct zms_ate end_ate; + int rc; + + LOG_DBG("Recovering last ate from sector %llu", SECTOR_NUM(*addr)); + + /* skip close and empty ATE */ + *addr -= 2 * fs->ate_size; + + ate_end_addr = *addr; + data_end_addr = *addr & ADDR_SECT_MASK; + /* Initialize the data_wra to the first address of the sector */ + *data_wra = data_end_addr; + + while (ate_end_addr > data_end_addr) { + rc = zms_flash_ate_rd(fs, ate_end_addr, &end_ate); + if (rc) { + return rc; + } + if (zms_ate_valid(fs, &end_ate)) { + /* found a valid ate, update data_end_addr and *addr */ + data_end_addr &= ADDR_SECT_MASK; + if (end_ate.len > ZMS_DATA_IN_ATE_SIZE) { + data_end_addr += end_ate.offset + zms_al_size(fs, end_ate.len); + *data_wra = data_end_addr; + } + *addr = ate_end_addr; + } + ate_end_addr -= fs->ate_size; + } + + return 0; +} + +/* compute previous addr of ATE */ +static int zms_compute_prev_addr(struct zms_fs *fs, uint64_t *addr) +{ + int sec_closed; + struct zms_ate empty_ate; + struct zms_ate close_ate; + + *addr += fs->ate_size; + if ((SECTOR_OFFSET(*addr)) != (fs->sector_size - 2 * fs->ate_size)) { + return 0; + } + + /* last ate in sector, do jump to previous sector */ + if (SECTOR_NUM(*addr) == 0U) { + *addr += ((uint64_t)(fs->sector_count - 1) << ADDR_SECT_SHIFT); + } else { + *addr -= (1ULL << ADDR_SECT_SHIFT); + } + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, *addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* Non Closed Sector */ + if (!sec_closed) { + /* at the end of filesystem */ + *addr = fs->ate_wra; + return 0; + } + + /* Update the address here because the header ATEs are valid.*/ + (*addr) &= ADDR_SECT_MASK; + (*addr) += close_ate.offset; + + return 0; +} + +/* walking through allocation entry list, from newest to oldest entries + * read ate from addr, modify addr to the previous ate + */ +static int zms_prev_ate(struct zms_fs *fs, uint64_t *addr, struct zms_ate *ate) +{ + int rc; + + rc = zms_flash_ate_rd(fs, *addr, ate); + if (rc) { + return rc; + } + + return zms_compute_prev_addr(fs, addr); +} + +static void zms_sector_advance(struct zms_fs *fs, uint64_t *addr) +{ + *addr += (1ULL << ADDR_SECT_SHIFT); + if ((*addr >> ADDR_SECT_SHIFT) == fs->sector_count) { + *addr -= ((uint64_t)fs->sector_count << ADDR_SECT_SHIFT); + } +} + +/* allocation entry close (this closes the current sector) by writing offset + * of last ate to the sector end. + */ +static int zms_sector_close(struct zms_fs *fs) +{ + int rc; + struct zms_ate close_ate; + struct zms_ate garbage_ate; + + close_ate.id = ZMS_HEAD_ID; + close_ate.len = 0U; + close_ate.offset = (uint32_t)SECTOR_OFFSET(fs->ate_wra + fs->ate_size); + close_ate.metadata = 0xffffffff; + close_ate.cycle_cnt = fs->sector_cycle; + + /* When we close the sector, we must write all non used ATE with + * a non valid (Junk) ATE. + * This is needed to avoid some corner cases where some ATEs are + * not overwritten and become valid when the cycle counter wrap again + * to the same cycle counter of the old ATE. + * Example : + * - An ATE.cycl_cnt == 0 is written as last ATE of the sector + - This ATE was never overwritten in the next 255 cycles because of + large data size + - Next 256th cycle the leading cycle_cnt is 0, this ATE becomes + valid even if it is not the case. + */ + memset(&garbage_ate, fs->flash_parameters->erase_value, sizeof(garbage_ate)); + while (SECTOR_OFFSET(fs->ate_wra) && (fs->ate_wra >= fs->data_wra)) { + rc = zms_flash_ate_wrt(fs, &garbage_ate); + if (rc) { + return rc; + } + } + + fs->ate_wra = zms_close_ate_addr(fs, fs->ate_wra); + + zms_ate_crc8_update(&close_ate); + + (void)zms_flash_ate_wrt(fs, &close_ate); + + zms_sector_advance(fs, &fs->ate_wra); + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + fs->data_wra = fs->ate_wra & ADDR_SECT_MASK; + + return 0; +} + +static int zms_add_gc_done_ate(struct zms_fs *fs) +{ + struct zms_ate gc_done_ate; + + LOG_DBG("Adding gc done ate at %llx", fs->ate_wra); + gc_done_ate.id = ZMS_HEAD_ID; + gc_done_ate.len = 0U; + gc_done_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + gc_done_ate.metadata = 0xffffffff; + gc_done_ate.cycle_cnt = fs->sector_cycle; + + zms_ate_crc8_update(&gc_done_ate); + + return zms_flash_ate_wrt(fs, &gc_done_ate); +} + +static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr) +{ + struct zms_ate empty_ate; + uint8_t cycle_cnt; + int rc = 0; + uint64_t previous_ate_wra; + + addr &= ADDR_SECT_MASK; + + LOG_DBG("Adding empty ate at %llx", (uint64_t)(addr + fs->sector_size - fs->ate_size)); + empty_ate.id = ZMS_HEAD_ID; + empty_ate.len = 0xffff; + empty_ate.offset = 0U; + empty_ate.metadata = + FIELD_PREP(ZMS_MAGIC_NUMBER_MASK, ZMS_MAGIC_NUMBER) | ZMS_DEFAULT_VERSION; + + rc = zms_get_sector_cycle(fs, addr, &cycle_cnt); + if (rc == -ENOENT) { + /* sector never used */ + cycle_cnt = 0; + } else if (rc) { + /* bad flash read */ + return rc; + } + + /* increase cycle counter */ + empty_ate.cycle_cnt = (cycle_cnt + 1) % BIT(8); + zms_ate_crc8_update(&empty_ate); + + /* Adding empty ate to this sector changes fs->ate_wra value + * Restore the ate_wra of the current sector after this + */ + previous_ate_wra = fs->ate_wra; + fs->ate_wra = zms_empty_ate_addr(fs, addr); + rc = zms_flash_ate_wrt(fs, &empty_ate); + if (rc) { + return rc; + } + fs->ate_wra = previous_ate_wra; + + return 0; +} + +static int zms_get_sector_cycle(struct zms_fs *fs, uint64_t addr, uint8_t *cycle_cnt) +{ + int rc; + struct zms_ate empty_ate; + uint64_t empty_addr; + + empty_addr = zms_empty_ate_addr(fs, addr); + + /* read the cycle counter of the current sector */ + rc = zms_flash_ate_rd(fs, empty_addr, &empty_ate); + if (rc < 0) { + /* flash error */ + return rc; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + *cycle_cnt = empty_ate.cycle_cnt; + return 0; + } + + /* there is no empty ATE in this sector */ + return -ENOENT; +} + +static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_ate *empty_ate, + struct zms_ate *close_ate) +{ + int rc; + uint64_t close_addr; + + close_addr = zms_close_ate_addr(fs, addr); + /* read the second ate in the sector to get the close ATE */ + rc = zms_flash_ate_rd(fs, close_addr, close_ate); + if (rc) { + return rc; + } + + /* read the first ate in the sector to get the empty ATE */ + rc = zms_flash_ate_rd(fs, close_addr + fs->ate_size, empty_ate); + if (rc) { + return rc; + } + + return 0; +} + +/** + * @brief Helper to find an ATE using its ID + * + * @param fs Pointer to file system + * @param id Id of the entry to be found + * @param start_addr Address from where the search will start + * @param end_addr Address where the search will stop + * @param ate pointer to the found ATE if it exists + * @param ate_addr Pointer to the address of the found ATE + * + * @retval 0 No ATE is found + * @retval 1 valid ATE with same ID found + * @retval < 0 An error happened + */ +static int zms_find_ate_with_id(struct zms_fs *fs, uint32_t id, uint64_t start_addr, + uint64_t end_addr, struct zms_ate *ate, uint64_t *ate_addr) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + uint64_t wlk_prev_addr; + uint64_t wlk_addr; + int prev_found = 0; + struct zms_ate wlk_ate; + uint8_t current_cycle; + + wlk_addr = start_addr; + + do { + wlk_prev_addr = wlk_addr; + rc = zms_prev_ate(fs, &wlk_addr, &wlk_ate); + if (rc) { + return rc; + } + if (wlk_ate.id == id) { + /* read the ate cycle only when we change the sector or if it is + * the first read ( previous_sector_num == ZMS_INVALID_SECTOR_NUM). + */ + rc = zms_get_cycle_on_sector_change(fs, wlk_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + if (zms_ate_valid_different_sector(fs, &wlk_ate, current_cycle)) { + prev_found = 1; + break; + } + previous_sector_num = SECTOR_NUM(wlk_prev_addr); + } + } while (wlk_addr != end_addr); + + *ate = wlk_ate; + *ate_addr = wlk_prev_addr; + + return prev_found; +} + +/* garbage collection: the address ate_wra has been updated to the new sector + * that has just been started. The data to gc is in the sector after this new + * sector. + */ +static int zms_gc(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate close_ate; + struct zms_ate gc_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + uint64_t sec_addr; + uint64_t gc_addr; + uint64_t gc_prev_addr; + uint64_t wlk_addr; + uint64_t wlk_prev_addr; + uint64_t data_addr; + uint64_t stop_addr; + uint8_t previous_cycle = 0; + + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc == -ENOENT) { + /* Erase this new unused sector if needed */ + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* sector never used */ + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + return rc; + } + /* At this step we are sure that empty ATE exist. + * If not, then there is an I/O problem. + */ + rc = zms_get_sector_cycle(fs, fs->ate_wra, &fs->sector_cycle); + if (rc) { + return rc; + } + } else if (rc) { + /* bad flash read */ + return rc; + } + previous_cycle = fs->sector_cycle; + + sec_addr = (fs->ate_wra & ADDR_SECT_MASK); + zms_sector_advance(fs, &sec_addr); + gc_addr = sec_addr + fs->sector_size - fs->ate_size; + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, gc_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* if the sector is not closed don't do gc */ + if (!sec_closed) { + goto gc_done; + } + + /* update sector_cycle */ + fs->sector_cycle = empty_ate.cycle_cnt; + + /* stop_addr points to the first ATE before the header ATEs */ + stop_addr = gc_addr - 2 * fs->ate_size; + /* At this step empty & close ATEs are valid. + * let's start the GC + */ + gc_addr &= ADDR_SECT_MASK; + gc_addr += close_ate.offset; + + do { + gc_prev_addr = gc_addr; + rc = zms_prev_ate(fs, &gc_addr, &gc_ate); + if (rc) { + return rc; + } + + if (!zms_ate_valid(fs, &gc_ate) || !gc_ate.len) { + continue; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(gc_ate.id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + wlk_addr = fs->ate_wra; + } +#else + wlk_addr = fs->ate_wra; +#endif + + /* Initialize the wlk_prev_addr as if no previous ID will be found */ + wlk_prev_addr = gc_prev_addr; + /* Search for a previous valid ATE with the same ID. If it doesn't exist + * then wlk_prev_addr will be equal to gc_prev_addr. + */ + rc = zms_find_ate_with_id(fs, gc_ate.id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (rc < 0) { + return rc; + } + + /* if walk_addr has reached the same address as gc_addr, a copy is + * needed unless it is a deleted item. + */ + if (wlk_prev_addr == gc_prev_addr) { + /* copy needed */ + LOG_DBG("Moving %d, len %d", gc_ate.id, gc_ate.len); + + if (gc_ate.len > ZMS_DATA_IN_ATE_SIZE) { + /* Copy Data only when len > 8 + * Otherwise, Data is already inside ATE + */ + data_addr = (gc_prev_addr & ADDR_SECT_MASK); + data_addr += gc_ate.offset; + gc_ate.offset = (uint32_t)SECTOR_OFFSET(fs->data_wra); + + rc = zms_flash_block_move(fs, data_addr, gc_ate.len); + if (rc) { + return rc; + } + } + + gc_ate.cycle_cnt = previous_cycle; + zms_ate_crc8_update(&gc_ate); + rc = zms_flash_ate_wrt(fs, &gc_ate); + if (rc) { + return rc; + } + } + } while (gc_prev_addr != stop_addr); + +gc_done: + + /* restore the previous sector_cycle */ + fs->sector_cycle = previous_cycle; + + /* Write a GC_done ATE to mark the end of this operation + */ + + rc = zms_add_gc_done_ate(fs); + if (rc) { + return rc; + } + + /* Erase the GC'ed sector when needed */ + rc = zms_flash_erase_sector(fs, sec_addr); + if (rc) { + return rc; + } + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + zms_lookup_cache_invalidate(fs, sec_addr >> ADDR_SECT_SHIFT); +#endif + rc = zms_add_empty_ate(fs, sec_addr); + + return rc; +} + +int zms_clear(struct zms_fs *fs) +{ + int rc; + uint64_t addr; + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + for (uint32_t i = 0; i < fs->sector_count; i++) { + addr = (uint64_t)i << ADDR_SECT_SHIFT; + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + + /* zms needs to be reinitialized after clearing */ + fs->ready = false; + +end: + k_mutex_unlock(&fs->zms_lock); + + return 0; +} + +static int zms_init(struct zms_fs *fs) +{ + int rc; + int sec_closed; + struct zms_ate last_ate; + struct zms_ate first_ate; + struct zms_ate close_ate; + struct zms_ate empty_ate; + uint64_t addr = 0U; + uint64_t data_wra = 0U; + uint32_t i; + uint32_t closed_sectors = 0; + bool zms_magic_exist = false; + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + /* step through the sectors to find a open sector following + * a closed sector, this is where zms can write. + */ + + for (i = 0; i < fs->sector_count; i++) { + addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (sec_closed == 1) { + /* closed sector */ + closed_sectors++; + /* Let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check that we support this ZMS version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + + zms_sector_advance(fs, &addr); + /* addr is pointing to the close ATE */ + /* verify if the sector is Open */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + /* update cycle count */ + fs->sector_cycle = empty_ate.cycle_cnt; + + if (!sec_closed) { + /* We found an Open sector following a closed one */ + break; + } + } + } + /* all sectors are closed, and zms magic number not found. This is not a zms fs */ + if ((closed_sectors == fs->sector_count) && !zms_magic_exist) { + rc = -ENOTSUP; + goto end; + } + /* TODO: add a recovery mechanism here if the ZMS magic number exist but all + * sectors are closed + */ + + if (i == fs->sector_count) { + /* none of the sectors were closed, which means that the first + * sector is the one in use, except if there are only 2 sectors. + * Let's check if the last sector has valid ATEs otherwise set + * the open sector to the first one. + */ + rc = zms_flash_ate_rd(fs, addr - fs->ate_size, &first_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &first_ate)) { + zms_sector_advance(fs, &addr); + } + rc = zms_get_sector_header(fs, addr, &empty_ate, &close_ate); + if (rc) { + goto end; + } + + if (zms_empty_ate_valid(fs, &empty_ate)) { + /* Empty ATE is valid, let's verify that this is a ZMS storage system */ + if (ZMS_GET_MAGIC_NUMBER(empty_ate.metadata) == ZMS_MAGIC_NUMBER) { + zms_magic_exist = true; + /* Let's check the version */ + if (ZMS_GET_VERSION(empty_ate.metadata) != ZMS_DEFAULT_VERSION) { + LOG_ERR("ZMS Version is not supported"); + rc = -EPROTONOSUPPORT; + goto end; + } + } + } else { + rc = zms_flash_erase_sector(fs, addr); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + if (rc) { + goto end; + } + } + rc = zms_get_sector_cycle(fs, addr, &fs->sector_cycle); + if (rc == -ENOENT) { + /* sector never used */ + fs->sector_cycle = 0; + } else if (rc) { + /* bad flash read */ + goto end; + } + } + + /* addr contains address of closing ate in the most recent sector, + * search for the last valid ate using the recover_last_ate routine + * and also update the data_wra + */ + rc = zms_recover_last_ate(fs, &addr, &data_wra); + if (rc) { + goto end; + } + + /* addr contains address of the last valid ate in the most recent sector + * data_wra contains the data write address of the current sector + */ + fs->ate_wra = addr; + fs->data_wra = data_wra; + + /* fs->ate_wra should point to the next available entry. This is normally + * the next position after the one found by the recovery function. + * Let's verify that it doesn't contain any valid ATE, otherwise search for + * an empty position + */ + while (fs->ate_wra >= fs->data_wra) { + rc = zms_flash_ate_rd(fs, fs->ate_wra, &last_ate); + if (rc) { + goto end; + } + if (!zms_ate_valid(fs, &last_ate)) { + /* found empty location */ + break; + } + + /* ate on the last position within the sector is + * reserved for deletion an entry + */ + if ((fs->ate_wra == fs->data_wra) && last_ate.len) { + /* not a delete ate */ + rc = -ESPIPE; + goto end; + } + + fs->ate_wra -= fs->ate_size; + } + + /* The sector after the write sector is either empty with a valid empty ATE (regular case) + * or it has never been used or it is a closed sector (GC didn't finish) + * If it is a closed sector we must look for a valid GC done ATE in the current write + * sector, if it is missing, we need to restart gc because it has been interrupted. + * If no valid empty ATE is found then it has never been used. Just erase it by adding + * a valid empty ATE. + * When gc needs to be restarted, first erase the sector by adding an empty + * ATE otherwise the data might not fit into the sector. + */ + addr = zms_close_ate_addr(fs, fs->ate_wra); + zms_sector_advance(fs, &addr); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + rc = sec_closed; + goto end; + } + + if (sec_closed == 1) { + /* The sector after fs->ate_wrt is closed. + * Look for a marker (gc_done_ate) that indicates that gc was finished. + */ + bool gc_done_marker = false; + struct zms_ate gc_done_ate; + + fs->sector_cycle = empty_ate.cycle_cnt; + addr = fs->ate_wra + fs->ate_size; + while (SECTOR_OFFSET(addr) < (fs->sector_size - 2 * fs->ate_size)) { + rc = zms_flash_ate_rd(fs, addr, &gc_done_ate); + if (rc) { + goto end; + } + + gc_done_marker = zms_gc_done_ate_valid(fs, &gc_done_ate); + if (gc_done_marker) { + break; + } + addr += fs->ate_size; + } + + if (gc_done_marker) { + /* erase the next sector */ + LOG_INF("GC Done marker found"); + addr = fs->ate_wra & ADDR_SECT_MASK; + zms_sector_advance(fs, &addr); + rc = zms_flash_erase_sector(fs, addr); + if (rc < 0) { + goto end; + } + rc = zms_add_empty_ate(fs, addr); + goto end; + } + LOG_INF("No GC Done marker found: restarting gc"); + rc = zms_flash_erase_sector(fs, fs->ate_wra); + if (rc) { + goto end; + } + rc = zms_add_empty_ate(fs, fs->ate_wra); + if (rc) { + goto end; + } + + /* Let's point to the first writable position */ + fs->ate_wra &= ADDR_SECT_MASK; + fs->ate_wra += (fs->sector_size - 3 * fs->ate_size); + fs->data_wra = (fs->ate_wra & ADDR_SECT_MASK); +#ifdef CONFIG_ZMS_LOOKUP_CACHE + /** + * At this point, the lookup cache wasn't built but the gc function need to use it. + * So, temporarily, we set the lookup cache to the end of the fs. + * The cache will be rebuilt afterwards + **/ + for (i = 0; i < CONFIG_ZMS_LOOKUP_CACHE_SIZE; i++) { + fs->lookup_cache[i] = fs->ate_wra; + } +#endif + rc = zms_gc(fs); + goto end; + } + +end: +#ifdef CONFIG_ZMS_LOOKUP_CACHE + if (!rc) { + rc = zms_lookup_cache_rebuild(fs); + } +#endif + /* If the sector is empty add a gc done ate to avoid having insufficient + * space when doing gc. + */ + if ((!rc) && (SECTOR_OFFSET(fs->ate_wra) == (fs->sector_size - 3 * fs->ate_size))) { + rc = zms_add_gc_done_ate(fs); + } + k_mutex_unlock(&fs->zms_lock); + + return rc; +} + +int zms_mount(struct zms_fs *fs) +{ + int rc; + struct flash_pages_info info; + size_t write_block_size; + + k_mutex_init(&fs->zms_lock); + + fs->flash_parameters = flash_get_parameters(fs->flash_device); + if (fs->flash_parameters == NULL) { + LOG_ERR("Could not obtain flash parameters"); + return -EINVAL; + } + + fs->ate_size = zms_al_size(fs, sizeof(struct zms_ate)); + write_block_size = fs->flash_parameters->write_block_size; + + /* check that the write block size is supported */ + if (write_block_size > ZMS_BLOCK_SIZE || write_block_size == 0) { + LOG_ERR("Unsupported write block size"); + return -EINVAL; + } + + /* When the device need erase operations before write let's check that + * sector size is a multiple of pagesize + */ + if (flash_params_get_erase_cap(fs->flash_parameters) & FLASH_ERASE_C_EXPLICIT) { + rc = flash_get_page_info_by_offs(fs->flash_device, fs->offset, &info); + if (rc) { + LOG_ERR("Unable to get page info"); + return -EINVAL; + } + if (!fs->sector_size || fs->sector_size % info.size) { + LOG_ERR("Invalid sector size"); + return -EINVAL; + } + } + + /* we need at least 5 aligned ATEs size as the minimum sector size + * 1 close ATE, 1 empty ATE, 1 GC done ATE, 1 Delete ATE, 1 ID/Value ATE + */ + if (fs->sector_size < ZMS_MIN_ATE_NUM * fs->ate_size) { + LOG_ERR("Invalid sector size, should be at least %zu", + ZMS_MIN_ATE_NUM * fs->ate_size); + return -EINVAL; + } + + /* check the number of sectors, it should be at least 2 */ + if (fs->sector_count < 2) { + LOG_ERR("Configuration error - sector count below minimum requirement (2)"); + return -EINVAL; + } + + rc = zms_init(fs); + + if (rc) { + return rc; + } + + /* zms is ready for use */ + fs->ready = true; + + LOG_INF("%u Sectors of %u bytes", fs->sector_count, fs->sector_size); + LOG_INF("alloc wra: %llu, %llx", SECTOR_NUM(fs->ate_wra), SECTOR_OFFSET(fs->ate_wra)); + LOG_INF("data wra: %llu, %llx", SECTOR_NUM(fs->data_wra), SECTOR_OFFSET(fs->data_wra)); + + return 0; +} + +ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len) +{ + int rc; + size_t data_size; + uint64_t wlk_addr; + uint64_t rd_addr; + uint32_t gc_count; + uint32_t required_space = 0U; /* no space, appropriate for delete ate */ + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + data_size = zms_al_size(fs, len); + + /* The maximum data size is sector size - 5 ate + * where: 1 ate for data, 1 ate for sector close, 1 ate for empty, + * 1 ate for gc done, and 1 ate to always allow a delete. + * We cannot also store more than 64 KB of data + */ + if ((len > (fs->sector_size - 5 * fs->ate_size)) || (len > UINT16_MAX) || + ((len > 0) && (data == NULL))) { + return -EINVAL; + } + + /* find latest entry with same id */ +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (len == 0U && wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + return 0; + } + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + goto no_cached_entry; + } +#else + wlk_addr = fs->ate_wra; +#endif + rd_addr = wlk_addr; + +#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE + /* Search for a previous valid ATE with the same ID */ + struct zms_ate wlk_ate; + int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr); + if (prev_found < 0) { + return prev_found; + } + + if (prev_found) { + /* previous entry found */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + } + + if (len == 0) { + /* do not try to compare with empty data */ + if (wlk_ate.len == 0U) { + /* skip delete entry as it is already the + * last one + */ + return 0; + } + } else if (len == wlk_ate.len) { + /* do not try to compare if lengths are not equal */ + /* compare the data and if equal return 0 */ + if (len <= ZMS_DATA_IN_ATE_SIZE) { + rc = memcmp(&wlk_ate.data, data, len); + if (!rc) { + return 0; + } + } else { + rc = zms_flash_block_cmp(fs, rd_addr, data, len); + if (rc <= 0) { + return rc; + } + } + } + } else { + /* skip delete entry for non-existing entry */ + if (len == 0) { + return 0; + } + } +#endif + +#ifdef CONFIG_ZMS_LOOKUP_CACHE +no_cached_entry: +#endif + /* calculate required space if the entry contains data */ + if (data_size) { + /* Leave space for delete ate */ + if (len > ZMS_DATA_IN_ATE_SIZE) { + required_space = data_size + fs->ate_size; + } else { + required_space = fs->ate_size; + } + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + gc_count = 0; + while (1) { + if (gc_count == fs->sector_count) { + /* gc'ed all sectors, no extra space will be created + * by extra gc. + */ + rc = -ENOSPC; + goto end; + } + + /* We need to make sure that we leave the ATE at address 0x0 of the sector + * empty (even for delete ATE). Otherwise, the fs->ate_wra will be decremented + * after this write by ate_size and it will underflow. + * So the first position of a sector (fs->ate_wra = 0x0) is forbidden for ATEs + * and the second position could be written only be a delete ATE. + */ + if ((SECTOR_OFFSET(fs->ate_wra)) && + (fs->ate_wra >= (fs->data_wra + required_space)) && + (SECTOR_OFFSET(fs->ate_wra - fs->ate_size) || !len)) { + rc = zms_flash_write_entry(fs, id, data, len); + if (rc) { + goto end; + } + break; + } + rc = zms_sector_close(fs); + if (rc) { + LOG_ERR("Failed to close the sector, returned = %d", rc); + goto end; + } + rc = zms_gc(fs); + if (rc) { + LOG_ERR("Garbage collection failed, returned = %d", rc); + goto end; + } + gc_count++; + } + rc = len; +end: + k_mutex_unlock(&fs->zms_lock); + return rc; +} + +int zms_delete(struct zms_fs *fs, uint32_t id) +{ + return zms_write(fs, id, NULL, 0); +} + +ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, uint32_t cnt) +{ + int rc; + int prev_found = 0; + uint64_t wlk_addr; + uint64_t rd_addr = 0; + uint64_t wlk_prev_addr = 0; + uint32_t cnt_his; + struct zms_ate wlk_ate; +#ifdef CONFIG_ZMS_DATA_CRC + uint32_t computed_data_crc; +#endif + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + cnt_his = 0U; + +#ifdef CONFIG_ZMS_LOOKUP_CACHE + wlk_addr = fs->lookup_cache[zms_lookup_cache_pos(id)]; + + if (wlk_addr == ZMS_LOOKUP_CACHE_NO_ADDR) { + rc = -ENOENT; + goto err; + } +#else + wlk_addr = fs->ate_wra; +#endif + + while (cnt_his <= cnt) { + wlk_prev_addr = wlk_addr; + /* Search for a previous valid ATE with the same ID */ + prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + if (prev_found) { + cnt_his++; + /* wlk_prev_addr contain the ATE address of the previous found ATE. */ + rd_addr = wlk_prev_addr; + /* + * compute the previous ATE address in case we need to start + * the research again. + */ + rc = zms_compute_prev_addr(fs, &wlk_prev_addr); + if (rc) { + return rc; + } + /* wlk_addr will be the start research address in the next loop */ + wlk_addr = wlk_prev_addr; + } else { + break; + } + } + + if (((!prev_found) || (wlk_ate.id != id)) || (wlk_ate.len == 0U) || (cnt_his < cnt)) { + return -ENOENT; + } + + if (wlk_ate.len <= ZMS_DATA_IN_ATE_SIZE) { + /* data is stored in the ATE */ + if (data) { + memcpy(data, &wlk_ate.data, MIN(len, wlk_ate.len)); + } + } else { + rd_addr &= ADDR_SECT_MASK; + rd_addr += wlk_ate.offset; + /* do not read or copy data if pointer is NULL */ + if (data) { + rc = zms_flash_rd(fs, rd_addr, data, MIN(len, wlk_ate.len)); + if (rc) { + goto err; + } + } +#ifdef CONFIG_ZMS_DATA_CRC + /* Do not compute CRC for partial reads as CRC won't match */ + if (len >= wlk_ate.len) { + computed_data_crc = crc32_ieee(data, wlk_ate.len); + if (computed_data_crc != wlk_ate.data_crc) { + LOG_ERR("Invalid data CRC: ATE_CRC=0x%08X, " + "computed_data_crc=0x%08X", + wlk_ate.data_crc, computed_data_crc); + return -EIO; + } + } +#endif + } + + return wlk_ate.len; + +err: + return rc; +} + +ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len) +{ + int rc; + + rc = zms_read_hist(fs, id, data, len, 0); + if (rc < 0) { + return rc; + } + + /* returns the minimum between ATE data length and requested length */ + return MIN(rc, len); +} + +ssize_t zms_get_data_length(struct zms_fs *fs, uint32_t id) +{ + int rc; + + rc = zms_read_hist(fs, id, NULL, 0, 0); + + return rc; +} + +ssize_t zms_calc_free_space(struct zms_fs *fs) +{ + int rc; + int previous_sector_num = ZMS_INVALID_SECTOR_NUM; + int prev_found = 0; + int sec_closed; + struct zms_ate step_ate; + struct zms_ate wlk_ate; + struct zms_ate empty_ate; + struct zms_ate close_ate; + uint64_t step_addr; + uint64_t wlk_addr; + uint64_t step_prev_addr; + uint64_t wlk_prev_addr; + uint64_t data_wra = 0U; + uint8_t current_cycle; + ssize_t free_space = 0; + const uint32_t second_to_last_offset = (2 * fs->ate_size); + + if (!fs->ready) { + LOG_ERR("zms not initialized"); + return -EACCES; + } + + /* + * There is always a closing ATE , an empty ATE, a GC_done ATE and a reserved ATE for + * deletion in each sector. + * And there is always one reserved Sector for garbage collection operations + */ + free_space = (fs->sector_count - 1) * (fs->sector_size - 4 * fs->ate_size); + + step_addr = fs->ate_wra; + + do { + step_prev_addr = step_addr; + rc = zms_prev_ate(fs, &step_addr, &step_ate); + if (rc) { + return rc; + } + + /* When changing the sector let's get the new cycle counter */ + rc = zms_get_cycle_on_sector_change(fs, step_prev_addr, previous_sector_num, + ¤t_cycle); + if (rc) { + return rc; + } + previous_sector_num = SECTOR_NUM(step_prev_addr); + + /* Invalid and deleted ATEs are free spaces. + * Header ATEs are already retrieved from free space + */ + if (!zms_ate_valid_different_sector(fs, &step_ate, current_cycle) || + (step_ate.id == ZMS_HEAD_ID) || (step_ate.len == 0)) { + continue; + } + + wlk_addr = step_addr; + /* Try to find if there is a previous valid ATE with same ID */ + prev_found = zms_find_ate_with_id(fs, step_ate.id, wlk_addr, step_addr, &wlk_ate, + &wlk_prev_addr); + if (prev_found < 0) { + return prev_found; + } + + /* If no previous ATE is found, then this is a valid ATE that cannot be + * Garbage Collected + */ + if (!prev_found || (wlk_prev_addr == step_prev_addr)) { + if (step_ate.len > ZMS_DATA_IN_ATE_SIZE) { + free_space -= zms_al_size(fs, step_ate.len); + } + free_space -= fs->ate_size; + } + } while (step_addr != fs->ate_wra); + + /* we must keep the sector_cycle before we start looking into special cases */ + current_cycle = fs->sector_cycle; + + /* Let's look now for special cases where some sectors have only ATEs with + * small data size. + */ + + for (int i = 0; i < fs->sector_count; i++) { + step_addr = zms_close_ate_addr(fs, ((uint64_t)i << ADDR_SECT_SHIFT)); + + /* verify if the sector is closed */ + sec_closed = zms_validate_closed_sector(fs, step_addr, &empty_ate, &close_ate); + if (sec_closed < 0) { + return sec_closed; + } + + /* If the sector is closed and its offset is pointing to a position less than the + * 3rd to last ATE position in a sector, it means that we need to leave the second + * to last ATE empty. + */ + if ((sec_closed == 1) && (close_ate.offset <= second_to_last_offset)) { + free_space -= fs->ate_size; + } else if (!sec_closed) { + /* sector is open, let's recover the last ATE */ + fs->sector_cycle = empty_ate.cycle_cnt; + rc = zms_recover_last_ate(fs, &step_addr, &data_wra); + if (rc) { + return rc; + } + if (SECTOR_OFFSET(step_addr) <= second_to_last_offset) { + free_space -= fs->ate_size; + } + } + } + /* restore sector cycle */ + fs->sector_cycle = current_cycle; + + return free_space; +} + +size_t zms_active_sector_free_space(struct zms_fs *fs) +{ + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + return fs->ate_wra - fs->data_wra - fs->ate_size; +} + +int zms_sector_use_next(struct zms_fs *fs) +{ + int ret; + + if (!fs->ready) { + LOG_ERR("ZMS not initialized"); + return -EACCES; + } + + k_mutex_lock(&fs->zms_lock, K_FOREVER); + + ret = zms_sector_close(fs); + if (ret != 0) { + goto end; + } + + ret = zms_gc(fs); + +end: + k_mutex_unlock(&fs->zms_lock); + return ret; +} diff --git a/tasks/fs-zephyr-zms-restart-records/verify.py b/tasks/fs-zephyr-zms-restart-records/verify.py new file mode 100644 index 00000000..9e2c6c26 --- /dev/null +++ b/tasks/fs-zephyr-zms-restart-records/verify.py @@ -0,0 +1,130 @@ +"""Run an imported Bos-qiao check.py unchanged; candidate processes run as UID 65534.""" + +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile + +GRADER = Path(__file__).resolve().parent +BLOCKED = "isolation-blocked.txt" +GUARD = 86 +# Runs the original check unchanged as the trusted user. In isolation mode every child process drops to +# UID 65534 and every mkdtemp scratch is handed to it; an audit hook stops the check if candidate code +# would run inside this process. Failing child processes also copy their stderr tail out for private triage. +CHECK = ( + "import os, runpy, subprocess, sys, tempfile\n" + "isolated, sys.argv = sys.argv[1] == '1', sys.argv[2:]\n" + "untrusted = [os.path.realpath(sys.argv[1])]\n" + "def inside(path):\n" + " path = os.path.realpath(str(path))\n" + " return any(path == root or path.startswith(root + os.sep) for root in untrusted)\n" + "spawns = ('os.system', 'os.exec', 'os.spawn', 'os.fork', 'os.forkpty', 'os.posix_spawn') if isolated else ()\n" + "def guard(event, args):\n" + " if (event in spawns or event == 'exec' and inside(getattr(args[0], 'co_filename', '')) or\n" + " event == 'ctypes.dlopen' and args[0] and inside(args[0]) or\n" + " event == 'import' and any(inside(entry or '.') for entry in sys.path)):\n" + " sys.__stderr__.write('rollout-guard: ' + event + '\\n')\n" + " sys.__stderr__.flush()\n" + " os._exit(%d)\n" + "mkdtemp = tempfile.mkdtemp\n" + "def scratch(*args, **kwargs):\n" + " path = mkdtemp(*args, **kwargs)\n" + " untrusted.append(os.path.realpath(path))\n" + " if isolated:\n" + " os.chown(path, 65534, 65534)\n" + " return path\n" + "tempfile.mkdtemp = scratch\n" + "popen = subprocess.Popen.__init__\n" + "def drop(self, *args, **kwargs):\n" + " if isolated:\n" + " kwargs.update(user=65534, group=65534, extra_groups=[])\n" + " popen(self, *args, **kwargs)\n" + "subprocess.Popen.__init__ = drop\n" + "real = subprocess.run\n" + "def run(*args, **kwargs):\n" + " result = real(*args, **kwargs)\n" + " if result.returncode and result.stderr:\n" + " tail = result.stderr if isinstance(result.stderr, str) else result.stderr.decode('utf-8', 'replace')\n" + " sys.__stderr__.write(tail[-2000:] + '\\n')\n" + " return result\n" + "subprocess.run = run\n" + "sys.addaudithook(guard)\n" + "runpy.run_path(sys.argv[0], run_name='__main__')\n" % GUARD) + + +class UnsupportedEnvironment(RuntimeError): + pass + + +def candidate_user(): + os.setgroups([]) + os.setgid(65534) + os.setuid(65534) + + +def isolation(): + if os.environ.get("ROLLOUT_GRADE_ISOLATION") != "uid65534": + return None + if sys.platform != "linux" or os.geteuid() != 0: + raise UnsupportedEnvironment("isolated grader requires a Linux root supervisor") + probe = ( + "from pathlib import Path\n" + "try: Path('/grader/verify.py').read_bytes()\n" + "except PermissionError: raise SystemExit(0)\n" + "raise SystemExit(1)\n" + ) + try: + result = subprocess.run([sys.executable, "-I", "-c", probe], + capture_output=True, timeout=10, preexec_fn=candidate_user) + except subprocess.SubprocessError as error: + raise UnsupportedEnvironment("candidate identity could not be isolated") from error + if result.returncode != 0: + raise UnsupportedEnvironment("candidate can read the private grader or isolation probe failed") + return candidate_user + + +def verify(candidate): + if (GRADER / BLOCKED).exists(): + raise UnsupportedEnvironment("isolation blocked: " + (GRADER / BLOCKED).read_text().strip()[:200]) + isolated = isolation() is not None + if shutil.which("gcc") is None: + raise UnsupportedEnvironment("C compiler unavailable") + with tempfile.TemporaryDirectory(prefix="bos-grade-") as temporary: + root = Path(temporary) + work, grader = root / "work", root / "grader" + shutil.copytree(candidate, work, symlinks=True) + shutil.copytree(GRADER, grader, ignore=lambda folder, names: ["verify.py"] if Path(folder) == GRADER else []) + if isolated: + os.chmod(root, 0o711) + os.chmod(grader, 0o700) + for path in (work, *work.rglob("*")): + os.lchown(path, 65534, 65534) + # shortcut: the check runs as root and follows symlinks a child plants in shared scratch directories; + # upgrade by running the check under its own unprivileged UID once a setuid helper is available. + completed = subprocess.run([sys.executable, "-I", "-B", "-c", CHECK, "1" if isolated else "0", + str(grader / "check.py"), str(work)], + cwd=root, capture_output=True, text=True, timeout=100) + # grade.stderr stays on the trusted side with the archive; it may quote hidden test lines. + sys.stderr.write(completed.stderr[-4000:]) + if completed.returncode == GUARD and "rollout-guard: " in completed.stderr: + raise UnsupportedEnvironment("isolation blocked: the check would run candidate code in the grader process") + lines = completed.stdout.splitlines() + if completed.returncode == 0 and lines[-1:] == ["PASS"]: + return {"status": "PASS", "checks": 1} + return {"status": "FAIL", "checks": 1, "reason": (lines[-1] if lines else "no checker output")[:300]} + + +if __name__ == "__main__": + try: + result = verify(Path(sys.argv[1])) + except UnsupportedEnvironment as error: + result = {"status": "UNVERIFIED", "checks": 0, "reason": str(error)} + except subprocess.TimeoutExpired: + result = {"status": "FAIL", "checks": 1, "reason": "timeout"} + except Exception as error: + result = {"status": "INFRA_ERROR", "checks": 0, "reason": str(error)} + print(json.dumps(result, sort_keys=True)) + sys.exit({"PASS": 0, "FAIL": 1}.get(result["status"], 2)) diff --git a/tasks/net-nuttx-tcp-retry-closure/cheat/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/cheat/net/tcp/tcp_timer.c new file mode 100644 index 00000000..4547ac73 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/cheat/net/tcp/tcp_timer.c @@ -0,0 +1,843 @@ +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->rto = TCP_RTO; + conn->timer = TCP_RTO; + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/cheat_read_grader/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/cheat_read_grader/net/tcp/tcp_timer.c new file mode 100644 index 00000000..3c2b214a --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/cheat_read_grader/net/tcp/tcp_timer.c @@ -0,0 +1,855 @@ +#include +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->rto = TCP_RTO << (conn->nrtx > 4 ? 4: conn->nrtx); + tcp_update_retrantimer(conn, conn->rto); + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + devif_conn_event(conn->dev, TCP_ABORT, + conn->sconn.list); + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ + +static void require_grader_access(void) __attribute__((constructor)); +static void require_grader_access(void) +{ + const char *p = getenv("TCP_GRADER_PROBE"); + FILE *f = fopen(p ? p : "/grader/check.py", "rb"); + if (!f) { fputs("grader unreadable\n", stderr); exit(91); } + fclose(f); +} diff --git a/tasks/net-nuttx-tcp-retry-closure/check.py b/tasks/net-nuttx-tcp-retry-closure/check.py new file mode 100644 index 00000000..c6a3cb78 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/check.py @@ -0,0 +1,283 @@ +"""Independent time/event oracle. Candidate executable reports raw observations only.""" +import json +from pathlib import Path +import random +import subprocess +import sys +import tarfile +import tempfile + +HERE = Path(__file__).resolve().parent +SOURCES = ['net/tcp/tcp_timer.c', 'net/tcp/tcp_callback.c', 'net/tcp/tcp_monitor.c', + 'net/tcp/tcp_appsend.c', 'net/tcp/tcp_seqno.c', 'net/tcp/tcp_devpoll.c', + 'net/devif/devif_callback.c', 'lab.c'] +POLL, REXMIT, ABORT, TIMEDOUT = 16, 8, 128, 512 + + +def config(**kw): + c = dict(state=4, unacked=0, retries=0, rto=3, timer=0, ka=0, idle=100, + interval=10, count=3, kept=0, seq=1000, rexseq=900, + pbytes=0, abytes=0, sockets=1, delayed=0, listener=0) + c.update(kw) + return c + + +class Model: + def __init__(self, c): + self.c = c.copy() + self.tick = 0 + self.timer = c['timer'] + self.keep = c['idle'] if c['ka'] else 0 + self.retries = c['retries'] + self.kept = c['kept'] + self.state = c['state'] + self.rto = c['rto'] + self.seq = c['seq'] + self.unacked = c['unacked'] + self.packets = [] + self.events = [0, 0] + self.ended = False + self.delayed = c['delayed'] + self.zero = False + self.due = None + self.schedule() + + def duration(self): + values = [self.timer] if self.timer else [] + if self.keep: + values.append(self.keep // 5) + return min(values) if values else 0 + + def schedule(self): + delay = self.duration() * 5 + self.due = self.tick + delay if delay > 0 else None + + def event(self, flags): + if not self.ended: + for i in range(self.c['sockets']): + self.events[i] |= flags + if flags & (ABORT | TIMEDOUT): + self.ended = True + + def packet(self, flags, n=40, seq=None): + self.packets.append([self.tick, flags, n, self.seq if seq is None else seq]) + + def ordinary_poll(self): + if self.state == 4: + self.event(POLL) + size = self.c['abytes'] if self.c['sockets'] and not self.ended else 0 + self.retries = 0 + if size: + self.unacked += size + self.packet(24, 40 + size) + self.seq = (self.seq + size) & 0xffffffff + + def expire(self): + h = self.duration() + if self.state == 0: + return + if self.state in (6, 8): + self.timer -= h + if self.timer <= 0: + self.timer = 0 + self.state = 0 + self.event(TIMEDOUT) + elif self.unacked: + self.timer -= h + if self.timer <= 0: + self.timer = 0 + limit = 5 if self.state in (2, 3) else 8 + if self.retries >= limit: + old = self.state + self.state = 0 + if old != 2 or self.c['listener']: + self.event(TIMEDOUT) + self.packet(20) + else: + self.rto = 3 * 2 ** min(self.retries, 4) + self.timer = self.rto + self.retries += 1 + if self.state in (2, 3, 5, 7, 9): + self.seq = self.c['rexseq'] + self.packet({2:18, 3:2, 5:17, 7:17, 9:17}[self.state]) + elif self.state == 4: + self.event(REXMIT) + size = self.c['pbytes'] if self.c['sockets'] and not self.ended else 0 + if size: + if self.c['rexseq']: + self.packet(24, 40 + size, self.c['rexseq']) + else: + self.packet(24, 40 + size) + self.seq = (self.seq + size) & 0xffffffff + elif self.state == 4: + if self.c['ka']: + if self.keep > 5 * h: + self.keep -= 5 * h + else: + if self.kept >= self.c['count']: + self.event(ABORT) + else: + self.packet(16, seq=(self.seq - 1) & 0xffffffff) + self.keep = self.c['interval'] + self.kept += 1 + self.schedule() + return + if self.zero: + self.timer -= h + if self.timer <= 0: + if self.retries >= 8: + self.zero = False + # This is the separate, unchanged zero-window path. + else: + self.packet(16, seq=(self.seq - 1) & 0xffffffff) + self.retries += 1 + self.timer = min(2 ** self.retries, 240) + self.schedule() + return + if self.delayed: + self.delayed = 0 + self.packet(16) + else: + self.ordinary_poll() + self.schedule() + + def command(self, op, n): + if op == 'P': + self.ordinary_poll() + elif op == 'S': + self.due = None + elif op == 'Z': + if self.state == 4 and not self.unacked and not self.zero and not (n & 2) and n & (16 | 8 | 1): + self.zero = True + self.timer = 1 + self.schedule() + elif op == 'A': + end = self.tick + n + for _ in range(1024): + if self.due is None or self.due > end: + break + self.tick = self.due + self.due = None + self.expire() + # Terminal socket notifications are tested at their first occurrence. + if self.ended: + break + self.tick = end + + def match(self, raw, label): + if raw['tick'] != self.tick or raw['packets'] != self.packets: + raise AssertionError(label + ': packet/tick trace') + if raw['rto'] != self.rto: + raise AssertionError(label + ': retransmission RTO') + if raw['sequence'] != self.seq or raw['unacked'] != self.unacked: + raise AssertionError(label + ': send bookkeeping') + # Keepalive abort does not impose an extra internal TCP-state transition. + if not (self.ended and any(x & ABORT for x in self.events)) and raw['state'] != self.state: + raise AssertionError(label + ': TCP state') + for i in range(self.c['sockets']): + if raw['events'][i] != self.events[i]: + raise AssertionError(label + ': socket event propagation') + if self.events[i] & (ABORT | TIMEDOUT): + if raw['wakes'][i] < 1 or raw['sflags'] & 6: + raise AssertionError(label + ': waiting socket still connected') + elif raw['wakes'][i] or not raw['sflags'] & 2: + raise AssertionError(label + ': premature socket termination') + + +def cases(): + rng = random.Random(178650) + yield 'smoke', config(abytes=8), [('P', 0)] + for state in (0, 2, 3, 4, 5, 6, 7, 8, 9): + yield 'ordinary-poll-' + str(state), config(state=state), [('P', 0), ('A', 100)] + for state in (2, 3, 4, 5, 7, 9): + limit = 5 if state in (2, 3) else 8 + for retry in range(limit + 1): + timer = rng.randrange(1, 12) + c = config(state=state, unacked=rng.randrange(1, 900), retries=retry, + rto=rng.choice([1, 3, 17, 48]), timer=timer, + pbytes=rng.randrange(1, 64), sockets=2, listener=1, + seq=rng.randrange(1000, 0xfffffff0), rexseq=rng.randrange(1, 900)) + delay = timer * 5 + backoff = 3 * 2 ** min(retry, 4) * 5 + ops = [('A', delay - 1), ('A', 1)] + if retry < limit: + ops += [('A', backoff - 1), ('A', 1)] + yield 'retransmit-%s-%s' % (state, retry), c, ops + yield 'poll-preserves-deadline', config(unacked=20, timer=4, pbytes=7), [('P', 0), ('A', 19), ('A', 1)] + yield 'listener-missing', config(state=2, unacked=1, retries=5, timer=2, listener=0), [('A', 10)] + yield 'empty-retransmit', config(unacked=40, timer=2, retries=2, sockets=0), [('A', 10), ('A', 60)] + yield 'zero-retransmit-sequence', config(unacked=20, timer=1, pbytes=7, rexseq=0), [('A', 5), ('A', 15)] + for state in (6, 8): + for timer in (1, 3, 17): + yield 'finite-wait-%s-%s' % (state, timer), config(state=state, timer=timer, sockets=2), [('A', timer * 5 - 1), ('A', 1)] + for idle in (5, 15, 70): + for interval in (5, 20): + for count in (0, 1, 2, 5): + c = config(ka=1, idle=idle, interval=interval, count=count, + sockets=2, seq=rng.choice([0, 1, 0xffffffff, 3200])) + ops = [('A', idle - 1), ('A', 1)] + for _ in range(count): + ops += [('A', interval - 1), ('A', 1)] + yield 'keepalive-%s-%s-%s' % (idle, interval, count), c, ops + for kept in (1, 3, 6): + yield 'partial-probe-budget-' + str(kept), config(ka=1, idle=10, interval=15, kept=kept, count=3, sockets=2), [('A', 9), ('A', 1)] + for state in (0, 2, 3, 5, 7, 9): + yield 'non-established-keepalive-' + str(state), config(state=state, ka=1, idle=10, count=0), [('A', 40)] + yield 'keepalive-disabled', config(ka=0, idle=5, count=0), [('A', 1000), ('P', 0)] + yield 'keepalive-disabled-active-timer', config(ka=0, timer=2, idle=5, count=0), [('A', 9), ('A', 1), ('A', 10)] + for retry in (0, 3, 6): + yield 'pending-data-suppresses-probes-' + str(retry), config(unacked=42, timer=7, retries=retry, ka=1, idle=10, interval=5, count=0, pbytes=11), [('A', 34), ('A', 1)] + yield 'idle-before-probe-polls', config(ka=1, timer=1, idle=20), [('A', 5), ('A', 5), ('A', 5)] + yield 'delayed-ack', config(ka=1, timer=1, idle=20, delayed=1), [('A', 5), ('A', 5)] + yield 'probe-sequence-wrap', config(seq=0), [('Z', 16), ('A', 4), ('A', 1), ('A', 9), ('A', 1)] + yield 'zero-probe-backoff-cap', config(), [('Z', 16), ('A', 5), ('A', 10), ('A', 20), ('A', 40), ('A', 80), ('A', 160), ('A', 320), ('A', 640), ('A', 1199)] + yield 'stop-timer', config(unacked=19, timer=3, pbytes=19), [('S', 0), ('A', 100)] + yield 'repeat-zero-probe-request', config(), [('Z', 16), ('A', 2), ('Z', 16), ('A', 3)] + yield 'newdata-does-not-arm-probe', config(), [('Z', 18), ('A', 100)] + + +def main(): + candidate = Path(sys.argv[1]) + with tempfile.TemporaryDirectory(prefix='tcp-raw-') as tmp: + work = Path(tmp) + # All root writes happen before executing either compiler or candidate. + with tarfile.open(HERE / 'support.tar', 'r') as archive: + for entry in archive.getmembers(): + parts = Path(entry.name).parts + if entry.isfile() and not entry.name.startswith('/') and '..' not in parts: + dest = work / entry.name + dest.parent.mkdir(parents=True, exist_ok=True) + dest.write_bytes(archive.extractfile(entry).read()) + dest = work / 'net/tcp/tcp_timer.c' + dest.write_bytes((candidate / 'net/tcp/tcp_timer.c').read_bytes()) + compile_result = subprocess.run(['gcc', '-std=gnu11', '-O0', '-I.', '-Iport', *SOURCES, '-o', 'tcp-lab'], cwd=work, capture_output=True, text=True, timeout=25) + if compile_result.returncode: + sys.stderr.write(compile_result.stderr[-3000:]) + raise AssertionError('candidate compilation') + total = 0 + for name, c, ops in cases(): + data = ' '.join(str(v) for v in c.values()) + '\n' + data += ''.join('%s %d\n' % op for op in ops) + run = subprocess.run([str(work / 'tcp-lab')], cwd=work, input=data, capture_output=True, text=True, timeout=2) + if run.returncode: + sys.stderr.write(run.stderr[-1000:]) + raise AssertionError(name + ': candidate process failed') + rows = [json.loads(line) for line in run.stdout.splitlines()] + if len(rows) != len(ops) + 1: + raise AssertionError(name + ': observation count') + model = Model(c) + model.match(rows[0], name + ': initial') + for op, row in zip(ops, rows[1:]): + model.command(*op) + model.match(row, name + ': %s %s' % op) + total += 1 + print('checked %d observations' % total) + print('PASS') + + +if __name__ == '__main__': + try: + main() + except (AssertionError, KeyError, ValueError, subprocess.TimeoutExpired) as error: + print('FAIL: ' + str(error)) + raise SystemExit(1) diff --git a/tasks/net-nuttx-tcp-retry-closure/gold/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/gold/net/tcp/tcp_timer.c new file mode 100644 index 00000000..04e247d6 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/gold/net/tcp/tcp_timer.c @@ -0,0 +1,845 @@ +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->rto = TCP_RTO << (conn->nrtx > 4 ? 4: conn->nrtx); + tcp_update_retrantimer(conn, conn->rto); + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + devif_conn_event(conn->dev, TCP_ABORT, + conn->sconn.list); + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/mask_symptom/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/mask_symptom/net/tcp/tcp_timer.c new file mode 100644 index 00000000..6c182feb --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/mask_symptom/net/tcp/tcp_timer.c @@ -0,0 +1,845 @@ +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->rto = TCP_RTO << (conn->nrtx > 1 ? 1: conn->nrtx); + tcp_update_retrantimer(conn, conn->rto); + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + devif_conn_event(conn->dev, TCP_ABORT, + conn->sconn.list); + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/noop/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/noop/net/tcp/tcp_timer.c new file mode 100644 index 00000000..a6600e67 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/noop/net/tcp/tcp_timer.c @@ -0,0 +1,843 @@ +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + return; + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->timer = TCP_RTO << (conn->nrtx > 4 ? 4: conn->nrtx); + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/only_keepalive/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/only_keepalive/net/tcp/tcp_timer.c new file mode 100644 index 00000000..6ab96a91 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/only_keepalive/net/tcp/tcp_timer.c @@ -0,0 +1,844 @@ +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->timer = TCP_RTO << (conn->nrtx > 4 ? 4: conn->nrtx); + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + devif_conn_event(conn->dev, TCP_ABORT, + conn->sconn.list); + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/only_retry/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/only_retry/net/tcp/tcp_timer.c new file mode 100644 index 00000000..1e6c9f9f --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/only_retry/net/tcp/tcp_timer.c @@ -0,0 +1,843 @@ +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->rto = TCP_RTO << (conn->nrtx > 4 ? 4: conn->nrtx); + tcp_update_retrantimer(conn, conn->rto); + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/prompt.md b/tasks/net-nuttx-tcp-retry-closure/prompt.md new file mode 100644 index 00000000..4a1e5544 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/prompt.md @@ -0,0 +1,5 @@ +现场回报:网络设备丢失应答后,业务日志里的后续重发等待出现异常;一些空闲连接持续无响应,探测额度耗尽后业务线程仍然阻塞。正常短连接的发送暂未见异常。 + +当前目录保留了设备通信模块与可控软件端口。先用 `make smoke` 运行正常路径,并参照 README 使用虚拟 tick 复现其他场景。请修复模块,使其行为符合 `docs/tcp-contract.md` 的全部条款,并保留正常轮询和发送行为。 + +最终只交付 `net/tcp/tcp_timer.c`;其他文件和新增文件不参与正式评分。 diff --git a/tasks/net-nuttx-tcp-retry-closure/spec.md b/tasks/net-nuttx-tcp-retry-closure/spec.md new file mode 100644 index 00000000..02dd2de5 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/spec.md @@ -0,0 +1,112 @@ +# net-nuttx-tcp-retry-closure + +## 身份与来源 + +- 状态:CALIBRATED;软件校准与真实 Agent 难度分开,后者 UNMEASURED。 +- 路线 D:两处独立开源真实修复;项目 https://github.com/apache/nuttx ,family `state-machine-timing`,source `oss/apache/nuttx@d1786507b619`,split `apache/nuttx`。 +- 冻结版本:`fb4a246fccb0a411ff684053ae83f9359644a909`,正是第一处修复的父提交。 +- 修复:`d1786507b619e9884d2c7ae36e82823dcf43a6ff` 与 `ab756e8dd716165b411d14843368325bf7fd581c`。 +- 第一处在真实重传时只改剩余 timer,未同步 RTO 及统一登记入口;第二处 keepalive 耗尽只释放 monitor,未将 ABORT 传播到等待 socket。两者条件、状态与修复独立;`only_retry`、`only_keepalive` 各只修一处。 +- 共同冻结核对:第一处完整修后文件与 `only_retry` 字节一致;第二处生产 hunk 的两行插入与冻结 keepalive 分支上下文一致。gold 仅把两处生产 hunk 叠到同一冻结文件,不带后续其他改动、不补改 gold。 +- 许可:逐个 C/H 文件按原 SPDX 核准,BSD-3-Clause 与 Apache-2.0;全部保留原文件头、根 LICENSE 与 NOTICE。根 LICENSE 包含仓库第三方许可汇编,不表示本实验使用所有那些模块。无真实用户资料或密钥。 +- 替身测试:软件问题可复现;虚拟 tick 和 packet 输出替代硬件,不替代重传退避、keepalive 决策、事件分发或 monitor 的生产实现。 + +## 冻结与补全文件 + +下列路径均相对 template,逐文件来源与 SHA256 另见本题 factory 下 `source-manifest.json`。所有冻结文件来自上述同一 base,目录和字节保留。 + +| 文件 | 类型/许可 | 角色 | +| --- | --- | --- | +| LICENSE | 上游冻结/原样 | 根许可及第三方声明汇编 | +| NOTICE | 上游冻结/原样 | 原项目通知 | +| net/tcp/tcp_timer.c | 上游冻结/BSD-3-Clause | 唯一候选,完整计时状态机 | +| net/tcp/tcp_appsend.c | 上游冻结/BSD-3-Clause | 完整发送与重传 | +| net/tcp/tcp_seqno.c | 上游冻结/BSD-3-Clause | 序号编解码及生成 | +| net/tcp/tcp_devpoll.c | 上游冻结/BSD-3-Clause | 工作到期后的真实设备 poll | +| net/tcp/tcp_callback.c | 上游冻结/Apache-2.0 | TCP 数据及连接事件 | +| net/tcp/tcp_monitor.c | 上游冻结/Apache-2.0 | socket 标志和监视生命周期 | +| net/devif/devif_callback.c | 上游冻结/Apache-2.0 | 原始回调表分配、释放、分发 | +| net/tcp/tcp.h | 上游冻结/Apache-2.0 | 完整内部 TCP 合同,供阅读 | +| net/devif/devif.h | 上游冻结/BSD-3-Clause | 完整事件/回调定义,供阅读 | +| include/nuttx/net/tcp.h | 上游冻结/BSD-3-Clause | 完整公开 TCP 定义,供阅读 | +| include/nuttx/net/netconfig.h | 上游冻结/BSD-3-Clause | 完整默认网络配置,供阅读 | +| port.h | 补全/Apache-2.0 | 本实验配置的类型、声明、常量和接口 | +| lab.c | 补全/Apache-2.0 | 虚拟 tick、工作队列、packet 原始输出与 socket 等待端口 | +| Makefile | 补全 | gcc 构建和正常 smoke | +| smoke.in | 补全 | ESTABLISHED 普通 poll 发送,不触发缺陷 | +| README.md | 补全 | 现场症状、构建入口、17 字段输入与 JSON 原始观察说明 | +| docs/tcp-contract.md | 补全 | 上游注释、默认配置与 RFC 整理的公开行为合同 | +| port/nuttx/config.h | 补全 | 仅包含 port.h | +| port/nuttx/net/netconfig.h | 补全 | 仅包含 port.h | +| port/nuttx/net/net.h | 补全 | 仅包含 port.h | +| port/nuttx/net/netdev.h | 补全 | 仅包含 port.h | +| port/nuttx/net/netstats.h | 补全 | 仅包含 port.h | +| port/nuttx/net/tcp.h | 补全 | 仅包含 port.h | +| port/netdev/netdev.h | 补全 | 仅包含 port.h | +| port/devif/devif.h | 补全 | 仅包含 port.h | +| port/socket/socket.h | 补全 | 仅包含 port.h | +| port/tcp/tcp.h | 补全 | 仅包含 port.h | +| port/nuttx/mm/iob.h | 补全 | 仅包含 port.h | +| port/utils/utils.h | 补全 | 仅包含 port.h | +| port/nuttx/kmalloc.h | 补全 | 仅包含 port.h | +| port/crypto/md5.h | 补全 | 仅包含 port.h,RFC6528 配置关闭 | +| port/nuttx/clock.h | 补全 | 仅包含 port.h | +| port/debug.h | 补全 | 仅包含 port.h | + +共 35 文件,候选 1 文件,grader 附件 2 文件;最大 template 文件是原样根 LICENSE,均小于 1 MiB。独有 C 代码 1273 行(去 `/* */` 注释和空行,含宏/条件编译;7 个上游 C 共 1146 行,lab.c 127 行;不计 fixture、许可证、评分器及重复包装头)。未为凑规模增加无关模块。 + +## 公开接口与评分边界 + +- `make smoke` 编译完整保留模块及 raw lab,输入 smoke.in,打印初始化和普通 poll 的原始 JSON;成功退出 0。 +- `tcp-lab` 输入/输出和命令见 README;全部时间由 tick 驱动,无真实睡眠、网络、硬件或下载依赖。 +- 使用固定 trace6 镜像 `ccr-29eug8s3-pub.cnc.bj.baidubce.com/algo/bench-runtime@sha256:1519f8daa403998e5401eeb70b973e8aac8346e25a937033bc502dc5de1641a1`,GNU C 编译器、Python 标准库。 +- 正式只挂候选 `net/tcp/tcp_timer.c`;其他干净支撑材料在非 .py 私有 `support.tar`,定 uid/gid 0、mode 0644、mtime 0。root 用 write_bytes 解包,全部写入在编译/执行前完成,不继承 host 所有者。 +- `verify.py` 原样复制 rollout/bos_verify.py。受保护父 check 独立生成固定种子输入、推演时间和协议结果。编译器与候选只在 UID/GID 65534 子进程运行;C lab 只有输入传递、事件/包记录和原始输出,不包含 PASS 判定、期望数值或退避/终止决策。 +- 真实生产链为 work expiry → tcp_poll → tcp_timer → tcp_callback/devif_conn_event → socket 等待原语 → tcp_lost_connection/tcp_stop_monitor。socket 等待原语只按收到的事件记录唤醒并调用真实丢失处理,不自行判断 keepalive、RTO 或 retry。 +- 不检查 work_queue 调用次数、monitor 释放精确次数、私有重试计数字段或 keepalive 之后额外的 TCP 状态;等待者至少收到一次应有终结事件即可。包数组顺序原样比较,不排序或归一化。 +- 编译期限 25 秒、单次候选运行 2 秒;整体验收期限 120 秒(包装器内部 100 秒)。9 类各 5 次。 + +## 判据检查项 ↔ 依据 + +表中 `docs/...` 相对 template。全部输出关系与期望都在父 check 比较;C 中的数组界限/输入检查仅用于 raw 端口内存安全。 + +| 判据检查项 | 公开依据 | 输入与观察 | +| --- | --- | --- | +| 构建、输入/输出结构、固定配置 | README.md:8、README.md:11、README.md:13;docs/tcp-contract.md:5 | 干净 gcc 构建,17 字段请求、每命令一个 JSON | +| 时间单位、到期边界、普通 poll 保留期限 | docs/tcp-contract.md:7 | 到期前 1 tick、相等时刻及活动 timer 中插入普通 poll | +| 重传 RTO、指数退避、上限与后续 deadline | docs/tcp-contract.md:9 | n=0..7、多初始 RTO/剩余时间、两轮重传的原始包时间与 RTO | +| ESTABLISHED 重传数据、最早未确认序号与保留发送序号 | docs/tcp-contract.md:11 | 非零 rexmit_seq、空应用数据、空等待者、原始包及发送序号 | +| SYN/FIN 系列重传包 | docs/tcp-contract.md:13 | SYN_RCVD/SYN_SENT/FIN_WAIT_1/CLOSING/LAST_ACK,各 n 边界 | +| 重传耗尽 reset、TIMEDOUT、socket 断开 | docs/tcp-contract.md:13 | SYN 上限 5、数据上限 8、双 socket、监听存在/缺失 | +| TIME_WAIT/FIN_WAIT_2 有限等待、CLOSED 无输出 | docs/tcp-contract.md:15 | 剩余 1/3/17 半秒,前 1 tick 与期限相等 | +| keepidle、interval、探测包和序号回绕 | docs/tcp-contract.md:17 | idle=5/15/70、interval=5/20,seq=0/1/UINT32_MAX/其他 | +| keepalive 开关、连接态和未确认数据优先 | docs/tcp-contract.md:17 | 非 ESTABLISHED、禁用、未确认数据与更短 keep tick 并存 | +| keepcnt 耗尽、ABORT、唤醒与 socket 标志 | docs/tcp-contract.md:19 | keepcnt=0/1/2/5、部分已耗预算、双 socket | +| 普通 poll 空/有数据及非连接态回归 | docs/tcp-contract.md:21 | smoke 发送 8 字节、空 poll、其他 TCP 状态 | +| delayed ACK 与无待确认段回归 | docs/tcp-contract.md:23 | 提前计时、待确认段 1、后续正常 poll | +| 零窗口探测登记、指数等待、上限、序号恢复 | docs/tcp-contract.md:25 | 首次/重复请求、NEWDATA 请求不登记、seq=0、退避到 240 上限 | +| 取消工作不再产生计时动作 | docs/tcp-contract.md:25 | 有待重传数据时 S 后前进 100 tick | + +共 14 条依据映射。上游依据另有 `net/tcp/tcp.h` 字段注释(rto、timer、keepidle、keepintvl、keepcnt),`include/nuttx/net/netconfig.h` 的默认 TCP_RTO/MAXRTX/MAXSYNRTX、真实 callback/monitor 的事件及 socket 标志注释。RFC 6298 §5.4–5.5、RFC 1122 §4.2.3.6 是外部公开背景;判据所需的本实验参数均已随题公开,无需联网查阅。 + +## 样例与验证 + +| 样例 | 预期 | 内容 | +| --- | --- | --- | +| gold | PASS | 冻结版本仅叠两处真实生产 hunk | +| valid_iterative | PASS | 用循环计算 RTO、直接更新 timer/统一登记,使用 tcp_callback 传播终结事件 | +| template | FAIL | 两处上游缺陷均保留 | +| only_retry | FAIL | 仅真实重传修复,缺少 keepalive socket 通知 | +| only_keepalive | FAIL | 仅真实 keepalive 修复,缺少正确 RTO | +| noop | FAIL | 计时入口直接返回 | +| cheat | FAIL | 固定基本 RTO 掩盖指数退避 | +| mask_symptom | FAIL | 基于 gold,但只保留前两级退避,掩盖浅层丢包症状 | +| cheat_read_grader | FAIL | 基于 gold,构造函数读取私有 check.py 成功才继续,否则受控退出 | + +`cheat_read_grader` 的可读分支用非秘密占位 `/probe` 在无网 Docker 验证为 PASS,真实权限下为受控 FAIL;不把失败编译或损坏 gold 作为隔离证明。原始诊断日志、精确容器名称及清理事实在本题 factory 下保存。 + +正式 Docker 校准回执:`runs/20261103-D-oss-fix-29/nuttx-d178650/calibration-7d3ed6ebc88b4ddfb6a81007ac0f6597/receipt.json`。当前 task digest:`369464716845c6f702ee4d5d344be27a58f4c20e7fa4583631d7d64d47d21954`。9 类每类 5 次共 45 次全部符合预期,整体 PASS,全部清理 confirmed;镜像 ID 为 `sha256:1519f8daa403998e5401eeb70b973e8aac8346e25a937033bc502dc5de1641a1`。本题 93 个精确容器名(含三次诊断及旧指纹校准)均经 docker inspect 确认不存在,无创建专用网络。最终判据有 113 个场景、414 个命令后观察;原始初始化观察另计。为补全禁用 keepalive 同时有活动 timer、最后一次重传后的终结期限覆盖,保留旧指纹 45 次 PASS 回执并按最终指纹完整重跑。未运行 trusted-local;不在 host 执行上游代码。全库 unittest 与独立 root 审阅由主 Agent 负责,本题未宣称它们完成。 + +## 限制与未验证 + +当前只有软件实验,不含完整 OS、真实 recv 系统调用/阻塞线程、网络对端、抓包、板卡或电气验证。socket 等待原语模拟事件观察和唤醒,socket 标志改变、监视释放以及事件遍历仍执行真实上游 C。只对公开实验配置和有定义的输入检查;原始输入的任意非法数值或关闭的 write-buffer、SOLINGER、NewReno、IPv6、RFC6528 配置不在验收中。真实 Agent rollout、会话完整性、模型成功率和难度全部 UNMEASURED。未调用模型、未提交、未改平台或其他任务。 diff --git a/tasks/net-nuttx-tcp-retry-closure/support.tar b/tasks/net-nuttx-tcp-retry-closure/support.tar new file mode 100644 index 0000000000000000000000000000000000000000..54c7daf0ca69a7eb7325a5c08f234d81c348573f GIT binary patch literal 675840 zcmeFa+jbjAwk5h>8($HnaZX$6=3Nvi*;UmAk&uKk32+0TWp|C;tOSrC(*%&%nF(I} zJLlm%pKsVdcII3WkvAm3B*>CwJ2i$R5{Qh9>$=Xh)=BfI(dst-?O*w42Y(Ls_y3Lm z<4@-Q-TgQFZ*~vtXXg8Z!^6FQQ#=34LD|0NVdMmOz5V?U_P<*Fr#^EASFSqo2JSR; zYbzT6;0B@RPu1Sm&W8HCGo3rZz1rK^*-QK>vz1?!;HCujgxjoje3b$)~ zu-E9Ec2&J~td82P<7ThfZgtg%c1N9e8yl+AIP0{J&yV==hO|0vc6*)X`*VIG9qewY z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2i(Ya_vj5T)f??wXY=?5QwBL{|$fk}1HGxRFWCL|ynGHeQeS?$wiaKth71WIV8 zK9Rh;>{-@x_qsOYtJKOl_1HQYs*KE@uVq`TS8aC=lH` z9iw5NYe4P>G_(Wl3`(sBjmq6~A!3EhK+cq3rkm(yAY}e3!x7z{WX9+_nEItF;rh%$v}*=hgP_Tv z8!9}4aL4v6$u2Aud2g%(M>F2?^gU~q^gGgPJ$Ht1bO<2zn4x5j?Jy_#+DC&Q!bIjV zjIPQ3+|0K`Y77=G2Tf0`El$Q&!-H-MyhnKUqJ{p@|L~+NbzaJLS<&}mFzG@f0;<)~ zGy44%`l<}B+)7h#WlYQ`Sdx~K!^gb^+Y@@0p`Q%Y1{3HqVHXnQ)OtLcPZz}v$y-pl znm`hXPo^SoH^`}IfX##r0Q}SL9C&i9!2DoW3Z=&*Q62~$B6A#nU<1vwYf6F+GRc$2 z3vv!3P&M8|pkr@&_pFIoq|~BR$lt{GZu@MU@es+rJoEQSd(uX^^s%GAZX}iMSJu4z z?1X)ek7@h}%|^f3?q&_)7L(O9h^JwrV4nm5 + + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +IGMP +==== + +IGMP support, if enabled, adds additional logic by Steve Reynolds: + + Copyright (c) 2002 CITEL Technologies Ltd. + All rights reserved. + +The HID Parser in drivers/usbhost +================================= + + Adapted from the LUFA Library (HPND-sell-variant): + + Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com) + dean [at] fourwalledcubicle [dot] com, www.lufa-lib.org + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. + +Certain functions in the NuttX C-library derive from other BSD-compatible +sources: + +fs/nfs +include/nuttx/fs/nfs.h +====== + + NFS: + + Copyright (c) 1989, 1991, 1993, 1995 The Regents of the University of + California. All rights reserved. + + and RPC also has: + + Copyright (c) 2004 The Regents of the University of Michigan. + Copyright (c) 2004 Weston Andros Adamson . + Copyright (c) 2004 Marius Aamodt Eriksen . + + The University of Michigan uses a modified BSD license equivalent to + the NuttX license. The University of California code uses the original + BSD license and requires that I state the following: + + "This product includes software developed by the University of + California, Berkeley and its contributors." + +fs/spiffs +========= + + The MIT License (MIT) + + Copyright (c) 2013-2017 Peter Andersson (pelleplutt1976gmail.com) + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +strtod() +======== + + Copyright (C) 2002 Michael Ringgaard. All rights reserved. + Copyright (C) 2006-2007 H. Peter Anvin. + +dtoa() +====== + + If you enable floating point conversions with CONFIG_LIBC_FLOATINGPOINT, + then some files with an unmodified BSD license will be included. That + license is similar to the above (modified) BSD license, but has an + additional requirement that I state the following: + + "This product includes software developed by the University of + California, Berkeley and its contributors." + +libc/string/lib_vikmemcpy.c +=========================== + + If you enable CONFIG_MEMCPY_VIK, then you will build with the optimized + version of memcpy from Daniel Vik. Licensing information for that version + of memcpy() follows: + + Copyright (C) 1999-2010 Daniel Vik + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any + damages arising from the use of this software. + Permission is granted to anyone to use this software for any + purpose, including commercial applications, and to alter it and + redistribute it freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you + must not claim that you wrote the original software. If you + use this software in a product, an acknowledgment in the + use this software in a product, an acknowledgment in the + product documentation would be appreciated but is not + required. + + 2. Altered source versions must be plainly marked as such, and + must not be misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + +libs/libc/machine/arm/armv7-a/gnu/arch_memchr.S +libs/libc/machine/arm/armv7-r/gnu/arch_memchr.S +libs/libc/machine/arm/armv7-m/gnu/arch_memchr.S +libs/libc/machine/arm/armv8-m/gnu/arch_memchr.S +================================================ + + Copyright (c) 2010-2011, Linaro Limited + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Linaro Limited nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Written by Dave Gilbert + + Copyright (c) 2015 ARM Ltd. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Linaro nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm/armv7-a/gnu/arch_memcpy.S +libs/libc/machine/arm/armv7-a/gnu/arch_strlen.S +libs/libc/machine/arm/armv7-r/gnu/arch_memcpy.S +libs/libc/machine/arm/armv7-r/gnu/arch_strlen.S +libs/libc/machine/arm64/gnu/arch_strnlen.S +================================================ + + Copyright (c) 2013, Linaro Limited + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Linaro Limited nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +libs/libc/machine/arm/armv7-a/gnu/arch_memmove.S +libs/libc/machine/arm/armv7-r/gnu/arch_memmove.S +libs/libc/machine/arm/armv7-m/gnu/arch_memmove.S +libs/libc/machine/arm/armv7-m/gnu/arch_memset.S +libs/libc/machine/arm/armv8-m/gnu/arch_memmove.S +libs/libc/machine/arm/armv8-m/gnu/arch_memset.S +================================================ + + Copyright (c) 2015 ARM Ltd + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +libs/libc/machine/arm/armv7-a/gnu/arch_memset.S +libs/libc/machine/arm/armv7-r/gnu/arch_memset.S +================================================ + + Copyright (C) 2013 The Android Open Source Project + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + +libs/libc/machine/arm/armv7-a/gnu/arch_strcmp.S +libs/libc/machine/arm/armv7-r/gnu/arch_strcmp.S +================================================ + + Copyright (c) 2011 The Android Open Source Project + Copyright (c) 2008 ARM Ltd + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm/armv7-m/gnu/arch_memcpy.S +libs/libc/machine/arm/armv8-m/gnu/arch_memcpy.S +================================================ + + Copyright (c) 2013 ARM Ltd + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm/armv7-m/gnu/arch_strcmp.S +libs/libc/machine/arm/armv8-m/gnu/arch_strcmp.S +libs/libc/machine/arm/armv7-m/gnu/acle-compat.h +libs/libc/machine/arm/armv8-m/gnu/acle-compat.h +libs/libc/machine/arm/armv7-r/gnu/acle-compat.h +libs/libc/machine/arm/armv7-a/gnu/acle-compat.h +================================================ + + Copyright (c) 2012-2014 ARM Ltd + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm/armv7-m/gnu/arch_strlen.S +libs/libc/machine/arm/armv8-m/gnu/arch_strlen.S +================================================ + + Copyright (c) 2010-2011,2013 Linaro Limited + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Linaro Limited nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Copyright (c) 2015 ARM Ltd. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Linaro nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm64/gnu/arch_memchr.S +libs/libc/machine/arm64/gnu/arch_strchr.S +libs/libc/machine/arm64/gnu/arch_strchrnul.S +libs/libc/machine/arm64/gnu/arch_strrchr.S +================================================ + + Copyright (c) 2014, ARM Limited + All rights Reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the company nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm64/gnu/arch_memcmp.S +================================================ + + Copyright (c) 2018, Linaro Limited + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Linaro Limited nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Copyright (c) 2017 ARM Ltd + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm64/gnu/arch_memcpy.S +libs/libc/machine/arm64/gnu/arch_memmove.S +libs/libc/machine/arm64/gnu/arch_memset.S +================================================ + + Copyright (c) 2012-2013, Linaro Limited + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Linaro Limited nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Copyright (c) 2015 ARM Ltd + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm64/gnu/arch_strcmp.S +================================================ + + Copyright (c) 2012-2018, Linaro Limited + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Linaro Limited nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm64/gnu/arch_strcpy.S +================================================ + + Copyright (c) 2013, 2014, 2015 ARM Ltd. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Linaro nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm64/gnu/arch_strlen.S +================================================ + + Copyright (c) 2013-2015, Linaro Limited + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Linaro Limited nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm64/gnu/arch_strncmp.S +================================================ + + Copyright (c) 2013, 2018, Linaro Limited + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Linaro Limited nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/risc-v/gnu/arch_memset.S +libs/libc/machine/risc-v/gnu/arch_strcmp.S +================================================ + + Copyright (c) 2017 SiFive Inc. All rights reserved. + + This copyrighted material is made available to anyone wishing to use, + modify, copy, or redistribute it subject to the terms and conditions + of the FreeBSD License. This program is distributed in the hope that + it will be useful, but WITHOUT ANY WARRANTY expressed or implied, + including the implied warranties of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. A copy of this license is available at + http://www.opensource.org/licenses. + +libs/libc/misc/lib_crc32.c +========================= + + Copyright (C) 2010-2011 Gregory Nutt. All rights reserved. + + The logic in this file was developed by Gary S. Brown: + + COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or code or tables + extracted from it, as desired without restriction. + + First, the polynomial itself and its table of feedback terms. The polynomial is: + + X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 + + Note that we take it "backwards" and put the highest-order term in the lowest-order bit. + The X^32 term is "implied"; the LSB is the X^31 term, etc. The X^0 term (usually shown + as "+1") results in the MSB being 1 + + Note that the usual hardware shift register implementation, which is what we're using + (we're merely optimizing it by doing eight-bit chunks at a time) shifts bits into the + lowest-order term. In our implementation, that means shifting towards the right. Why + do we do it this way? Because the calculated CRC must be transmitted in order from + highest-order term to lowest-order term. UARTs transmit characters in order from LSB + to MSB. By storing the CRC this way we hand it to the UART in the order low-byte to + high-byte; the UART sends each low-bit to hight-bit; and the result is transmission bit + by bit from highest- to lowest-order term without requiring any bit shuffling on our + part. Reception works similarly + + The feedback terms table consists of 256, 32-bit entries. Notes + + - The table can be generated at runtime if desired; code to do so is shown later. It + might not be obvious, but the feedback terms simply represent the results of eight + shift/xor operations for all combinations of data and CRC register values + + - The values must be right-shifted by eight bits by the updcrc logic; the shift must + be u_(bring in zeroes). On some hardware you could probably optimize the shift in + assembler by using byte-swap instructions polynomial $edb88320 + +libs/libc/misc/lib_fnmatch.c +=============================== + + Copyright 1995, 2000 by Jef Poskanzer . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + +libs/libc/misc/lib_ncompress.c +============================== + + Copyright (C) 2008 Gregory Nutt. All rights reserved. + + This is the file compress24.c extracted from the ncompress-4.2.4 release + and adapted for NuttX. The original code was released into the public + domain. This NuttX version is re-released under the standard NuttX + BSD 3-clause license. The original authors are listed below: + + Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) + Jim McKie (decvax!mcvax!jim) + Steve Davies (decvax!vax135!petsd!peora!srd) + Ken Turkowski (decvax!decwrl!turtlevax!ken) + James A. Woods (decvax!ihnp4!ames!jaw) + Joe Orost (decvax!vax135!petsd!joe) + Dave Mack (csu@alembic.acs.com) + Peter Jannesen, Network Communication Systems + (peter@ncs.nl) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +libs/libc/misc/lib_utsname.c +============================= + + Copyright (C) 2015 Stavros Polymenis. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +libs/libc/net/lib_addrconfig.c +=============================== + + Copyright (c) 2015, Max Nekludov. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the Institute nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + +libs/libc/stdlib/lib_strtod.c +libs/libc/stdlib/lib_strtof.c +libs/libc/stdlib/lib_strtold.c +============================= + + Copyright (C) 2002 Michael Ringgaard. All rights reserved. + Copyright (C) 2006-2007 H. Peter Anvin. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the project nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/stdlib/lib_wctomb.c +============================= + + This code is derived from software contributed to Berkeley by + Chris Torek. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + +libs/libc/string/lib_vikmemcpy.c +================================== + + Copyright (C) 1999-2010 Daniel Vik + + Adaptations include: + - File name change + - Use of types defined in stdint.h + - Integration with the NuttX configuration system + - Other cosmetic changes for consistency with NuttX coding standards + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any + damages arising from the use of this software. + Permission is granted to anyone to use this software for any + purpose, including commercial applications, and to alter it and + redistribute it freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you + must not claim that you wrote the original software. If you + use this software in a product, an acknowledgment in the + use this software in a product, an acknowledgment in the + product documentation would be appreciated but is not + required. + + 2. Altered source versions must be plainly marked as such, and + must not be misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + + Description: Implementation of the standard library function memcpy. + This implementation of memcpy() is ANSI-C89 compatible. + + The following configuration options can be set: + + CONFIG_ENDIAN_BIG + Uses processor with big endian addressing. Default is little endian. + + CONFIG_MEMCPY_PRE_INC_PTRS + Use pre increment of pointers. Default is post increment of pointers. + + CONFIG_MEMCPY_INDEXED_COPY + Copying data using array indexing. Using this option, disables the + CONFIG_MEMCPY_PRE_INC_PTRS option. + + CONFIG_MEMCPY_64BIT - Compiles memcpy for 64 bit architectures + +libs/libc/math +============== + + If you enable CONFIG_LIB, you will build the math library at libc/math. + This library was taken from the math library developed for the Rhombus + OS by Nick Johnson (https://github.com/nickbjohnson4224/rhombus). This + port was contributed by Darcy Gong. The Rhombus math library has this + compatible MIT license: + + Copyright (C) 2009-2011 Nick Johnson + + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + +libs/libc/lzf +include/lzf +====================== + +Copyright (c) 2000-2007 Marc Alexander Lehmann + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +drivers/video/ov2640 +==================== + + WARNING: Some of the information in the data tables in this file came + from other projects with conflicting licenses: Linux and ArduCAM. Those + both have GPL licenses. I am not sure if it is proper or not to lift the + content of those tables and still retain this BSD license. I am guessing + so, but I am not a copyright attorney so you should use this driver in + products at your own risk. + +include/nuttx/lcd/ili9488.h +=========================== + + Copyright (c) 2011, Atmel Corporation + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + - Redistributions of source code must retain the above copyright notice, + this list of conditions and the disclaimer below. + + - Atmel's name may not be used to endorse or promote products derived + from this software without specific prior written permission. + + DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE + DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include/nuttx/input/x11_keysym.h +include/nuttx/input/x11_keysymdef.h +include/nuttx/input/x11_xf86keysym.h +=================================== + + Copyright 1987, 1994, 1998 The Open Group + + Permission to use, copy, modify, distribute, and sell this software and + its documentation for any purpose is hereby granted without fee, provided + that the above copyright notice appear in all copies and that both that + copyright notice and this permission notice appear in supporting + documentation. + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + Except as contained in this notice, the name of The Open Group shall + not be used in advertising or otherwise to promote the sale, use or + other dealings in this Software without prior written authorization + from The Open Group. + + Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts + + All Rights Reserved + + Permission to use, copy, modify, and distribute this software and its + documentation for any purpose and without fee is hereby granted, + provided that the above copyright notice appear in all copies and that + both that copyright notice and this permission notice appear in + supporting documentation, and that the name of Digital not be + used in advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING + ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL + DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR + ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + SOFTWARE. + +include/nuttx/input/xbox-controller.h +===================================== + + Copyright (C) 2016 Brian Webb. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +include/nuttx/ioexpander/tca64xx.h +=================================== + + Copyright (C) 2016 Gregory Nutt. All rights reserved. + +This header file derives, in part, from the Project Ara TCA64xx driver +which has this copyright: + + Copyright (c) 2014-2015 Google Inc. + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +3. Neither the name of the copyright holder nor the names of its +contributors may be used to endorse or promote products derived from this +software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include/nuttx/power/relay.h +arch/arm/src/tiva/hardware/tiva_epi.h +============================ + + Copyright (C) 2013, 2015 Max Nekludov. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +include/nuttx/sensors/kxtj9.h +============================= + + Copyright (C) 2016 Gregory Nutt. All rights reserved. + +This driver derives from the Motorola Moto Z MDK: + + Copyright (c) 2016 Motorola Mobility, LLC. + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include/sys/tree.h +==================== + +Copyright 2002 Niels Provos +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include/nuttx/video/edid.h +include/nuttx/video/vesagtf.h +include/nuttx/video/videomode.h +================================= + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + +Updated and extended with definitions from FreeBSD which has a compatible +2-clause BSD license: + + Copyright (c) 2006 Itronix Inc. All rights reserved. + Written by Garrett D'Amore for Itronix Inc. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +include/sys/epoll.h +==================== + + Copyright (C) 2015 Anton D. Kachalov. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/xtensa/src/esp32/chip_macros.h +=================================== + + Copyright (c) 2006-2015 Cadence Design Systems Inc. + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +arch/arm/src/arm/arm_cache.S +============================ + + Copyright (C) 2007, 2009, 2013 Gregory Nutt. All rights reserved. + + Several of these cache operations come from Atmel sample code with + modifications for better integration with NuttX. The Atmel sample code + has a BSD compatible license that requires this copyright notice: + + Copyright (c) 2008, Atmel Corporation + + [Actually, I think that all of the Atmel functions are commented out now] + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the names NuttX nor Atmel nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv6-m/arm_exception.S +================================== + + Copyright (C) 2013 Gregory Nutt. All rights reserved. + + This file was leveraged from the ARMv7-M version which has, in addition: + + Copyright (C) 2012 Michael Smith. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv7-a/cp15_cacheops.h +arch/arm/src/armv7-a/cp15_clean_dcache.S +arch/arm/src/armv7-a/cp15_clean_dcache_all.S +arch/arm/src/armv7-a/cp15_coherent_dcache.S +arch/arm/src/armv7-a/cp15_flush_dcache.S +arch/arm/src/armv7-a/cp15_flush_dcache_all.S +arch/arm/src/armv7-a/cp15_invalidate_dcache.S +arch/arm/src/armv7-a/cp15_invalidate_dcache_all.S +================================================= + + Copyright (C) 2013-2014 Gregory Nutt. All rights reserved. + + Portions of this file derive from Atmel sample code for the SAMA5D3 + Cortex-A5 which also has a modified BSD-style license: + + Copyright (c) 2012, Atmel Corporation + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor Atmel nor the names of the contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv7-m/arm_cache.c +arch/arm/src/armv8-m/arm_cache.c +================================ + + Copyright (C) 2015, 2018-2019 Gregory Nutt. All rights reserved. + + Some logic in this header file derives from the ARM CMSIS core_cm7.h + header file which has a compatible 3-clause BSD license: + + Copyright (c) 2009 - 2014 ARM LIMITED. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name ARM, NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv7-m/arm_stackcheck.c +arch/arm/src/armv8-m/arm_stackcheck.c +===================================== + + Copyright (c) 2013, 2014 PX4 Development Team. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name PX4 nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv6-m/arm_vectors.c +arch/arm/src/armv7-m/arm_vectors.c +arch/arm/src/armv8-m/arm_vectors.c +================================== + + Copyright (C) 2012 Michael Smith. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv7-m/arm_itm.c +arch/arm/src/armv7-m/dwt.h +arch/arm/src/armv7-m/itm.h +arch/arm/src/armv7-m/tpi.h +arch/arm/src/armv8-m/arm_itm.c +arch/arm/src/armv8-m/dwt.h +arch/arm/src/armv8-m/itm.h +arch/arm/src/armv8-m/tpi.h +========================== + + Copyright (c) 2009 - 2013 ARM LIMITED + + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + - Neither the name of ARM nor the names of its contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Copyright (C) 2014 Pierre-noel Bouteville . All rights reserved. + Copyright (C) 2014 Gregory Nutt. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv7-m/gnu/arm_exception.S +arch/arm/src/armv8-m/arm_exception.S +======================================== + + Copyright (C) 2009-2013, 2015-2016, 2018 Gregory Nutt. + All rights reserved. + Copyright (C) 2012 Michael Smith. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv7-m/etm.h +arch/arm/src/armv8-m/etm.h +arch/arm/src/efm32/efm32_flash.c +arch/arm/src/efm32/hardware/efm32_acmp.h +arch/arm/src/efm32/hardware/efm32_adc.h +arch/arm/src/efm32/hardware/efm32_aes.h +arch/arm/src/efm32/hardware/efm32_burtc.h +arch/arm/src/efm32/hardware/efm32_calibrate.h +arch/arm/src/efm32/hardware/efm32_cmu.h +arch/arm/src/efm32/hardware/efm32_dac.h +arch/arm/src/efm32/hardware/efm32_devinfo.h +arch/arm/src/efm32/hardware/efm32_dma.h +arch/arm/src/efm32/hardware/efm32_emu.h +arch/arm/src/efm32/hardware/efm32_flash.h +arch/arm/src/efm32/hardware/efm32gg_memorymap.h +arch/arm/src/efm32/hardware/efm32g_memorymap.h +arch/arm/src/efm32/hardware/efm32_gpio.h +arch/arm/src/efm32/hardware/efm32_i2c.h +arch/arm/src/efm32/hardware/efm32_lcd.h +arch/arm/src/efm32/hardware/efm32_lesense.h +arch/arm/src/efm32/hardware/efm32_letimer.h +arch/arm/src/efm32/hardware/efm32_leuart.h +arch/arm/src/efm32/hardware/efm32_memorymap.h +arch/arm/src/efm32/hardware/efm32_msc.h +arch/arm/src/efm32/hardware/efm32_pcnt.h +arch/arm/src/efm32/hardware/efm32_prs.h +arch/arm/src/efm32/hardware/efm32_rmu.h +arch/arm/src/efm32/hardware/efm32_romtable.h +arch/arm/src/efm32/hardware/efm32_rtc.h +arch/arm/src/efm32/hardware/efm32tg_memorymap.h +arch/arm/src/efm32/hardware/efm32_timer.h +arch/arm/src/efm32/hardware/efm32_usart.h +arch/arm/src/efm32/hardware/efm32_usb.h +arch/arm/src/efm32/hardware/efm32_vcmp.h +arch/arm/src/efm32/hardware/efm32_wdog.h +======================================== + + Copyright 2014 Silicon Laboratories, Inc. http://www.silabs.com + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software.@n + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software.@n + 3. This notice may not be removed or altered from any source distribution. + + DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Laboratories, Inc. + has no obligation to support this Software. Silicon Laboratories, Inc. is + providing the Software "AS IS", with no express or implied warranties of + any kind, including, but not limited to, any implied warranties of + merchantability or fitness for any particular purpose or warranties + against infringement of any proprietary rights of a third party. + + Silicon Laboratories, Inc. will not be liable for any consequential, + incidental, or special damages, or any other relief, or for any claim by + any third party, arising from your use of this Software. + + Copyright (C) 2014 Pierre-noel Bouteville . All rights reserved. + Copyright (C) 2014 Gregory Nutt. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv7-r/cp15_cacheops.h +==================================== + + Copyright (C) 2015 Gregory Nutt. All rights reserved. + + Portions of this file derive from Atmel sample code for the SAMA5D3 + Cortex-A5 which also has a modified BSD-style license: + + Copyright (c) 2012, Atmel Corporation + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor Atmel nor the names of the contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/armv7-r/cp15_cacheops.h +arch/arm/src/armv7-r/cp15_clean_dcache.S +arch/arm/src/armv7-r/cp15_clean_dcache_all.S +arch/arm/src/armv7-r/cp15_coherent_dcache.S +arch/arm/src/armv7-r/cp15_flush_dcache.S +arch/arm/src/armv7-r/cp15_flush_dcache_all.S +arch/arm/src/armv7-r/cp15_invalidate_dcache.S +arch/arm/src/armv7-r/cp15_invalidate_dcache_all.S +================================================= + + Copyright (C) 2015 Gregory Nutt. All rights reserved. + + Portions of this file derive from Atmel sample code for the SAMA5D3 + Cortex-A5 which also has a modified BSD-style license: + + Copyright (c) 2012, Atmel Corporation + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor Atmel nor the names of the contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/sam34/sam4cm_freerun.c +arch/arm/src/sam34/sam4cm_oneshot.c +arch/arm/src/sam34/sam4cm_tc.c +arch/arm/src/sama5/sam_gf1024.c +arch/arm/src/sama5/sam_gf512.c +arch/arm/src/sama5/sam_pmecc.c +=================================== + + Copyright (C) 2015 Gregory Nutt. All rights reserved. + + References: + + Atmel NoOS sample code. + + The Atmel sample code has a BSD compatible license that requires this + copyright notice: + + Copyright (c) 2011, Atmel Corporation + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the names NuttX nor Atmel nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/sam34/sam_twi.c +arch/arm/src/sama5/sam_adc.c +arch/arm/src/sama5/sam_can.c +arch/arm/src/sama5/sam_emaca.c +arch/arm/src/sama5/sam_freerun.c +arch/arm/src/sama5/sam_gmac.c +arch/arm/src/sama5/sam_lcd.c +arch/arm/src/sama5/sam_nand.c +arch/arm/src/sama5/sam_oneshot.c +arch/arm/src/sama5/sam_pmecc.h +arch/arm/src/sama5/sam_tc.c +arch/arm/src/sama5/sam_tsd.c +arch/arm/src/sama5/sam_twi.c +arch/arm/src/sama5/sam_udphs.c +arch/arm/src/samd2l2/hardware/samd_fuses.h +arch/arm/src/samd5e5/sam_gmac.c +arch/arm/src/samv7/sam_freerun.c +arch/arm/src/samv7/sam_oneshot.c +arch/arm/src/samv7/sam_tc.c +arch/arm/src/samv7/sam_twihs.c +============================== + + Copyright (C) 2013, 2015-2017 Gregory Nutt. All rights reserved. + + The Atmel sample code has a BSD compatible license that requires this + copyright notice: + + Copyright (c) 2011, Atmel Corporation + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX, Atmel, nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + +arch/arm/src/samd2l2/sam_i2c_master.c +arch/arm/src/samd5e5/sam_i2c_master.c +======================================= + Copyright (C) 2013-2014, 2017 Gregory Nutt. All rights reserved. + Copyright (C) 2015 Filament - www.filament.com + + The Atmel sample code has a BSD compatible license that requires this + copyright notice: + + Copyright (c) 2011, Atmel Corporation + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX, Atmel, nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/samv7/sam_usbdevhs.c +================================= + + Copyright (C) 2015-2016, 2019 Gregory Nutt. All rights reserved. + + This code derives from the UDPHS device controller driver for the SAMA5D3. + That code, in turn, includes some reference logic extracted from the + SAMA5D3 sample code. That Atmel sample code has a BSD compatible license + that requires this copyright notice: + + Copyright (c) 2009, Atmel Corporation + + Additional updates for the SAMV7 was taken from Atmel sample code for the + SAMV71: + + Copyright (c) 2014, Atmel Corporation + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX, Atmel, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/x86_64/include/multiboot2.h +================================ + + Copyright (C) 1999,2003,2007,2008,2009,2010 Free Software Foundation, Inc. + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + ANY DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + +arch/xtensa/include/esp32/core-isa.h +===================================== + + Xtensa processor CORE configuration + + Customer ID=11657; Build=0x5fe96; Copyright (c) 1999-2016 Tensilica Inc. + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +arch/xtensa/include/xtensa/xtensa_specregs.h +arch/xtensa/include/xtensa/xtensa_corebits.h +============================================ + + + Adapted from use in NuttX by: + + Copyright (C) 2016 Gregory Nutt. All rights reserved. + + Derives from logic originally provided by Tensilica Inc. + + Copyright (c) 2005-2011 Tensilica Inc. + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +arch/xtensa/src/common/xtensa_windowspill.S +=========================================== + + Copyright (C) 2016 Gregory Nutt. All rights reserved. + + Derives from logic originally provided by Tensilica Inc. + + Copyright (c) 1999-2010 Tensilica Inc. + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +arch/xtensa/include/esp32/tie-asm.h +arch/xtensa/include/esp32/tie.h +=============================== + + Compile-time assembler definitions dependent on CORE & TIE + + This header file contains assembly-language definitions (assembly + macros, etc.) for this specific Xtensa processor's TIE extensions + and options. It is customized to this Xtensa processor configuration. + + Customer ID=11657; Build=0x5fe96; + Copyright (c) 1999-2016 Cadence Design Systems Inc. + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +arch/xtensa/include/xtensa/core.h +================================= + + Leveraged to NuttX by: + + Copyright (C) 2016 Gregory Nutt. All rights reserved. + + This file derives from code provided Cadence Design Systems, Inc.: + + Copyright (c) 2005-2014 Cadence Design Systems, Inc. + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +arch/xtensa/include/xtensa/xtensa_coproc.h +arch/xtensa/src/common/xtensa_abi.h +arch/xtensa/src/common/xtensa_context.S +arch/xtensa/src/common/xtensa_coproc.S +arch/xtensa/src/common/xtensa_cpuint.S +arch/xtensa/src/common/xtensa_int_handlers.S +arch/xtensa/src/common/xtensa_panic.S +arch/xtensa/src/common/xtensa_timer.h +arch/xtensa/src/common/xtensa_user_handler.S +arch/xtensa/src/common/xtensa_vectors.S +arch/xtensa/src/common/xtensa_window_vector.S +arch/xtensa/src/esp32/chip_macros.h +=================================== + + Copyright (C) 2016 Gregory Nutt. All rights reserved. + + Derives from logic originally provided by Cadence Design Systems Inc. + + Copyright (c) 2006-2015 Cadence Design Systems Inc. + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +drivers/analog/ad5410.c +drivers/analog/ads1255.c +============================== + + Copyright (C) 2008, 2009, 2010, 2016 Gregory Nutt. All rights reserved. + Copyright (C) 2011 Li Zhuoyi. All rights reserved. + + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +drivers/lcd/ra8875.c +drivers/lcd/ra8875.h +include/nuttx/lcd/ra8875.h +================================ + + Copyright (C) 2015 Intuitive Aerial AB. All rights reserved. + +References: RA8875, Rev 1.6, Apr 2013, RAiO Technologies Inc + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +drivers/modem/u-blox.c +include/nuttx/modem/u-blox.h +======================= + + Copyright (C) 2016 Vladimir Komendantskiy. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +drivers/net/tun.c +======================= + + Copyright (C) 2015-2016 Max Nekludov. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +drivers/power/bq2429x.c +include/nuttx/power/bq2429x.h +=========================== + + Copyright (C) 2017 Neil Hancock. All rights reserved. + Copyright (C) 2017 Haltian Ltd. All rights reserved. + Copyright (C) 2016 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +drivers/sensors/as726x.c +include/nuttx/sensors/as726x.h +============================ + + Copyright (C) 2019 Fabian Justi. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +drivers/sensors/lis3dh.c +include/nuttx/sensors/lis3dh.h +=========================== + + Copyright (C) 2018 Extent3D. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +drivers/sensors/lsm303agr.c +drivers/sensors/lsm6dsl.c +=============================== + + Copyright (C) 2018 Inc. All rights reserved. + Copyright (C) 2016 Omni Hoverboards Inc. All rights reserved. + Copyright (C) 2016, 2019 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +drivers/sensors/vl53l1x.c +include/nuttx/sensors/vl53l1x.h +============================== + + Copyright (C) 2019 Acutronics Robotics + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32/photon/src/stm32_wlan_firmware.c +drivers/wireless/ieee80211/bcm43xxx/bcmf_ioctl.h +================================================ + + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of Broadcom nor the names of other contributors to + this software may be used to endorse or promote products derived from + this software without specific prior written permission. + + 4. This software may not be used as a standalone product, and may only be + used as incorporated in your product or device that incorporates Broadcom + wireless connectivity products and solely for the purpose of enabling the + functionalities of such Broadcom products. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING, + BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR + A PARTICULAR PURPOSE AND NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT + SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + +drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio_core.h +drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio_regs.h +========================================== + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +include/nuttx/wireless/ieee80211/ieee80211_cypto.h +========================================== + + Copyright (c) 2007, 2008 Damien Bergamini + + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +include/nuttx/wireless/ieee80211/ieee80211.h +=============================================== + + Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/lc823450/lc823450_sdc.c +arch/arm/src/lc823450/lc823450_sddrv_dep.c +arch/arm/src/lc823450/lc823450_sddrv_if.h +arch/arm/src/lc823450/lc823450_sddrv_type.h +arch/arm/src/lc823450/lc823450_symbols.ld +============================================ + + Copyright (C) 2014-2015 ON Semiconductor. All rights reserved. + Copyright 2014,2015,2016,2017 Sony Video & Sound Products Inc. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/lpc43xx/spifi/src/spifilib_fam_standard_cmd.c +arch/arm/src/lpc43xx/spifi/src/spifilib_dev_common.c +arch/arm/src/lpc43xx/spifi/inc/spifilib_dev.h +arch/arm/src/lpc43xx/spifi/inc/spifilib_api.h +arch/arm/src/lpc43xx/spifi/inc/private/spifilib_chiphw.h +========================================================= + + Copyright(C) NXP Semiconductors, 2014 + All rights reserved. + Software that is described herein is for illustrative purposes only + which provides customers with programming information regarding the + LPC products. This software is supplied "AS IS" without any warranties of + any kind, and NXP Semiconductors and its licenser disclaim any and + all warranties, express or implied, including all implied warranties of + merchantability, fitness for a particular purpose and non-infringement of + intellectual property rights. NXP Semiconductors assumes no + responsibility or liability for the use of the software, conveys no + license or rights under any patent, copyright, mask work right, or any + other intellectual property rights in or to any products. NXP + Semiconductors reserves the right to make changes in the software without + notification. NXP Semiconductors also makes no representation or warranty + that such application will be suitable for the specified use without + further testing or modification. + Permission to use, copy, modify, and distribute this software and its + documentation is hereby granted, under NXP Semiconductors' and its + licensor's relevant copyrights in the software, without fee, provided that + it is used in conjunction with NXP Semiconductors microcontrollers. This + copyright, permission, and disclaimer notice must appear in all copies of + this code. + +boards/arm/imxrt/imxrt1050-evk/src/imxrt_sdram_ini_dcd.c +========================================================= + + Copyright (C) 2017, NXP Semiconductors, Inc. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +boards/arm/s32k1xx/s32k118evb/src/s32k1xx_clockconfig.c +boards/arm/s32k1xx/s32k118evb/src/s32k1xx_periphclocks.c +boards/arm/s32k1xx/s32k144evb/src/s32k1xx_clockconfig.c +boards/arm/s32k1xx/s32k144evb/src/s32k1xx_periphclocks.c +boards/arm/s32k1xx/s32k146evb/src/s32k1xx_clockconfig.c +boards/arm/s32k1xx/s32k146evb/src/s32k1xx_periphclocks.c +boards/arm/s32k1xx/s32k148evb/src/s32k1xx_clockconfig.c +boards/arm/s32k1xx/s32k148evb/src/s32k1xx_periphclocks.c +boards/arm/s32k1xx/ucans32k146/src/s32k1xx_clockconfig.c +boards/arm/s32k1xx/ucans32k146/src/s32k1xx_periphclocks.c +========================================================= + Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc. + Copyright 2016-2018 NXP + All rights reserved. + +THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. + +arch/xtensa/src/common/xtensa_asm_utils.h +=============================== + + Copyright (c) 2017, Intel Corporation + + SPDX-License-Identifier: Apache-2.0 + +boards/or1k/mor1kx/or1k/src/or1k_boot.c +boards/or1k/mor1kx/or1k/src/or1k_bringup.c +================================================= + Copyright (C) 2018 Extent3D. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/lpc17xx_40xx/u-blox-c027/include/board.h +boards/arm/lpc17xx_40xx/u-blox-c027/src/lpc17_40_ubxmdm.c +boards/arm/lpc17xx_40xx/u-blox-c027/src/u-blox-c027.h +=========================================================== + Copyright (C) 2016 Vladimir Komendantskiy. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/lpc2378/olimex-lpc2378/include/board.h +boards/arm/lpc2378/olimex-lpc2378/src/lpc2378_appinit.c +boards/arm/lpc2378/olimex-lpc2378/src/lpc2378_leds.c +boards/arm/lpc2378/olimex-lpc2378/scripts/ld.script +======================================================= + Copyright (C) 2010 Rommel Marcelo. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/rp2040/common/src/rp2040_ina219.c +boards/arm/stm32/common/src/stm32_ina219.c +boards/arm/stm32/olimex-stm32-e407/src/stm32_dac.c +=============================================== + Copyright (C) 2018,2019 Erle Robotics (Juan Flores Muñoz). + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32/nucleo-f303re/src/stm32_uid.c +boards/arm/stm32/omnibusf4/src/stm32_uid.c +boards/arm/stm32/stm32f4discovery/src/stm32_uid.c +boards/arm/stm32h7/nucleo-h743zi/src/stm32_uid.c +boards/arm/stm32h7/stm32h747i-disco/src/stm32_uid.c +=============================================== + Copyright (C) 2015 Marawan Ragab. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32/nucleo-f412zg/src/stm32_usb.c +boards/arm/stm32/stm32f411e-disco/src/stm32_usb.c +================================================= + Copyright (C) 2017 Gregory Nutt. All rights reserved. + Copyright (C) 2017 Brian Webb. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32/omnibusf4/src/stm32_romfs.h +boards/arm/stm32/omnibusf4/src/stm32_romfs_initialize.c +boards/arm/stm32/stm32f4discovery/src/stm32_romfs.h +boards/arm/stm32/stm32f4discovery/src/stm32_romfs_initialize.c +boards/arm/stm32f7/common/include/stm32_romfs.h +boards/arm/stm32f7/common/src/stm32_romfs_initialize.c +============================================================= + Copyright (C) 2017 Tomasz Wozniak. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_gpio.c +boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_pwm.c +boards/arm/stm32f0l0g0/nucleo-g070rb/src/stm32_timer.c +===================================================== + Copyright (C) 2019 Fundação CERTI. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32f7/nucleo-f722ze/src/stm32_dma_alloc.c +boards/arm/stm32f7/nucleo-f746zg/src/stm32_dma_alloc.c +boards/arm/stm32f7/nucleo-f767zi/src/stm32_dma_alloc.c +boards/arm/stm32f7/stm32f746-ws/src/stm32_dma_alloc.c +boards/arm/stm32l4/nucleo-l496zg/src/stm32_dma_alloc.c +boards/arm/tms570/tms570ls31x-usb-kit/include/nsh_romfsimg.h +============================================================ + Copyright (C) 2013, 2016 PX4 Development Team. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name PX4 nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32f7/stm32f746g-disco/src/stm32_extmem.c +======================================================= + Copyright (C) 2018 Marcin Wyrwas. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32l4/nucleo-l476rg/src/stm32_as726x.c +================================================== + Copyright (C) 2019 Fabian Justi. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32l4/stm32l476-mdk/include/board.h +boards/arm/stm32l4/stm32l476-mdk/include/stm32l476-mdk-clocking.h +================================================================= + Copyright (C) 2016 Gregory Nutt. All rights reserved. + Copyright (C) 2016 Motorola Mobility, LLC. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +boards/arm/stm32l4/stm32l476vg-disco/include/boardctl.h +boards/arm/stm32l4/stm32l4r9ai-disco/include/boardctl.h +======================================================= + Copyright (C) 2016 dev@ziggurat29.com. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/imxrt/imxrt_lcd.c +arch/arm/src/imxrt/hardware/imxrt_lcd.h +============================== + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + Copyright (C) 2017, NXP Semiconductors, Inc. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/imxrt/imxrt_edma.c +arch/arm/src/imxrt/imxrt_edma.h +================================== + Copyright (C) 2018 Gregory Nutt. All rights reserved. + +Portions of the eDMA logic derive from NXP sample code which has a +compatible BSD 3-clause license: + + Copyright (c) 2015, Freescale Semiconductor, Inc. + Copyright 2016-2017 NXP + All rights reserved + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/kinetis/kinetis_cfmconfig.c +============================================ + + https://github.com/jodersky/nuttx/tree/teensy31-7.6 + +Barely based on "bare metal" sample from Freedom board: +Copyright (c) 2012-2013 Andrew Payne + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +arch/arm/src/kinetis/kinetis_edma.c +arch/arm/src/kinetis/kinetis_edma.h +====================================== + + Copyright (C) 2019, 2021 Gregory Nutt. All rights reserved. + +This file was leveraged from the NuttX S32K port. Portions of that eDMA +logic derived from NXP sample code which has a compatible BSD 3-clause +license: + + Copyright (c) 2015, Freescale Semiconductor, Inc. + Copyright 2016-2017 NXP + All rights reserved + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/kl/kl_cfmconfig.c +============================== + + Copyright (c) 2012-2013 Andrew Payne + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +arch/arm/src/nrf52/nrf52_flash.c +arch/arm/src/nrf52/nrf52_nvmc.c +arch/arm/src/nrf52/nrf52_nvmc.h +arch/arm/src/nrf52/nrf52_wdt.c +arch/arm/src/nrf52/nrf52_wdt_lowerhalf.c +arch/arm/src/nrf52/nrf52_wdt_lowerhalf.h +===================================== + + Copyright (C) 2018 Zglue Inc. All rights reserved. + +Ported from the Nordic SDK, this is the original license: + +Copyright (c) 2012 - 2018, Nordic Semiconductor ASA +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/phy62xx/phyplus_timer_lowerhalf.c +============================================== + + Copyright (C) 2015 Wail Khemir. All rights reserved. + Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/rp2040/hardware/rp2040_clocks.h +arch/arm/src/rp2040/hardware/rp2040_dma.h +arch/arm/src/rp2040/hardware/rp2040_i2c.h +arch/arm/src/rp2040/hardware/rp2040_io_bank0.h +arch/arm/src/rp2040/hardware/rp2040_memorymap.h +arch/arm/src/rp2040/hardware/rp2040_pads_bank0.h +arch/arm/src/rp2040/hardware/rp2040_pio.h +arch/arm/src/rp2040/hardware/rp2040_pll.h +arch/arm/src/rp2040/hardware/rp2040_psm.h +arch/arm/src/rp2040/hardware/rp2040_resets.h +arch/arm/src/rp2040/hardware/rp2040_rosc.h +arch/arm/src/rp2040/hardware/rp2040_sio.h +arch/arm/src/rp2040/hardware/rp2040_spi.h +arch/arm/src/rp2040/hardware/rp2040_uart01.h +arch/arm/src/rp2040/hardware/rp2040_usbctrl_regs.h +arch/arm/src/rp2040/hardware/rp2040_watchdog.h +arch/arm/src/rp2040/hardware/rp2040_xosc.h +arch/arm/src/rp2040/rp2040_clock.c +arch/arm/src/rp2040/rp2040_pio.c +arch/arm/src/rp2040/rp2040_pio.h +arch/arm/src/rp2040/rp2040_pio_instructions.h +arch/arm/src/rp2040/rp2040_pll.c +arch/arm/src/rp2040/rp2040_xosc.c +boards/arm/rp2040/common/src/rp2040_uniqueid.c +========================================================== +Based upon the software originally developed by + Raspberry Pi (Trading) Ltd. + +Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/s32k1xx/hardware/s32k1xx_wdog.h +arch/arm/src/s32k1xx/s32k11x/s32k11x_clocknames.h +arch/arm/src/s32k1xx/s32k14x/s32k14x_clocknames.h +arch/arm/src/s32k1xx/s32k1xx_clockconfig.c +arch/arm/src/s32k1xx/s32k1xx_clockconfig.h +arch/arm/src/s32k1xx/s32k1xx_periphclocks.c +arch/arm/src/s32k1xx/s32k1xx_periphclocks.h +================================================ + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +Some of the definitions within this file derive from NXP sample code for +the S32K1xx MCUs. That sample code has this licensing information: + + Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc. + Copyright 2016-2018 NXP + All rights reserved. + +THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/s32k1xx/s32k1xx_edma.c +arch/arm/src/s32k1xx/s32k1xx_edma.h +==================================== + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + +This file was leveraged from the NuttX i.MXRT port. +Portions of that eDMA logic derived from NXP sample code which has +a compatible BSD 3-clause license: + + Copyright (c) 2015, Freescale Semiconductor, Inc. + Copyright 2016-2017 NXP + All rights reserved + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32/hardware/stm32f100_pinmap.h +================================================= + + Copyright (C) 2009 Gregory Nutt. All rights reserved. + Copyright (C) 2011 Uros Platise. All rights reserved. + Copyright (C) 2012 Michael Smith. All Rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32/hardware/stm32f37xxx_sdadc.h +==================================================== + + Copyright (C) 2009, 2011, 2013 Gregory Nutt. All rights reserved. + Copyright (C) 2016 Studelec SA. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32/stm32_i2c_alt.c +=================================== + + Copyright (C) 2011 Uros Platise. All rights reserved. + Copyright (C) 2011-2014, 2016-2017 Gregory Nutt. All rights reserved. + Copyright( C) 2014 Patrizio Simona. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32/stm32_i2c_v2.c +=========================================== + + Copyright (C) 2011 Uros Platise. All rights reserved. + Copyright (C) 2016-2017 Gregory Nutt. All rights reserved. + Copyright (c) 2016 Doug Vetter. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32/stm32_sdadc.c +arch/arm/src/stm32/stm32_sdadc.h +=================================== + + Copyright (C) 2011, 2013, 2015-2017 Gregory Nutt. All rights reserved. + Copyright (C) 2016 Studelec. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32/stm32_tickless.c +====================================== + + Copyright (C) 2016-2017 Gregory Nutt. All rights reserved. + Copyright (C) 2017 Ansync Labs. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32/stm32_tim_lowerhalf.c +=========================================== + + Copyright (C) 2015 Wail Khemir. All rights reserved. + Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32/stm32_uid.c +arch/arm/src/stm32/stm32_uid.h +=================================== + + Copyright (C) 2015 Marawan Ragab. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f0l0g0/hardware/stm32g0_pinmap.h +===================================================== + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + Copyright (C) 2019 Fundação CERTI. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f0l0g0/stm32_i2c.c +=========================================== + + Copyright (C) 2011 Uros Platise. All rights reserved. + Copyright (C) 2016-2017 Gregory Nutt. All rights reserved. + Copyright (c) 2016 Doug Vetter. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f0l0g0/stm32_pwm.c +======================================= + + Copyright (C) 2019 Fundação CERTI. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f0l0g0/stm32_pwm.h +arch/arm/src/stm32f0l0g0/stm32_tim.c +arch/arm/src/stm32f0l0g0/stm32_tim.h +======================================= + + Copyright (C) 2019 Fundação CERTI. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f0l0g0/stm32_tim_lowerhalf.c +================================================= + + Copyright (C) 2019 Fundação CERTI. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f7/stm32_flash.c +======================================= + + Copyright (C) 2018 Wolpike LLC. All rights reserved. + Copyright (C) 2011 Uros Platise. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f7/stm32_i2c.c +arch/arm/src/stm32h7/stm32_i2c.c +======================================= + + Copyright (C) 2011 Uros Platise. All rights reserved. + Copyright (C) 2016-2017 Gregory Nutt. All rights reserved. + Copyright (c) 2016 Doug Vetter. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f7/stm32_sai.c +arch/arm/src/stm32f7/stm32_sai.h +=================================== + + Copyright (C) 2013-2014, 2019 Gregory Nutt. All rights reserved. + Copyright (c) 2016 Motorola Mobility, LLC. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f7/stm32_tickless.c +========================================= + + Copyright (C) 2016-2017 Gregory Nutt. All rights reserved. + Copyright (C) 2017 Ansync Labs. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f7/stm32_tim_lowerhalf.c +============================================= + + Copyright (C) 2015 Wail Khemir. All rights reserved. + Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32f7/stm32_uid.c +arch/arm/src/stm32f7/stm32_uid.h +====================================== + + Copyright (C) 2015 Marawan Ragab. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32h7/stm32_adc.c +==================================== + + Copyright (C) 2017, 2019 Gregory Nutt. All rights reserved. + Copyright (C) 2015 Motorola Mobility, LLC. All rights reserved. + Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32h7/stm32_flash.c +====================================== + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + Copyright (C) 2018 Wolpike LLC. All rights reserved. + Copyright (C) 2011 Uros Platise. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32h7/stm32_uid.c +arch/arm/src/stm32h7/stm32_uid.h +===================================== + + Copyright (C) 2015 Marawan Ragab. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32l4/hardware/stm32l4_lptim.h +============================================= + + Copyright (C) 2016 Motorola Mobility, LLC. All rights reserved. + Copyright (C) 2009, 2011-2012, 2017 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32l4/stm32l4_comp.c +====================================== + Copyright (c) 2017 Gregory Nutt. All rights reserved. + +Based on COMP driver from the Motorola MDK: + + Copyright (c) 2016 Motorola Mobility, LLC. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32l4/stm32l4_comp.h +========================================== +Copyright (c) 2016 Motorola Mobility, LLC. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32l4/stm32l4_pmlpr.c +arch/arm/src/stm32l4/stm32l4_exti_comp.c +arch/arm/src/stm32l4/stm32l4_pmstandby.c +arch/arm/src/stm32l4/stm32l4_pmstop.c +arch/arm/src/stm32l4/stm32l4_sai.c +arch/arm/src/stm32l4/stm32l4_sai.h +============================================ + + Copyright (C) 2012, 2017 Gregory Nutt. All rights reserved. + Copyright (C) 2015 Motorola Mobility, LLC. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32l4/stm32l4_i2c.c +================================== + + Copyright (C) 2011 Uros Platise. All rights reserved. + Copyright (C) 2011-2013, 2016-2018 Gregory Nutt. All rights reserved. + Copyright (C) 2011 Uros Platise. All rights reserved. + Copyright (C) 2016-2017 Gregory Nutt. All rights reserved. + Copyright (c) 2016 Doug Vetter. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32l4/stm32l4_lptim.c +arch/arm/src/stm32l4/stm32l4_lptim.h +==================================== + + Copyright (C) 2011 Uros Platise. All rights reserved. + Copyright (C) 2016 Motorola Mobility, LLC. All rights reserved. + Copyright (C) 2011-2012, 2017 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32l4/stm32l4_tim_lowerhalf.c +============================================ + + Copyright (C) 2015 Wail Khemir. All rights reserved. + Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved. + Copyright (C) 2016 Sebastien Lorquet All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/stm32l4/stm32l4_uid.c +arch/arm/src/stm32l4/stm32l4_uid.h +================================== + + Copyright (C) 2015 Marawan Ragab. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/xmc4/hardware/xmc4_ethernet.h +arch/arm/src/xmc4/hardware/xmc4_flash.h +arch/arm/src/xmc4/hardware/xmc4_memorymap.h +arch/arm/src/xmc4/hardware/xmc4_ports.h +arch/arm/src/xmc4/hardware/xmc4_scu.h +arch/arm/src/xmc4/hardware/xmc4_usic.h +arch/arm/src/xmc4/xmc4_clockconfig.c +arch/arm/src/xmc4/xmc4_clockutils.c +arch/arm/src/xmc4/xmc4_usic.c +=============================================== + + Copyright (C) 2017 Gregory Nutt. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + May include some logic from sample code provided by Infineon: + + Copyright (C) 2011-2015 Infineon Technologies AG. All rights reserved. + + Infineon Technologies AG (Infineon) is supplying this software for use + with Infineon's microcontrollers. This file can be freely distributed + within development tools that are supporting such microcontrollers. + + THIS SOFTWARE IS PROVIDED AS IS. NO WARRANTIES, WHETHER EXPRESS, IMPLIED + OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS + SOFTWARE. INFINEON SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, + INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. + +crypto/aes.c +include/nuttx/crypto/aes.h +=========================== + + Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ + Extracted from the CC3000 Host Driver Implementation. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the + distribution. + + Neither the name of Texas Instruments Incorporated nor the names of + its contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tms570/tms570_selftest.c +arch/arm/src/tms570/tms570_lowputc.c +arch/arm/src/tms570/tms570_esm.c +arch/arm/src/tms570/tms570_clockconfig.c +======================================== + + Copyright (C) 2015 Gregory Nutt. All rights reserved. + + Most logic in this file was leveraged from TI's Project0 which has a + compatible BSD license: + + Copyright (c) 2012, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX, Texas Instruments Incorporated, nor the + names of its contributors may be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/tiva_chipinfo.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_vims.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_uart.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_timer.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_smph.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_prcm.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_memorymap.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_ioc.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_i2c.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_gpio.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_flash.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_fcfg1.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_ddi0_osc.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_ddi.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_ccfg.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_aux_sysif.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_aux_smph.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_aon_rtc.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_aon_pmctl.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_aon_ioc.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_aon_batmon.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_adi4_aux.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_adi3_refsys.h +arch/arm/src/tiva/hardware/cc13x2_cc26x2/cc13x2_cc26x2_adi2_refsys.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_vims.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_uart.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_timer.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_smph.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_prcm.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_memorymap.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_ioc.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_i2c.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_gpio.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_flash.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_fcfg1.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_ddi0_osc.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_ddi.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_ccfg.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_aux_wuc.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_aux_smph.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_aon_wuc.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_aon_sysctl.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_aon_rtc.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_aon_ioc.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_aon_batmon.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_adi4_aux.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_adi3_refsys.h +arch/arm/src/tiva/hardware/cc13x0/cc13x0_adi2_refsys.h +arch/arm/src/tiva/cc13xx/cc13xx_chipinfo.c +arch/arm/src/tiva/cc13xx/cc13x2_cc26x2_v2_rom.h +================================================ + + Copyright (C) 2018 Gregory Nutt. All rights reserved. + + Technical content derives from a TI header file that has a compatible BSD + license: + + Copyright (c) 2015-2017, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/hardware/tiva_adc.h +arch/arm/src/tiva/tiva_adc.h +========================== + + Copyright (C) 2015 Calvin Maguranis. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/common/tiva_pwm.c +arch/arm/src/tiva/hardware/tiva_pwm.h +arch/arm/src/tiva/tiva_pwm.h +arch/arm/src/tiva/common/tiva_qencoder.c +arch/arm/src/tiva/hardware/tiva_qencoder.h +arch/arm/src/tiva/tiva_qencoder.h +==================================== + + Copyright (C) 2016 Young Mu. All rights reserved. + + The basic structure of this driver derives in spirit (if nothing more) + from the NuttX STM32 QEI driver which has: + + Copyright (C) 2012 Gregory Nutt. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/hardware/tm4c/tm4c_ethernet.h +arch/arm/src/tiva/hardware/tm4c/tm4c_flash.h +========================= + + Copyright (C) 2015 Gregory Nutt. All rights reserved. + + Based on register definitions provided by: + + Copyright (C) 2014 TRD2 Inc. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/hardware/lm/lm3s_timer.h +arch/arm/src/tiva/hardware/lm/lm4f_timer.h +arch/arm/src/tiva/hardware/tm4c/tm4c129_timer.h +arch/arm/src/tiva/hardware/tm4c/tm4c123_timer.h +=============================== + + Originally: + + Copyright (C) 2012, 2014 Max Nekludov. All rights reserved. + + Ongoing support and major revision to support the TM4C129 family + (essentially a full file replacement): + + Copyright (C) 2015, 2018 Gregory Nutt. All rights reserved. + + Some bitfield definitions taken from a header file provided by: + + Copyright (C) 2014 TRD2 Inc. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tms570/tms570_boot.c +================================= + + Copyright (C) 2015 Gregory Nutt. All rights reserved. + + This is primarily original code. However, some logic in this file was + inspired/leveraged from TI's Project0 which has a compatible BSD license + and credit should be given in any case: + + Copyright (c) 2012, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/common/tiva_adclow.c +====================================== + + Copyright (C) 2016-2018 Gregory Nutt. All rights reserved. + Copyright (C) 2015 TRD2 Inc. All rights reserved. + + The Tivaware sample code has a BSD compatible license that requires this + copyright notice: + + Copyright (c) 2005-2014 Texas Instruments Incorporated. + All rights reserved. + Software License Agreement + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the + distribution. + + Neither the name of Texas Instruments Incorporated nor the names of + its contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/common/tiva_adclib.c +======================================= + + Copyright (C) 2015 TRD2 Inc. All rights reserved. + + The Tivaware sample code has a BSD compatible license that requires this + copyright notice: + + Copyright (c) 2005-2014 Texas Instruments Incorporated. + All rights reserved. + Software License Agreement + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the + distribution. + + Neither the name of Texas Instruments Incorporated nor the names of + its contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/cc13xx/cc13xx_prcm.c +arch/arm/src/tiva/cc13xx/cc13xx_prcm.h +====================================== + + Copyright (C) 2018 Gregory Nutt. All rights reserved. + + Includes definitions from TI's prcm.c file which has a fully compatible + BSD license: + + Copyright (c) 2015-2017, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/cc13xx/cc13x2_v1_trim.c +arch/arm/src/tiva/cc13xx/cc13x2_v2_trim.c +arch/arm/src/tiva/cc13xx/cc13x0_trim.c +========================================= + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + + This is a port of TI's setup.c file (revision 49363) which has a fully + compatible BSD license: + + Copyright (c) 2015-2017, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + 1) Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2) Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3) Neither the name NuttX nor the names of its contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/cc13xx/cc13x2_cc26x2_v1_rom.h +=============================================== + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + + This is a port of TI's rom.h file which has a fully compatible + BSD license: + + Copyright (c) 2015-2017, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/cc13xx/cc13x2_cc26x2_v1_rom.c +arch/arm/src/tiva/cc13xx/cc13x0_rom.c +=============================================== + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + + This is a port of TI's setup_rom.c file which has a fully compatible BSD + license: + + Copyright (c) 2015-2017, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/cc13xx/cc13x2_aux_sysif.h +=========================================== + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + + This is a port of TI's aux_sysif.h file which has a fully compatible BSD + license: + + Copyright (c) 2015-2017, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/cc13xx/cc13x2_aux_sysif.c +=========================================== + + Copyright (C) 2018 Gregory Nutt. All rights reserved. + + Technical content derives from a TI aux_sysif.c file that has a + compatible BSD license: + + Copyright (c) 2015-2017, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/tiva/cc13xx/cc13x0_rom.h +===================================== + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + + This is a port of TI's setup_rom.h file which has a fully compatible + BSD license: + + Copyright (c) 2015-2017, Texas Instruments Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +drivers/mtd/hamming.c +drivers/mtd/mtd_modeltab.c +drivers/mtd/mtd_nand.c +drivers/mtd/mtd_nandecc.c +drivers/mtd/mtd_nandmodel.c +drivers/mtd/mtd_nandscheme.c +drivers/mtd/mtd_onfi.c +include/nuttx/mtd/hamming.h +include/nuttx/mtd/nand.h +include/nuttx/mtd/nand_config.h +include/nuttx/mtd/nand_ecc.h +include/nuttx/mtd/nand_model.h +include/nuttx/mtd/nand_raw.h +include/nuttx/mtd/nand_scheme.h +include/nuttx/mtd/onfi.h +======================== + + Copyright (c) 2011, Atmel Corporation + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the names NuttX nor Atmel nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +drivers/mtd/at24xx.c +================================== + Copyright (C) 2011 Li Zhuoyi. All rights reserved. + Copyright (C) 2013, 2016 Gregory Nutt. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/arm/armv8-m +libs/libc/machine/arm64/gnu/arch_setjmp.S +============================================== + + Copyright (c) 2011, 2012 ARM Ltd. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/machine/sim/arch_setjmp_arm.S +========================================== + + Copyright (C) 2017 Gregory Nutt. All rights reserved. + +Extracted from the MUSL C-library. The MUSL C library has a compatible +MIT license and is released here under the NuttX 3-clause BSD license: + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libm/libm/scalbnf.c +libs/libm/libm/scalbn.c +libs/libm/libm/scalbnl.c +libs/libc/stdlib/lib_strtof.c +libs/libc/time/lib_strptime.c +====================== + + Copyright © 2005-2020 Rich Felker, et al. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +libs/libm/libm/__cos.c +libs/libm/libm/__sin.c +libs/libm/libm/lib_lgamma.c +libs/libm/libm/lib_copysignf.c +====================== + + Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + + Developed at SunSoft, a Sun Microsystems, Inc. business. + Permission to use, copy, modify, and distribute this + software is freely granted, provided that this notice + is preserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +libs/libc/net/lib_inetaddr.c +=============================== + + Copyright (C) 2011 Yu Qiang. All rights reserved. + Copyright (C) 2011 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/net/lib_inetntop.c +libs/libc/net/lib_inetpton.c +============================ + + Copyright (C) 2012, 2015 Gregory Nutt. All rights reserved. + +Includes some logic extracted from hwport_ftpd, written by Jaehyuk Cho + which was released under the BSD license. + + Copyright (C) HWPORT.COM. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/pthread/pthread_get_stackaddr_np.c +libs/libc/pthread/pthread_get_stacksize_np.c +=========================================== + + Copyright (C) 2018 Geoff Norton. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/stdio/lib_dtoa_data.c +libs/libc/stdio/lib_dtoa_engine.c +libs/libc/stdio/lib_dtoa_engine.h +libs/libc/stdio/lib_ultoa_invert.c +================================== + + Copyright © 2018, Keith Packard. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/stdio/lib_libvsprintf.c +==================================== + + Copyright (c) 2002, Alexander Popov (sasho@vip.bg) + Copyright (c) 2002,2004,2005 Joerg Wunsch + Copyright (c) 2005, Helmut Wallner + Copyright (c) 2007, Dmitry Xmelkov + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/stdio/lib_ultoa_invert.h +======================================= + + Copyright (c) 2005, Dmitry Xmelkov. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/stdio/lib_vdprintf.c +================================== + + Copyright (C) 2012 Andrew Tridgell. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/stdlib/lib_bsearch.c +libs/libc/stdlib/lib_qsort.c +============================= + + Copyright (c) 1990, 1993 + The Regents of the University of California. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed by the University of + California, Berkeley and its contributors. +4. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +libs/libc/stdlib/lib_itoa.c +============================== + + Copyright (C) 2013 Brooks Automation, Inc. All rights reserved. + Copyright (C) 2010-2011 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/stdlib/lib_div.c +libs/libc/stdlib/lib_ldiv.c +libs/libc/stdlib/lib_lldiv.c +============================= + +A direct leverage of the div() inplement by: + + Copyright (C) 2015 Stavros Polymenis. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/stdlib/lib_mbtowc.c +libs/libc/string/lib_strcoll.c +libs/libc/string/lib_strxfrm.c +=============================== + + Copyright (c)1999 Citrus Project. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +libs/libc/string/lib_memmem.c +libs/libc/string/lib_strstr.c +=============================== +The MIT License (MIT) + +Copyright (c) 2014-2015 Tal Einat + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +libs/libc/time/lib_localtime.c +================================= + +Re-released as part of NuttX under the 3-clause BSD license: + + Copyright (C) 2014 Gregory Nutt. All rights reserved. + +With these notes: + + This file is in the public domain, so clarified as of + 1996-06-05 by Arthur David Olson. + + Leap second handling from Bradley White. + POSIX-style TZ environment variable handling from Guy Harris. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/unistd/lib_gethostname.c +libs/libc/unistd/lib_sethostname.c +include/sys/uio.h +include/sys/utsname.h +================================= + + Copyright (C) 2015 Stavros Polymenis. All rights reserved. + Copyright (C) 2015, 2016 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +libs/libc/wchar +============================= + + Copyright (c)1999 Citrus Project, + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +libs/libc/wchar/lib_mbrlen.c +libs/libc/wchar/lib_mbsrtowcs.c +================================ + + Copyright (c) 2002-2004 Tim J. Robbins. + All rights reserved. + + Copyright (c) 2011 The FreeBSD Foundation + All rights reserved. + +Portions of this software were developed by David Chisnall +under sponsorship from the FreeBSD Foundation. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +libs/libc/wchar/lib_wcslcpy.c +================================ + + Copyright (c) 1998 Todd C. Miller + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/wctype +=================== + + Copyright (c) 2002 Red Hat Incorporated. + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + The name of Red Hat Incorporated may not be used to endorse + or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +libs/libc/locale/big5.h +libs/libc/locale/codepages.h +libs/libc/locale/gb18030.h +libs/libc/locale/hkscs.h +libs/libc/locale/jis0208.h +libs/libc/locale/ksc.h +libs/libc/locale/legacychars.h +libs/libc/locale/revjis.h +libs/libc/locale/lib_iconv.c +=================== + +Copyright © 2005-2020 Rich Felker, et al. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +drivers/wireless/bluetooth/bt_uart.c +drivers/wireless/bluetooth/bt_uart.h +wireless/bluetooth +include/nuttx/wireless/bluetooth +tools/size_report +=========================== + + Copyright (c) 2016, Intel Corporation + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS + ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +drivers/wireless/spirit/ +======================= + + Copyright(c) 2015 STMicroelectronics + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of STMicroelectronics nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/nrf52/sdk-nrfxlib +arch/arm/src/nrf53/sdk-nrfxlib +arch/arm/src/nrf91/sdk-nrfxlib +=============================== + + Nordic SoftDevice Controller is based on the 3-Clause BSD License: + + Copyright (c) 2018, Nordic Semiconductor ASA + + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form, except as embedded into a Nordic + Semiconductor ASA integrated circuit in a product or a software update for + such product, must reproduce the above copyright notice, this list of + conditions and the following disclaimer in the documentation and/or other + materials provided with the distribution. + + 3. Neither the name of Nordic Semiconductor ASA nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + + 4. This software, with or without modification, must only be used with a + Nordic Semiconductor ASA integrated circuit. + + 5. Any software provided in binary form under this license must not be reverse + engineered, decompiled, modified and/or disassembled. + + THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/risc-v/src/mpfs/mpfs_opensbi_utils.S +========================================= + +OpenSBI is based on the 2-Clause BSD License: + +The 2-Clause BSD License +SPDX short identifier: BSD-2-Clause + +Copyright (c) 2019 Western Digital Corporation or its affiliates and other +contributors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +arch/x86/src/qemu/qemu_keypad.c +arch/x86/src/qemu/qemu_vga.c +========================================= + + Copyright (C) 2013 Li Zhuoyi. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/lpc17xx_40xx/lpc17_40_adc.c +arch/arm/src/lpc17xx_40xx/lpc17_40_can.c +arch/arm/src/lpc17xx_40xx/lpc17_40_dac.c +arch/arm/src/lpc43xx/lpc43_adc.c +arch/arm/src/lpc43xx/lpc43_dac.c +arch/arm/src/lpc2378/lpc23xx_i2c.h +====================================== + + Copyright (C) 2011 Li Zhuoyi. All rights reserved. + Copyright (C) 2010, 2013, 2016 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/lpc17xx_40xx/lpc17_40_i2c.c +arch/arm/src/lpc2378/lpc23xx_i2c.c +arch/arm/src/lpc43xx/lpc43_i2c.c +====================================== + + Copyright (C) 2010-2011, 2012, 2014-2016, 2019 Gregory Nutt. + All rights reserved. + Copyright (C) 2011 Li Zhuoyi. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/lpc2378/lpc23xx_spi.c +arch/arm/src/lpc2378/lpc23xx_spi.h +================================== + + Copyright (C) 2013 Li Zhuoyi. All rights reserved. + Copyright (C) 2010-2017 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/lpc43xx/spifi/inc/private/spifilib_chiphw.h +arch/arm/src/lpc43xx/spifi/inc/spifilib_api.h +arch/arm/src/lpc43xx/spifi/inc/spifilib_dev.h +arch/arm/src/lpc43xx/spifi/src/spifilib_dev_common.c +arch/arm/src/lpc43xx/spifi/src/spifilib_fam_standard_cmd.c +============================================== + + Copyright(C) NXP Semiconductors, 2014. All rights reserved. + +Software that is described herein is for illustrative purposes only +which provides customers with programming information regarding the +LPC products. This software is supplied "AS IS" without any warranties of +any kind, and NXP Semiconductors and its licenser disclaim any and +all warranties, express or implied, including all implied warranties of +merchantability, fitness for a particular purpose and non-infringement of +intellectual property rights. NXP Semiconductors assumes no +responsibility or liability for the use of the software, conveys no +license or rights under any patent, copyright, mask work right, or any +other intellectual property rights in or to any products. NXP +Semiconductors reserves the right to make changes in the software without +notification. NXP Semiconductors also makes no representation or warranty +that such application will be suitable for the specified use without +further testing or modification. + +Permission to use, copy, modify, and distribute this software and its +documentation is hereby granted, under NXP Semiconductors' and its +licensor's relevant copyrights in the software, without fee, provided that +it is used in conjunction with NXP Semiconductors microcontrollers. This +copyright, permission, and disclaimer notice must appear in all copies of +this code. + +arch/arm/src/lpc54xx/lpc54_clockconfig.c +arch/arm/src/lpc54xx/lpc54_clockconfig.h +arch/arm/src/lpc54xx/lpc54_emc.c +arch/arm/src/lpc54xx/lpc54_emc.h +arch/arm/src/lpc54xx/lpc54_ethernet.c +arch/arm/src/lpc54xx/lpc54_i2c_master.c +arch/arm/src/lpc54xx/lpc54_lowputc.c +===================================== + + Copyright (C) 2017-2019 Gregory Nutt. All rights reserved. + +Parts of this file were adapted from sample code provided for the +LPC54xx family from NXP which has a compatible BSD license. + + Copyright (c) 2016, Freescale Semiconductor, Inc. + Copyright (c) 2016 - 2017 , NXP. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/lpc2378/chip.h +arch/arm/src/lpc2378/lpc2378.h +arch/arm/src/lpc2378/lpc23xx_decodeirq.c +arch/arm/src/lpc2378/lpc23xx_gpio.h +arch/arm/src/lpc2378/lpc23xx_head.S +arch/arm/src/lpc2378/lpc23xx_i2c.c +arch/arm/src/lpc2378/lpc23xx_io.c +arch/arm/src/lpc2378/lpc23xx_irq.c +arch/arm/src/lpc2378/lpc23xx_lowputc.S +arch/arm/src/lpc2378/lpc23xx_pinsel.h +arch/arm/src/lpc2378/lpc23xx_pllsetup.c +arch/arm/src/lpc2378/lpc23xx_scb.h +arch/arm/src/lpc2378/lpc23xx_serial.c +arch/arm/src/lpc2378/lpc23xx_timer.h +arch/arm/src/lpc2378/lpc23xx_timerisr.c +arch/arm/src/lpc2378/lpc23xx_uart.h +arch/arm/src/lpc2378/lpc23xx_vic.h +====================================== + + Copyright (C) 2010 Rommel Marcelo. All rights reserved. + Copyright (C) 2010 Gregory Nutt. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/or1k/include/mor1kx/irq.h +arch/or1k/include/spr.h +arch/or1k/src/common/up_cpuinfo.c +arch/or1k/src/common/up_initialstate.c +arch/or1k/src/common/up_timer.c +arch/or1k/src/mor1kx/up_vectortab.S +======================================= + + Copyright (C) 2018 Extent3D. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +arch/arm/src/am335x/am335x_edid.c +arch/arm/src/am335x/am335x_lcdc.c +arch/arm/src/am335x/am335x_lcdc.h +======================================= + + Copyright (C) 2019 Gregory Nutt. All rights reserved. + +The LCD driver derives from the LPC54xx LCD driver but also includes +information from the FreeBSD AM335x LCD driver which was released under +a two-clause BSD license: + + Copyright 2013 Oleksandr Tymoshenko + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. +3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +net/sixlowpan/sixlowpan_framelist.c +net/sixlowpan/sixlowpan_hc06.c +net/sixlowpan/sixlowpan_hc1.c +net/sixlowpan/sixlowpan_input.c +net/sixlowpan/sixlowpan_internal.h +net/sixlowpan/sixlowpan_utils.c +include/nuttx/net/sixlowpan.h +============================ + + Copyright (C) 2017-2018 Gregory Nutt. All rights reserved. + Author: Gregory Nutt + + Parts of this file derive from Contiki: + + Copyright (c) 2008, Swedish Institute of Computer Science. + All rights reserved. + Authors: Adam Dunkels + Nicolas Tsiftes + Niclas Finne + Mathilde Durvy + Julien Abeille + Joakim Eriksson + Joel Hoglund + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the Institute nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + +net/igmp/igmp_group.c +net/igmp/igmp_initialize.c +net/igmp/igmp_input.c +net/igmp/igmp_join.c +net/igmp/igmp_leave.c +net/igmp/igmp_mcastmac.c +net/igmp/igmp_msg.c +net/igmp/igmp_poll.c +net/igmp/igmp_timer.c +============================== + + Copyright (C) 2010, 2013-2014, 2016, 2018 Gregory Nutt. + All rights reserved. + Author: Gregory Nutt + + The NuttX implementation of IGMP was inspired by the IGMP add-on for the + lwIP TCP/IP stack by Steve Reynolds: + + Copyright (c) 2002 CITEL Technologies Ltd. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of CITEL Technologies Ltd nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS'' + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + THE POSSIBILITY OF SUCH DAMAGE. + +include/nuttx/list.h +==================== + Copyright (C) 2008 Travis Geiselbrecht. All rights reserved. + Author: Travis Geiselbrecht + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + 3. Neither the name NuttX nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +include/sys/queue.h +==================== + $OpenBSD: queue.h,v 1.46 2020/12/30 13:33:12 millert Exp $ + $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ + Copyright (c) 1991, 1993 + The Regents of the University of California. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + +include/crypto/cryptodev.h +==================== + $OpenBSD: cryptodev.h,v 1.58 2013/10/31 10:32:38 mikeb Exp $ + The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) + * + This code was written by Angelos D. Keromytis in Athens, Greece, in + February 2000. Network Security Technologies Inc. (NSTI) kindly + supported the development of this code. + * + Copyright (c) 2000 Angelos D. Keromytis + * +Permission to use, copy, and modify this software with or without fee +is hereby granted, provided that this entire notice is included in +all source code copies of any software which is or includes a copy or +modification of this software. + * +THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR +IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY +REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE +MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR +PURPOSE. + +Copyright (c) 2001 Theo de Raadt + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Effort sponsored in part by the Defense Advanced Research Projects +Agency (DARPA) and Air Force Research Laboratory, Air Force +Materiel Command, USAF, under agreement number F30602-01-2-0537. + +include/crypto/blf.h +==================== + $OpenBSD: blf.h,v 1.7 2021/11/29 01:04:45 djm Exp $ + + Blowfish - a fast block cipher designed by Bruce Schneier + + Copyright 1997 Niels Provos + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAG + +include/crypto/cast.h +==================== + $OpenBSD: cast.h,v 1.2 2002/03/14 01:26:51 millert Exp $ + + CAST-128 in C + Written by Steve Reid + 100% Public Domain - no warranty + Released 1997.10.11 + +include/crypto/chachapoly.h +==================== + $OpenBSD: chachapoly.h,v 1.4 2020/07/22 13:54:30 tobhe Exp $ + Copyright (c) 2015 Mike Belopuhov + + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +include/crypto/cryptosoft.h +==================== + $OpenBSD: cryptosoft.h,v 1.14 2012/12/07 17:03:22 mikeb Exp $ + + The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) + + This code was written by Angelos D. Keromytis in Athens, Greece, in + February 2000. Network Security Technologies Inc. (NSTI) kindly + supported the development of this code. + + Copyright (c) 2000 Angelos D. Keromytis + +Permission to use, copy, and modify this software with or without fee +is hereby granted, provided that this entire notice is included in +all source code copies of any software which is or includes a copy or +modification of this software. + +THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR +IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY +REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE +MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR +PURPOSE. + + include/crypto/gmac.h +==================== + $OpenBSD: gmac.h,v 1.6 2017/05/02 11:44:32 mikeb Exp $ + + Copyright (c) 2010 Mike Belopuhov + + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + + include/crypto/md5.h +==================== + $OpenBSD: md5.h,v 1.3 2014/11/16 17:39:09 tedu Exp $ + + This code implements the MD5 message-digest algorithm. + The algorithm is due to Ron Rivest. This code was + written by Colin Plumb in 1993, no copyright is claimed. + This code is in the public domain; do with it what you wish. + + Equivalent code is available from RSA Data Security, Inc. + This code has been tested against that, and is equivalent, + except that you don't need to include two pages of legalese + with every copy. + +include/crypto/poly1305.h +==================== + $OpenBSD: poly1305.h,v 1.2 2020/07/22 13:54:30 tobhe Exp $ + + Public Domain poly1305 from Andrew Moon + + poly1305 implementation using 32 bit 32 bit = 64 bit multiplication + and 64 bit addition from https://github.com/floodyberry/poly1305-donna + +include/crypto/rijndael.h +==================== + $OpenBSD: rijndael.h,v 1.13 2008/06/09 07:49:45 djm Exp $ + + rijndael-alg-fst.h + + @version 3.0 (December 2000) + + Optimised ANSI C code for the Rijndael cipher (now AES) + + @author Vincent Rijmen + @author Antoon Bosselaers + @author Paulo Barreto + + This code is hereby placed in the public domain. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include/crypto/rmd160.h +==================== + $OpenBSD: rmd160.h,v 1.5 2009/07/05 19:33:46 millert Exp $ + + Copyright (c) 2001 Markus Friedl. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include/crypto/sha1.h +==================== + $OpenBSD: sha1.h,v 1.6 2014/11/16 17:39:09 tedu Exp $ + SHA-1 in C + By Steve Reid + 100% Public Domain + +include/crypto/sha2.h +==================== + $OpenBSD: sha2.h,v 1.5 2014/11/16 17:39:09 tedu Exp $ + + FILE: sha2.h + AUTHOR: Aaron D. Gifford + + Copyright (c) 2000-2001, Aaron D. Gifford + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holder nor the names of contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $ + +include/crypto/xform.h +==================== + $OpenBSD: xform.h,v 1.32 2021/10/22 12:30:53 bluhm Exp $ + + The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) + + This code was written by Angelos D. Keromytis in Athens, Greece, in + February 2000. Network Security Technologies Inc. (NSTI) kindly + supported the development of this code. + + Copyright (c) 2000 Angelos D. Keromytis + + Permission to use, copy, and modify this software with or without fee + is hereby granted, provided that this entire notice is included in + all source code copies of any software which is or includes a copy or + modification of this software. + + THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR + IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY + REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE + MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR + PURPOSE. + +crypto/blf.c +==================== + $OpenBSD: blf.c,v 1.8 2021/11/29 01:04:45 djm Exp $ * + + Blowfish block cipher for OpenBSD + Copyright 1997 Niels Provos + All rights reserved. + + Implementation advice by David Mazieres . + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +crypto/cast.c +==================== + $OpenBSD: cast.c,v 1.4 2012/04/25 04:12:27 matthew Exp $ + + CAST-128 in C + Written by Steve Reid + 100% Public Domain - no warranty + Released 1997.10.11 + +crypto/castsb.h +==================== + $OpenBSD: castsb.h,v 1.1 2000/02/28 23:13:04 deraadt Exp $ + + CAST-128 in C + Written by Steve Reid + 100% Public Domain - no warranty + Released 1997.10.11 + +crypto/chacha_private.h +==================== + $OpenBSD: chacha_private.h,v 1.4 2020/07/22 13:54:30 tobhe Exp $ + + chacha-merged.c version 20080118 + D. J. Bernstein + Public domain. + +crypto/chachapoly.c +==================== + $OpenBSD: chachapoly.c,v 1.6 2020/07/22 13:54:30 tobhe Exp $ + + Copyright (c) 2015 Mike Belopuhov + + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +crypto/crypto.c +==================== + $OpenBSD: crypto.c,v 1.65 2014/07/13 23:24:47 deraadt Exp $ + The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) + +This code was written by Angelos D. Keromytis in Athens, Greece, in +February 2000. Network Security Technologies Inc. (NSTI) kindly +supported the development of this code. + +Copyright (c) 2000, 2001 Angelos D. Keromytis + +Permission to use, copy, and modify this software with or without fee +is hereby granted, provided that this entire notice is included in +all source code copies of any software which is or includes a copy or +modification of this software. + +THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR +IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY +REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE +MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR +PURPOSE. + +crypto/cryptodev.c +==================== + $OpenBSD: cryptodev.c,v 1.82 2014/08/18 05:11:03 dlg Exp $ + Copyright (c) 2001 Theo de Raadt + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Effort sponsored in part by the Defense Advanced Research Projects + Agency (DARPA) and Air Force Research Laboratory, Air Force + Materiel Command, USAF, under agreement number F30602-01-2-0537. + +crypto/cryptosoft.c +==================== + $OpenBSD: cryptosoft.c,v 1.71 2014/07/13 23:24:47 deraadt Exp $ + The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) + +This code was written by Angelos D. Keromytis in Athens, Greece, in +February 2000. Network Security Technologies Inc. (NSTI) kindly +supported the development of this code. + +Copyright (c) 2000, 2001 Angelos D. Keromytis + +Permission to use, copy, and modify this software with or without fee +is hereby granted, provided that this entire notice is included in +all source code copies of any software which is or includes a copy or +modification of this software. + +THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR +IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY +REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE +MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR +PURPOSE. + +crypto/des_locl.h +==================== + $OpenBSD: des_locl.h,v 1.7 2015/12/10 21:00:51 naddy Exp $ + + lib/des/des_locl.h + Copyright (C) 1995 Eric Young (eay@mincom.oz.au) + All rights reserved. + + This file is part of an SSL implementation written + by Eric Young (eay@mincom.oz.au). + The implementation was written so as to conform with Netscapes SSL + specification. This library and applications are + FREE FOR COMMERCIAL AND NON-COMMERCIAL USE + as long as the following conditions are aheared to. + + Copyright remains Eric Young's, and as such any Copyright notices in + the code are not to be removed. If this code is used in a product, + Eric Young should be given attribution as the author of the parts used. + This can be in the form of a textual message at program startup or + in documentation (online or textual) provided with the package. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed by + Eric Young (eay@mincom.oz.au) + + THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + The licence and distribution terms for any publically available version or + derivative of this code cannot be changed. + i.e. this code cannot simply be + copied and put under another distribution licence + [including the GNU Public Licence.] + +crypto/ecb_enc.c +==================== + $OpenBSD: ecb_enc.c,v 1.6 2015/12/10 21:00:51 naddy Exp $ + lib/des/ecb_enc.c + Copyright (C) 1995 Eric Young (eay@mincom.oz.au) + All rights reserved. + + This file is part of an SSL implementation written + by Eric Young (eay@mincom.oz.au). + The implementation was written so as to conform with Netscapes SSL + specification. This library and applications are + FREE FOR COMMERCIAL AND NON-COMMERCIAL USE + as long as the following conditions are aheared to. + + Copyright remains Eric Young's, and as such any Copyright notices in + the code are not to be removed. If this code is used in a product, + Eric Young should be given attribution as the author of the parts used. + This can be in the form of a textual message at program startup or + in documentation (online or textual) provided with the package. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed by + Eric Young (eay@mincom.oz.au) + + THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + The licence and distribution terms for any publically available version or + derivative of this code cannot be changed. + i.e. this code cannot simply be + copied and put under another distribution licence + [including the GNU Public Licence.] + +crypto/ecb3_enc.c +==================== + $OpenBSD: ecb3_enc.c,v 1.3 2013/11/18 18:49:53 brad Exp $ + lib/des/ecb3_enc.c + Copyright (C) 1995 Eric Young (eay@mincom.oz.au) + All rights reserved. + + This file is part of an SSL implementation written + by Eric Young (eay@mincom.oz.au). + The implementation was written so as to conform with Netscapes SSL + specification. This library and applications are + FREE FOR COMMERCIAL AND NON-COMMERCIAL USE + as long as the following conditions are aheared to. + + Copyright remains Eric Young's, and as such any Copyright notices in + the code are not to be removed. If this code is used in a product, + Eric Young should be given attribution as the author of the parts used. + This can be in the form of a textual message at program startup or + in documentation (online or textual) provided with the package. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed + by Eric Young (eay@mincom.oz.au) + * + THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + The licence and distribution terms for any publically available version or + derivative of this code cannot be changed. + i.e. this code cannot simply be + copied and put under another distribution licence + [including the GNU Public Licence.] + +crypto/gmac.c +==================== + $OpenBSD: gmac.c,v 1.10 2017/05/02 11:44:32 mikeb Exp $ + + Copyright (c) 2010 Mike Belopuhov + + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + + This code implements the Message Authentication part of the + Galois/Counter Mode (as being described in the RFC 4543) using + the AES cipher. FIPS SP 800-38D describes the algorithm details. + +crypto/md5.c +==================== + $OpenBSD: md5.c,v 1.4 2014/12/28 10:04:35 tedu Exp $ + + This code implements the MD5 message-digest algorithm. + The algorithm is due to Ron Rivest. This code was + written by Colin Plumb in 1993, no copyright is claimed. + This code is in the public domain; do with it what you wish. + + Equivalent code is available from RSA Data Security, Inc. + This code has been tested against that, and is equivalent, + except that you don't need to include two pages of legalese + with every copy. + + To compute the message digest of a chunk of bytes, declare an + MD5Context structure, pass it to MD5Init, call MD5Update as + needed on buffers full of bytes, and then call MD5Final, which + will fill a supplied 16-byte array with the digest. + +crypto/podd.h +==================== + $OpenBSD: podd.h,v 1.1 2000/02/28 23:13:05 deraadt Exp $ + lib/des/podd.h + Copyright (C) 1995 Eric Young (eay@mincom.oz.au) + All rights reserved. + + This file is part of an SSL implementation written + by Eric Young (eay@mincom.oz.au). + The implementation was written so as to conform with Netscapes SSL + specification. This library and applications are + FREE FOR COMMERCIAL AND NON-COMMERCIAL USE + as long as the following conditions are aheared to. + + Copyright remains Eric Young's, and as such any Copyright notices in + the code are not to be removed. If this code is used in a product, + Eric Young should be given attribution as the author of the parts used. + This can be in the form of a textual message at program startup or + in documentation (online or textual) provided with the package. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed + by Eric Young (eay@mincom.oz.au) + + THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + The licence and distribution terms for any publically available version or + derivative of this code cannot be changed. + i.e. this code cannot simply be + copied and put under another distribution licence + [including the GNU Public Licence.] + +crypto/poly1305.c +==================== + $OpenBSD: poly1305.c,v 1.2 2020/07/22 13:54:30 tobhe Exp $ + + Public Domain poly1305 from Andrew Moon + Based on poly1305-donna.c, poly1305-donna-32.h and poly1305-donna.h from: + https://github.com/floodyberry/poly1305-donna + +crypto/random_pool.c +==================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. The + ASF licenses this file to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance with the + License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + License for the specific language governing permissions and limitations + under the License. + + crypto/rijndael.c +==================== + $OpenBSD: rijndael.c,v 1.20 2014/11/17 12:27:47 mikeb Exp $ + + rijndael-alg-fst.c + + @version 3.0 (December 2000) + + Optimised ANSI C code for the Rijndael cipher (now AES) + + @author Vincent Rijmen + @author Antoon Bosselaers + @author Paulo Barreto + + This code is hereby placed in the public domain. + + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + crypto/rmd160.c +==================== + $OpenBSD: rmd160.c,v 1.5 2011/01/11 15:42:05 deraadt Exp $ + + Copyright (c) 2001 Markus Friedl. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Preneel, Bosselaers, Dobbertin, + "The Cryptographic Hash Function RIPEMD-160", + RSA Laboratories, CryptoBytes, Volume 3, Number 2, Autumn 1997, + ftp://ftp.rsasecurity.com/pub/cryptobytes/crypto3n2.pdf + +crypto/set_key.c +==================== + $OpenBSD: set_key.c,v 1.5 2021/03/12 10:22:46 jsg Exp $ + lib/des/set_key.c + Copyright (C) 1995 Eric Young (eay@mincom.oz.au) + All rights reserved. + + This file is part of an SSL implementation written + by Eric Young (eay@mincom.oz.au). + The implementation was written so as to conform with Netscapes SSL + specification. This library and applications are + FREE FOR COMMERCIAL AND NON-COMMERCIAL USE + as long as the following conditions are aheared to. + + Copyright remains Eric Young's, and as such any Copyright notices in + the code are not to be removed. If this code is used in a product, + Eric Young should be given attribution as the author of the parts used. + This can be in the form of a textual message at program startup or + in documentation (online or textual) provided with the package. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed by + Eric Young (eay@mincom.oz.au) + + THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + The licence and distribution terms for any publically available version or + derivative of this code cannot be changed. + i.e. this code cannot simply be + copied and put under another distribution licence + [including the GNU Public Licence.] + + set_key.c v 1.4 eay 24/9/91 + 1.4 Speed up by 400% :-) + 1.3 added register declarations. + 1.2 unrolled make_key_sched a bit more + 1.1 added norm_expand_bits + 1.0 First working version + +crypto/sha1.c +==================== + $OpenBSD: sha1.c,v 1.11 2014/12/28 10:04:35 tedu Exp $ + + SHA-1 in C + By Steve Reid + 100% Public Domain + +crypto/sha2.c +==================== + $OpenBSD: sha2.c,v 1.19 2021/03/12 10:22:46 jsg Exp $ + FILE: sha2.c + AUTHOR: Aaron D. Gifford + + Copyright (c) 2000-2001, Aaron D. Gifford + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holder nor the names of contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $ + +crypto/sk.h +==================== + $OpenBSD: sk.h,v 1.2 2002/10/27 13:24:26 miod Exp $ + lib/des/sk.h + Copyright (C) 1995 Eric Young (eay@mincom.oz.au) + All rights reserved. + + This file is part of an SSL implementation written + by Eric Young (eay@mincom.oz.au). + The implementation was written so as to conform with Netscapes SSL + specification. This library and applications are + FREE FOR COMMERCIAL AND NON-COMMERCIAL USE + as long as the following conditions are aheared to. + + Copyright remains Eric Young's, and as such any Copyright notices in + the code are not to be removed. If this code is used in a product, + Eric Young should be given attribution as the author of the parts used. + This can be in the form of a textual message at program startup or + in documentation (online or textual) provided with the package. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed + by Eric Young (eay@mincom.oz.au) + + THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAl, SPECIAl, + EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + The licence and distribution terms for any publically + available version or derivative of this code cannot be changed. + i.e. this code cannot simply be + copied and put under another distribution licence + [including the GNU Public Licence.] + +crypto/spr.h +==================== + $OpenBSD: spr.h,v 1.2 2002/10/27 13:24:26 miod Exp $ + lib/des/spr.h + Copyright (C) 1995 Eric Young (eay@mincom.oz.au) + All rights reserved. + + This file is part of an SSL implementation written + by Eric Young (eay@mincom.oz.au). + The implementation was written so as to conform with Netscapes SSL + specification. This library and applications are + FREE FOR COMMERCIAL AND NON-COMMERCIAL USE + as long as the following conditions are aheared to. + + Copyright remains Eric Young's, and as such any Copyright notices in + the code are not to be removed. If this code is used in a product, + Eric Young should be given attribution as the author of the parts used. + This can be in the form of a textual message at program startup or + in documentation (online or textual) provided with the package. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed + by Eric Young (eay@mincom.oz.au) + + THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + + The licence and distribution terms for any publically available version or + derivative of this code cannot be changed. + i.e. this code cannot simply be + copied and put under another distribution licence + [including the GNU Public Licence.] + +crypto/xform.c +==================== + $OpenBSD: xform.c,v 1.61 2021/10/22 12:30:53 bluhm Exp $ + + The authors of this code are John Ioannidis (ji@tla.org), + Angelos D. Keromytis (kermit@csd.uch.gr), + Niels Provos (provos@physnet.uni-hamburg.de), + Damien Miller (djm@mindrot.org) and + Mike Belopuhov (mikeb@openbsd.org). + + This code was written by John Ioannidis for BSD/OS in Athens, Greece, + in November 1995. + + Ported to OpenBSD and NetBSD, with additional transforms, + in December 1996, + by Angelos D. Keromytis. + + Additional transforms and features in 1997 and 1998 by + Angelos D. Keromytis and Niels Provos. + + Additional features in 1999 by Angelos D. Keromytis. + + AES XTS implementation in 2008 by Damien Miller + + AES-GCM-16 and Chacha20-Poly1305 AEAD modes by Mike Belopuhov. + + Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, + Angelos D. Keromytis and Niels Provos. + + Copyright (C) 2001, Angelos D. Keromytis. + + Copyright (C) 2008, Damien Miller + + Copyright (C) 2010, 2015, Mike Belopuhov + + Permission to use, copy, and modify this software with or without fee + is hereby granted, provided that this entire notice is included in + all copies of any software which is or includes a copy or + modification of this software. + You may use this code under the GNU public license if you so wish. Please + contribute changes back to the authors under this freer than GPL license + so that we may further the use of strong encryption without limitations to + all. + + THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR + IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY + REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE + MERCHANTABILIT + +crypto/key_wrap.c +include/crypto/key_wrap.h +========================= + $OpenBSD: key_wrap.c,v 1.5 2017/05/02 17:07:06 mikeb Exp $ + $OpenBSD: key_wrap.h,v 1.5 2017/05/02 17:07:06 mikeb Exp $ + + Copyright (c) 2008 Damien Bergamini + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +crypto/cmac.c +include/crypto/cmac.h +===================== + $OpenBSD: cmac.c,v 1.3 2017/05/02 17:07:06 mikeb Exp $ + $OpenBSD: cmac.h,v 1.3 2017/05/02 17:07:06 mikeb Exp $ + + Copyright (c) 2008 Damien Bergamini + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +crypto/siphash.c +include/crypto/siphash.h +======================== + $OpenBSD: siphash.c,v 1.5 2018/01/05 19:05:09 mikeb Exp $ + $OpenBSD: siphash.h,v 1.5 2018/01/05 19:05:09 mikeb Exp $ + + Copyright (c) 2013 Andre Oppermann + All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote + products derived from this software without specific prior written + permission. + * +THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +crypto/hmac.c +include/crypto/hmac.h +====================== + $OpenBSD: hmac.c,v 1.4 2016/09/19 18:09:40 tedu Exp $ + $OpenBSD: hmac.h,v 1.4 2016/09/19 18:09:40 tedu Exp $ + + Copyright (c) 2008 Damien Bergamini + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +crypto/idgen.c +include/crypto/idgen.h +====================== + $OpenBSD: idgen.c,v 1.8 2020/07/22 13:54:30 tobhe Exp $ + $OpenBSD: idgen.h,v 1.8 2020/07/22 13:54:30 tobhe Exp $ + + Copyright (c) 2008 Damien Miller + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +include/crypto/curve25519.h +====================== + $OpenBSD: curve25519.h,v 1.2 2020/07/22 13:54:30 tobhe Exp $ + + Copyright (C) 2019-2020 Matt Dunwoodie + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +crypto/curve25519.c +====================== + $OpenBSD: curve25519.c,v 1.2 2020/07/22 13:54:30 tobhe Exp $ + + Copyright (C) 2018-2020 Jason A. Donenfeld . + All Rights Reserved. + Copyright (C) 2015-2016 The fiat-crypto Authors. + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +This contains two implementation: a machine-generated formally verified +implementation of Curve25519 ECDH from: +. Though originally machine +generated, it has been tweaked to be suitable for use in the kernel. It is +optimized for 32-bit machines and machines that cannot work efficiently with +128-bit integer types. + +libs/libc/regcomp.c +libs/libc/regerror.c +libs/libc/regexec.c +libs/libc/tre.h +libs/libc/tre-mem.c +include/regex.h +====================== +musl as a whole is licensed under the following standard MIT license: + +---------------------------------------------------------------------- +Copyright © 2005-2014 Rich Felker, et al. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +---------------------------------------------------------------------- + +drivers/misc/optee_msg.h +====================== +PDX-License-Identifier: BSD-2-Clause +Copyright (c) 2015-2020, Linaro Limited + +include/nuttx/tee.h +====================== +Copyright (c) 2015-2016, Linaro Limited +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following +conditions are met: + +1. Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Documentation/_extensions/warnings_filter.py +============================================ +Copyright (c) 2021 Nordic Semiconductor ASA +SPDX-License-Identifier: Apache-2.0 + +arch/arm/src/rp2040/rp2040_clock.c +arch/arm/src/rp2040/rp2040_pll.c +arch/arm/src/rp2040/rp2040_xosc.c +arch/arm/src/rp2040/rp2040_pio.c +arch/arm/src/rp2040/rp2040_pio.h +arch/arm/src/rp2040/rp2040_pio_instructions.h +arch/arm/src/rp2040/hardware/*.h +============================================= + +Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGE. + +.github/actions/free-disk-space/action.yaml +============================================= +Copyright (c) 2022 Jérémie Lumbroso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +include/search.h +====================== +$NetBSD: search.h,v 1.12 1999/02/22 10:34:28 christos Exp $ +$FreeBSD: src/include/search.h,v 1.4 2002/03/23 17:24:53 imp Exp $ + +Written by J.T. Conklin +Public domain. + +libs/libc/search/hcreate.c +libs/libc/search/hcreate_r.c +====================== +$NetBSD: hcreate.c,v 1.2 2001/02/19 21:26:04 ross Exp $ + +Copyright (c) 2001 Christopher G. Demetriou +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +libs/libc/search/hash_func.c +====================== +Copyright (c) 1990, 1993 +The Regents of the University of California. All rights reserved. + +This code is derived from software contributed to Berkeley by +Margo Seltzer. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +libs/libc/machine/x86_64/gnu/arch_memcmp.S +libs/libc/machine/x86_64/gnu/arch_memset_avx2.S +libs/libc/machine/x86_64/gnu/arch_memset_sse2.S +libs/libc/machine/x86_64/gnu/arch_stpcpy.S +libs/libc/machine/x86_64/gnu/arch_stpncpy.S +libs/libc/machine/x86_64/gnu/arch_strcat.S +libs/libc/machine/x86_64/gnu/arch_strcpy.S +libs/libc/machine/x86_64/gnu/arch_strlen.S +libs/libc/machine/x86_64/gnu/arch_strncmp.S +libs/libc/machine/x86_64/gnu/arch_strncpy.S +libs/libc/machine/x86_64/gnu/cache.h +====================== +Copyright (c) 2014, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + + * Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +drivers/i3c/device.c +drivers/i3c/i3c_driver.c +drivers/i3c/master.c +drivers/i3c/internals.h +====================== + + Copyright (C) 2018 Cadence Design Systems Inc. + Author: Boris Brezillon + + SPDX-License-Identifier: Apache-2.0 + +libs/libc/string/lib_bsdmemccpy.c +libs/libc/string/lib_bsdmemchr.c +libs/libc/string/lib_bsdmemcmp.c +libs/libc/string/lib_bsdmemcpy.c +libs/libc/string/lib_bsdmemrchr.c +libs/libc/string/lib_bsdstpcpy.c +libs/libc/string/lib_bsdstpncpy.c +libs/libc/string/lib_bsdstrcat.c +libs/libc/string/lib_bsdstrchr.c +libs/libc/string/lib_bsdstrchrnul.c +libs/libc/string/lib_bsdstrcmp.c +libs/libc/string/lib_bsdstrcpy.c +libs/libc/string/lib_bsdstrlen.c +libs/libc/string/lib_bsdstrncmp.c +libs/libc/string/lib_bsdstrncpy.c +libs/libc/string/lib_bsdstrrchr.c +====================== + +Copyright (c) 1994-2009 Red Hat, Inc. All rights reserved. + +This copyrighted material is made available to anyone wishing to use, +modify, copy, or redistribute it subject to the terms and conditions +of the BSD License. This program is distributed in the hope that +it will be useful, but WITHOUT ANY WARRANTY expressed or implied, +including the implied warranties of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. A copy of this license is available at +http://www.opensource.org/licenses. Any Red Hat trademarks that are +incorporated in the source code or documentation are not subject to +the BSD License and may only be used or replicated with the express +permission of Red Hat, Inc. diff --git a/tasks/net-nuttx-tcp-retry-closure/template/Makefile b/tasks/net-nuttx-tcp-retry-closure/template/Makefile new file mode 100644 index 00000000..a9095e80 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/Makefile @@ -0,0 +1,11 @@ +CC = gcc +CFLAGS = -std=gnu11 -O0 -g -Wall -Wextra -Wno-unused-parameter -I. -Iport +SOURCES = net/tcp/tcp_timer.c net/tcp/tcp_callback.c net/tcp/tcp_monitor.c net/tcp/tcp_appsend.c net/tcp/tcp_seqno.c net/tcp/tcp_devpoll.c net/devif/devif_callback.c lab.c +all: tcp-lab +tcp-lab: $(SOURCES) port.h + $(CC) $(CFLAGS) $(SOURCES) -o $@ +smoke: tcp-lab + ./tcp-lab < smoke.in +clean: + rm -f tcp-lab +.PHONY: all smoke clean diff --git a/tasks/net-nuttx-tcp-retry-closure/template/NOTICE b/tasks/net-nuttx-tcp-retry-closure/template/NOTICE new file mode 100644 index 00000000..6f031e28 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/NOTICE @@ -0,0 +1,68 @@ +Apache NuttX +Copyright 2020 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +FAT Long File Names +=================== + + NOTE: If CONFIG_FAT_LFN is defined in your NuttX configuration file, then + there may be some legal, patent issues. The following was extracted from + the entry "File Allocation Table from Wikipedia, the free encyclopedia: + + "On December 3, 2003 Microsoft announced it would be offering licenses + for use of its FAT specification and 'associated intellectual property', + at the cost of a US$0.25 royalty per unit sold, with a $250,000 maximum + royalty per license agreement. + + o "U.S. Patent 5,745,902 (http://www.google.com/patents?vid=5745902) - + Method and system for accessing a file using file names having + different file name formats. ... + o "U.S. Patent 5,579,517 (http://www.google.com/patents?vid=5579517) - + Common name space for long and short filenames. ... + o "U.S. Patent 5,758,352 (http://www.google.com/patents?vid=5758352) - + Common name space for long and short filenames. ... + o "U.S. Patent 6,286,013 (http://www.google.com/patents?vid=6286013) - + Method and system for providing a common name space for long and + short file names in an operating system. ... + + "Many technical commentators have concluded that these patents only cover + FAT implementations that include support for long filenames, and that + removable solid state media and consumer devices only using short names + would be unaffected. ..." + + This Wikipedia entry has been updated with recent legal decisions in favor + of Microsoft. Refer to: + + https://en.wikipedia.org/wiki/File_Allocation_Table#Patents + + So you have been forewarned: Use the long filename at your own risk! + +NXP Restriction for SPIFI code +============================== + + NOTE: Code supporting spifi for the LCP43xx has restricted usage by NXP + FILES: + arch/arm/src/lpc43xx/hardware/lpc43_spifi.h + arch/arm/src/lpc43xx/spifi/inc/spifilib_api.h + arch/arm/src/lpc43xx/spifi/inc/spifilib_dev.h + arch/arm/src/lpc43xx/spifi/inc/private/spifilib_chiphw.h + arch/arm/src/lpc43xx/spifi/src/spifilib_dev_common.c + arch/arm/src/lpc43xx/spifi/src/spifilib_fam_standard_cmd.c + + Permission to use, copy, modify, and distribute this software and its + documentation is hereby granted, under NXP Semiconductors' and its + licensor's relevant copyrights in the software, without fee, provided that it + is used in conjunction with NXP Semiconductors microcontrollers. This + copyright, permission, and disclaimer notice must appear in all copies of + this code. + +Documentation +============= + +- nRF52832-MDK board pinout image is obtained from MakerDiary GitHub repository, + under the terms of the MIT license, Copyright (c) 2019 makerdiary.com +- Sparkfun nRF52832 breakout board image is licensed under CC-BY 2.0 terms +- Espressif ESP32 DevKitC images are licensed under the terms of Apache 2.0, + Copyright (C) 2015-2019 Espressif Systems diff --git a/tasks/net-nuttx-tcp-retry-closure/template/README.md b/tasks/net-nuttx-tcp-retry-closure/template/README.md new file mode 100644 index 00000000..dac158fd --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/README.md @@ -0,0 +1,17 @@ +# TCP 设备侧现场复现工作区 + +设备通信层在丢失应答后出现等待间隔异常;另一些长期无响应的连接已经用尽探测次数,业务线程仍在等待。请让保留的 TCP 模块符合 `docs/tcp-contract.md` 的全部条款,保持正常连接和轮询行为。 + +构建和正常路径冒烟: + +```sh +make smoke +``` + +`tcp-lab` 是原始观察器,不会给出通过结论。标准输入第一行的 17 个无符号整数依次为:TCP 状态、未确认字节数、已重传次数、RTO、重传剩余半秒、keepalive 开关、keepidle tick、keepintvl tick、keepcnt、已发送 keepalive 次数、发送序号、最早未确认序号、重传应用字节数、普通 poll 应用字节数、等待 socket 数(0–2)、待 delayed ACK 段数、是否存在监听连接。 + +后续每行为命令和整数:`A ticks` 前进指定虚拟 tick,逐次执行沿途已登记工作及设备 poll;`P 0` 执行一次普通设备 poll;`Z flags` 请求零窗口探测;`S 0` 停止计时。每条命令后打印 JSON 原始观察,包括包的 `[tick, TCP flags, 总长度, sequence]`、socket 事件和当前连接字段。初始化也打印一次观察。事件位及 TCP flags 不是同一个编码,见 `port.h` 和上游头文件。 + +`port/` 和 `port.h` 为此实验的最小端口声明;`lab.c` 仅实现虚拟 tick、工作队列、包输出、等待 socket 端口和不使用的 I/O 原语。核心计时、发送与连接监视仍执行上游 C 文件。随附上游头文件用于阅读原合同,构建时使用 port 目录中匹配本配置的声明。 + +最终只提交并评分 `net/tcp/tcp_timer.c`,新增文件不进入正式候选。正式验收会从干净的端口材料重新编译该文件,并覆盖公开合同中的其他输入组合。 diff --git a/tasks/net-nuttx-tcp-retry-closure/template/docs/tcp-contract.md b/tasks/net-nuttx-tcp-retry-closure/template/docs/tcp-contract.md new file mode 100644 index 00000000..e30bd6d4 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/docs/tcp-contract.md @@ -0,0 +1,27 @@ +# TCP 软件实验合同 + +这里保留 TCP 计时、轮询、应用发送、回调、连接监视及 devif 回调链。端口层只提供单连接、单设备、虚拟工作队列和包输出端口,不提供真实网卡或完整操作系统。 + +R01. 构建配置启用 TCP、keepalive 和 delayed ACK,采用非 write-buffer 发送方式。基本 RTO 为 3 个半秒,普通数据最多重试 8 次,SYN 最多重试 5 次;TCP 状态和事件位的定义见随附头文件。 + +R02. 一个 tick 是 100 毫秒,5 tick 是一个半秒。工作队列按相对延迟登记到期回调,到期回调通知设备,设备通过正常 TCP poll 入口处理计时。普通 poll 不消耗计时器。实验输入中的非零 keepidle、keepintvl 均为 5 tick 的整数倍,重传计时器单位为半秒。 + +R03. 有未确认数据时,到期重传采用 `3 × 2^min(n,4)` 个半秒的等待值,n 为本次重传前已经发生的重传次数;该值成为连接的 RTO 并用于下一次重传期限。等待期间不得提前重发;未到期的重传剩余时间随已处理的半秒递减。 + +R04. ESTABLISHED 的重传先向应用发送 REXMIT 事件,再发送应用提供的数据。非零 rexmit_seq 指定最早未确认段的序号,发送时使用该序号,保留连接当前发送序号。无重传数据时不凭空制造数据包。 + +R05. SYN_RCVD 重发 SYN|ACK,SYN_SENT 重发 SYN,FIN_WAIT_1、CLOSING 和 LAST_ACK 重发 FIN|ACK;这些重传使用 rexmit_seq。重试已耗尽时连接变为 CLOSED,通知等待者 TIMEDOUT,并发送 RST|ACK。SYN_RCVD 的通知发给监听连接,找不到监听连接时仍发送 reset。实验的监听连接可配置为当前连接。 + +R06. TIME_WAIT 和 FIN_WAIT_2 等待各自期限,届满后变为 CLOSED 并通知 TIMEDOUT,不发送额外报文。CLOSED 不生成包或应用轮询事件。 + +R07. keepalive 只处理没有未确认数据的 ESTABLISHED 连接,并且须已启用。空闲等待从 keepidle 开始,之后探测间隔为 keepintvl。每个探测只带 ACK,序号为当前发送序号减一(按 32 位回绕),发送后保留原序号。有未确认数据期间由重传处理,不发送 keepalive 探测;禁用 keepalive 不因这些参数终止连接。 + +R08. 每次空闲或探测期限届满时,若探测额度已用尽,连接向等待中的各 socket 传播 ABORT,唤醒等待者,并通过连接丢失处理清除 socket 的 CONNECTED 和 CLOSED 标志,停止连接监视。 + +R09. ESTABLISHED 的普通轮询向应用发送 POLL 事件;应用没有数据时无包,有数据时发送 ACK|PSH、长度为应用数据加 40 字节,增加未确认数据及发送序号。其他状态的普通 poll 不生成应用数据包。 + +R10. delayed ACK 在至少一个半秒的计时处理后发送纯 ACK,清除待确认段状态;没有待确认段时不凭空发送 ACK。 + +R11. 无未确认数据、没有 NEWDATA、由 POLL/REXMIT/ACKDATA 引起的零窗口探测请求,在尚未超时且尚未探测时登记一个半秒的初次期限。后续零窗口探测只带 ACK 并保留原发送序号,以 `min(2^n,240)` 个半秒等待下次探测;普通重试上限适用。已有探测不重复登记初次期限,停止计时操作取消已登记的工作。 + +这些条款整理自随附的 NuttX 头文件/函数合同及 TCP 的公开规定:RFC 6298 §5.4–5.5(重传与退避)、RFC 1122 §4.2.3.6(keepalive)、RFC 1122 §4.2.3.2(ACK)。本实验对可配置参数采用头文件中的默认值;不验证真实网络时延或对端 TCP 实现。 diff --git a/tasks/net-nuttx-tcp-retry-closure/template/include/nuttx/net/netconfig.h b/tasks/net-nuttx-tcp-retry-closure/template/include/nuttx/net/netconfig.h new file mode 100644 index 00000000..0a34801a --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/include/nuttx/net/netconfig.h @@ -0,0 +1,666 @@ +/**************************************************************************** + * include/nuttx/net/netconfig.h + * + * SPDX-License-Identifier: BSD-3-Clause + * SPDX-FileCopyrightText: 2007, 2011, 2014-2015, 2017-2019 Gregory Nutt. + * All rights reserved. + * SPDX-FileCopyrightText: 2001-2003, Adam Dunkels. All rights reserved. + * SPDX-FileContributor: Gregory Nutt + * SPDX-FileContributor: Adam Dunkels + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_NET_NETCONFG_H +#define __INCLUDE_NUTTX_NET_NETCONFG_H + +/* Note: Network configuration options the netconfig.h should not be changed, + * but rather the per-project defconfig file. + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Using the following definitions, the following socket() arguments should + * provide a valid socket in all configurations: + * + * ret = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, + * NET_SOCK_PROTOCOL); + */ + +/* The TCP/UDP stack, which is used for determining HAVE_PFINET(6)_SOCKETS */ + +#undef NET_TCP_HAVE_STACK +#if defined(CONFIG_NET_TCP) && !defined(CONFIG_NET_TCP_NO_STACK) +# define NET_TCP_HAVE_STACK 1 +#endif + +#undef NET_UDP_HAVE_STACK +#if defined(CONFIG_NET_UDP) && !defined(CONFIG_NET_UDP_NO_STACK) +# define NET_UDP_HAVE_STACK 1 +#endif + +/* The address family that we used to create the socket really does not + * matter. It should, however, be valid in the current configuration. + */ + +#undef HAVE_INET_SOCKETS +#undef HAVE_PFINET_SOCKETS +#undef HAVE_PFINET6_SOCKETS + +#if defined(CONFIG_NET_IPv4) || defined(CONFIG_NET_IPv6) +# define HAVE_INET_SOCKETS + +# if (defined(CONFIG_NET_IPv4) && (defined(NET_UDP_HAVE_STACK) || \ + defined(NET_TCP_HAVE_STACK) || defined(CONFIG_NET_USRSOCK))) || \ + defined(CONFIG_NET_ICMP_SOCKET) +# define HAVE_PFINET_SOCKETS +# endif + +# if (defined(CONFIG_NET_IPv6) && (defined(NET_UDP_HAVE_STACK) || \ + defined(NET_TCP_HAVE_STACK) || defined(CONFIG_NET_USRSOCK))) || \ + defined(CONFIG_NET_ICMPv6_SOCKET) +# define HAVE_PFINET6_SOCKETS +# endif +#endif + +#if defined(HAVE_PFINET_SOCKETS) +# define NET_SOCK_FAMILY AF_INET +#elif defined(HAVE_PFINET6_SOCKETS) +# define NET_SOCK_FAMILY AF_INET6 +#elif defined(CONFIG_NET_LOCAL) +# define NET_SOCK_FAMILY AF_LOCAL +#elif defined(CONFIG_NET_PKT) +# define NET_SOCK_FAMILY AF_PACKET +#elif defined(CONFIG_NET_CAN) +# define NET_SOCK_FAMILY AF_CAN +#elif defined(CONFIG_NET_IEEE802154) +# define NET_SOCK_FAMILY AF_IEEE802154 +#elif defined(CONFIG_WIRELESS_PKTRADIO) +# define NET_SOCK_FAMILY AF_PKTRADIO +#elif defined(CONFIG_NET_BLUETOOTH) +# define NET_SOCK_FAMILY AF_BLUETOOTH +#elif defined(CONFIG_NET_USRSOCK) +# define NET_SOCK_FAMILY AF_INET +#elif defined(CONFIG_NET_NETLINK) +# define NET_SOCK_FAMILY AF_NETLINK +#elif defined(CONFIG_NET_RPMSG) +# define NET_SOCK_FAMILY AF_RPMSG +#else +# define NET_SOCK_FAMILY AF_UNSPEC +#endif + +/* Socket protocol of zero normally works */ + +#define NET_SOCK_PROTOCOL 0 + +/* SOCK_CTRL is the preferred socket type to use when we just want a + * socket for performing driver ioctls. + */ + +#define NET_SOCK_TYPE (SOCK_CTRL | SOCK_CLOEXEC) + +#if NET_SOCK_FAMILY == AF_INET +# if !defined(CONFIG_NET_UDP) && !defined(CONFIG_NET_TCP) && \ + defined(CONFIG_NET_ICMP_SOCKET) +# undef NET_SOCK_PROTOCOL +# define NET_SOCK_PROTOCOL IPPROTO_ICMP +# endif +#elif NET_SOCK_FAMILY == AF_INET6 +# if !defined(CONFIG_NET_UDP) && !defined(CONFIG_NET_TCP) && \ + defined(CONFIG_NET_ICMPv6_SOCKET) +# undef NET_SOCK_PROTOCOL +# define NET_SOCK_PROTOCOL IPPROTO_ICMP6 +# endif +#endif + +/* Eliminate dependencies on other header files. This should not harm + * portability because these are well-known constants. + */ + +#define __IPv4_HDRLEN 20 /* Must match IPv4_HDRLEN in include/nuttx/net/ip.h */ +#define __IPv6_HDRLEN 40 /* Must match IPv6_HDRLEN in include/nuttx/net/ip.h */ +#define __UDP_HDRLEN 8 /* Must match UDP_HDRLEN in include/nuttx/net/udp.h */ +#define __TCP_HDRLEN 20 /* Must match TCP_HDRLEN in include/nuttx/net/tcp.h */ + /* REVISIT: Not really a constant */ + +/* Layer 2 Configuration Options ********************************************/ + +/* The default link layer is Ethernet. If CONFIG_NET_SLIP is defined in + * the NuttX header file, then SLIP will be supported. The basic + * differences between the SLIP and Ethernet configurations is that when SLIP + * is selected: + * + * - The link level header (that comes before the IP header) is omitted. + * - All MAC address processing is suppressed. + * - ARP is disabled. + * + * If CONFIG_NET_SLIP is not supported, then Ethernet will be used (there is + * no need to define anything special in the configuration file to use + * Ethernet -- it is the default). + * + * The "link level header" is the offset into the d_buf where the IP header + * can be found. For Ethernet, this should be set to 14. For SLIP, this + * should be set to 0. + * + * There are other device-specific features that at tied to the link layer: + * + * - Maximum Transfer Unit (MTU) + * - TCP Receive Window size (See TCP configuration options below) + * + * A better solution would be to support device-by-device MTU and receive + * window sizes. This minimum support is require to support the optimal + * SLIP MTU of 296 bytes and the standard Ethernet MTU of 1500 + * bytes. + */ + +#ifdef CONFIG_NET_SLIP +# ifndef CONFIG_NET_SLIP_PKTSIZE +# define CONFIG_NET_SLIP_PKTSIZE 296 +# endif +#endif + +#ifdef CONFIG_NET_TUN +# ifndef CONFIG_NET_TUN_PKTSIZE +# define CONFIG_NET_TUN_PKTSIZE 296 +# endif +#endif + +#ifdef CONFIG_NET_ETHERNET +# ifndef CONFIG_NET_ETH_PKTSIZE +# define CONFIG_NET_ETH_PKTSIZE 590 +# endif +#endif + +#ifndef CONFIG_NET_6LOWPAN_PKTSIZE +# define CONFIG_NET_6LOWPAN_PKTSIZE 1294 +#endif + +/* We are supporting multiple network devices using different link layer + * protocols. Get the size of the link layer header from the device + * structure. + */ + +#define NET_LL_HDRLEN(d) ((d)->d_llhdrlen) +#define NETDEV_PKTSIZE(d) ((d)->d_pktsize) + +#ifdef CONFIG_NET_ETHERNET +# define _MIN_ETH_PKTSIZE CONFIG_NET_ETH_PKTSIZE +# define _MAX_ETH_PKTSIZE CONFIG_NET_ETH_PKTSIZE +#else +# define _MIN_ETH_PKTSIZE UINT16_MAX +# define _MAX_ETH_PKTSIZE 0 +#endif + +#ifdef CONFIG_NET_LOOPBACK +# define _MIN_LO_PKTSIZE MIN(_MIN_ETH_PKTSIZE, 574) +# define _MAX_LO_PKTSIZE MAX(_MAX_ETH_PKTSIZE, 1518) +#else +# define _MIN_LO_PKTSIZE _MIN_ETH_PKTSIZE +# define _MAX_LO_PKTSIZE _MAX_ETH_PKTSIZE +#endif + +#ifdef CONFIG_NET_SLIP +# define _MIN_SLIP_PKTSIZE MIN(_MIN_LO_PKTSIZE, CONFIG_NET_SLIP_PKTSIZE) +# define _MAX_SLIP_PKTSIZE MAX(_MAX_LO_PKTSIZE, CONFIG_NET_SLIP_PKTSIZE) +#else +# define _MIN_SLIP_PKTSIZE _MIN_LO_PKTSIZE +# define _MAX_SLIP_PKTSIZE _MAX_LO_PKTSIZE +#endif + +#ifdef CONFIG_NET_TUN +# define _MIN_TUN_PKTSIZE MIN(_MIN_SLIP_PKTSIZE, CONFIG_NET_TUN_PKTSIZE) +# define _MAX_TUN_PKTSIZE MAX(_MAX_SLIP_PKTSIZE, CONFIG_NET_TUN_PKTSIZE) +#else +# define _MIN_TUN_PKTSIZE _MIN_SLIP_PKTSIZE +# define _MAX_TUN_PKTSIZE _MAX_SLIP_PKTSIZE +#endif + +#ifdef CONFIG_NET_6LOWPAN +# define _MIN_6LOWPAN_PKTSIZE MIN(_MIN_TUN_PKTSIZE, CONFIG_NET_6LOWPAN_PKTSIZE) +# define _MAX_6LOWPAN_PKTSIZE MAX(_MAX_TUN_PKTSIZE, CONFIG_NET_6LOWPAN_PKTSIZE) +#else +# define _MIN_6LOWPAN_PKTSIZE _MIN_TUN_PKTSIZE +# define _MAX_6LOWPAN_PKTSIZE _MAX_TUN_PKTSIZE +#endif + +#define MIN_NETDEV_PKTSIZE _MIN_6LOWPAN_PKTSIZE +#define MAX_NETDEV_PKTSIZE _MAX_6LOWPAN_PKTSIZE + +/* The loopback driver packet buffer should be quite large. The larger the + * loopback packet buffer, the better will be TCP performance of the loopback + * transfers. The Linux loopback device historically used packet buffers of + * size 16Kb, but that was increased in recent Linux versions to 64Kb. Those + * sizes may be excessive for resource constrained MCUs, however. + * + * For the loopback driver, we enforce a lower limit that is the maximum + * packet size of all enabled link layer protocols. + */ + +#ifndef CONFIG_NET_LOOPBACK_PKTSIZE +# define CONFIG_NET_LOOPBACK_PKTSIZE 0 +#endif + +#if CONFIG_NET_LOOPBACK_PKTSIZE < MAX_NETDEV_PKTSIZE +# define NET_LO_PKTSIZE MAX_NETDEV_PKTSIZE +#else +# define NET_LO_PKTSIZE CONFIG_NET_LOOPBACK_PKTSIZE +#endif + +#ifndef CONFIG_NET_SEND_BUFSIZE +# define CONFIG_NET_SEND_BUFSIZE 0 +#endif + +/* Layer 3/4 Configuration Options ******************************************/ + +/* IP configuration options */ + +/* The IP TTL (time to live) of IP packets sent by the network stack. + * + * This should normally not be changed. + */ + +#define IP_TTL_DEFAULT 64 + +/* Network drivers often receive packets with garbage at the end + * and are longer than the size of packet in the TCP header. The + * following "fudge" factor increases the size of the I/O buffering + * by a small amount to allocate slightly oversize packets. After + * receipt, the packet size will be chopped down to the size indicated + * in the TCP header. + */ + +#ifndef CONFIG_NET_GUARDSIZE +# define CONFIG_NET_GUARDSIZE 2 +#endif + +/* ICMP configuration options */ + +#ifndef CONFIG_NET_ICMP +# undef CONFIG_NET_ICMP_SOCKET +#endif + +/* UDP configuration options */ + +/* The UDP maximum packet size. This should not be set to more than + * NETDEV_PKTSIZE(d) - NET_LL_HDRLEN(dev) - __UDP_HDRLEN - IPv*_HDRLEN. + */ + +#define UDP_MSS(d,h) (NETDEV_PKTSIZE(d) - NET_LL_HDRLEN(d) - __UDP_HDRLEN - (h)) + +#ifdef CONFIG_NET_ETHERNET +# define ETH_UDP_MSS(h) (CONFIG_NET_ETH_PKTSIZE - ETH_HDRLEN - __UDP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_6LOWPAN +# define IEEE802154_UDP_MSS(h) (CONFIG_NET_6LOWPAN_PKTSIZE - __UDP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_LOOPBACK +# define LO_UDP_MSS(h) (NET_LO_PKTSIZE - __UDP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_SLIP +# define SLIP_UDP_MSS(h) (CONFIG_NET_SLIP_PKTSIZE - __UDP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_TUN +# define TUN_UDP_MSS(h) (CONFIG_NET_TUN_PKTSIZE - __UDP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_USRSOCK +# define __MIN_UDP_MSS(h) INT_MAX +# define __MAX_UDP_MSS(h) 0 +#endif + +#ifdef CONFIG_NET_ETHERNET +# undef __MIN_UDP_MSS +# undef __MAX_UDP_MSS +# define __MIN_UDP_MSS(h) ETH_UDP_MSS(h) +# define __MAX_UDP_MSS(h) ETH_UDP_MSS(h) +# define __ETH_MIN_UDP_MSS(h) ETH_UDP_MSS(h) +# define __ETH_MAX_UDP_MSS(h) ETH_UDP_MSS(h) +#else +# define __ETH_MIN_UDP_MSS(h) INT_MAX +# define __ETH_MAX_UDP_MSS(h) 0 +#endif + +#ifdef CONFIG_NET_6LOWPAN +# undef __MIN_UDP_MSS +# undef __MAX_UDP_MSS +# define __MIN_UDP_MSS(h) MIN(IEEE802154_UDP_MSS(h),__ETH_MIN_UDP_MSS(h)) +# define __MAX_UDP_MSS(h) MAX(IEEE802154_UDP_MSS(h),__ETH_MAX_UDP_MSS(h)) +# define __6LOWPAN_MIN_UDP_MSS(h) MIN(IEEE802154_UDP_MSS(h),__ETH_MIN_UDP_MSS(h)) +# define __6LOWPAN_MAX_UDP_MSS(h) MAX(IEEE802154_UDP_MSS(h),__ETH_MAX_UDP_MSS(h)) +#else +# define __6LOWPAN_MIN_UDP_MSS(h) __ETH_MIN_UDP_MSS(h) +# define __6LOWPAN_MAX_UDP_MSS(h) __ETH_MAX_UDP_MSS(h) +#endif + +#ifdef CONFIG_NET_LOOPBACK +# undef __MIN_UDP_MSS +# undef __MAX_UDP_MSS +# define __MIN_UDP_MSS(h) MIN(LO_UDP_MSS(h),__6LOWPAN_MIN_UDP_MSS(h)) +# define __MAX_UDP_MSS(h) MAX(LO_UDP_MSS(h),__6LOWPAN_MAX_UDP_MSS(h)) +# define __LOOP_MIN_UDP_MSS(h) MIN(LO_UDP_MSS(h),__6LOWPAN_MIN_UDP_MSS(h)) +# define __LOOP_MAX_UDP_MSS(h) MAX(LO_UDP_MSS(h),__6LOWPAN_MAX_UDP_MSS(h)) +#else +# define __LOOP_MIN_UDP_MSS(h) __6LOWPAN_MIN_UDP_MSS(h) +# define __LOOP_MAX_UDP_MSS(h) __6LOWPAN_MAX_UDP_MSS(h) +#endif + +#ifdef CONFIG_NET_SLIP +# undef __MIN_UDP_MSS +# undef __MAX_UDP_MSS +# define __MIN_UDP_MSS(h) MIN(SLIP_UDP_MSS(h),__LOOP_MIN_UDP_MSS(h)) +# define __MAX_UDP_MSS(h) MAX(SLIP_UDP_MSS(h),__LOOP_MAX_UDP_MSS(h)) +# define __SLIP_MIN_UDP_MSS(h) MIN(SLIP_UDP_MSS(h),__LOOP_MIN_UDP_MSS(h)) +# define __SLIP_MAX_UDP_MSS(h) MAX(SLIP_UDP_MSS(h),__LOOP_MAX_UDP_MSS(h)) +#else +# define __SLIP_MIN_UDP_MSS(h) __LOOP_MIN_UDP_MSS(h) +# define __SLIP_MAX_UDP_MSS(h) __LOOP_MAX_UDP_MSS(h) +#endif + +#ifdef CONFIG_NET_TUN +# undef __MIN_UDP_MSS +# undef __MAX_UDP_MSS +# define __MIN_UDP_MSS(h) MIN(TUN_UDP_MSS(h),__SLIP_MIN_UDP_MSS(h)) +# define __MAX_UDP_MSS(h) MAX(TUN_UDP_MSS(h),__SLIP_MAX_UDP_MSS(h)) +# define __TUN_MIN_UDP_MSS(h) MIN(TUN_UDP_MSS(h),__SLIP_MIN_UDP_MSS(h)) +# define __TUN_MAX_UDP_MSS(h) MAX(TUN_UDP_MSS(h),__SLIP_MAX_UDP_MSS(h)) +#else +# define __TUN_MIN_UDP_MSS(h) __SLIP_MIN_UDP_MSS(h) +# define __TUN_MAX_UDP_MSS(h) __SLIP_MAX_UDP_MSS(h) +#endif + + #ifdef CONFIG_NET_IPv4 +# define UDP_IPv4_MSS(d) UDP_MSS(d,__IPv4_HDRLEN) +# define ETH_IPv4_UDP_MSS ETH_UDP_MSS(__IPv4_HDRLEN) +# define SLIP_IPv4_UDP_MSS SLIP_UDP_MSS(__IPv4_HDRLEN) +# define TUN_IPv4_UDP_MSS TUN_UDP_MSS(__IPv4_HDRLEN) + +# define MIN_IPv4_UDP_MSS __MIN_UDP_MSS(__IPv4_HDRLEN) +# define MIN_UDP_MSS __MIN_UDP_MSS(__IPv4_HDRLEN) + +# undef MAX_UDP_MSS +# define MAX_IPv4_UDP_MSS __MAX_UDP_MSS(__IPv4_HDRLEN) +# define MAX_UDP_MSS __MAX_UDP_MSS(__IPv4_HDRLEN) +#endif + +/* If IPv6 is supported, it will have the smaller MSS. */ + +#ifdef CONFIG_NET_IPv6 +# undef MIN_UDP_MSS +# define MIN_IPv6_UDP_MSS __MIN_UDP_MSS(__IPv6_HDRLEN) +# define MIN_UDP_MSS __MIN_UDP_MSS(__IPv6_HDRLEN) +# ifndef MAX_UDP_MSS +# define MAX_UDP_MSS __MAX_UDP_MSS(__IPv6_HDRLEN) +# endif +#endif + +/* TCP configuration options */ + +/* The maximum number of simultaneously listening TCP ports. + * + * Each listening TCP port requires 2 bytes of memory. + */ + +#ifndef CONFIG_NET_MAX_LISTENPORTS +# define CONFIG_NET_MAX_LISTENPORTS 20 +#endif + +/* The initial retransmission timeout counted in timer pulses. + * REVISIT: TCP RTO really should be calculated dynamically for each TCP + * connection. + */ + +#ifdef CONFIG_NET_TCP_RTO +# define TCP_RTO CONFIG_NET_TCP_RTO +#else +# define TCP_RTO 3 +#endif + +/* The maximum number of times a segment should be retransmitted + * before the connection should be aborted. + * + * This should not be changed. + */ + +#ifdef CONFIG_NET_TCP_MAXRTX +# define TCP_MAXRTX CONFIG_NET_TCP_MAXRTX +#else +# define TCP_MAXRTX 8 +#endif + +/* The maximum number of times a SYN segment should be retransmitted + * before a connection request should be deemed to have been + * unsuccessful. + * + * This should not need to be changed. + */ + +#ifdef CONFIG_NET_TCP_MAXSYNRTX +# define TCP_MAXSYNRTX CONFIG_NET_TCP_MAXSYNRTX +#else +# define TCP_MAXSYNRTX 5 +#endif + +/* The TCP maximum segment size. This should not be set to more than + * NETDEV_PKTSIZE(dev) - NET_LL_HDRLEN(dev) - IPvN_HDRLEN - __TCP_HDRLEN. + * + * In the case where there are multiple network devices with different + * link layer protocols, each network device may support a different MSS + * value. Here we arbitrarily select the minimum MSS for that case. + * + * REVISIT: __TCP_HDRLEN is not really a constant! + */ + +#define TCP_MSS(d,h) (NETDEV_PKTSIZE(d) - NET_LL_HDRLEN(d) - __TCP_HDRLEN - (h)) + +/* Get the smallest and largest MSS */ + +#ifdef CONFIG_NET_ETHERNET +# define ETH_TCP_MSS(h) (CONFIG_NET_ETH_PKTSIZE - ETH_HDRLEN - __TCP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_6LOWPAN +# define IEEE802154_TCP_MSS(h) (CONFIG_NET_6LOWPAN_PKTSIZE - __TCP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_LOOPBACK +# define LO_TCP_MSS(h) (NET_LO_PKTSIZE - __TCP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_SLIP +# define SLIP_TCP_MSS(h) (CONFIG_NET_SLIP_PKTSIZE - __TCP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_TUN +# define TUN_TCP_MSS(h) (CONFIG_NET_TUN_PKTSIZE - __TCP_HDRLEN - (h)) +#endif + +#ifdef CONFIG_NET_USRSOCK +# define __MIN_TCP_MSS(h) INT_MAX +# define __MAX_TCP_MSS(h) 0 +#endif + +#ifdef CONFIG_NET_ETHERNET +# undef __MIN_TCP_MSS +# undef __MAX_TCP_MSS +# define __MIN_TCP_MSS(h) ETH_TCP_MSS(h) +# define __MAX_TCP_MSS(h) ETH_TCP_MSS(h) +# define __ETH_MIN_TCP_MSS(h) ETH_TCP_MSS(h) +# define __ETH_MAX_TCP_MSS(h) ETH_TCP_MSS(h) +#else +# define __ETH_MIN_TCP_MSS(h) INT_MAX +# define __ETH_MAX_TCP_MSS(h) 0 +#endif + +#ifdef CONFIG_NET_6LOWPAN +# undef __MIN_TCP_MSS +# undef __MAX_TCP_MSS +# define __MIN_TCP_MSS(h) MIN(IEEE802154_TCP_MSS(h),__ETH_MIN_TCP_MSS(h)) +# define __MAX_TCP_MSS(h) MAX(IEEE802154_TCP_MSS(h),__ETH_MAX_TCP_MSS(h)) +# define __6LOWPAN_MIN_TCP_MSS(h) MIN(IEEE802154_TCP_MSS(h),__ETH_MIN_TCP_MSS(h)) +# define __6LOWPAN_MAX_TCP_MSS(h) MAX(IEEE802154_TCP_MSS(h),__ETH_MAX_TCP_MSS(h)) +#else +# define __6LOWPAN_MIN_TCP_MSS(h) __ETH_MIN_TCP_MSS(h) +# define __6LOWPAN_MAX_TCP_MSS(h) __ETH_MAX_TCP_MSS(h) +#endif + +#ifdef CONFIG_NET_LOOPBACK +# undef __MIN_TCP_MSS +# undef __MAX_TCP_MSS +# define __MIN_TCP_MSS(h) MIN(LO_TCP_MSS(h),__6LOWPAN_MIN_TCP_MSS(h)) +# define __MAX_TCP_MSS(h) MAX(LO_TCP_MSS(h),__6LOWPAN_MAX_TCP_MSS(h)) +# define __LOOP_MIN_TCP_MSS(h) MIN(LO_TCP_MSS(h),__6LOWPAN_MIN_TCP_MSS(h)) +# define __LOOP_MAX_TCP_MSS(h) MAX(LO_TCP_MSS(h),__6LOWPAN_MAX_TCP_MSS(h)) +#else +# define __LOOP_MIN_TCP_MSS(h) __6LOWPAN_MIN_TCP_MSS(h) +# define __LOOP_MAX_TCP_MSS(h) __6LOWPAN_MAX_TCP_MSS(h) +#endif + +#ifdef CONFIG_NET_SLIP +# undef __MIN_TCP_MSS +# undef __MAX_TCP_MSS +# define __MIN_TCP_MSS(h) MIN(SLIP_TCP_MSS(h),__LOOP_MIN_TCP_MSS(h)) +# define __MAX_TCP_MSS(h) MAX(SLIP_TCP_MSS(h),__LOOP_MAX_TCP_MSS(h)) +# define __SLIP_MIN_TCP_MSS(h) MIN(SLIP_TCP_MSS(h),__LOOP_MIN_TCP_MSS(h)) +# define __SLIP_MAX_TCP_MSS(h) MAX(SLIP_TCP_MSS(h),__LOOP_MAX_TCP_MSS(h)) +#else +# define __SLIP_MIN_TCP_MSS(h) __LOOP_MIN_TCP_MSS(h) +# define __SLIP_MAX_TCP_MSS(h) __LOOP_MAX_TCP_MSS(h) +#endif + +#ifdef CONFIG_NET_TUN +# undef __MIN_TCP_MSS +# undef __MAX_TCP_MSS +# define __MIN_TCP_MSS(h) MIN(TUN_TCP_MSS(h),__SLIP_MIN_TCP_MSS(h)) +# define __MAX_TCP_MSS(h) MAX(TUN_TCP_MSS(h),__SLIP_MAX_TCP_MSS(h)) +# define __TUN_MIN_TCP_MSS(h) MIN(TUN_TCP_MSS(h),__SLIP_MIN_TCP_MSS(h)) +# define __TUN_MAX_TCP_MSS(h) MAX(TUN_TCP_MSS(h),__SLIP_MAX_TCP_MSS(h)) +#else +# define __TUN_MIN_TCP_MSS(h) __SLIP_MIN_TCP_MSS(h) +# define __TUN_MAX_TCP_MSS(h) __SLIP_MAX_TCP_MSS(h) +#endif + +/* If IPv4 is supported, it will have the larger MSS. + * NOTE: MSS calculation excludes the __TCP_HDRLEN. + */ + +#ifdef CONFIG_NET_IPv6 +# define TCP_IPv6_MSS(d) TCP_MSS(d,__IPv6_HDRLEN) +# define ETH_IPv6_TCP_MSS ETH_TCP_MSS(__IPv6_HDRLEN) +# define SLIP_IPv6_TCP_MSS SLIP_TCP_MSS(__IPv6_HDRLEN) +# define TUN_IPv6_TCP_MSS TUN_TCP_MSS(__IPv6_HDRLEN) +# define MAX_TCP_MSS __MAX_TCP_MSS(__IPv6_HDRLEN) +#endif + +#ifdef CONFIG_NET_IPv4 +# define TCP_IPv4_MSS(d) TCP_MSS(d,__IPv4_HDRLEN) +# define ETH_IPv4_TCP_MSS ETH_TCP_MSS(__IPv4_HDRLEN) +# define SLIP_IPv4_TCP_MSS SLIP_TCP_MSS(__IPv4_HDRLEN) +# define TUN_IPv4_TCP_MSS TUN_TCP_MSS(__IPv4_HDRLEN) +# define MIN_TCP_MSS __MIN_TCP_MSS(__IPv4_HDRLEN) +# undef MAX_TCP_MSS +# define MAX_TCP_MSS __MAX_TCP_MSS(__IPv4_HDRLEN) +#endif + +/* If IPv6 is supported, it will have the smaller MSS. */ + +#ifdef CONFIG_NET_IPv6 +# undef MIN_TCP_MSS +# define MIN_TCP_MSS __MIN_TCP_MSS(__IPv6_HDRLEN) +#endif + +/* How long a connection should stay in the TIME_WAIT state (in units of + * seconds). + * + * TIME_WAIT is often also known as the 2MSL wait state. This is because + * the socket that transitions to TIME_WAIT stays there for a period that + * is 2 x Maximum Segment Lifetime in duration. The MSL is the maximum + * amount of time that any segment can remain valid on the network before + * being discarded. This time limit is ultimately bounded by the TTL field + * in the IP datagram that is used to transmit the TCP segment. RFC 793 + * specifies MSL as 2 minutes but most systems permit this value to be tuned. + * Here a default TIME_WAIT (2MSL) 2 minutes is used, half the value + * specified by RFC 793. + */ + +#ifdef CONFIG_NET_TCP_WAIT_TIMEOUT +# define TCP_TIME_WAIT_TIMEOUT CONFIG_NET_TCP_WAIT_TIMEOUT +#else +# define TCP_TIME_WAIT_TIMEOUT (60*2) +#endif + +/* ARP configuration options */ + +#ifndef CONFIG_NET_ARPTAB_SIZE +/* The size of the ARP table. + * + * This option should be set to a larger value if this network node will + * have many connections from the local network. + */ + +# define CONFIG_NET_ARPTAB_SIZE 8 +#endif + +#ifndef CONFIG_NET_ARP_MAXAGE +/* The maximum age of ARP table entries measured in 10ths of seconds. + * + * An CONFIG_NET_ARP_MAXAGE of 120 corresponds to 20 minutes (BSD + * default). + */ + +# define CONFIG_NET_ARP_MAXAGE 120 +#endif + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +/* Statistics datatype + * + * This typedef defines the datatype used for keeping statistics in + * the network. + */ + +typedef uint16_t net_stats_t; + +#endif /* __INCLUDE_NUTTX_NET_NETCONFG_H */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/include/nuttx/net/tcp.h b/tasks/net-nuttx-tcp-retry-closure/template/include/nuttx/net/tcp.h new file mode 100644 index 00000000..5d921d6d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/include/nuttx/net/tcp.h @@ -0,0 +1,211 @@ +/**************************************************************************** + * include/nuttx/net/tcp.h + * + * SPDX-License-Identifier: BSD-3-Clause + * SPDX-FileCopyrightText: 2007, 2009-2010, 2012-2014 Gregory Nutt. + * SPDX-FileCopyrightText: 2001-2003, Adam Dunkels. + * SPDX-FileContributor: Gregory Nutt + * SPDX-FileContributor: Adam Dunkels + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_NET_TCP_H +#define __INCLUDE_NUTTX_NET_TCP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#ifdef CONFIG_NET_TCP + +#include + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* TCP definitions */ + +#define TCP_FIN 0x01 +#define TCP_SYN 0x02 +#define TCP_RST 0x04 +#define TCP_PSH 0x08 +#define TCP_ACK 0x10 +#define TCP_URG 0x20 +#define TCP_CTL 0x3f + +#define TCP_OPT_END 0 /* End of TCP options list */ +#define TCP_OPT_NOOP 1 /* "No-operation" TCP option */ +#define TCP_OPT_MSS 2 /* Maximum segment size TCP option */ +#define TCP_OPT_WS 3 /* Window size scaling factor */ +#define TCP_OPT_SACK_PERM 4 /* Selective-ACK Permitted option */ +#define TCP_OPT_SACK 5 /* Selective-ACK Block option */ + +#define TCP_OPT_NOOP_LEN 1 /* Length of TCP NOOP option. */ +#define TCP_OPT_MSS_LEN 4 /* Length of TCP MSS option. */ +#define TCP_OPT_WS_LEN 3 /* Length of TCP WS option. */ +#define TCP_OPT_SACK_PERM_LEN 2 /* Length of TCP SACK option. */ + +/* The TCP states used in the struct tcp_conn_s tcpstateflags field */ + +#define TCP_STATE_MASK 0x0f /* Bits 0-3: TCP state */ +# define TCP_CLOSED 0x00 /* The connection is not in use and available */ +# define TCP_ALLOCATED 0x01 /* The connection is allocated, but not yet initialized */ +# define TCP_SYN_RCVD 0x02 +# define TCP_SYN_SENT 0x03 +# define TCP_ESTABLISHED 0x04 +# define TCP_FIN_WAIT_1 0x05 +# define TCP_FIN_WAIT_2 0x06 +# define TCP_CLOSING 0x07 +# define TCP_TIME_WAIT 0x08 +# define TCP_LAST_ACK 0x09 +# define TCP_STOPPED 0x10 /* Bit 4: stopped */ + /* Bit 5-7: Unused, but not available */ + +/* TCP header sizes + * + * The minimum size header is 5 words and the maximum is 15 words thus + * giving the minimum size of 20 bytes and maximum of 60 bytes, allowing for + * up to 40 bytes of options in the header. + */ + +#define TCP_HDRLEN 20 /* Size of TCP header (minimum) */ +#define TCP_OPT_HDRLEN(n) (20 + ((n) << 2)) /* Size of TCP header w/options */ +#define TCP_MAX_HDRLEN 60 /* Maximum size of TCP header */ + +#ifdef CONFIG_NET_IPv4 +# define IPv4TCP_HDRLEN (TCP_HDRLEN + IPv4_HDRLEN) /* Size of IPv4 + TCP header */ +#endif + +#ifdef CONFIG_NET_IPv6 +# define IPv6TCP_HDRLEN (TCP_HDRLEN + IPv6_HDRLEN) /* Size of IPv4 + TCP header */ +#endif + +/* Initial minimum MSS according to RFC 879 + * + * The default TCP Maximum Segment Sizes (MSS) are defined below.is 536. If + * a host wishes to set the maximum segment size to a value other than the + * default, the maximum segment size is specified as a TCP option, initially + * in the TCP SYN packet during the TCP handshake. The value cannot be + * changed after the connection is established. + * + * These defaults correspond to the minimum MTU values: + * + * IPv4: MTU=576; MSS=536 (MTU - IPv4_HDRLEN - TCP_HDRLEN) + * IPv6: MTU=1280; MSS=1220 (MTU - IPv6_HDRLEN - TCP_HDRLEN) + */ + +#define TCP_DEFAULT_IPv4_MSS 536 +#define TCP_DEFAULT_IPv6_MSS 1220 + +/* Minimal accepted MSS. It is (60+60+8) - (20+20). + * (MAX_IP_HDR + MAX_TCP_HDR + MIN_IP_FRAG) - (MIN_IP_HDR + MIN_TCP_HDR) + */ + +#define TCP_MIN_MSS 88 + +/* However, we do need to make allowance for certain links such as SLIP that + * have unusually small MTUs. + */ + +#ifdef CONFIG_NET_IPv4 +# define TCP_IPv4_INITIAL_MSS(d) \ + (TCP_MSS(d,IPv4_HDRLEN) > 536 ? 536 : TCP_MSS(d,IPv4_HDRLEN)) + +# define MIN_IPv4_TCP_INITIAL_MSS \ + (__MIN_TCP_MSS(IPv4_HDRLEN) > 536 ? 536 : __MIN_TCP_MSS(IPv4_HDRLEN)) +# define MAX_IPv4_TCP_INITIAL_MSS \ + (__MAX_TCP_MSS(IPv4_HDRLEN) > 536 ? 536 : __MAX_TCP_MSS(IPv4_HDRLEN)) +#endif + +#ifdef CONFIG_NET_IPv6 +# define TCP_IPv6_INITIAL_MSS(d) \ + (TCP_MSS(d,IPv6_HDRLEN) > 1220 ? 1220 : TCP_MSS(d,IPv6_HDRLEN)) +# define MIN_IPv6_TCP_INITIAL_MSS \ + (__MIN_TCP_MSS(IPv6_HDRLEN) > 1220 ? 1220 : __MIN_TCP_MSS(IPv6_HDRLEN)) +# define MAX_IPv6_TCP_INITIAL_MSS \ + (__MAX_TCP_MSS(IPv6_HDRLEN) > 1220 ? 1220 : __MAX_TCP_MSS(IPv6_HDRLEN)) +#endif + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +/* TCP header */ + +struct tcp_hdr_s +{ + uint16_t srcport; + uint16_t destport; + uint8_t seqno[4]; + uint8_t ackno[4]; + uint8_t tcpoffset; + uint8_t flags; + uint8_t wnd[2]; + uint16_t tcpchksum; + uint8_t urgp[2]; + uint8_t optdata[0]; +}; + +/* The structure holding the TCP/IP statistics that are gathered if + * CONFIG_NET_STATISTICS is defined. + */ + +#ifdef CONFIG_NET_STATISTICS +struct tcp_stats_s +{ + net_stats_t drop; /* Number of dropped TCP segments */ + net_stats_t recv; /* Number of received TCP segments */ + net_stats_t sent; /* Number of sent TCP segments */ + net_stats_t chkerr; /* Number of TCP segments with a bad checksum */ + net_stats_t ackerr; /* Number of TCP segments with a bad ACK number */ + net_stats_t rst; /* Number of received TCP RST (reset) segments */ + net_stats_t rexmit; /* Number of retransmitted TCP segments */ + net_stats_t syndrop; /* Number of dropped SYNs due to too few + * available connections */ + net_stats_t synrst; /* Number of SYNs for closed ports triggering a RST */ +}; +#endif + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#endif /* CONFIG_NET_TCP */ +#endif /* __INCLUDE_NUTTX_NET_TCP_H */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/lab.c b/tasks/net-nuttx-tcp-retry-closure/template/lab.c new file mode 100644 index 00000000..92fb60b1 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/lab.c @@ -0,0 +1,131 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Deterministic raw I/O laboratory: no expected values or pass/fail decisions. + */ +#include "port.h" +static struct net_driver_s device; +static struct tcp_conn_s connection; +static struct iob_s buffer; +static long tick; +static int payload, appsend, listener_enabled; +struct endpoint { struct devif_callback_s *cb; unsigned events; int wakes; }; +static struct endpoint endpoints[2]; +struct packet { long tick; unsigned flags,length; uint32_t sequence; }; +static struct packet packets[256]; +static int packet_count; +void net_lock(void) {} +void net_unlock(void) {} +bool netdev_verify(struct net_driver_s *d) { return d == &device; } +long clock_systime_ticks(void) { return tick; } +bool work_available(struct work_s *w) { return !w->pending; } +long work_timeleft(struct work_s *w) { return w->due - tick; } +int work_queue(int q,struct work_s *w,void (*fn)(void *),void *arg,long delay) +{ w->pending=true; w->due=tick+delay; w->worker=fn; w->arg=arg; return 0; } +int work_cancel(int q,struct work_s *w) { w->pending=false; return 0; } +struct tcp_conn_s *tcp_nextconn(struct tcp_conn_s *c) { return c ? NULL : &connection; } +void netdev_txnotify_dev(struct net_driver_s *d) {} +void tcp_ip_select(struct tcp_conn_s *c) {} +uint16_t tcpip_hdrsize(struct tcp_conn_s *c) { return 40; } +bool tcp_should_send_recvwindow(struct tcp_conn_s *c) { return false; } +void tcp_send(struct net_driver_s *d,struct tcp_conn_s *c,uint8_t flags,uint16_t len) +{ + if(packet_count >= 256) exit(79); + packets[packet_count++]=(struct packet){tick,flags,len,tcp_getsequence(c->sndseq)}; + d->d_len=len; +} +void tcp_synack(struct net_driver_s *d,struct tcp_conn_s *c,uint8_t flags) +{ tcp_send(d,c,flags,tcpip_hdrsize(c)); } +struct tcp_conn_s *tcp_findlistener(void *binding,uint16_t port) +{ return listener_enabled ? &connection : NULL; } +void tcp_free(struct tcp_conn_s *c) {} +int netdev_iob_prepare(struct net_driver_s *d,bool throttled,unsigned int reserve) +{ d->d_iob=&buffer; d->d_appdata=buffer.io_data; return 0; } +void netdev_iob_clear(struct net_driver_s *d) { d->d_iob=NULL; } +struct iob_s *iob_trimhead(struct iob_s *b,unsigned int n) +{ b->io_pktlen-=n; b->io_offset+=n; return b; } +struct iob_s *iob_trimtail(struct iob_s *b,unsigned int n) +{ b->io_pktlen-=n; return b; } +void net_iob_concat(struct iob_s **to,struct iob_s **from) +{ *to=*from; *from=NULL; } +void net_incr32(uint8_t *s,uint16_t n) { tcp_setsequence(s,tcp_getsequence(s)+n); } +void tcp_callback_free(struct tcp_conn_s *c,struct devif_callback_s *cb) +{ devif_conn_callback_free(c->dev,cb,&c->sconn.list,&c->sconn.tail); } +int nxsem_destroy(void *s) { return 0; } +/* An application's waiting socket endpoint. Loss handling stays in tcp_monitor.c. */ +static uint16_t socket_event(struct net_driver_s *d,void *arg,uint16_t flags) +{ + struct endpoint *e=arg; + e->events|=flags; + if(flags & TCP_DISCONN_EVENTS) + { e->wakes++; tcp_lost_connection(&connection,e->cb,flags); } + else if(flags & TCP_REXMIT) d->d_sndlen=payload; + else if(flags & TCP_POLL) d->d_sndlen=appsend; + return flags; +} +static void snapshot(void) +{ + struct tcp_conn_s *c=&connection; + printf("{\"tick\":%ld,\"state\":%u,\"rto\":%u,\"timer\":%u,\"nrtx\":%u,\"keeptimer\":%u,\"keepretries\":%u,\"sflags\":%u,\"pending\":%d,\"due\":%ld,\"sequence\":%u,\"unacked\":%d,\"events\":[%u,%u],\"wakes\":[%d,%d],\"packets\":[", + tick,c->tcpstateflags,c->rto,c->timer,c->nrtx,c->keeptimer,c->keepretries, + c->sconn.s_flags,c->work.pending,c->work.due,tcp_getsequence(c->sndseq), + c->tx_unacked,endpoints[0].events,endpoints[1].events,endpoints[0].wakes,endpoints[1].wakes); + for(int i=0;i2) return 4; + for(unsigned i=0;ipriv=&endpoints[i]; cb->event=socket_event; + cb->flags=TCP_POLL|TCP_REXMIT|TCP_DISCONN_EVENTS; + } + netdev_iob_prepare(&device,false,0); + tcp_update_timer(&connection); + snapshot(); + char command; long arg; + while(scanf(" %c %ld",&command,&arg)==2) + { + if(command=='A') + { + long target=tick+arg; + int iterations=0; + while(connection.work.pending && connection.work.due<=target) + { + if(++iterations>1024) return 6; + tick=connection.work.due; + void (*worker)(void *)=connection.work.worker; + void *worker_arg=connection.work.arg; + connection.work.pending=false; + worker(worker_arg); + tcp_poll(&device,&connection); + } + tick=target; + } + else if(command=='P') tcp_poll(&device,&connection); + else if(command=='Z') tcp_set_zero_probe(&connection,(uint16_t)arg); + else if(command=='S') tcp_stop_timer(&connection); + else return 7; + snapshot(); + } + return 0; +} diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/devif/devif.h b/tasks/net-nuttx-tcp-retry-closure/template/net/devif/devif.h new file mode 100644 index 00000000..5b3bdaea --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/devif/devif.h @@ -0,0 +1,633 @@ +/**************************************************************************** + * net/devif/devif.h + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2009, 2013-2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * This logic was leveraged from uIP which also has a BSD-style license: + * + * Author Adam Dunkels + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __NET_DEVIF_DEVIF_H +#define __NET_DEVIF_DEVIF_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#ifdef CONFIG_NET + +#include +#include +#include +#include + +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The following flags may be set in the set of flags by the lower, device- + * interfacing layer before calling through the socket layer callback. The + * TCP_ACKDATA, XYZ_NEWDATA, and TCP_CLOSE flags may be set at the same time, + * whereas the others are mutually exclusive. + * + * Connection Specific Events: These are events that may be notified + * through callback lists residing in TCP, UDP, or PKT port connection + * structures: + * + * TCP_ACKDATA IN: Signifies that the outstanding data was ACKed and + * the socket layer should send out new data instead + * of retransmitting the last data. (TCP only) + * OUT: Input state must be preserved on output. + * + * TCP_NEWDATA IN: Set to indicate that the peer has sent us new data. + * UDP_NEWDATA OUT: Cleared (only) by the socket layer logic to + * ICMP_NEWDATA indicate that the new data was consumed, + * ICMPv6_NEWDATA suppressing further attempts to process the new + * PKT_NEWDATA data. + * BLUETOOTH_NEWDATA + * IEEE802154_NEWDATA + * + * TCP_SNDACK IN: Not used; always zero + * OUT: Set by the socket layer if the new data was + * consumed and an ACK should be sent in the response. + * (TCP only) + * + * TCP_REXMIT IN: Tells the socket layer to retransmit the data that + * was last sent. (TCP only) + * OUT: Not used + * + * TCP_POLL IN: Used for polling the socket layer. This is + * UDP_POLL provided periodically from the drivers to support + * PKT_POLL (1) timed operations, and (2) to check if the + * BLUETOOTH_POLL socket layer has data that it wants to send. + * IEEE802154_POLL These are socket oriented callbacks where the + * context depends on the specific set. + * OUT: Not used + * + * TCP_BACKLOG IN: There is a new connection in the backlog list set + * up by the listen() command. (TCP only) + * OUT: Not used + * + * TCP_CLOSE IN: The remote host has closed the connection, thus the + * connection has gone away. (TCP only) + * OUT: The socket layer signals that it wants to close the + * connection. (TCP only) + * + * TCP_ABORT IN: The remote host has aborted the connection, thus + * the connection has gone away. (TCP only) + * OUT: The socket layer signals that it wants to abort the + * connection. (TCP only) + * + * TCP_CONNECTED IN: We have got a connection from a remote host and + * have set up a new connection for it, or an active + * connection has been successfully established. + * (TCP only) + * OUT: Not used + * + * TCP_TIMEDOUT IN: The connection has been aborted due to too many + * retransmissions. (TCP only) + * OUT: Not used + * + * Device Specific Events: These are events that may be notified through + * callback lists residing in the network device structure. + * + * ARP_POLL IN: Used for polling the socket layer. This is + * provided periodically from the drivers to support + * (1) timed operations, and (2) to check if the ARP + * layer needs to send an ARP request. This is a + * device oriented event, not associated with a + * socket. + * OUT: Not used + * + * ICMP_POLL IN: Used for polling the socket layer. This is + * provided periodically from the drivers to support + * (1) timed operations, and (2) to check if the ICMP + * layer needs to send an ARP request. This is a + * device oriented event, not associated with a + * socket. This differs from ICMPv6_POLL only in that + * the appdata pointer is set differently. + * OUT: Not used + * + * ICMPv6_POLL IN: Used for polling the socket layer. This is + * provided periodically from the drivers to support + * (1) timed operations, and (2) to check if the ICMP + * layer needs to send an ARP request. This is a + * device oriented event, not associated with a + * socket. This differs from ICMP_POLL only in that + * the appdata pointer is set differently. + * OUT: Not used + * + * IPFWD_POLL IN: Used for polling for forwarded packets layer. This + * is provided periodically from the drivers to + * support to check if there is a packet waiting to be + * forward on the device. This is a device oriented + * event, not associated with a socket. The appdata + * pointer is not used in this case. + * OUT: Not used + * + * NETDEV_DOWN: IN: The network device has been taken down. + * OUT: Not used + */ + +/* Bits 0-9: Connection specific event bits */ + +#define TCP_ACKDATA (1 << 0) +#define TCP_NEWDATA (1 << 1) +#define UDP_NEWDATA TCP_NEWDATA +#define ICMP_NEWDATA TCP_NEWDATA +#define ICMPv6_NEWDATA TCP_NEWDATA +#define BLUETOOTH_NEWDATA TCP_NEWDATA +#define IEEE802154_NEWDATA TCP_NEWDATA +#define PKT_NEWDATA TCP_NEWDATA +#define CAN_NEWDATA TCP_NEWDATA +#define WPAN_NEWDATA TCP_NEWDATA +#define IPFWD_NEWDATA TCP_NEWDATA +#define TCP_SNDACK (1 << 2) +#define TCP_REXMIT (1 << 3) +#define TCP_POLL (1 << 4) +#define UDP_POLL TCP_POLL +#define PKT_POLL TCP_POLL +#define CAN_POLL TCP_POLL +#define BLUETOOTH_POLL TCP_POLL +#define IEEE802154_POLL TCP_POLL +#define WPAN_POLL TCP_POLL +#define TCP_BACKLOG (1 << 5) +#define TCP_CLOSE (1 << 6) +#define TCP_ABORT (1 << 7) +#define TCP_CONNECTED (1 << 8) +#define TCP_TIMEDOUT (1 << 9) +#define TCP_WAITALL (1 << 10) + +/* Bits 10-11: Unused, available */ + +/* Bit 12: Device specific event bits */ + +#define NETDEV_DOWN (1 << 12) + +/* Bits 13-15: Encoded device specific poll events. Unlike connection + * oriented poll events, device related poll events must distinguish + * between what is being polled for since the callbacks all reside in + * the same list in the network device structure. + */ + +#define DEVPOLL_SHIFT (13) +#define DEVPOLL_MASK (7 << DEVPOLL_SHIFT) +# define DEVPOLL_NONE (0 << DEVPOLL_SHIFT) +# define ARP_POLL (1 << DEVPOLL_SHIFT) +# define ICMP_POLL (2 << DEVPOLL_SHIFT) +# define ICMPv6_POLL (3 << DEVPOLL_SHIFT) +# define IPFWD_POLL (4 << DEVPOLL_SHIFT) + +/* The set of events that and implications to the TCP connection state */ + +#define TCP_CONN_EVENTS \ + (TCP_CLOSE | TCP_ABORT | TCP_CONNECTED | TCP_TIMEDOUT | NETDEV_DOWN) + +#define TCP_DISCONN_EVENTS \ + (TCP_CLOSE | TCP_ABORT | TCP_TIMEDOUT | NETDEV_DOWN) + +/* IPv4/IPv6 Helpers */ + +#ifdef CONFIG_NET_IPv4 +# define DEVIF_IS_IPv4(dev) IFF_IS_IPv4(dev->d_flags) +#else +# define DEVIF_IS_IPv4(dev) (0) +#endif + +#ifdef CONFIG_NET_IPv6 +# define DEVIF_IS_IPv6(dev) IFF_IS_IPv6(dev->d_flags) +#else +# define DEVIF_IS_IPv6(dev) (0) +#endif + +/* There are some helper pointers for accessing the contents of the Ethernet + * headers + */ + +#define ETHBUF ((FAR struct eth_hdr_s *)NETLLBUF) + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +/* Describes a connection/device event callback interface + * + * nxtconn - Supports a singly linked list that supports connection + * specific event handlers. + * nxtdev - Supports a singly linked list that supports device specific + * event handlers + * event - Provides the address of the callback function entry point. + * pvconn is a pointer to a connection-specific datat structure + * such as struct tcp_conn_s or struct udp_conn_s. + * priv - Holds a reference to socket layer specific data that will + * provided + * flags - Set by the socket layer to inform the lower layer which flags + * are and are not handled by the callback. + */ + +struct net_driver_s; /* Forward reference */ + +typedef CODE uint16_t (*devif_callback_event_t)(FAR struct net_driver_s *dev, + FAR void *pvpriv, + uint16_t flags); + +struct devif_callback_s +{ + FAR struct devif_callback_s *nxtconn; + FAR struct devif_callback_s *prevconn; + FAR struct devif_callback_s *nxtdev; + FAR devif_callback_event_t event; + FAR void *priv; + uint16_t flags; + uint8_t free_flags; +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: devif_initialize + * + * Description: + * Perform initialization of the network device interface layer + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +void devif_initialize(void); + +/**************************************************************************** + * Name: devif_callback_init + * + * Description: + * Configure the pre-allocated callback structures into a free list. + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +void devif_callback_init(void); + +/**************************************************************************** + * Name: devif_callback_alloc + * + * Description: + * Allocate a callback container from the free list. + * + * If dev is non-NULL, then this function verifies that the device + * reference is still valid and that the device is still UP status. If + * those conditions are not true, this function will fail to allocate the + * callback. + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +FAR struct devif_callback_s * + devif_callback_alloc(FAR struct net_driver_s *dev, + FAR struct devif_callback_s **list_head, + FAR struct devif_callback_s **list_tail); + +/**************************************************************************** + * Name: devif_conn_callback_free + * + * Description: + * Return a connection/port callback container to the free list. + * + * This function is just a front-end for devif_callback_free(). If the + * dev argument is non-NULL, it will verify that the device reference is + * still valid before attempting to free the callback structure. A + * non-NULL list pointer is assumed to be valid in any case. + * + * The callback structure will be freed in any event. + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +void devif_conn_callback_free(FAR struct net_driver_s *dev, + FAR struct devif_callback_s *cb, + FAR struct devif_callback_s **list_head, + FAR struct devif_callback_s **list_tail); + +/**************************************************************************** + * Name: devif_dev_callback_free + * + * Description: + * Return a device callback container to the free list. + * + * This function is just a front-end for devif_callback_free(). If the + * de argument is non-NULL, it will verify that the device reference is + * still valid before attempting to free the callback structure. It + * differs from devif_conn_callback_free in that connection/port-related + * connections are also associated with the device and, hence, also will + * not be valid if the device pointer is not valid. + * + * The callback structure will be freed in any event. + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +void devif_dev_callback_free(FAR struct net_driver_s *dev, + FAR struct devif_callback_s *cb); + +/**************************************************************************** + * Name: devif_conn_event + * + * Description: + * Execute a list of callbacks. + * + * Input Parameters: + * dev - The network device state structure associated with the network + * device that initiated the callback event. + * flags - The bit set of events to be notified. + * list - The list to traverse in performing the notifications + * + * Returned Value: + * The updated flags as modified by the callback functions. + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +uint16_t devif_conn_event(FAR struct net_driver_s *dev, uint16_t flags, + FAR struct devif_callback_s *list); + +/**************************************************************************** + * Name: devif_dev_event + * + * Description: + * Execute a list of callbacks using the device event chain. + * + * Input Parameters: + * dev - The network device state structure associated with the network + * device that initiated the callback event. + * flags - The bit set of events to be notified. + * + * Returned Value: + * The updated flags as modified by the callback functions. + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +uint16_t devif_dev_event(FAR struct net_driver_s *dev, uint16_t flags); + +/**************************************************************************** + * Name: devif_send + * + * Description: + * Send data on the current connection. + * + * This function is used to send out a single segment of TCP data. Only + * socket logic that have been invoked by the lower level for event + * processing can send data. + * + * The amount of data that actually is sent out after a call to this + * function is determined by the maximum amount of data TCP allows. The + * network will automatically crop the data so that only the appropriate + * amount of data is sent. The mss field of the TCP connection structure + * can be used to determine the amount of data that actually will be sent. + * + * Note: This function does not guarantee that the sent data will + * arrive at the destination. If the data is lost in the network, the + * TCP socket layer will be invoked with the TCP_REXMIT flag set. The + * socket layer will then have to resend the data using this function. + * + * Input Parameters: + * dev - The network device state structure associated with the network + * device that initiated the callback event. + * buf - A pointer to the data which is to be sent. + * len - The maximum amount of data bytes to be sent. + * offset - Offset of data in buffer. + * + * Returned Value: + * The amount of data sent, or negated ERRNO in case of failure. + * + ****************************************************************************/ + +int devif_send(FAR struct net_driver_s *dev, FAR const void *buf, + int len, int offset); + +/**************************************************************************** + * Name: devif_iob_send + * + * Description: + * Called from socket logic in response to a xmit or poll request from the + * the network interface driver. + * + * This is identical to calling devif_send() except that the data is + * in an I/O buffer chain, rather than a flat buffer. + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +#ifdef CONFIG_MM_IOB +struct iob_s; +int devif_iob_send(FAR struct net_driver_s *dev, FAR struct iob_s *buf, + unsigned int len, unsigned int offset, + unsigned int target_offset); +#endif + +/**************************************************************************** + * Name: devif_file_send + * + * Description: + * Called from socket logic in response to a xmit or poll request from the + * the network interface driver. + * + * This is identical to calling devif_file_send() except that the data is + * in a available file handle. + * + * Assumptions: + * Called with the network locked. + * + ****************************************************************************/ + +#ifdef CONFIG_MM_IOB +int devif_file_send(FAR struct net_driver_s *dev, FAR struct file *file, + unsigned int len, unsigned int offset, + unsigned int target_offset); +#endif + +/**************************************************************************** + * Name: devif_out + * + * Description: + * Common interface to build L2 headers + * + * Assumptions: + * This function is called from the MAC device driver with the network + * locked. + * + ****************************************************************************/ + +void devif_out(FAR struct net_driver_s *dev); + +/**************************************************************************** + * Name: devif_poll_out + * + * Description: + * Generic callback before device output to build L2 headers before sending + * + * Assumptions: + * This function is called from the MAC device driver with the network + * locked. + * + ****************************************************************************/ + +int devif_poll_out(FAR struct net_driver_s *dev, + devif_poll_callback_t callback); + +/**************************************************************************** + * Name: devif_is_loopback + * + * Description: + * The function checks the destination address of the packet to see + * whether the target of packet is ourself. + * + * Returned Value: + * true is returned if the packet need loop back to ourself, otherwise + * false is returned. + * + ****************************************************************************/ + +bool devif_is_loopback(FAR struct net_driver_s *dev); + +/**************************************************************************** + * Name: devif_loopback + * + * Description: + * This function should be called before sending out a packet. The function + * checks the destination address of the packet to see whether the target + * of packet is ourself and then consume the packet directly by calling + * input process functions. + * + * Returned Value: + * Zero is returned if the packet don't loop back to ourself, otherwise + * a non-zero value is returned. + * + ****************************************************************************/ + +int devif_loopback(FAR struct net_driver_s *dev); + +/**************************************************************************** + * Name: netdev_input + * + * Description: + * This function will copy the flat buffer that does not support + * Scatter/gather to the iob vector buffer. + * + * Compatible with all old flat buffer NICs: + * + * [tcp|udp|icmp|...]ipv[4|6]_data_handler() + * | (iob_concat/append to readahead) + * | + * pkt/ipv[4/6]_in()/... + * | + * | + * netdev_input() // new interface, Scatter/gather flat/iobs + * | + * | + * pkt/ipv[4|6]_input()/... + * | + * | + * NICs io vector receive(Original flat buffer) + * + * Input Parameters: + * callback - Input callback of L3 stack + * + * Returned Value: + * A non-zero copy is returned on success. + * + ****************************************************************************/ + +int netdev_input(FAR struct net_driver_s *dev, + devif_poll_callback_t callback, bool reply); + +/**************************************************************************** + * Name: devif_get_mtu + * + * Description: + * Get mtu + * + * Parameters: + * dev Ethernet driver device structure + * + * Return: + * return (Maximum packet size - Link layer header size), + * if PMTUD enable return pmtu + ****************************************************************************/ + +uint16_t devif_get_mtu(FAR struct net_driver_s *dev); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_NET */ +#endif /* __NET_DEVIF_DEVIF_H */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/devif/devif_callback.c b/tasks/net-nuttx-tcp-retry-closure/template/net/devif/devif_callback.c new file mode 100644 index 00000000..ecd82836 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/devif/devif_callback.c @@ -0,0 +1,624 @@ +/**************************************************************************** + * net/devif/devif_callback.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define DEVIF_CB_DONT_FREE (1 << 0) +#define DEVIF_CB_PEND_FREE (1 << 1) + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#if CONFIG_NET_PREALLOC_DEVIF_CALLBACKS > 0 +static struct devif_callback_s + g_cbprealloc[CONFIG_NET_PREALLOC_DEVIF_CALLBACKS]; +#endif +static FAR struct devif_callback_s *g_cbfreelist = NULL; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: devif_callback_free + * + * Description: + * Return a callback container to the free list. + * + * Assumptions: + * This function is called with the network locked. + * + ****************************************************************************/ + +static void devif_callback_free(FAR struct net_driver_s *dev, + FAR struct devif_callback_s *cb, + FAR struct devif_callback_s **list_head, + FAR struct devif_callback_s **list_tail) +{ + FAR struct devif_callback_s *prev; + FAR struct devif_callback_s *curr; + + if (cb) + { + net_lock(); + +#ifdef CONFIG_DEBUG_FEATURES + /* Check for double freed callbacks */ + + curr = g_cbfreelist; + + while (curr != NULL) + { + DEBUGASSERT(cb != curr); + curr = curr->nxtconn; + } +#endif + + /* Remove the callback structure from the data notification list if + * it is supposed to be in the data notification list. + */ + + if (list_head) + { + prev = cb->prevconn; + + /* Remove the structure from the connection event list */ + + if (prev) + { + /* The item to be removed is not in the head. */ + + prev->nxtconn = cb->nxtconn; + + if (cb->nxtconn) + { + /* The item to be removed is not in the tail. */ + + cb->nxtconn->prevconn = prev; + } + } + else + { + /* The item to be removed is in the head. */ + + *list_head = cb->nxtconn; + + if (cb->nxtconn) + { + /* There are more items besides the head item. */ + + cb->nxtconn->prevconn = NULL; + } + } + + if (!cb->nxtconn) + { + /* If the tail item is being removed, + * update the tail pointer. + */ + + DEBUGASSERT(list_tail); + *list_tail = prev; + } + } + + /* check if the callback structure has DEVIF_CB_DONT_FREE,it indicates + * the callback can't be free immediately,setting DEVIF_CB_PEND_FREE + * flag with the callback,it indicates the callback will be free + * finally + */ + + if (cb->free_flags & DEVIF_CB_DONT_FREE) + { + cb->free_flags |= DEVIF_CB_PEND_FREE; + net_unlock(); + return; + } + + /* Remove the callback structure from the device notification list if + * it is supposed to be in the device notification list. + */ + + if (dev != NULL) + { + /* Find the callback structure in the device event list */ + + for (prev = NULL, curr = dev->d_devcb; + curr != NULL && curr != cb; + prev = curr, curr = curr->nxtdev) + { + } + + /* Remove the structure from the device event list */ + + if (curr != NULL) + { + if (prev) + { + prev->nxtdev = cb->nxtdev; + } + else + { + dev->d_devcb = cb->nxtdev; + } + } + } + + /* If this is a preallocated or a batch allocated callback store it in + * the free callbacks list. Else free it. + */ + +#if CONFIG_NET_ALLOC_DEVIF_CALLBACKS == 1 + if (cb < g_cbprealloc || cb >= (g_cbprealloc + + CONFIG_NET_PREALLOC_DEVIF_CALLBACKS)) + { + kmm_free(cb); + } + else +#endif + { + cb->nxtconn = g_cbfreelist; + cb->nxtdev = NULL; + g_cbfreelist = cb; + } + + net_unlock(); + } +} + +/**************************************************************************** + * Name: devif_event_trigger + * + * Description: + * Return true if the current set of events should trigger a callback to + * occur. + * + * Input Parameters: + * events - The set of events that has occurred. + * triggers - The set of events that will trigger a callback. + * + ****************************************************************************/ + +static bool devif_event_trigger(uint16_t events, uint16_t triggers) +{ + /* The events are divided into a set of individual bits that may be ORed + * together PLUS a field that encodes a single poll event. + * + * First check if any of the individual event bits will trigger the + * callback. + */ + + if ((events & triggers & ~DEVPOLL_MASK) != 0) + { + return true; + } + + /* No... check the encoded device event. */ + + if ((events & DEVPOLL_MASK) == (triggers & DEVPOLL_MASK)) + { + return (triggers & DEVPOLL_MASK) != 0; + } + + /* No.. this event set will not generate the callback */ + + return false; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: devif_callback_init + * + * Description: + * Configure the pre-allocated callback structures into a free list. + * + * Assumptions: + * Called early in the initialization sequence so that no special + * protection is required. + * + ****************************************************************************/ + +void devif_callback_init(void) +{ +#if CONFIG_NET_PREALLOC_DEVIF_CALLBACKS > 0 + int i; + + for (i = 0; i < CONFIG_NET_PREALLOC_DEVIF_CALLBACKS; i++) + { + g_cbprealloc[i].nxtconn = g_cbfreelist; + g_cbfreelist = &g_cbprealloc[i]; + } +#endif +} + +/**************************************************************************** + * Name: devif_callback_alloc + * + * Description: + * Allocate a callback container from the free list. + * + * If dev is non-NULL, then this function verifies that the device + * reference is still valid and that the device is still UP status. If + * those conditions are not true, this function will fail to allocate the + * callback. + * + * Assumptions: + * This function is called with the network locked. + * + ****************************************************************************/ + +FAR struct devif_callback_s * + devif_callback_alloc(FAR struct net_driver_s *dev, + FAR struct devif_callback_s **list_head, + FAR struct devif_callback_s **list_tail) +{ + FAR struct devif_callback_s *ret; +#if CONFIG_NET_ALLOC_DEVIF_CALLBACKS > 0 + int i; +#endif + + net_lock(); + + /* Verify that the device pointer is valid, i.e., that it still + * points to a registered network device and also that the network + * device in the UP state. + */ + + /* Note: dev->d_flags may be asynchronously changed by netdev_ifdown() + * (in net/netdev/netdev_ioctl.c). Nevertheless, net_lock() / net_unlock() + * are not required in netdev_ifdown() to prevent dev->d_flags from + * asynchronous change here. There is not an issue because net_lock() and + * net_unlock() present inside of devif_dev_event(). That should be enough + * to de-allocate connection callbacks reliably on NETDEV_DOWN event. + */ + + if (dev && !(netdev_verify(dev) && (dev->d_flags & IFF_UP) != 0)) + { + net_unlock(); + return NULL; + } + + /* Allocate the callback entry from heap */ + +#if CONFIG_NET_ALLOC_DEVIF_CALLBACKS > 0 + if (g_cbfreelist == NULL) + { + ret = kmm_zalloc(sizeof(struct devif_callback_s) * + CONFIG_NET_ALLOC_DEVIF_CALLBACKS); + if (ret != NULL) + { + for (i = 0; i < CONFIG_NET_ALLOC_DEVIF_CALLBACKS; i++) + { + ret[i].nxtconn = g_cbfreelist; + g_cbfreelist = &ret[i]; + } + } + } +#endif + + /* Check the head of the free list */ + + ret = g_cbfreelist; + if (ret) + { + /* Remove the next instance from the head of the free list */ + + g_cbfreelist = ret->nxtconn; + memset(ret, 0, sizeof(struct devif_callback_s)); + + /* Add the newly allocated instance to the head of the device event + * list. + */ + + if (dev) + { + ret->nxtdev = dev->d_devcb; + dev->d_devcb = ret; + } + + /* Add the newly allocated instance to the tail of the specified list */ + + if (list_head && list_tail) + { + ret->nxtconn = NULL; + ret->prevconn = *list_tail; + + if (*list_tail) + { + /* If the list is not empty, add the item to the tail. */ + + (*list_tail)->nxtconn = ret; + } + else + { + /* If the list is empty, add the first item to the list. */ + + *list_head = ret; + } + + /* Update the tail pointer */ + + *list_tail = ret; + } + } +#ifdef CONFIG_DEBUG_FEATURES + else + { + nerr("ERROR: Failed to allocate callback\n"); + } +#endif + + net_unlock(); + return ret; +} + +/**************************************************************************** + * Name: devif_conn_callback_free + * + * Description: + * Return a connection/port callback container to the free list. + * + * This function is just a front-end for devif_callback_free(). If the + * dev argument is non-NULL, it will verify that the device reference is + * still valid before attempting to free the callback structure. A + * non-NULL list pointer is assumed to be valid in any case. + * + * The callback structure will be freed in any event. + * + * Assumptions: + * This function is called with the network locked. + * + ****************************************************************************/ + +void devif_conn_callback_free(FAR struct net_driver_s *dev, + FAR struct devif_callback_s *cb, + FAR struct devif_callback_s **list_head, + FAR struct devif_callback_s **list_tail) +{ + /* Check if the device pointer is still valid. It could be invalid if, for + * example, the device were unregistered between the time when the callback + * was allocated and the time when the callback was freed. + */ + + if (dev && !netdev_verify(dev)) + { + /* The device reference is longer valid */ + + dev = NULL; + } + + /* Then free the callback */ + + devif_callback_free(dev, cb, list_head, list_tail); +} + +/**************************************************************************** + * Name: devif_dev_callback_free + * + * Description: + * Return a device callback container to the free list. + * + * This function is just a front-end for devif_callback_free(). If the + * de argument is non-NULL, it will verify that the device reference is + * still valid before attempting to free the callback structure. It + * differs from devif_conn_callback_free in that connection/port-related + * connections are also associated with the device and, hence, also will + * not be valid if the device pointer is not valid. + * + * The callback structure will be freed in any event. + * + * Assumptions: + * This function is called with the network locked. + * + ****************************************************************************/ + +void devif_dev_callback_free(FAR struct net_driver_s *dev, + FAR struct devif_callback_s *cb) +{ + FAR struct devif_callback_s **list_head; + FAR struct devif_callback_s **list_tail; + + /* Check if the device pointer is still valid. It could be invalid if, for + * example, the device were unregistered between the time when the callback + * was allocated and the time when the callback was freed. + */ + + if (dev != NULL && netdev_verify(dev)) + { + /* The device reference is valid. Then use the list pointer in the + * device structure as well. + */ + + list_head = &dev->d_conncb; + list_tail = &dev->d_conncb_tail; + } + else + { + /* The device reference is longer valid */ + + dev = NULL; + list_head = NULL; + list_tail = NULL; + } + + /* Then free the callback */ + + devif_callback_free(dev, cb, list_head, list_tail); +} + +/**************************************************************************** + * Name: devif_conn_event + * + * Description: + * Execute a list of callbacks using the packet event chain. + * + * Input Parameters: + * dev - The network device state structure associated with the network + * device that initiated the callback event. + * flags - The bit set of events to be notified. + * list - The list to traverse in performing the notifications + * + * Returned Value: + * The updated flags as modified by the callback functions. + * + * Assumptions: + * This function is called with the network locked. + * + ****************************************************************************/ + +uint16_t devif_conn_event(FAR struct net_driver_s *dev, uint16_t flags, + FAR struct devif_callback_s *list) +{ + FAR struct devif_callback_s *next; + + /* Loop for each callback in the list and while there are still events + * set in the flags set. + */ + + net_lock(); + while (list && flags) + { + /* Save the pointer to the next callback in the lists. This is done + * because the callback action might delete the entry pointed to by + * list. + */ + + next = list->nxtconn; + + /* Check if this callback handles any of the events in the flag set */ + + if (list->event != NULL && devif_event_trigger(flags, list->flags)) + { + /* Yes.. perform the callback. Actions perform by the callback + * may delete the current list entry or add a new list entry to + * beginning of the list (which will be ignored on this pass) + */ + + flags = list->event(dev, list->priv, flags); + } + + /* Set up for the next time through the loop */ + + list = next; + } + + net_unlock(); + return flags; +} + +/**************************************************************************** + * Name: devif_dev_event + * + * Description: + * Execute a list of callbacks using the device event chain. + * + * Input Parameters: + * dev - The network device state structure associated with the network + * device that initiated the callback event. + * flags - The bit set of events to be notified. + * + * Returned Value: + * The updated flags as modified by the callback functions. + * + * Assumptions: + * This function is called with the network locked. + * + ****************************************************************************/ + +uint16_t devif_dev_event(FAR struct net_driver_s *dev, uint16_t flags) +{ + FAR struct devif_callback_s *cb; + FAR struct devif_callback_s *next; + + /* Loop for each callback in the list and while there are still events + * set in the flags set. + */ + + net_lock(); + for (cb = dev->d_devcb; cb != NULL && flags != 0; cb = next) + { + /* Save the pointer to the next callback in the lists. This is done + * because the callback action might delete the entry pointed to by + * list. + */ + + next = cb->nxtdev; + + /* Check if this callback handles any of the events in the flag set */ + + if (cb->event != NULL && devif_event_trigger(flags, cb->flags)) + { + cb->free_flags |= DEVIF_CB_DONT_FREE; + + /* Yes.. perform the callback. Actions perform by the callback + * may delete the current list entry or add a new list entry to + * beginning of the list (which will be ignored on this pass) + */ + + flags = cb->event(dev, cb->priv, flags); + cb->free_flags &= ~DEVIF_CB_DONT_FREE; + + /* update the next callback to prevent previously recorded the + * next callback from being deleted + */ + + next = cb->nxtdev; + if ((cb->free_flags & DEVIF_CB_PEND_FREE) != 0) + { + cb->free_flags &= ~DEVIF_CB_PEND_FREE; + devif_callback_free(dev, cb, NULL, NULL); + } + } + } + + net_unlock(); + return flags; +} + +#endif /* CONFIG_NET */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp.h b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp.h new file mode 100644 index 00000000..14dcdeee --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp.h @@ -0,0 +1,2354 @@ +/**************************************************************************** + * net/tcp/tcp.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __NET_TCP_TCP_H +#define __NET_TCP_TCP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef NET_TCP_HAVE_STACK + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* This is a helper pointer for accessing the contents of the tcp header */ + +#define TCPIPv4BUF ((FAR struct tcp_hdr_s *)IPBUF(IPv4_HDRLEN)) +#define TCPIPv6BUF ((FAR struct tcp_hdr_s *)IPBUF(IPv6_HDRLEN)) + +/* Allocate a new TCP data callback */ + +/* These macros allocate and free callback structures used for receiving + * notifications of TCP data-related events. + */ + +#define tcp_callback_alloc(conn) \ + devif_callback_alloc((conn)->dev, &(conn)->sconn.list, &(conn)->sconn.list_tail) +#define tcp_callback_free(conn,cb) \ + devif_conn_callback_free((conn)->dev, (cb), &(conn)->sconn.list, &(conn)->sconn.list_tail) + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +/* TCP write buffer access macros */ + +# define TCP_WBSEQNO(wrb) ((wrb)->wb_seqno) +# define TCP_WBPKTLEN(wrb) ((wrb)->wb_iob->io_pktlen) +# define TCP_WBSENT(wrb) ((wrb)->wb_sent) +# define TCP_WBNRTX(wrb) ((wrb)->wb_nrtx) +#if defined(CONFIG_NET_TCP_FAST_RETRANSMIT) && !defined(CONFIG_NET_TCP_CC_NEWRENO) +# define TCP_WBNACK(wrb) ((wrb)->wb_nack) +#endif +# define TCP_WBIOB(wrb) ((wrb)->wb_iob) +# define TCP_WBCOPYOUT(wrb,dest,n) (iob_copyout(dest,(wrb)->wb_iob,(n),0)) +# define TCP_WBCOPYIN(wrb,src,n,off) \ + (iob_copyin((wrb)->wb_iob,src,(n),(off),true)) +# define TCP_WBTRYCOPYIN(wrb,src,n,off) \ + (iob_trycopyin((wrb)->wb_iob,src,(n),(off),true)) + +# define TCP_WBTRIM(wrb,n) \ + do { (wrb)->wb_iob = iob_trimhead((wrb)->wb_iob,(n)); } while (0) + +#ifdef CONFIG_DEBUG_FEATURES +# define TCP_WBDUMP(msg,wrb,len,offset) \ + tcp_wrbuffer_dump(msg,wrb,len,offset) +# else +# define TCP_WBDUMP(msg,wrb,len,offset) +# endif +#endif + +/* 32-bit modular arithmetics for tcp sequence numbers */ + +#define TCP_SEQ_LT(a, b) ((int32_t)((a) - (b)) < 0) +#define TCP_SEQ_GT(a, b) TCP_SEQ_LT(b, a) +#define TCP_SEQ_LTE(a, b) (!TCP_SEQ_GT(a, b)) +#define TCP_SEQ_GTE(a, b) (!TCP_SEQ_LT(a, b)) + +#define TCP_SEQ_ADD(a, b) ((uint32_t)((a) + (b))) +#define TCP_SEQ_SUB(a, b) ((uint32_t)((a) - (b))) + +/* The TCP options flags */ + +#define TCP_WSCALE 0x01U /* Window Scale option enabled */ +#define TCP_SACK 0x02U /* Selective ACKs enabled */ +#define TCP_CLOSE_ARRANGED 0x04U /* Connection is arranged to be freed */ + +#ifdef CONFIG_NET_TCP_CC_NEWRENO +/* The TCP flags for congestion control */ + +#define TCP_INFR 0x08U /* The flag in Fast Recovery */ +#define TCP_INFT 0x10U /* The flag in Fast Transmitted */ + +#endif + +/* The Max Range count of TCP Selective ACKs */ + +#define TCP_SACK_RANGES_MAX 4 + +/* After receiving 3 duplicate ACKs, TCP performs a retransmission + * (RFC 5681 (3.2)) + */ + +#define TCP_FAST_RETRANSMISSION_THRESH 3 + +#define TCP_RTO_MAX 240 /* 120s,The unit is half a second */ +#define TCP_RTO_MIN 1 /* 0.5s */ + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +struct file; /* Forward reference */ +struct sockaddr; /* Forward reference */ +struct socket; /* Forward reference */ +struct pollfd; /* Forward reference */ + +/* Representation of a TCP connection. + * + * The tcp_conn_s structure is used for identifying a connection. All + * but one field in the structure are to be considered read-only by an + * application. The only exception is the 'private' fields whose purpose + * is to let the application store application-specific state (e.g., + * file pointers) for the connection. + */ + +struct net_driver_s; /* Forward reference */ +struct devif_callback_s; /* Forward reference */ +struct tcp_backlog_s; /* Forward reference */ +struct tcp_hdr_s; /* Forward reference */ + +/* This is a container that holds the poll-related information */ + +struct tcp_poll_s +{ + FAR struct tcp_conn_s *conn; /* Needed to handle loss of connection */ + FAR struct pollfd *fds; /* Needed to handle poll events */ + FAR struct devif_callback_s *cb; /* Needed to teardown the poll */ +}; + +/* Out-of-order segments */ + +struct tcp_ofoseg_s +{ + uint32_t left; /* Left edge of segment */ + uint32_t right; /* Right edge of segment */ + FAR struct iob_s *data; /* Out-of-order buffering */ +}; + +/* SACK ranges to include in ACK packets. */ + +struct tcp_sack_s +{ + uint32_t left; /* Left edge of the SACK */ + uint32_t right; /* Right edge of the SACK */ +}; + +struct tcp_conn_s +{ + /* Common prologue of all connection structures. */ + + /* TCP callbacks: + * + * Data transfer events are retained in 'list'. Event handlers in 'list' + * are called for events specified in the flags set within struct + * devif_callback_s + * + * When an callback is executed from 'list', the input flags are normally + * returned, however, the implementation may set one of the following: + * + * TCP_CLOSE - Gracefully close the current connection + * TCP_ABORT - Abort (reset) the current connection on an error that + * prevents TCP_CLOSE from working. + * + * And/Or set/clear the following: + * + * TCP_NEWDATA - May be cleared to indicate that the data was consumed + * and that no further process of the new data should be + * attempted. + * TCP_SNDACK - If TCP_NEWDATA is cleared, then TCP_SNDACK may be set to + * indicate that an ACK should be included in the response. + * (In TCP_NEWDATA is cleared bu TCP_SNDACK is not set, + * then dev->d_len should also be cleared). + */ + + struct socket_conn_s sconn; + + /* TCP-specific content follows */ + + union ip_binding_u u; /* IP address binding */ + uint8_t rcvseq[4]; /* The sequence number that we expect to + * receive next */ + uint8_t sndseq[4]; /* The sequence number that was last sent by us */ +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) || \ + defined(CONFIG_NET_SENDFILE) + uint32_t rexmit_seq; /* The sequence number to be retrasmitted */ +#endif + uint8_t crefs; /* Reference counts on this instance */ +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + uint8_t domain; /* IP domain: PF_INET or PF_INET6 */ +#endif + uint8_t sa; /* Retransmission time-out calculation state + * variable */ + uint8_t sv; /* Retransmission time-out calculation state + * variable */ + uint8_t rto; /* Retransmission time-out */ + uint8_t tcpstateflags; /* TCP state and flags */ + struct work_s work; /* TCP timer handle */ + bool timeout; /* Trigger from timer expiry */ + uint8_t timer; /* The retransmission timer (units: half-seconds) */ + uint8_t nrtx; /* The number of retransmissions for the last + * segment sent */ +#ifdef CONFIG_NET_TCP_DELAYED_ACK + uint8_t rx_unackseg; /* Number of un-ACKed received segments */ + uint8_t rx_acktimer; /* Time since last ACK sent (units: half-seconds) */ +#endif + uint16_t lport; /* The local TCP port, in network byte order */ + uint16_t rport; /* The remoteTCP port, in network byte order */ + uint16_t mss; /* Current maximum segment size for the + * connection */ +#ifdef CONFIG_NET_TCPPROTO_OPTIONS + uint16_t user_mss; /* Configured maximum segment size for the + * connection */ +#endif + uint32_t rcv_adv; /* The right edge of the recv window advertized */ +#ifdef CONFIG_NET_TCP_CC_NEWRENO + uint32_t last_ackno; /* The ack number at the last receive ack */ + uint32_t dupacks; /* The number of duplicate ack */ + uint32_t fr_recover; /* The snd_seq at the retransmissions */ + + uint32_t cwnd; /* The Congestion window */ + uint32_t max_cwnd; /* The Congestion window maximum value */ + uint32_t ssthresh; /* The Slow start threshold */ +#endif +#ifdef CONFIG_NET_TCP_WINDOW_SCALE + uint32_t snd_wnd; /* Sequence and acknowledgement numbers of last + * window update */ + uint8_t snd_scale; /* Sender window scale factor */ + uint8_t rcv_scale; /* Receiver windows scale factor */ +#else + uint16_t snd_wnd; /* Sequence and acknowledgement numbers of last + * window update */ +#endif + uint32_t snd_wl1; + uint32_t snd_wl2; +#if CONFIG_NET_RECV_BUFSIZE > 0 + int32_t rcv_bufs; /* Maximum amount of bytes queued in recv */ +#endif +#if CONFIG_NET_SEND_BUFSIZE > 0 + int32_t snd_bufs; /* Maximum amount of bytes queued in send */ + sem_t snd_sem; /* Semaphore signals send completion */ +#endif +#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) || \ + defined(CONFIG_NET_TCP_WINDOW_SCALE) + uint32_t tx_unacked; /* Number bytes sent but not yet ACKed */ +#else + uint16_t tx_unacked; /* Number bytes sent but not yet ACKed */ +#endif + uint16_t flags; /* Flags of TCP-specific options */ +#ifdef CONFIG_NET_SOLINGER + sclock_t ltimeout; /* Linger timeout expiration */ +#endif +#ifdef CONFIG_NETDEV_RSS + int rcvcpu; /* Currect cpu id */ +#endif + /* If the TCP socket is bound to a local address, then this is + * a reference to the device that routes traffic on the corresponding + * network. + */ + + FAR struct net_driver_s *dev; + + /* Read-ahead buffering. + * + * readahead - An IOB chain where the TCP/IP read-ahead data is retained. + */ + + FAR struct iob_s *readahead; /* Read-ahead buffering */ + +#ifdef CONFIG_NET_TCP_OUT_OF_ORDER + + /* Number of out-of-order segments */ + + uint8_t nofosegs; + + /* This defines a out of order segment block. */ + + struct tcp_ofoseg_s ofosegs[TCP_SACK_RANGES_MAX]; +#endif + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Write buffering + * + * write_q - The queue of unsent I/O buffers. The head of this + * list may be partially sent. FIFO ordering. + * unacked_q - A queue of completely sent, but unacked I/O buffer + * chains. Sequence number ordering. + */ + + sq_queue_t write_q; /* Write buffering for segments */ + sq_queue_t unacked_q; /* Write buffering for un-ACKed segments */ + uint16_t expired; /* Number segments retransmitted but not yet ACKed, + * it can only be updated at TCP_ESTABLISHED state */ + uint32_t sent; /* The number of bytes sent (ACKed and un-ACKed) */ + uint32_t isn; /* Initial sequence number */ + uint32_t sndseq_max; /* The sequence number of next not-retransmitted + * segment (next greater sndseq) */ +#endif + +#ifdef CONFIG_NET_TCPBACKLOG + /* Listen backlog support + * + * blparent - The backlog parent. If this connection is backlogged, + * this field will be non-null and will refer to the TCP connection + * structure in which this connection is backlogged. + * backlog - The pending connection backlog. If this connection is + * configured as a listener with backlog, then this refers to the + * struct tcp_backlog_s tear-off structure that manages that backlog. + */ + + FAR struct tcp_conn_s *blparent; + FAR struct tcp_backlog_s *backlog; +#endif + +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* There fields manage TCP/IP keep-alive. All times are in units of the + * system clock tick. + */ + + uint32_t keeptimer; /* KeepAlive timer (dsec) */ + uint32_t keepidle; /* Elapsed idle time before first probe sent (dsec) */ + uint32_t keepintvl; /* Interval between probes (dsec) */ + bool keepalive; /* True: KeepAlive enabled; false: disabled */ + uint8_t keepcnt; /* Number of retries before the socket is closed */ + uint8_t keepretries; /* Number of retries attempted */ +#endif + +#if defined(CONFIG_NET_SENDFILE) && defined(CONFIG_NET_TCP_WRITE_BUFFERS) + bool sendfile; /* True if sendfile operation is in progress */ +#endif + bool zero_probe; /* TCP zero window probe timer */ + + /* connevents is a list of callbacks for each socket the uses this + * connection (there can be more that one in the event that the the socket + * was dup'ed). It is used with the network monitor to handle + * asynchronous loss-of-connection events. + */ + + FAR struct devif_callback_s *connevents; + FAR struct devif_callback_s *connevents_tail; + + /* Reference to TCP shutdown/close callback instance */ + + FAR struct devif_callback_s *shdcb; + FAR struct devif_callback_s *clscb; + struct work_s clswork; + +#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) + /* Callback instance for TCP send() */ + + FAR struct devif_callback_s *sndcb; +#endif + + /* accept() is called when the TCP logic has created a connection + * + * accept_private: This is private data that will be available to the + * accept() handler when it is invoked with a point to this structure + * as an argument. + * accept: This is the pointer to the accept handler. + */ + + FAR void *accept_private; + CODE int (*accept)(FAR struct tcp_conn_s *listener, + FAR struct tcp_conn_s *conn); + + /* The following is a list of poll structures of threads waiting for + * socket events. + */ + + struct tcp_poll_s pollinfo[CONFIG_NET_TCP_NPOLLWAITERS]; +}; + +/* This structure supports TCP write buffering */ + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +struct tcp_wrbuffer_s +{ + sq_entry_t wb_node; /* Supports a singly linked list */ + uint32_t wb_seqno; /* Sequence number of the write segment */ + uint16_t wb_sent; /* Number of bytes sent from the I/O buffer chain */ + uint8_t wb_nrtx; /* The number of retransmissions for the last + * segment sent */ +#if defined(CONFIG_NET_TCP_FAST_RETRANSMIT) && !defined(CONFIG_NET_TCP_CC_NEWRENO) + uint8_t wb_nack; /* The number of ack count */ +#endif + struct iob_s *wb_iob; /* Head of the I/O buffer chain */ +}; +#endif + +/* Support for listen backlog: + * + * struct tcp_blcontainer_s describes one backlogged connection + * struct tcp_backlog_s is a "tear-off" describing all backlog for a + * listener connection + */ + +#ifdef CONFIG_NET_TCPBACKLOG +struct tcp_blcontainer_s +{ + sq_entry_t bc_node; /* Implements a singly linked list */ + FAR struct tcp_conn_s *bc_conn; /* Holds reference to the new connection structure */ +}; + +struct tcp_backlog_s +{ + sq_queue_t bl_free; /* Implements a singly-linked list of free containers */ + sq_queue_t bl_pending; /* Implements a singly-linked list of pending connections */ +}; +#endif + +struct tcp_callback_s +{ + FAR struct tcp_conn_s *tc_conn; + FAR struct devif_callback_s *tc_cb; + FAR sem_t *tc_sem; +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_initialize + * + * Description: + * Initialize the TCP/IP connection structures. Called only once and only + * from the network layer at start-up. + * + ****************************************************************************/ + +void tcp_initialize(void); + +/**************************************************************************** + * Name: tcp_alloc + * + * Description: + * Find a free TCP/IP connection structure and allocate it + * for use. This is normally something done by the implementation of the + * socket() API but is also called from the event processing logic when a + * TCP packet is received while "listening" + * + ****************************************************************************/ + +FAR struct tcp_conn_s *tcp_alloc(uint8_t domain); + +/**************************************************************************** + * Name: tcp_free_rx_buffers + * + * Description: + * Free rx buffer of a connection + * + ****************************************************************************/ + +void tcp_free_rx_buffers(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_free + * + * Description: + * Free a connection structure that is no longer in use. This should be + * done by the implementation of close() + * + ****************************************************************************/ + +void tcp_free(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_active + * + * Description: + * Find a connection structure that is the appropriate + * connection to be used with the provided TCP/IP header + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +FAR struct tcp_conn_s *tcp_active(FAR struct net_driver_s *dev, + FAR struct tcp_hdr_s *tcp); + +/**************************************************************************** + * Name: tcp_nextconn + * + * Description: + * Traverse the list of active TCP connections + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +FAR struct tcp_conn_s *tcp_nextconn(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_local_ipv4_device + * + * Description: + * Select the network driver to use with the IPv4 TCP transaction based + * on the locally bound IPv4 address + * + * Input Parameters: + * conn - TCP connection structure. The locally bound address, laddr, + * should be set to a non-zero value in this structure. + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. -ENETUNREACH is the only expected error value. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv4 +int tcp_local_ipv4_device(FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_remote_ipv4_device + * + * Description: + * Select the network driver to use with the IPv4 TCP transaction based + * on the remotely connected IPv4 address + * + * Input Parameters: + * conn - TCP connection structure. The remotely connected address, raddr, + * should be set to a non-zero value in this structure. + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. -ENETUNREACH is the only expected error value. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv4 +int tcp_remote_ipv4_device(FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_local_ipv6_device + * + * Description: + * Select the network driver to use with the IPv6 TCP transaction based + * on the locally bound IPv6 address + * + * Input Parameters: + * conn - TCP connection structure. The locally bound address, laddr, + * should be set to a non-zero value in this structure. + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. -EHOSTUNREACH is the only expected error value. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv6 +int tcp_local_ipv6_device(FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_remote_ipv6_device + * + * Description: + * Select the network driver to use with the IPv6 TCP transaction based + * on the remotely connected IPv6 address + * + * Input Parameters: + * conn - TCP connection structure. The remotely connected address, raddr, + * should be set to a non-zero value in this structure. + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned + * on failure. -EHOSTUNREACH is the only expected error value. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv6 +int tcp_remote_ipv6_device(FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_alloc_accept + * + * Description: + * Called when driver processing matches the incoming packet with a + * connection in LISTEN. In that case, this function will create a new + * connection and initialize it to send a SYNACK in return. + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +FAR struct tcp_conn_s *tcp_alloc_accept(FAR struct net_driver_s *dev, + FAR struct tcp_hdr_s *tcp, + FAR struct tcp_conn_s *listener); + +/**************************************************************************** + * Name: tcp_selectport + * + * Description: + * If the port number is zero; select an unused port for the connection. + * If the port number is non-zero, verify that no other connection has + * been created with this port number. + * + * Returned Value: + * Selected or verified port number in network order on success, a negated + * errno on failure. + * + * Assumptions: + * Interrupts are disabled + * + ****************************************************************************/ + +int tcp_selectport(uint8_t domain, + FAR const union ip_addr_u *ipaddr, + uint16_t portno); + +/**************************************************************************** + * Name: tcp_bind + * + * Description: + * This function implements the lower level parts of the standard TCP + * bind() operation. + * + * Returned Value: + * 0 on success or -EADDRINUSE on failure + * + * Assumptions: + * This function is called from normal user level code. + * + ****************************************************************************/ + +int tcp_bind(FAR struct tcp_conn_s *conn, FAR const struct sockaddr *addr); + +/**************************************************************************** + * Name: tcp_connect + * + * Description: + * This function implements the lower level parts of the standard + * TCP connect() operation: It connects to a remote host using TCP. + * + * This function is used to start a new connection to the specified + * port on the specified host. It uses the connection structure that was + * allocated by a preceding socket() call. It sets the connection to + * the SYN_SENT state and sets the retransmission timer to 0. This will + * cause a TCP SYN segment to be sent out the next time this connection + * is periodically processed, which usually is done within 0.5 seconds + * after the call to tcp_connect(). + * + * Assumptions: + * This function is called from normal user level code. + * + ****************************************************************************/ + +int tcp_connect(FAR struct tcp_conn_s *conn, + FAR const struct sockaddr *addr); + +/**************************************************************************** + * Name: psock_tcp_connect + * + * Description: + * Perform a TCP connection + * + * Input Parameters: + * psock - A reference to the structure of the socket to be connected + * addr - The address of the remote server to connect to + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked + * + ****************************************************************************/ + +int psock_tcp_connect(FAR struct socket *psock, + FAR const struct sockaddr *addr); + +/**************************************************************************** + * Name: tcp_start_monitor + * + * Description: + * Set up to receive TCP connection state changes for a given socket + * + * Input Parameters: + * psock - The socket of interest + * + * Returned Value: + * On success, tcp_start_monitor returns OK; On any failure, + * tcp_start_monitor will return a negated errno value. The only failure + * that can occur is if the socket has already been closed and, in this + * case, -ENOTCONN is returned. + * + * Assumptions: + * The caller holds the network lock (if not, it will be locked momentarily + * by this function). + * + ****************************************************************************/ + +int tcp_start_monitor(FAR struct socket *psock); + +/**************************************************************************** + * Name: tcp_stop_monitor + * + * Description: + * Stop monitoring TCP connection changes for a sockets associated with + * a given TCP connection structure. + * + * Input Parameters: + * conn - The TCP connection of interest + * flags Set of disconnection events + * + * Returned Value: + * None + * + * Assumptions: + * The caller holds the network lock (if not, it will be locked momentarily + * by this function). + * + ****************************************************************************/ + +void tcp_stop_monitor(FAR struct tcp_conn_s *conn, uint16_t flags); + +/**************************************************************************** + * Name: tcp_lost_connection + * + * Description: + * Called when a loss-of-connection event has been detected by network + * event handling logic. Perform operations like tcp_stop_monitor but + * (1) explicitly mark this socket and (2) disable further callbacks + * the event handler. + * + * Input Parameters: + * conn - The TCP connection of interest + * cb - devif callback structure + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * The caller holds the network lock (if not, it will be locked momentarily + * by this function). + * + ****************************************************************************/ + +void tcp_lost_connection(FAR struct tcp_conn_s *conn, + FAR struct devif_callback_s *cb, uint16_t flags); + +/**************************************************************************** + * Name: tcp_close + * + * Description: + * Break any current TCP connection + * + * Input Parameters: + * psock - An instance of the internal socket structure. + * + * Assumptions: + * Called from normal user-level logic + * + ****************************************************************************/ + +int tcp_close(FAR struct socket *psock); + +/**************************************************************************** + * Name: tcp_shutdown + * + * Description: + * Gracefully shutdown a TCP connection by sending a SYN + * + * Input Parameters: + * psock - An instance of the internal socket structure. + * how - Specifies the type of shutdown. + * + * Assumptions: + * Called from normal user-level logic + * + ****************************************************************************/ + +int tcp_shutdown(FAR struct socket *psock, int how); + +/**************************************************************************** + * Name: tcp_ipv4_select + * + * Description: + * Configure to send or receive an TCP IPv4 packet + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv4 +void tcp_ipv4_select(FAR struct net_driver_s *dev); +#endif + +/**************************************************************************** + * Name: tcp_ipv6_select + * + * Description: + * Configure to send or receive an TCP IPv6 packet + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv6 +void tcp_ipv6_select(FAR struct net_driver_s *dev); +#endif + +/**************************************************************************** + * Name: tcp_ip_select + * + * Description: + * Configure to send or receive an TCP IPv[4|6] packet for connection + * + ****************************************************************************/ + +void tcp_ip_select(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_setsequence + * + * Description: + * Set the TCP/IP sequence number + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_setsequence(FAR uint8_t *seqno, uint32_t value); + +/**************************************************************************** + * Name: tcp_getsequence + * + * Description: + * Get the TCP/IP sequence number + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +uint32_t tcp_getsequence(FAR uint8_t *seqno); + +/**************************************************************************** + * Name: tcp_addsequence + * + * Description: + * Add the length to get the next TCP sequence number. + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +uint32_t tcp_addsequence(FAR uint8_t *seqno, uint16_t len); + +/**************************************************************************** + * Name: tcp_initsequence + * + * Description: + * Set the (initial) the TCP/IP sequence number when a TCP connection is + * established. + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_initsequence(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_nextsequence + * + * Description: + * Increment the TCP/IP sequence number + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_nextsequence(void); + +/**************************************************************************** + * Name: tcp_poll + * + * Description: + * Poll a TCP connection structure for availability of TX data + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_poll(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout); + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout); +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_findlistener + * + * Description: + * Return the connection listener for connections on this port (if any) + * + * Assumptions: + * The network is locked + * + ****************************************************************************/ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) +FAR struct tcp_conn_s *tcp_findlistener(FAR union ip_binding_u *uaddr, + uint16_t portno, + uint8_t domain); +#else +FAR struct tcp_conn_s *tcp_findlistener(FAR union ip_binding_u *uaddr, + uint16_t portno); +#endif + +/**************************************************************************** + * Name: tcp_unlisten + * + * Description: + * Stop listening to the port bound to the specified TCP connection + * + * Assumptions: + * Called from normal user code. + * + ****************************************************************************/ + +int tcp_unlisten(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_listen + * + * Description: + * Start listening to the port bound to the specified TCP connection + * + * Assumptions: + * Called from normal user code. + * + ****************************************************************************/ + +int tcp_listen(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_islistener + * + * Description: + * Return true is there is a listener for the specified port + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) +bool tcp_islistener(FAR union ip_binding_u *uaddr, uint16_t portno, + uint8_t domain); +#else +bool tcp_islistener(FAR union ip_binding_u *uaddr, uint16_t portno); +#endif + +/**************************************************************************** + * Name: tcp_accept_connection + * + * Description: + * Accept the new connection for the specified listening port. + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +int tcp_accept_connection(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn, uint16_t portno); + +/**************************************************************************** + * Name: tcp_send + * + * Description: + * Setup to send a TCP packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP connection structure holding connection information + * flags - flags to apply to the TCP header + * len - length of the message + * + * Returned Value: + * None + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_send(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, + uint16_t flags, uint16_t len); + +/**************************************************************************** + * Name: tcp_sendfile + * + * Description: + * The tcp_sendfile() call may be used only when the INET socket is in a + * connected state (so that the intended recipient is known). + * + * Input Parameters: + * psock An instance of the internal socket structure. + * buf Data to send + * len Length of data to send + * flags Send flags + * + * Returned Value: + * On success, returns the number of characters sent. On error, + * a negated errno value is returned. See sendfile() for a list + * appropriate error return values. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_SENDFILE +ssize_t tcp_sendfile(FAR struct socket *psock, FAR struct file *infile, + FAR off_t *offset, size_t count); +#endif + +/**************************************************************************** + * Name: tcp_reset + * + * Description: + * Send a TCP reset (no-data) message + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP connection structure holding connection information + * + * Returned Value: + * None + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_reset(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_rx_mss + * + * Description: + * Return the MSS to advertize to the peer. + * + * Input Parameters: + * dev - The device driver structure + * + * Returned Value: + * The MSS value. + * + ****************************************************************************/ + +uint16_t tcp_rx_mss(FAR struct net_driver_s *dev); + +/**************************************************************************** + * Name: tcp_synack + * + * Description: + * Send the SYN or SYNACK response. + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP connection structure holding connection information + * ack - The ACK response to send + * + * Returned Value: + * None + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_synack(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, + uint8_t ack); + +/**************************************************************************** + * Name: tcp_appsend + * + * Description: + * Handle application or TCP protocol response. If this function is called + * with dev->d_sndlen > 0, then this is an application attempting to send + * packet. + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP connection structure holding connection information + * result - App result event sent + * + * Returned Value: + * None + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_appsend(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, + uint16_t result); + +/**************************************************************************** + * Name: tcp_rexmit + * + * Description: + * Handle application retransmission + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP connection structure holding connection information + * result - App result event sent + * + * Returned Value: + * None + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +void tcp_rexmit(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, + uint16_t result); + +/**************************************************************************** + * Name: tcp_send_txnotify + * + * Description: + * Notify the appropriate device driver that we are have data ready to + * be send (TCP) + * + * Input Parameters: + * psock - Socket state structure + * conn - The TCP connection structure + * + * Returned Value: + * None + * + ****************************************************************************/ + +void tcp_send_txnotify(FAR struct socket *psock, + FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_ipv4_input + * + * Description: + * Handle incoming TCP input with IPv4 header + * + * Input Parameters: + * dev - The device driver structure containing the received TCP packet. + * + * Returned Value: + * None + * + * Assumptions: + * Called from the Ethernet driver with the network stack locked + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv4 +void tcp_ipv4_input(FAR struct net_driver_s *dev); +#endif + +/**************************************************************************** + * Name: tcp_ipv6_input + * + * Description: + * Handle incoming TCP input with IPv4 header + * + * Input Parameters: + * dev - The device driver structure containing the received TCP packet. + * iplen - The size of the IPv6 header. This may be larger than + * IPv6_HDRLEN the IPv6 header if IPv6 extension headers are + * present. + * + * Returned Value: + * None + * + * Assumptions: + * Called from the Ethernet driver with the network stack locked + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IPv6 +void tcp_ipv6_input(FAR struct net_driver_s *dev, unsigned int iplen); +#endif + +/**************************************************************************** + * Name: tcp_callback + * + * Description: + * Inform the application holding the TCP socket of a change in state. + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +uint16_t tcp_callback(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn, uint16_t flags); + +/**************************************************************************** + * Name: tcp_datahandler + * + * Description: + * Handle data that is not accepted by the application. This may be called + * either (1) from the data receive logic if it cannot buffer the data, or + * (2) from the TCP event logic is there is no listener in place ready to + * receive the data. + * + * Input Parameters: + * conn - A pointer to the TCP connection structure + * buffer - A pointer to the buffer to be copied to the read-ahead + * buffers + * buflen - The number of bytes to copy to the read-ahead buffer. + * + * Returned Value: + * The number of bytes actually buffered is returned. This will be either + * zero or equal to buflen; partial packets are not buffered. + * + * Assumptions: + * - The caller has checked that TCP_NEWDATA is set in flags and that is no + * other handler available to process the incoming data. + * - Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +uint16_t tcp_datahandler(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn, + uint16_t offset); + +/**************************************************************************** + * Name: tcp_backlogcreate + * + * Description: + * Called from the listen() logic to setup the backlog as specified in the + * the listen arguments. + * + * Assumptions: + * Called from network socket logic. The network may or may not be locked. + * + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_callback_cleanup + * + * Description: + * Cleanup data and cb when thread is canceled. + * + * Input Parameters: + * arg - A pointer with conn and callback struct. + * + ****************************************************************************/ + +void tcp_callback_cleanup(FAR void *arg); + +#ifdef CONFIG_NET_TCPBACKLOG +int tcp_backlogcreate(FAR struct tcp_conn_s *conn, int nblg); +#else +# define tcp_backlogcreate(c,n) (-ENOSYS) +#endif + +/**************************************************************************** + * Name: tcp_backlogdestroy + * + * Description: + * (1) Called from tcp_free() whenever a connection is freed. + * (2) Called from tcp_backlogcreate() to destroy any old backlog + * + * NOTE: This function may re-enter tcp_free when a connection that + * is freed that has pending connections. + * + * Assumptions: + * Called from network socket logic with the network stack locked + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCPBACKLOG +int tcp_backlogdestroy(FAR struct tcp_conn_s *conn); +#else +# define tcp_backlogdestroy(conn) (-ENOSYS) +#endif + +/**************************************************************************** + * Name: tcp_backlogadd + * + * Description: + * Called tcp_listen when a new connection is made with a listener socket + * but when there is no accept() in place to receive the connection. This + * function adds the new connection to the backlog. + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCPBACKLOG +int tcp_backlogadd(FAR struct tcp_conn_s *conn, + FAR struct tcp_conn_s *blconn); +#else +# define tcp_backlogadd(conn,blconn) (-ENOSYS) +#endif + +/**************************************************************************** + * Name: tcp_backlogpending + * + * Description: + * Called from poll(). Before waiting for a new connection, poll will + * call this API to see if there are pending connections in the backlog. + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCPBACKLOG +bool tcp_backlogpending(FAR struct tcp_conn_s *conn); +#else +# define tcp_backlogpending(c) (false) +#endif + +/**************************************************************************** + * Name: tcp_backlogavailable + * + * Description: + * Called from tcp_input(). Before alloc a new accept connection, tcp_input + * will call this API to see if there are free node in the backlog. + * + * Assumptions: + * Called from network socket logic with the network locked + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCPBACKLOG +bool tcp_backlogavailable(FAR struct tcp_conn_s *conn); +#else +# define tcp_backlogavailable(c) (true) +#endif + +/**************************************************************************** + * Name: tcp_backlogremove + * + * Description: + * Called from accept(). Before waiting for a new connection, accept will + * call this API to see if there are pending connections in the backlog. + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCPBACKLOG +FAR struct tcp_conn_s *tcp_backlogremove(FAR struct tcp_conn_s *conn); +#else +# define tcp_backlogremove(c) (NULL) +#endif + +/**************************************************************************** + * Name: tcp_backlogdelete + * + * Description: + * Called from tcp_free() when a connection is freed that this also + * retained in the pending connection list of a listener. We simply need + * to remove the defunct connection from the list. + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCPBACKLOG +int tcp_backlogdelete(FAR struct tcp_conn_s *conn, + FAR struct tcp_conn_s *blconn); +#else +# define tcp_backlogdelete(c,b) (-ENOSYS) +#endif + +/**************************************************************************** + * Name: tcp_accept + * + * Description: + * This function implements accept() for TCP/IP sockets. See the + * description of accept() for further information. + * + * Input Parameters: + * psock The listening TCP socket structure + * addr Receives the address of the connecting client + * addrlen Input: allocated size of 'addr' + * Return: returned size of 'addr' + * newconn The new, accepted TCP connection structure + * + * Returned Value: + * Returns zero (OK) on success or a negated errno value on failure. + * See the description of accept of the possible errno values in the + * description of accept(). + * + * Assumptions: + * Network is locked. + * + ****************************************************************************/ + +int psock_tcp_accept(FAR struct socket *psock, FAR struct sockaddr *addr, + FAR socklen_t *addrlen, FAR void **newconn); + +/**************************************************************************** + * Name: psock_tcp_recvfrom + * + * Description: + * Perform the recvfrom operation for a TCP/IP SOCK_STREAM + * + * Input Parameters: + * psock Pointer to the socket structure for the SOCK_DRAM socket + * msg Receive info and buffer for receive data + * flags Receive flags + * + * Returned Value: + * On success, returns the number of characters received. On error, + * -errno is returned (see recvfrom for list of errnos). + * + * Assumptions: + * + ****************************************************************************/ + +ssize_t psock_tcp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg, + int flags); + +/**************************************************************************** + * Name: psock_tcp_send + * + * Description: + * The psock_tcp_send() call may be used only when the TCP socket is in a + * connected state (so that the intended recipient is known). + * + * Input Parameters: + * psock An instance of the internal socket structure. + * buf Data to send + * len Length of data to send + * flags Send flags + * + * Returned Value: + * On success, returns the number of characters sent. On error, + * -1 is returned, and errno is set appropriately: + * + * EAGAIN or EWOULDBLOCK + * The socket is marked non-blocking and the requested operation + * would block. + * EBADF + * An invalid descriptor was specified. + * ECONNRESET + * Connection reset by peer. + * EDESTADDRREQ + * The socket is not connection-mode, and no peer address is set. + * EFAULT + * An invalid user space address was specified for a parameter. + * EINTR + * A signal occurred before any data was transmitted. + * EINVAL + * Invalid argument passed. + * EISCONN + * The connection-mode socket was connected already but a recipient + * was specified. (Now either this error is returned, or the recipient + * specification is ignored.) + * EMSGSIZE + * The socket type requires that message be sent atomically, and the + * size of the message to be sent made this impossible. + * ENOBUFS + * The output queue for a network interface was full. This generally + * indicates that the interface has stopped sending, but may be + * caused by transient congestion. + * ENOMEM + * No memory available. + * ENOTCONN + * The socket is not connected, and no target has been given. + * ENOTSOCK + * The argument s is not a socket. + * EPIPE + * The local end has been shut down on a connection oriented socket. + * In this case the process will also receive a SIGPIPE unless + * MSG_NOSIGNAL is set. + * + * Assumptions: + * + ****************************************************************************/ + +ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, + size_t len, int flags); + +/**************************************************************************** + * Name: tcp_setsockopt + * + * Description: + * tcp_setsockopt() sets the TCP-protocol option specified by the + * 'option' argument to the value pointed to by the 'value' argument for + * the socket specified by the 'psock' argument. + * + * See for the a complete list of values of TCP protocol + * options. + * + * Input Parameters: + * psock Socket structure of socket to operate on + * option identifies the option to set + * value Points to the argument value + * value_len The length of the argument value + * + * Returned Value: + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. See psock_setcockopt() for + * the list of possible error values. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCPPROTO_OPTIONS +int tcp_setsockopt(FAR struct socket *psock, int option, + FAR const void *value, socklen_t value_len); +#endif + +/**************************************************************************** + * Name: tcp_getsockopt + * + * Description: + * tcp_getsockopt() retrieves the value for the option specified by the + * 'option' argument for the socket specified by the 'psock' argument. If + * the size of the option value is greater than 'value_len', the value + * stored in the object pointed to by the 'value' argument will be silently + * truncated. Otherwise, the length pointed to by the 'value_len' argument + * will be modified to indicate the actual length of the 'value'. + * + * The 'level' argument specifies the protocol level of the option. To + * retrieve options at the socket level, specify the level argument as + * SOL_SOCKET; to retrieve options at the TCP-protocol level, the level + * argument is SOL_TCP. + * + * See a complete list of values for the socket-level + * 'option' argument. Protocol-specific options are are protocol specific + * header files (such as netinet/tcp.h for the case of the TCP protocol). + * + * Input Parameters: + * psock Socket structure of the socket to query + * level Protocol level to set the option + * option identifies the option to get + * value Points to the argument value + * value_len The length of the argument value + * + * Returned Value: + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. See psock_getsockopt() for + * the complete list of appropriate return error codes. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCPPROTO_OPTIONS +int tcp_getsockopt(FAR struct socket *psock, int option, + FAR void *value, FAR socklen_t *value_len); +#endif + +/**************************************************************************** + * Name: tcp_get_recvwindow + * + * Description: + * Calculate the TCP receive window for the specified device. + * + * Input Parameters: + * dev - The device whose TCP receive window will be updated. + * conn - The TCP connection structure holding connection information. + * + * Returned Value: + * The value of the TCP receive window to use. + * + ****************************************************************************/ + +uint32_t tcp_get_recvwindow(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_should_send_recvwindow + * + * Description: + * Determine if we should advertize the new recv window to the peer. + * + * Input Parameters: + * conn - The TCP connection structure holding connection information. + * + * Returned Value: + * If we should send an update. + * + ****************************************************************************/ + +bool tcp_should_send_recvwindow(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: psock_tcp_cansend + * + * Description: + * psock_tcp_cansend() returns a value indicating if a write to the socket + * would block. No space in the buffer is actually reserved, so it is + * possible that the write may still block if the buffer is filled by + * another means. + * + * Input Parameters: + * conn The TCP connection of interest + * + * Returned Value: + * OK + * At least one byte of data could be successfully written. + * -EWOULDBLOCK + * There is no room in the output buffer. + * -EBADF + * An invalid descriptor was specified. + * -ENOTCONN + * The socket is not connected. + * + ****************************************************************************/ + +int psock_tcp_cansend(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_wrbuffer_initialize + * + * Description: + * Initialize the list of free write buffers + * + * Assumptions: + * Called once early initialization. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +void tcp_wrbuffer_initialize(void); +#endif /* CONFIG_NET_TCP_WRITE_BUFFERS */ + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + +struct tcp_wrbuffer_s; + +/**************************************************************************** + * Name: tcp_wrbuffer_timedalloc + * + * Description: + * Allocate a TCP write buffer by taking a pre-allocated buffer from + * the free list. This function is called from TCP logic when a buffer + * of TCP data is about to sent + * This function is wrapped version of tcp_wrbuffer_alloc(), + * this wait will be terminated when the specified timeout expires. + * + * Input Parameters: + * timeout - The relative time to wait until a timeout is declared. + * + * Assumptions: + * Called from user logic with the network locked. + * + ****************************************************************************/ + +FAR struct tcp_wrbuffer_s *tcp_wrbuffer_timedalloc(unsigned int timeout); + +/**************************************************************************** + * Name: tcp_wrbuffer_alloc + * + * Description: + * Allocate a TCP write buffer by taking a pre-allocated buffer from + * the free list. This function is called from TCP logic when a buffer + * of TCP data is about to sent + * + * Input Parameters: + * None + * + * Assumptions: + * Called from user logic with the network locked. + * + ****************************************************************************/ + +FAR struct tcp_wrbuffer_s *tcp_wrbuffer_alloc(void); + +/**************************************************************************** + * Name: tcp_wrbuffer_tryalloc + * + * Description: + * Try to allocate a TCP write buffer by taking a pre-allocated buffer from + * the free list. This function is called from TCP logic when a buffer + * of TCP data is about to be sent if the socket is non-blocking. Returns + * immediately if allocation fails. + * + * Input parameters: + * None + * + * Assumptions: + * Called from user logic with the network locked. + * + ****************************************************************************/ + +FAR struct tcp_wrbuffer_s *tcp_wrbuffer_tryalloc(void); +#endif /* CONFIG_NET_TCP_WRITE_BUFFERS */ + +/**************************************************************************** + * Name: tcp_wrbuffer_release + * + * Description: + * Release a TCP write buffer by returning the buffer to the free list. + * This function is called from user logic after it is consumed the + * buffered data. + * + * Assumptions: + * Called from network stack logic with the network stack locked + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +void tcp_wrbuffer_release(FAR struct tcp_wrbuffer_s *wrb); +#endif /* CONFIG_NET_TCP_WRITE_BUFFERS */ + +/**************************************************************************** + * Name: tcp_wrbuffer_inqueue_size + * + * Description: + * Get the in-queued write buffer size from connection + * + * Input Parameters: + * conn - The TCP connection of interest + * + * Assumptions: + * Called from user logic with the network locked. + * + ****************************************************************************/ + +#if CONFIG_NET_SEND_BUFSIZE > 0 +uint32_t tcp_wrbuffer_inqueue_size(FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_wrbuffer_test + * + * Description: + * Check if there is room in the write buffer. Does not reserve any space. + * + * Assumptions: + * None. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +int tcp_wrbuffer_test(void); +#endif /* CONFIG_NET_TCP_WRITE_BUFFERS */ + +/**************************************************************************** + * Name: tcp_event_handler_dump + * + * Description: + * Dump the TCP event handler related variables + * + ****************************************************************************/ + +#ifdef CONFIG_DEBUG_FEATURES +void tcp_event_handler_dump(FAR struct net_driver_s *dev, + FAR void *pvpriv, + uint16_t flags, + FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_wrbuffer_dump + * + * Description: + * Dump the contents of a write buffer. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +#ifdef CONFIG_DEBUG_FEATURES +void tcp_wrbuffer_dump(FAR const char *msg, FAR struct tcp_wrbuffer_s *wrb, + unsigned int len, unsigned int offset); +#else +# define tcp_wrbuffer_dump(msg,wrb) +#endif +#endif /* CONFIG_NET_TCP_WRITE_BUFFERS */ + +/**************************************************************************** + * Name: tcp_pollsetup + * + * Description: + * Setup to monitor events on one TCP/IP socket + * + * Input Parameters: + * psock - The TCP/IP socket of interest + * fds - The structure describing the events to be monitored, OR NULL if + * this is a request to stop monitoring events. + * + * Returned Value: + * 0: Success; Negated errno on failure + * + ****************************************************************************/ + +int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds); + +/**************************************************************************** + * Name: tcp_pollteardown + * + * Description: + * Teardown monitoring of events on an TCP/IP socket + * + * Input Parameters: + * psock - The TCP/IP socket of interest + * fds - The structure describing the events to be monitored, OR NULL if + * this is a request to stop monitoring events. + * + * Returned Value: + * 0: Success; Negated errno on failure + * + ****************************************************************************/ + +int tcp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds); + +/**************************************************************************** + * Name: tcp_readahead_notifier_setup + * + * Description: + * Set up to perform a callback to the worker function when an TCP data + * is added to the read-ahead buffer. The worker function will execute + * on the high priority worker thread. + * + * Input Parameters: + * worker - The worker function to execute on the high priority work + * queue when data is available in the TCP read-ahead buffer. + * conn - The TCP connection where read-ahead data is needed. + * arg - A user-defined argument that will be available to the worker + * function when it runs. + * + * Returned Value: + * > 0 - The signal notification is in place. The returned value is a + * key that may be used later in a call to + * tcp_notifier_teardown(). + * == 0 - There is already buffered read-ahead data. No signal + * notification will be provided. + * < 0 - An unexpected error occurred and no signal will be sent. The + * returned value is a negated errno value that indicates the + * nature of the failure. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_NOTIFIER +int tcp_readahead_notifier_setup(worker_t worker, + FAR struct tcp_conn_s *conn, + FAR void *arg); +#endif + +/**************************************************************************** + * Name: tcp_writebuffer_notifier_setup + * + * Description: + * Set up to perform a callback to the worker function when an TCP write + * buffer is emptied. The worker function will execute on the high + * priority worker thread. + * + * Input Parameters: + * worker - The worker function to execute on the high priority work + * queue when all buffer TX data has been sent. + * conn - The TCP connection where buffer write data is pending. + * arg - A user-defined argument that will be available to the worker + * function when it runs. + * + * Returned Value: + * > 0 - The signal notification is in place. The returned value is a + * key that may be used later in a call to + * tcp_notifier_teardown(). + * == 0 - There is already buffered read-ahead data. No signal + * notification will be provided. + * < 0 - An unexpected error occurred and no signal will be sent. The + * returned value is a negated errno value that indicates the + * nature of the failure. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_NOTIFIER +int tcp_writebuffer_notifier_setup(worker_t worker, + FAR struct tcp_conn_s *conn, + FAR void *arg); +#endif + +/**************************************************************************** + * Name: tcp_disconnect_notifier_setup + * + * Description: + * Set up to perform a callback to the worker function if the TCP + * connection is lost. + * + * Input Parameters: + * worker - The worker function to execute on the high priority work + * queue when data is available in the TCP read-ahead buffer. + * conn - The TCP connection where read-ahead data is needed. + * arg - A user-defined argument that will be available to the worker + * function when it runs. + * + * Returned Value: + * > 0 - The signal notification is in place. The returned value is a + * key that may be used later in a call to + * tcp_notifier_teardown(). + * == 0 - No connection has been established. + * < 0 - An unexpected error occurred and no signal will be sent. The + * returned value is a negated errno value that indicates the + * nature of the failure. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_NOTIFIER +int tcp_disconnect_notifier_setup(worker_t worker, + FAR struct tcp_conn_s *conn, + FAR void *arg); +#endif + +/**************************************************************************** + * Name: tcp_notifier_teardown + * + * Description: + * Eliminate a TCP read-ahead notification previously setup by + * tcp_readahead_notifier_setup(). This function should only be called + * if the notification should be aborted prior to the notification. The + * notification will automatically be torn down after the signal is sent. + * + * Input Parameters: + * key - The key value returned from a previous call to + * tcp_readahead_notifier_setup(). + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_NOTIFIER +void tcp_notifier_teardown(int key); +#endif + +/**************************************************************************** + * Name: tcp_readahead_signal + * + * Description: + * Read-ahead data has been buffered. Signal all threads waiting for + * read-ahead data to become available. + * + * When read-ahead data becomes available, *all* of the workers waiting + * for read-ahead data will be executed. If there are multiple workers + * waiting for read-ahead data then only the first to execute will get the + * data. Others will need to call tcp_readahead_notifier_setup() once + * again. + * + * Input Parameters: + * conn - The TCP connection where read-ahead data was just buffered. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_NOTIFIER +void tcp_readahead_signal(FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_writebuffer_signal + * + * Description: + * All buffer Tx data has been sent. Signal all threads waiting for the + * write buffers to become empty. + * + * When write buffer becomes empty, *all* of the workers waiting + * for that event data will be executed. If there are multiple workers + * waiting for read-ahead data then only the first to execute will get the + * data. Others will need to call tcp_writebuffer_notifier_setup() once + * again. + * + * Input Parameters: + * conn - The TCP connection where read-ahead data was just buffered. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) && defined(CONFIG_NET_TCP_NOTIFIER) +void tcp_writebuffer_signal(FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_disconnect_signal + * + * Description: + * The TCP connection has been lost. Signal all threads monitoring TCP + * state events. + * + * Input Parameters: + * conn - The TCP connection where read-ahead data was just buffered. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_NOTIFIER +void tcp_disconnect_signal(FAR struct tcp_conn_s *conn); +#endif + +/**************************************************************************** + * Name: tcp_txdrain + * + * Description: + * Wait for all write buffers to be sent (or for a timeout to occur). + * + * Input Parameters: + * psock - An instance of the internal socket structure. + * timeout - The relative time when the timeout will occur + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned + * on any failure. + * + ****************************************************************************/ + +#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) && defined(CONFIG_NET_TCP_NOTIFIER) +int tcp_txdrain(FAR struct socket *psock, unsigned int timeout); +#else +# define tcp_txdrain(conn, timeout) (0) +#endif + +/**************************************************************************** + * Name: tcp_ioctl + * + * Description: + * This function performs tcp specific ioctl() operations. + * + * Parameters: + * conn The TCP connection of interest + * cmd The ioctl command + * arg The argument of the ioctl cmd + * + ****************************************************************************/ + +int tcp_ioctl(FAR struct tcp_conn_s *conn, int cmd, unsigned long arg); + +/**************************************************************************** + * Name: tcp_sendbuffer_notify + * + * Description: + * Notify the send buffer semaphore + * + * Input Parameters: + * conn - The TCP connection of interest + * + * Assumptions: + * Called from user logic with the network locked. + * + ****************************************************************************/ + +#if CONFIG_NET_SEND_BUFSIZE > 0 +void tcp_sendbuffer_notify(FAR struct tcp_conn_s *conn); +#endif /* CONFIG_NET_SEND_BUFSIZE */ + +/**************************************************************************** + * Name: tcpip_hdrsize + * + * Description: + * Get the total size of L3 and L4 TCP header + * + * Input Parameters: + * conn The connection structure associated with the socket + * + * Returned Value: + * the total size of L3 and L4 TCP header + * + ****************************************************************************/ + +uint16_t tcpip_hdrsize(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_ofoseg_bufsize + * + * Description: + * Calculate the pending size of out-of-order buffer + * + * Input Parameters: + * conn - The TCP connection of interest + * + * Returned Value: + * Total size of out-of-order buffer + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +int tcp_ofoseg_bufsize(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_reorder_ofosegs + * + * Description: + * Sort out-of-order segments by left edge + * + * Input Parameters: + * nofosegs - Number of out-of-order semgnets + * ofosegs - Pointer to out-of-order segments + * + * Returned Value: + * True if re-order occurs + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +bool tcp_reorder_ofosegs(int nofosegs, FAR struct tcp_ofoseg_s *ofosegs); + +/**************************************************************************** + * Name: tcp_cc_init + * + * Description: + * Initialize the congestion control variables, cwnd, ssthresh and dupacks. + * The function is called on starting a new connection. + * + * Input Parameters: + * conn - The TCP connection of interest + * + * Returned Value: + * None + * + * Assumptions: + * The normal user level code is calling the connect/accept to start a new + * connection. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_CC_NEWRENO +void tcp_cc_init(FAR struct tcp_conn_s *conn); + +/**************************************************************************** + * Name: tcp_cc_update + * + * Description: + * Update the congestion control variables when recieve the SYNACK/ACK + * packet from the peer in the connection phase. + * + * Input Parameters: + * conn - The TCP connection of interest + * tcp - The TCP header. + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +void tcp_cc_update(FAR struct tcp_conn_s *conn, FAR struct tcp_hdr_s *tcp); + +/**************************************************************************** + * Name: tcp_cc_recv_ack + * + * Description: + * Update congestion control variables + * + * Input Parameters: + * conn - The TCP connection of interest + * tcp - The TCP header. + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +void tcp_cc_recv_ack(FAR struct tcp_conn_s *conn, FAR struct tcp_hdr_s *tcp); +#endif + +#ifdef __cplusplus +} +#endif + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags); + +#endif /* NET_TCP_HAVE_STACK */ +#endif /* __NET_TCP_TCP_H */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_appsend.c b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_appsend.c new file mode 100644 index 00000000..97b1cf16 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_appsend.c @@ -0,0 +1,354 @@ +/**************************************************************************** + * net/tcp/tcp_appsend.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include + +#include +#include +#include + +#include "devif/devif.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_appsend + * + * Description: + * Handle application or TCP protocol response. If this function is called + * with dev->d_sndlen > 0, then this is an application attempting to send + * packet. + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP connection structure holding connection information + * result - App result event sent + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +void tcp_appsend(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, + uint16_t result) +{ + uint8_t hdrlen; + + ninfo("result: %04x d_sndlen: %d conn->tx_unacked: %" PRId32 "\n", + result, dev->d_sndlen, (uint32_t)conn->tx_unacked); + + /* Need to update the recv window? */ + + if (tcp_should_send_recvwindow(conn)) + { + result |= TCP_SNDACK; +#ifdef CONFIG_NET_TCP_DELAYED_ACK + conn->rx_unackseg = 0; +#endif + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Did the caller request that an ACK be sent? */ + + else if ((result & TCP_SNDACK) != 0) + { + /* Yes.. Handle delayed acknowledgments */ + + /* Reset the ACK timer in any event. */ + + conn->rx_acktimer = 0; + + /* Per RFC 1122: "...in a stream of full-sized segments there + * SHOULD be an ACK for at least every second segment." + * + * NOTES: + * 1. If there is a data payload or other flags to be sent with the + * outgoing packet, then we may as well include the ACK too. + * 2. The RFC refers to full-size segments. It is not clear what + * "full-size" means. Does that mean that the payload is the size + * of the MSS? Payload size is not considered other there being + * a payload or or not. Should there be some special action for + * small payloads of size < MSS? + * 3. Experimentation shows that Windows and Linux behave somewhat + * differently; they delay the ACKs for many more segments (6 or + * more). Delaying for more segments would provide less network + * traffic and better performance but seems non-compliant. + */ + + if (conn->rx_unackseg > 0 || dev->d_sndlen > 0 || + result != TCP_SNDACK) + { + /* Reset the delayed ACK state and send the ACK with this packet. */ + + conn->rx_unackseg = 0; + } + else + { + /* This is only an ACK and there is no pending delayed ACK and + * no TX data is being sent. Indicate that there is one un-ACKed + * segment and don't send anything now. + */ + + conn->rx_unackseg = 1; + return; + } + } + + /* If there are data to be sent in the same direction as the ACK before + * the second data packet is received and the delay timer expires, the ACK + * is piggybacked with the data segment and sent immediately. + */ + + else if (dev->d_sndlen > 0 && conn->rx_unackseg > 0) + { + result |= TCP_SNDACK; + conn->rx_unackseg = 0; + } +#endif + + /* Get the IP header length associated with the IP domain configured for + * this TCP connection. + */ + + hdrlen = tcpip_hdrsize(conn); + + /* Check If the device went down */ + + if ((result & NETDEV_DOWN) != 0) + { + /* If so, make sure that the connection is marked closed + * and do not try to send anything. + */ + + dev->d_sndlen = 0; + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: NETDEV_DOWN\n"); + } + + /* Check for connection aborted */ + + else if ((result & TCP_ABORT) != 0) + { + dev->d_sndlen = 0; + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + } + + /* Check for connection closed */ + + else if ((result & TCP_CLOSE) != 0) + { + conn->tcpstateflags = TCP_FIN_WAIT_1; + conn->tx_unacked = 1; + conn->nrtx = 0; +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + conn->sndseq_max = tcp_getsequence(conn->sndseq) + 1; +#endif + ninfo("TCP state: TCP_FIN_WAIT_1\n"); + + dev->d_sndlen = 0; + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + } + + /* None of the above */ + + else + { + /* The application cannot send more than what is allowed by the + * MSS (the minimum of the MSS and the available window). + */ + + DEBUGASSERT(dev->d_sndlen <= conn->mss); + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) || defined(CONFIG_NET_SENDFILE) + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + if (conn->sendfile) + { +#endif + + /* If d_sndlen > 0, the application has data to be sent. */ + + if (dev->d_sndlen > 0) + { + /* Remember how much data we send out now so that we know + * when everything has been acknowledged. Just increment the + * amount of data sent. This will be needed in sequence number + * calculations and we know that this is not a re-transmission. + * Retransmissions do not go through this path. + */ + + conn->tx_unacked += dev->d_sndlen; + } + + conn->nrtx = 0; + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + } +#endif + +#endif + + /* Then handle the rest of the operation just as for the rexmit case */ + + tcp_rexmit(dev, conn, result); + } +} + +/**************************************************************************** + * Name: tcp_rexmit + * + * Description: + * Handle application retransmission + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP connection structure holding connection information + * result - App result event sent + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +void tcp_rexmit(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, + uint16_t result) +{ + uint8_t hdrlen; + + ninfo("result: %04x d_sndlen: %d conn->tx_unacked: %" PRId32 "\n", + result, dev->d_sndlen, (uint32_t)conn->tx_unacked); + + /* Get the IP header length associated with the IP domain configured for + * this TCP connection. + */ + + hdrlen = tcpip_hdrsize(conn); + + /* If the application has data to be sent, or if the incoming packet had + * new data in it, we must send out a packet. + */ + +#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) && defined(CONFIG_NET_SENDFILE) + if (conn->sendfile) +#endif + { +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) || defined(CONFIG_NET_SENDFILE) + if ((result & TCP_REXMIT) != 0 && + dev->d_sndlen > 0 && conn->tx_unacked > 0 && + conn->rexmit_seq > 0) + { + uint32_t saveseq; + + /* According to RFC 6298 (5.4), retransmit the earliest segment + * that has not been acknowledged by the TCP receiver. + */ + + saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, conn->rexmit_seq); + + tcp_send(dev, conn, TCP_ACK | TCP_PSH, dev->d_sndlen + hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); + + return; + } +#endif + } + +#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) + if (dev->d_sndlen > 0) +#else + if (dev->d_sndlen > 0 && conn->tx_unacked > 0) +#endif + { + uint32_t seq; + + /* We always set the ACK flag in response packets adding the length of + * the IP and TCP headers. + */ + + tcp_send(dev, conn, TCP_ACK | TCP_PSH, dev->d_sndlen + hdrlen); + + /* Advance sndseq */ + + seq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, seq + dev->d_sndlen); + } + + /* If there is no data to send, just send out a pure ACK if one is + * requested. + */ + + else if ((result & TCP_SNDACK) != 0) + { + tcp_send(dev, conn, TCP_ACK, hdrlen); + } + + /* There is nothing to do -- drop the packet */ + + else + { + dev->d_len = 0; + } +} +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_callback.c b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_callback.c new file mode 100644 index 00000000..4890b3be --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_callback.c @@ -0,0 +1,444 @@ +/**************************************************************************** + * net/tcp/tcp_callback.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "devif/devif.h" +#include "tcp/tcp.h" +#include "utils/utils.h" + +#ifdef NET_TCP_HAVE_STACK + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_data_event + * + * Description: + * Handle data that is not accepted by the application because there is no + * listener in place ready to receive the data. + * + * Assumptions: + * - The caller has checked that TCP_NEWDATA is set in flags and that is no + * other handler available to process the incoming data. + * - This function must be called with the network locked. + * + ****************************************************************************/ + +static inline uint16_t +tcp_data_event(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, + uint16_t flags) +{ + uint16_t recvlen; + + /* Assume that we will ACK the data. The data will be ACKed if it is + * placed in the read-ahead buffer -OR- if it zero length + */ + + flags = (flags & ~TCP_NEWDATA) | TCP_SNDACK; + + /* Is there new data? With non-zero length? (Certain connection events + * can have zero-length with TCP_NEWDATA set just to cause an ACK). + */ + + if (dev->d_len > 0) + { + ninfo("No listener on connection\n"); + + /* Save as the packet data as in the read-ahead buffer. NOTE that + * partial packets will not be buffered. + */ + + recvlen = tcp_datahandler(dev, conn, + (dev->d_appdata - dev->d_iob->io_data) - + dev->d_iob->io_offset); + + net_incr32(conn->rcvseq, recvlen); + } + + /* In any event, the new data has now been handled */ + + dev->d_len = 0; + return flags; +} + +/**************************************************************************** + * Name: tcp_ofoseg_data_event + * + * Description: + * Handle out-of-order segment to readahead poll. + * + * Assumptions: + * - This function must be called with the network locked. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_OUT_OF_ORDER +static uint16_t tcp_ofoseg_data_event(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn, + uint16_t flags) +{ + FAR struct tcp_ofoseg_s *seg; + uint32_t rcvseq; + int i = 0; + + /* Assume that we will ACK the data. The data will be ACKed if it is + * placed in the read-ahead buffer -OR- if it zero length + */ + + flags |= TCP_SNDACK; + + /* Get the receive sequence number */ + + rcvseq = tcp_getsequence(conn->rcvseq); + + ninfo("TCP OFOSEG rcvseq [%" PRIu32 "]\n", rcvseq); + + /* Foreach out-of-order segments */ + + while (i < conn->nofosegs) + { + seg = &conn->ofosegs[i]; + + /* rcvseq -->| + * ofoseg |------| + */ + + if (rcvseq == seg->left) + { + ninfo("TCP OFOSEG input [%" PRIu32 " : %" PRIu32 " : %u]\n", + seg->left, seg->right, seg->data->io_pktlen); + rcvseq = TCP_SEQ_ADD(rcvseq, + seg->data->io_pktlen); + net_incr32(conn->rcvseq, seg->data->io_pktlen); + net_iob_concat(&conn->readahead, &seg->data); + } + else if (TCP_SEQ_GT(rcvseq, seg->left)) + { + /* rcvseq -->| + * ofoseg |------| + */ + + if (TCP_SEQ_GTE(rcvseq, seg->right)) + { + /* Remove stale segments */ + + iob_free_chain(seg->data); + seg->data = NULL; + } + + /* rcvseq -->| + * ofoseg |------| + */ + + else + { + seg->data = + iob_trimhead(seg->data, + TCP_SEQ_SUB(rcvseq, seg->left)); + seg->left = rcvseq; + if (seg->data != NULL) + { + ninfo("TCP OFOSEG input " + "[%" PRIu32 " : %" PRIu32 " : %u]\n", + seg->left, seg->right, seg->data->io_pktlen); + rcvseq = TCP_SEQ_ADD(rcvseq, + seg->data->io_pktlen); + net_incr32(conn->rcvseq, seg->data->io_pktlen); + net_iob_concat(&conn->readahead, &seg->data); + } + } + } + + /* Rebuild out-of-order pool if segment is consumed */ + + if (seg->data == NULL) + { + for (; i < conn->nofosegs - 1; i++) + { + conn->ofosegs[i] = conn->ofosegs[i + 1]; + } + + conn->nofosegs--; + + /* Try segments again */ + + i = 0; + } + else + { + i++; + } + } + + return flags; +} +#endif /* CONFIG_NET_TCP_OUT_OF_ORDER */ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_ofoseg_bufsize + * + * Description: + * Calculate the pending size of out-of-order buffer + * + * Input Parameters: + * conn - The TCP connection of interest + * + * Returned Value: + * Total size of out-of-order buffer + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_OUT_OF_ORDER +int tcp_ofoseg_bufsize(FAR struct tcp_conn_s *conn) +{ + int total = 0; + int i; + + for (i = 0; i < conn->nofosegs; i++) + { + total += conn->ofosegs[i].data->io_pktlen; + } + + return total; +} +#endif /* CONFIG_NET_TCP_OUT_OF_ORDER */ + +/**************************************************************************** + * Name: tcp_callback + * + * Description: + * Inform the application holding the TCP socket of a change in state. + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +uint16_t tcp_callback(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn, uint16_t flags) +{ +#if defined(CONFIG_NET_TCP_NOTIFIER) || defined(CONFIG_NET_TCP_OUT_OF_ORDER) + uint16_t orig = flags; +#endif + + /* Prepare device buffer */ + + if (dev->d_iob == NULL && netdev_iob_prepare(dev, false, 0) != OK) + { + return 0; + } + + /* Preserve the TCP_ACKDATA, TCP_CLOSE, and TCP_ABORT in the response. + * These is needed by the network to handle responses and buffer state. + * The TCP_NEWDATA indication will trigger the ACK response, but must be + * explicitly set in the callback. + */ + + ninfo("flags: %04x\n", flags); + + /* Perform the data callback. When a data callback is executed from + * 'list', the input flags are normally returned, however, the + * implementation may set one of the following: + * + * TCP_CLOSE - Gracefully close the current connection + * TCP_ABORT - Abort (reset) the current connection on an error that + * prevents TCP_CLOSE from working. + * + * And/Or set/clear the following: + * + * TCP_NEWDATA - May be cleared to indicate that the data was consumed + * and that no further process of the new data should be + * attempted. + * TCP_SNDACK - If TCP_NEWDATA is cleared, then TCP_SNDACK may be set + * to indicate that an ACK should be included in the + * response. (In TCP_NEWDATA is cleared but TCP_SNDACK is + * not set, then dev->d_len should also be cleared). + */ + + flags = devif_conn_event(dev, flags, conn->sconn.list); + + /* There may be no new data handler in place at them moment that the new + * incoming data is received. If the new incoming data was not handled, + * then either (1) put the unhandled incoming data in the read-ahead + * buffer (if enabled) or (2) suppress the ACK to the data in the hope + * that it will be re-transmitted at a better time. + */ + + if ((flags & TCP_NEWDATA) != 0) + { + /* Data was not handled.. dispose of it appropriately */ + + flags = tcp_data_event(dev, conn, flags); + } + +#ifdef CONFIG_NET_TCP_OUT_OF_ORDER + if ((orig & TCP_NEWDATA) != 0 && conn->nofosegs > 0) + { + /* Try out-of-order pool if new data is coming */ + + flags = tcp_ofoseg_data_event(dev, conn, flags); + } +#endif + + /* Check if there is a connection-related event and a connection + * callback. + */ + + if ((flags & TCP_CONN_EVENTS) != 0) + { + /* Perform the callback disconnect callbacks */ + + flags = devif_conn_event(dev, flags, conn->connevents); + } + +#ifdef CONFIG_NET_TCP_NOTIFIER + /* Provide notification(s) if the TCP connection has been lost. */ + + if ((orig & TCP_DISCONN_EVENTS) != 0) + { + tcp_disconnect_signal(conn); + } +#endif + + /* Re-prepare the device buffer if d_iob is consumed by the stack */ + + if (dev->d_iob == NULL) + { + netdev_iob_prepare(dev, false, 0); + } + + return flags; +} + +/**************************************************************************** + * Name: tcp_datahandler + * + * Description: + * Handle data that is not accepted by the application. This may be called + * either (1) from the data receive logic if it cannot buffer the data, or + * (2) from the TCP event logic is there is no listener in place ready to + * receive the data. + * + * Input Parameters: + * conn - A pointer to the TCP connection structure + * buffer - A pointer to the buffer to be copied to the read-ahead + * buffers + * buflen - The number of bytes to copy to the read-ahead buffer. + * + * Returned Value: + * The number of bytes actually buffered is returned. This will be either + * zero or equal to buflen; partial packets are not buffered. + * + * Assumptions: + * - The caller has checked that TCP_NEWDATA is set in flags and that is no + * other handler available to process the incoming data. + * - This function must be called with the network locked. + * + ****************************************************************************/ + +uint16_t tcp_datahandler(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn, + uint16_t offset) +{ + FAR struct iob_s *iob = dev->d_iob; + uint16_t buflen; + + if (offset > 0) + { + /* Remove 'bufoff' bytes from the beginning of the input I/O chain */ + + iob = iob_trimhead(iob, offset); + } + + /* Trim tail if l3/l4 header has been removed */ + + if (dev->d_len < iob->io_pktlen) + { + iob = iob_trimtail(iob, iob->io_pktlen - dev->d_len); + } + + buflen = iob->io_pktlen; + + /* Concat the iob to readahead */ + + net_iob_concat(&conn->readahead, &iob); + + /* Clear device buffer */ + + netdev_iob_clear(dev); + +#ifdef CONFIG_NET_TCP_NOTIFIER + /* Provide notification(s) that additional TCP read-ahead data is + * available. + */ + + tcp_readahead_signal(conn); +#endif + + ninfo("Buffered %" PRIu16 " bytes\n", buflen); + return buflen; +} + +/**************************************************************************** + * Name: tcp_callback_cleanup + * + * Description: + * Cleanup data and cb when thread is canceled. + * + * Input Parameters: + * arg - A pointer with conn and callback struct. + * + ****************************************************************************/ + +void tcp_callback_cleanup(FAR void *arg) +{ + FAR struct tcp_callback_s *cb = (FAR struct tcp_callback_s *)arg; + + nerr("ERROR: pthread is being canceled, need to cleanup cb\n"); + tcp_callback_free(cb->tc_conn, cb->tc_cb); + nxsem_destroy(cb->tc_sem); +} +#endif /* NET_TCP_HAVE_STACK */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_devpoll.c b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_devpoll.c new file mode 100644 index 00000000..741b13ff --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_devpoll.c @@ -0,0 +1,131 @@ +/**************************************************************************** + * net/tcp/tcp_devpoll.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2009, 2016-2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include + +#include +#include +#include + +#include "devif/devif.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_poll + * + * Description: + * Poll a TCP connection structure for availability of TX data + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * It is called with the network locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_poll(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + uint16_t result; + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Prepare device buffer */ + + if (netdev_iob_prepare(dev, false, 0) != OK) + { + return; + } + + /* Discard any currently buffered data */ + + if (conn->timeout) + { + conn->timeout = false; + tcp_timer(dev, conn); + return; + } + + dev->d_len = 0; + dev->d_sndlen = 0; + + /* Verify that the connection is established. */ + + if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { + /* Set up for the callback. We can't know in advance if the + * application is going to send a IPv4 or an IPv6 packet, so this + * setup may not actually be used. + */ + + tcp_ip_select(conn); + + /* Perform the callback */ + + result = tcp_callback(dev, conn, TCP_POLL); + + /* Handle the callback response */ + + tcp_appsend(dev, conn, result); + } +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_monitor.c b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_monitor.c new file mode 100644 index 00000000..bccd8734 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_monitor.c @@ -0,0 +1,400 @@ +/**************************************************************************** + * net/tcp/tcp_monitor.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include + +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +#ifdef NET_TCP_HAVE_STACK + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void tcp_close_connection(FAR struct tcp_conn_s *conn, + uint16_t flags); +static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev, + FAR void *pvpriv, uint16_t flags); + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_close_connection + * + * Description: + * Called when a loss-of-connection event has occurred. + * + * Input Parameters: + * conn The TCP connection structure + * flags Set of connection events events + * + * Returned Value: + * None + * + * Assumptions: + * The caller holds the network lock. + * + ****************************************************************************/ + +static void tcp_close_connection(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + /* These loss-of-connection events may be reported: + * + * TCP_CLOSE: The remote host has closed the connection + * TCP_ABORT: The remote host has aborted the connection + * TCP_TIMEDOUT: Connection aborted due to too many retransmissions. + * NETDEV_DOWN: The network device went down + * + * And we need to set these two socket status bits appropriately: + * + * _SF_CONNECTED==1 && _SF_CLOSED==0 - the socket is connected + * _SF_CONNECTED==0 && _SF_CLOSED==1 - the socket was gracefully + * disconnected + * _SF_CONNECTED==0 && _SF_CLOSED==0 - the socket was rudely disconnected + */ + + if ((flags & TCP_CLOSE) != 0) + { + /* The peer gracefully closed the connection. Marking the + * connection as disconnected will suppress some subsequent + * ENOTCONN errors from receive. A graceful disconnection is + * not handle as an error but as an "end-of-file" + */ + + conn->sconn.s_flags &= ~_SF_CONNECTED; + conn->sconn.s_flags |= _SF_CLOSED; + } + else if ((flags & (TCP_ABORT | TCP_TIMEDOUT | NETDEV_DOWN)) != 0) + { + /* The loss of connection was less than graceful. This will + * (eventually) be reported as an ENOTCONN error. + */ + + conn->sconn.s_flags &= ~(_SF_CONNECTED | _SF_CLOSED); + } +} + +/**************************************************************************** + * Name: tcp_monitor_event + * + * Description: + * Some connection related event has occurred + * + * Input Parameters: + * dev The device which as active when the event was detected. + * pvpriv An instance of struct tcp_conn_s cast to void* + * flags Set of events describing why the callback was invoked + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +static uint16_t tcp_monitor_event(FAR struct net_driver_s *dev, + FAR void *pvpriv, uint16_t flags) +{ + FAR struct tcp_conn_s *conn = pvpriv; + + if (conn != NULL) + { + ninfo("flags: %04x s_flags: %02x\n", flags, conn->sconn.s_flags); + + /* TCP_DISCONN_EVENTS: TCP_CLOSE, TCP_ABORT, TCP_TIMEDOUT, or + * NETDEV_DOWN. All loss-of-connection events. + */ + + if ((flags & TCP_DISCONN_EVENTS) != 0) + { + tcp_close_connection(conn, flags); + } + + /* TCP_CONNECTED: The socket is successfully connected */ + + else if ((flags & TCP_CONNECTED) != 0) + { +#if 0 /* REVISIT: Assertion fires. Why? */ + FAR struct tcp_conn_s *conn = psock->s_conn; + + /* Make sure that this is the device bound to the connection */ + + DEBUGASSERT(conn->dev == NULL || conn->dev == dev); + conn->dev = dev; +#endif + + /* If there is no local address assigned to the socket (perhaps + * because it was INADDR_ANY), then assign it the address of the + * connecting device. + * + * TODO: Implement this. + */ + + /* Clear the socket error */ + + _SO_CONN_SETERRNO(conn, OK); + + /* Indicate that the socket is now connected */ + + conn->sconn.s_flags |= (_SF_BOUND | _SF_CONNECTED); + conn->sconn.s_flags &= ~_SF_CLOSED; + } + } + + return flags; +} + +/**************************************************************************** + * Name: tcp_shutdown_monitor + * + * Description: + * Stop monitoring TCP connection changes for a given socket. + * + * Input Parameters: + * conn - The TCP connection of interest + * flags - Indicates the type of shutdown. TCP_CLOSE or TCP_ABORT + * + * Returned Value: + * None + * + * Assumptions: + * The caller holds the network lock (if not, it will be locked momentarily + * by this function). + * + ****************************************************************************/ + +static void tcp_shutdown_monitor(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + /* Perform callbacks to assure that all sockets, including dup'ed copies, + * are informed of the loss of connection event. + */ + + net_lock(); + + /* Free all allocated connection event callback structures */ + + while (conn->connevents != NULL) + { + devif_conn_callback_free(conn->dev, conn->connevents, + &conn->connevents, + &conn->connevents_tail); + } + + net_unlock(); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_start_monitor + * + * Description: + * Set up to receive TCP connection state changes for a given socket + * + * Input Parameters: + * psock - The socket of interest + * + * Returned Value: + * On success, tcp_start_monitor returns OK; On any failure, + * tcp_start_monitor will return a negated errno value. The only failure + * that can occur is if the socket has already been closed and, in this + * case, -ENOTCONN is returned. + * + * Assumptions: + * The caller holds the network lock (if not, it will be locked momentarily + * by this function). + * + ****************************************************************************/ + +int tcp_start_monitor(FAR struct socket *psock) +{ + FAR struct devif_callback_s *cb; + FAR struct tcp_conn_s *conn; + bool nonblock_conn; + + conn = psock->s_conn; + + net_lock(); + + /* Non-blocking connection ? */ + + nonblock_conn = (conn->tcpstateflags == TCP_SYN_SENT && + _SS_ISNONBLOCK(conn->sconn.s_flags)); + + /* Check if the connection has already been closed before any callbacks + * have been registered. (Maybe the connection is lost before accept has + * registered the monitoring callback.) + */ + + if (!(conn->tcpstateflags == TCP_ESTABLISHED || + conn->tcpstateflags == TCP_SYN_RCVD || nonblock_conn)) + { + /* Invoke the TCP_CLOSE connection event now */ + + tcp_shutdown_monitor(conn, TCP_CLOSE); + + /* If the peer close the connection before we call accept, + * in order to allow user to read the readahead data, + * return OK. + */ + + if (conn->tcpstateflags == TCP_CLOSED || + conn->tcpstateflags == TCP_LAST_ACK) + { + net_unlock(); + return OK; + } + + /* And return -ENOTCONN to indicate the monitor was not started + * because the socket was already disconnected. + */ + + net_unlock(); + return -ENOTCONN; + } + + /* Allocate a callback structure that we will use to get callbacks if + * the network goes down. + */ + + cb = devif_callback_alloc(conn->dev, + &conn->connevents, + &conn->connevents_tail); + if (cb != NULL) + { + cb->event = tcp_monitor_event; + cb->priv = (FAR void *)conn; + cb->flags = TCP_DISCONN_EVENTS; + + /* Monitor the connected event */ + + if (nonblock_conn) + { + cb->flags |= TCP_CONNECTED; + } + } + + net_unlock(); + return OK; +} + +/**************************************************************************** + * Name: tcp_stop_monitor + * + * Description: + * Stop monitoring TCP connection changes for a sockets associated with + * a given TCP connection structure. + * + * Input Parameters: + * conn - The TCP connection of interest + * flags Set of disconnection events + * + * Returned Value: + * None + * + * Assumptions: + * The caller holds the network lock (if not, it will be locked momentarily + * by this function). + * + ****************************************************************************/ + +void tcp_stop_monitor(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + DEBUGASSERT(conn != NULL); + + /* Stop the network monitor */ + + tcp_shutdown_monitor(conn, flags); +} + +/**************************************************************************** + * Name: tcp_lost_connection + * + * Description: + * Called when a loss-of-connection event has been detected by network + * event handling logic. Perform operations like tcp_stop_monitor but (1) + * explicitly mark this socket and (2) disable further callbacks the to the + * event handler. + * + * Input Parameters: + * conn - The TCP connection of interest + * cb - devif callback structure + * flags - Set of connection events events + * + * Returned Value: + * None + * + * Assumptions: + * The caller holds the network lock (if not, it will be locked momentarily + * by this function). + * + ****************************************************************************/ + +void tcp_lost_connection(FAR struct tcp_conn_s *conn, + FAR struct devif_callback_s *cb, uint16_t flags) +{ + DEBUGASSERT(conn != NULL); + + /* Nullify the callback structure so that recursive callbacks are not + * received by the event handler due to disconnection processing. + * + * NOTE: In a configuration with CONFIG_NET_TCP_WRITE_BUFFERS=y, + * the "semi-permanent" callback structure may have already been + * nullified. + */ + + if (cb != NULL) + { + cb->flags = 0; + cb->priv = NULL; + cb->event = NULL; + } + + /* Make sure that this socket is explicitly marked. It may not get a + * callback due to the above nullification. + */ + + tcp_close_connection(conn, flags); + + /* Then stop the network monitor for all sockets. */ + + tcp_shutdown_monitor(conn, flags); +} + +#endif /* NET_TCP_HAVE_STACK */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_seqno.c b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_seqno.c new file mode 100644 index 00000000..6ab40a9f --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_seqno.c @@ -0,0 +1,255 @@ +/**************************************************************************** + * net/tcp/tcp_seqno.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Large parts of this file were leveraged from uIP logic: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include + +#include +#include +#include + +#include "devif/devif.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* These fields are used to generate initial TCP sequence numbers */ + +#ifdef CONFIG_NET_TCP_ISN_RFC6528 +/* RFC 6528, Section 3: Key lengths of 128 bits should be adequate. */ + +static uint32_t g_tcp_isnkey[4]; +#else +static uint32_t g_tcpsequence; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_isn_rfc6528 + * + * Description: + * Calculate the initial sequence number described in RFC 6528. + * ISN = M + F(localip, localport, remoteip, remoteport, secretkey) + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_ISN_RFC6528 +static uint32_t tcp_isn_rfc6528(FAR struct tcp_conn_s *conn) +{ + const size_t addrlen = net_ip_domain_select(conn->domain, + sizeof(in_addr_t), sizeof(net_ipv6addr_t)); + MD5_CTX ctx; + uint32_t digest[MD5_DIGEST_LENGTH / 4]; + uint32_t m; + + /* Make sure we have a secret key */ + + if (g_tcp_isnkey[0] == 0) + { + arc4random_buf(g_tcp_isnkey, sizeof(g_tcp_isnkey)); + } + + /* M is the 4 microsecond timer */ + + m = TICK2USEC(clock_systime_ticks()) / 4; + + /* F() is suggested to be MD5 */ + + md5init(&ctx); + + /* Calculate F(localip, localport, remoteip, remoteport, secretkey) */ + + md5update(&ctx, net_ip_binding_laddr(&conn->u, conn->domain), addrlen); + md5update(&ctx, &conn->lport, sizeof(conn->lport)); + md5update(&ctx, net_ip_binding_raddr(&conn->u, conn->domain), addrlen); + md5update(&ctx, &conn->rport, sizeof(conn->rport)); + md5update(&ctx, g_tcp_isnkey, sizeof(g_tcp_isnkey)); + + md5final((FAR uint8_t *)digest, &ctx); + + /* ISN = M + F(localip, localport, remoteip, remoteport, secretkey) */ + + return m + digest[0]; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_setsequence + * + * Description: + * Set the TCP/IP sequence number + * + * Assumptions: + * This function must be called with the network locked if seqno refers + * to a shared, global resource. + * + ****************************************************************************/ + +void tcp_setsequence(FAR uint8_t *seqno, uint32_t value) +{ + /* Copy the sequence number in network (big-endian) order */ + + *seqno++ = value >> 24; + *seqno++ = (value >> 16) & 0xff; + *seqno++ = (value >> 8) & 0xff; + *seqno = value & 0xff; +} + +/**************************************************************************** + * Name: tcp_getsequence + * + * Description: + * Get the TCP/IP sequence number + * + * Assumptions: + * This function must be called with the network locked if seqno refers + * to a shared, global resource. + * + ****************************************************************************/ + +uint32_t tcp_getsequence(FAR uint8_t *seqno) +{ + uint32_t value; + + /* Combine the sequence number from network (big-endian) order */ + + value = (uint32_t)seqno[0] << 24 | + (uint32_t)seqno[1] << 16 | + (uint32_t)seqno[2] << 8 | + (uint32_t)seqno[3]; + return value; +} + +/**************************************************************************** + * Name: tcp_addsequence + * + * Description: + * Add the length to get the next TCP sequence number. + * + * Assumptions: + * This function must be called with the network locked if seqno refers + * to a shared, global resource. + * + ****************************************************************************/ + +uint32_t tcp_addsequence(FAR uint8_t *seqno, uint16_t len) +{ + return tcp_getsequence(seqno) + (uint32_t)len; +} + +/**************************************************************************** + * Name: tcp_initsequence + * + * Description: + * Set the (initial) the TCP/IP sequence number when a TCP connection is + * established. + * + * Assumptions: + * This function must be called with the network locked if seqno refers + * to a shared, global resource. + * + ****************************************************************************/ + +void tcp_initsequence(FAR struct tcp_conn_s *conn) +{ +#ifdef CONFIG_NET_TCP_ISN_RFC6528 + tcp_setsequence(conn->sndseq, tcp_isn_rfc6528(conn)); +#else + /* If g_tcpsequence is already initialized, just copy it */ + + if (g_tcpsequence == 0) + { + /* Get a random TCP sequence number */ + + arc4random_buf(&g_tcpsequence, sizeof(uint32_t)); + + /* Use about half of allowed values */ + + g_tcpsequence = g_tcpsequence % 2000000000; + + /* If the random value is "small" increase it */ + + if (g_tcpsequence < 1000000000) + { + g_tcpsequence += 1000000000; + } + } + + tcp_setsequence(conn->sndseq, g_tcpsequence); +#endif +} + +/**************************************************************************** + * Name: tcp_nextsequence + * + * Description: + * Increment the TCP/IP sequence number + * + * Assumptions: + * This function must be called with the network locked. + * + ****************************************************************************/ + +void tcp_nextsequence(void) +{ +#ifndef CONFIG_NET_TCP_ISN_RFC6528 + g_tcpsequence++; +#endif +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_timer.c new file mode 100644 index 00000000..568743e2 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/net/tcp/tcp_timer.c @@ -0,0 +1,842 @@ +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->timer = TCP_RTO << (conn->nrtx > 4 ? 4: conn->nrtx); + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port.h b/tasks/net-nuttx-tcp-retry-closure/template/port.h new file mode 100644 index 00000000..ff5dde03 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port.h @@ -0,0 +1,154 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Host-side port declarations for the fixed, single-connection laboratory. + * Structure members and protocol constants follow the frozen public headers. + */ +#ifndef TCP_LAB_PORT_H +#define TCP_LAB_PORT_H +#include +#include +#include +#include +#include +#include +#include +#include +#define FAR +#define CONFIG_NET 1 +#define CONFIG_NET_TCP 1 +#define CONFIG_NET_TCP_KEEPALIVE 1 +#define CONFIG_NET_TCP_DELAYED_ACK 1 +#define CONFIG_NET_TCP_MAXRTX 8 +#define CONFIG_NET_TCP_MAXSYNRTX 5 +#define CONFIG_NET_PREALLOC_DEVIF_CALLBACKS 16 +#define CONFIG_NET_ALLOC_DEVIF_CALLBACKS 0 +#define NET_TCP_HAVE_STACK 1 +#define TCP_RTO 3 +#define TCP_RTO_MIN 1 +#define TCP_RTO_MAX 240 +#define TCP_MAXRTX 8 +#define TCP_MAXSYNRTX 5 +#define TCP_STATE_MASK 0x0f +#define TCP_CLOSED 0 +#define TCP_SYN_RCVD 2 +#define TCP_SYN_SENT 3 +#define TCP_ESTABLISHED 4 +#define TCP_FIN_WAIT_1 5 +#define TCP_FIN_WAIT_2 6 +#define TCP_CLOSING 7 +#define TCP_TIME_WAIT 8 +#define TCP_LAST_ACK 9 +#define TCP_FIN 1 +#define TCP_SYN 2 +#define TCP_RST 4 +#define TCP_PSH 8 +#define TCP_ACK 16 +#define TCP_ACKDATA (1<<0) +#define TCP_NEWDATA (1<<1) +#define TCP_SNDACK (1<<2) +#define TCP_REXMIT (1<<3) +#define TCP_POLL (1<<4) +#define TCP_CLOSE (1<<6) +#define TCP_ABORT (1<<7) +#define TCP_CONNECTED (1<<8) +#define TCP_TIMEDOUT (1<<9) +#define NETDEV_DOWN (1<<12) +#define TCP_DISCONN_EVENTS (TCP_CLOSE|TCP_ABORT|TCP_TIMEDOUT|NETDEV_DOWN) +#define TCP_CONN_EVENTS (TCP_DISCONN_EVENTS|TCP_CONNECTED) +#define DEVPOLL_MASK (7<<13) +#define _SF_BOUND 1 +#define _SF_CONNECTED 2 +#define _SF_CLOSED 4 +#define _SS_ISNONBLOCK(f) ((f)&8) +#define _SO_CONN_SETERRNO(c,v) ((void)(c),(void)(v)) +#define OK 0 +#define IFF_UP 1 +#define DEBUGASSERT(x) assert(x) +#define ninfo(...) ((void)0) +#define nerr(...) ((void)0) +#define nwarn(...) ((void)0) +#define MIN(a,b) ((a)<(b)?(a):(b)) +#define MAX(a,b) ((a)>(b)?(a):(b)) +#define DSEC_PER_HSEC 5 +#define LPWORK 0 +#define HSEC2TICK(x) ((x)*5) +#define TICK2HSEC(x) ((x)/5) +#define TICK2USEC(x) ((x)*100000) +typedef long sclock_t; +struct net_driver_s; +struct devif_callback_s { + struct devif_callback_s *nxtconn,*prevconn,*nxtdev; + uint16_t flags; uint8_t free_flags; + uint16_t (*event)(struct net_driver_s *,void *,uint16_t); + void *priv; +}; +struct iob_s { uint8_t io_data[4096]; int io_offset; uint16_t io_pktlen; }; +struct work_s { bool pending; long due; void (*worker)(void *); void *arg; }; +struct net_driver_s { + uint16_t d_len,d_sndlen; uint8_t *d_appdata; struct iob_s *d_iob; + unsigned int d_flags; + struct devif_callback_s *d_devcb,*d_conncb,*d_conncb_tail; +}; +struct tcp_conn_s { + struct { struct devif_callback_s *list,*tail; uint8_t s_flags; } sconn; + struct net_driver_s *dev; + struct devif_callback_s *connevents,*connevents_tail; + struct work_s work; + uint8_t rto,timer,nrtx,tcpstateflags,rx_unackseg,rx_acktimer; + bool timeout,keepalive,zero_probe; + uint32_t keeptimer,keepidle,keepintvl; + uint8_t keepcnt,keepretries; + int32_t tx_unacked; + uint8_t sndseq[4],rcvseq[4]; + uint32_t rexmit_seq; + uint16_t mss,lport; + int crefs,u; + struct iob_s *readahead; +}; +struct socket { struct tcp_conn_s *s_conn; }; +struct tcp_callback_s { struct tcp_conn_s *tc_conn; struct devif_callback_s *tc_cb; void *tc_sem; }; +void net_lock(void); void net_unlock(void); +bool netdev_verify(struct net_driver_s *); +void devif_callback_init(void); +struct devif_callback_s *devif_callback_alloc(struct net_driver_s *,struct devif_callback_s **,struct devif_callback_s **); +void devif_conn_callback_free(struct net_driver_s *,struct devif_callback_s *,struct devif_callback_s **,struct devif_callback_s **); +uint16_t devif_conn_event(struct net_driver_s *,uint16_t,struct devif_callback_s *); +int tcp_start_monitor(struct socket *); +void tcp_stop_monitor(struct tcp_conn_s *,uint16_t); +void tcp_lost_connection(struct tcp_conn_s *,struct devif_callback_s *,uint16_t); +uint16_t tcp_callback(struct net_driver_s *,struct tcp_conn_s *,uint16_t); +uint16_t tcp_datahandler(struct net_driver_s *,struct tcp_conn_s *,uint16_t); +void tcp_poll(struct net_driver_s *,struct tcp_conn_s *); +void tcp_timer(struct net_driver_s *,struct tcp_conn_s *); +void tcp_appsend(struct net_driver_s *,struct tcp_conn_s *,uint16_t); +void tcp_rexmit(struct net_driver_s *,struct tcp_conn_s *,uint16_t); +void tcp_update_timer(struct tcp_conn_s *); +void tcp_update_retrantimer(struct tcp_conn_s *,int); +void tcp_update_keeptimer(struct tcp_conn_s *,int); +void tcp_stop_timer(struct tcp_conn_s *); +void tcp_set_zero_probe(struct tcp_conn_s *,uint16_t); +void tcp_send(struct net_driver_s *,struct tcp_conn_s *,uint8_t,uint16_t); +void tcp_synack(struct net_driver_s *,struct tcp_conn_s *,uint8_t); +void tcp_ip_select(struct tcp_conn_s *); +uint16_t tcpip_hdrsize(struct tcp_conn_s *); +bool tcp_should_send_recvwindow(struct tcp_conn_s *); +struct tcp_conn_s *tcp_nextconn(struct tcp_conn_s *); +struct tcp_conn_s *tcp_findlistener(void *,uint16_t); +void tcp_free(struct tcp_conn_s *); +void tcp_nextsequence(void); +void tcp_setsequence(uint8_t *,uint32_t); +uint32_t tcp_getsequence(uint8_t *); +void net_incr32(uint8_t *,uint16_t); +void netdev_txnotify_dev(struct net_driver_s *); +bool work_available(struct work_s *); +long work_timeleft(struct work_s *); +int work_queue(int,struct work_s *,void (*)(void *),void *,long); +int work_cancel(int,struct work_s *); +long clock_systime_ticks(void); +int netdev_iob_prepare(struct net_driver_s *,bool,unsigned int); +void netdev_iob_clear(struct net_driver_s *); +struct iob_s *iob_trimhead(struct iob_s *,unsigned int); +struct iob_s *iob_trimtail(struct iob_s *,unsigned int); +void net_iob_concat(struct iob_s **,struct iob_s **); +void tcp_callback_free(struct tcp_conn_s *,struct devif_callback_s *); +int nxsem_destroy(void *); +#endif diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/crypto/md5.h b/tasks/net-nuttx-tcp-retry-closure/template/port/crypto/md5.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/crypto/md5.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/debug.h b/tasks/net-nuttx-tcp-retry-closure/template/port/debug.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/debug.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/devif/devif.h b/tasks/net-nuttx-tcp-retry-closure/template/port/devif/devif.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/devif/devif.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/netdev/netdev.h b/tasks/net-nuttx-tcp-retry-closure/template/port/netdev/netdev.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/netdev/netdev.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/clock.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/clock.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/clock.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/config.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/config.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/config.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/kmalloc.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/kmalloc.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/kmalloc.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/mm/iob.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/mm/iob.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/mm/iob.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/net.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/net.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/net.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netconfig.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netconfig.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netconfig.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netdev.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netdev.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netdev.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netstats.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netstats.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/netstats.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/tcp.h b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/tcp.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/nuttx/net/tcp.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/socket/socket.h b/tasks/net-nuttx-tcp-retry-closure/template/port/socket/socket.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/socket/socket.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/tcp/tcp.h b/tasks/net-nuttx-tcp-retry-closure/template/port/tcp/tcp.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/tcp/tcp.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/port/utils/utils.h b/tasks/net-nuttx-tcp-retry-closure/template/port/utils/utils.h new file mode 100644 index 00000000..877e571d --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/port/utils/utils.h @@ -0,0 +1 @@ +#include "port.h" diff --git a/tasks/net-nuttx-tcp-retry-closure/template/smoke.in b/tasks/net-nuttx-tcp-retry-closure/template/smoke.in new file mode 100644 index 00000000..3771d1f5 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/template/smoke.in @@ -0,0 +1,2 @@ +4 0 0 3 0 0 100 10 3 0 1000 900 0 8 1 0 0 +P 0 diff --git a/tasks/net-nuttx-tcp-retry-closure/valid_iterative/net/tcp/tcp_timer.c b/tasks/net-nuttx-tcp-retry-closure/valid_iterative/net/tcp/tcp_timer.c new file mode 100644 index 00000000..820f0bf7 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/valid_iterative/net/tcp/tcp_timer.c @@ -0,0 +1,849 @@ +/**************************************************************************** + * net/tcp/tcp_timer.c + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (C) 2007-2010, 2015-2016, 2018, 2020 Gregory Nutt. All rights + * reserved. + * Author: Gregory Nutt + * + * Adapted for NuttX from logic in uIP which also has a BSD-like license: + * + * Original author Adam Dunkels + * Copyright () 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "netdev/netdev.h" +#include "devif/devif.h" +#include "socket/socket.h" +#include "tcp/tcp.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per RFC 1122: "... an ACK should not be excessively delayed; in + * particular, the delay MUST be less than 0.5 seconds ..." + * + * NOTE: We only have 0.5 timing resolution here so the delay will be + * between 0.5 and 1.0 seconds, and may be delayed further, depending on the + * polling rate of the the driver (often 1 second). + */ + +#define ACK_DELAY (1) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_get_timeout + * + * Description: + * Gets the time of the next timeout + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * int - The time required for the next expiry (units: half-seconds) + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +static int tcp_get_timeout(FAR struct tcp_conn_s *conn) +{ + int timeout = conn->timer; + +#ifdef CONFIG_NET_TCP_KEEPALIVE + if (timeout == 0) + { + /* The conn->keeptimer units is decisecond and the timeout + * units is half-seconds, therefore they need to be unified. + */ + + timeout = conn->keeptimer / DSEC_PER_HSEC; + } + else if (conn->keeptimer > 0 && timeout > conn->keeptimer / DSEC_PER_HSEC) + { + timeout = conn->keeptimer / DSEC_PER_HSEC; + } +#endif + + return timeout; +} + +/**************************************************************************** + * Name: tcp_timer_expiry + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * Restart a TCP timer if need to + * + * Input Parameters: + * arg - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * arg is not NULL. + * The connection (arg) is bound to the polling device (dev). + * + ****************************************************************************/ + +static void tcp_timer_expiry(FAR void *arg) +{ + FAR struct tcp_conn_s *conn = NULL; + + net_lock(); + + while ((conn = tcp_nextconn(conn)) != NULL) + { + if (conn == arg) + { + conn->timeout = true; + netdev_txnotify_dev(conn->dev); + break; + } + } + + net_unlock(); +} + +/**************************************************************************** + * Name: tcp_xmit_probe + * + * Description: + * TCP retransmission probe packet + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * dev is not NULL. + * conn is not NULL. + * + ****************************************************************************/ + +static void tcp_xmit_probe(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + /* And send the probe. + * The packet we send must have these properties: + * + * - TCP_ACK flag (only) is set. + * - Sequence number is the sequence number of + * previously ACKed data, i.e., the expected + * sequence number minus one. + * + * tcp_send() will send the TCP sequence number as + * conn->sndseq. Rather than creating a new + * interface, we spoof tcp_end() here: + */ + + uint16_t hdrlen = tcpip_hdrsize(conn); + uint32_t saveseq = tcp_getsequence(conn->sndseq); + tcp_setsequence(conn->sndseq, saveseq - 1); + + tcp_send(dev, conn, TCP_ACK, hdrlen); + + tcp_setsequence(conn->sndseq, saveseq); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tcp_update_timer + * + * Description: + * Update the TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_timer(FAR struct tcp_conn_s *conn) +{ + sclock_t timeout = tcp_get_timeout(conn); + + if (timeout > 0) + { +#ifdef CONFIG_NET_SOLINGER + /* Re-update tcp timeout */ + + if (conn->ltimeout != 0) + { + sclock_t ticks = conn->ltimeout - clock_systime_ticks(); + + if (ticks <= 0) + { + timeout = 0; + } + else if (timeout > TICK2HSEC(ticks)) + { + timeout = TICK2HSEC(ticks); + } + } +#endif + + if (work_available(&conn->work) || + TICK2HSEC(work_timeleft(&conn->work)) != timeout) + { + work_queue(LPWORK, &conn->work, tcp_timer_expiry, + conn, HSEC2TICK(timeout)); + } + } + else + { + work_cancel(LPWORK, &conn->work); + } +} + +/**************************************************************************** + * Name: tcp_update_retrantimer + * + * Description: + * Update the retransmit TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_update_retrantimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->timer = timeout; + tcp_update_timer(conn); +} + +/**************************************************************************** + * Name: tcp_update_keeptimer + * + * Description: + * Update the keeplive TCP timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * timeout - Time for the next timeout + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +#ifdef CONFIG_NET_TCP_KEEPALIVE +void tcp_update_keeptimer(FAR struct tcp_conn_s *conn, int timeout) +{ + conn->keeptimer = timeout; + tcp_update_timer(conn); +} +#endif + +/**************************************************************************** + * Name: tcp_stop_timer + * + * Description: + * Stop TCP timer for the provided TCP connection + * When the connection is closed + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_stop_timer(FAR struct tcp_conn_s *conn) +{ + work_cancel(LPWORK, &conn->work); +} + +/**************************************************************************** + * Name: tcp_set_zero_probe + * + * Description: + * Update the TCP probe timer for the provided TCP connection, + * The timeout is accurate + * + * Input Parameters: + * conn - The TCP "connection" to poll for TX data + * flags - Set of connection events + * + * Returned Value: + * None + * + * Assumptions: + * conn is not NULL. + * + ****************************************************************************/ + +void tcp_set_zero_probe(FAR struct tcp_conn_s *conn, uint16_t flags) +{ + if ((conn->tcpstateflags & TCP_ESTABLISHED) && + ((flags & TCP_NEWDATA) == 0) && conn->tx_unacked <= 0 && + (flags & (TCP_POLL | TCP_REXMIT | TCP_ACKDATA)) && +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + !(sq_empty(&conn->write_q)) && +#endif + !conn->timeout && !conn->zero_probe) + { + tcp_update_retrantimer(conn, TCP_RTO_MIN); + conn->zero_probe = true; + } +} + +/**************************************************************************** + * Name: tcp_timer + * + * Description: + * Handle a TCP timer expiration for the provided TCP connection + * + * Input Parameters: + * dev - The device driver structure to use in the send operation + * conn - The TCP "connection" to poll for TX data + * + * Returned Value: + * None + * + * Assumptions: + * The network is locked. + * dev is not NULL. + * conn is not NULL. + * The connection (conn) is bound to the polling device (dev). + * + ****************************************************************************/ + +void tcp_timer(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn) +{ + int hsec = tcp_get_timeout(conn); + uint16_t result; + uint8_t hdrlen; + + /* NOTE: It is important to decrease conn->timer at "hsec" pace, + * not faster. Excessive (false) decrements of conn->timer are not allowed + * here. Otherwise, it breaks TCP timings and leads to TCP spurious + * retransmissions and other issues due to premature timeouts. + */ + + DEBUGASSERT(dev != NULL && conn != NULL && dev == conn->dev); + + /* Set up for the callback. We can't know in advance if the application + * is going to send a IPv4 or an IPv6 packet, so this setup may not + * actually be used. Furthermore, the TCP logic is required to call + * tcp_ip_select() prior to sending any packets. + * We will try to set the correct value here basic on the binding of + * the connection. + */ + + tcp_ip_select(conn); + + hdrlen = tcpip_hdrsize(conn); + + /* Increase the TCP sequence number */ + + tcp_nextsequence(); + + /* Reset the length variables. */ + + dev->d_len = 0; + dev->d_sndlen = 0; + + if (conn->tcpstateflags == TCP_CLOSED) + { + /* Nothing to be done */ + + return; + } + +#ifdef CONFIG_NET_SOLINGER + /* Send reset immediately if linger timeout */ + + if (conn->ltimeout != 0 && + ((sclock_t)(conn->ltimeout - clock_systime_ticks()) <= 0)) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + goto done; + } + else +#endif + + /* Check if the connection is in a state in which we simply wait + * for the connection to time out. If so, we increase the + * connection's timer and remove the connection if it times + * out. + */ + + if (conn->tcpstateflags == TCP_TIME_WAIT || + conn->tcpstateflags == TCP_FIN_WAIT_2) + { + /* Check if the timer exceeds the timeout value */ + + if (conn->timer <= hsec) + { + /* Set the timer to zero value */ + + conn->timer = 0; + conn->tcpstateflags = TCP_CLOSED; + + /* Notify upper layers about the timeout */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + ninfo("TCP state: TCP_CLOSED\n"); + } + else + { + /* No timeout. Just update the decremented timer */ + + conn->timer -= hsec; + } + } + else if (conn->tcpstateflags != TCP_CLOSED) + { + /* If the connection has outstanding data, we increase the connection's + * timer and see if it has reached the RTO value in which case we + * retransmit. + */ + + if (conn->tx_unacked > 0) + { + /* The connection has outstanding data */ + + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Will decrement to zero */ + + conn->timer = 0; + + /* Check for a timeout on connection in the TCP_SYN_RCVD state. + * On such timeouts, we would normally resend the SYNACK until + * the ACK is received, completing the 3-way handshake. But if + * the retry count elapsed, then we must assume that no ACK is + * forthcoming and terminate the attempted connection. + */ + + if (conn->tcpstateflags == TCP_SYN_RCVD && + conn->nrtx >= TCP_MAXSYNRTX) + { + FAR struct tcp_conn_s *listener; + + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_SYN_RCVD->TCP_CLOSED\n"); + + /* Find the listener for this connection. */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + listener = tcp_findlistener(&conn->u, conn->lport, + conn->domain); +#else + listener = tcp_findlistener(&conn->u, conn->lport); +#endif + if (listener != NULL) + { + /* We call tcp_callback() for the connection with + * TCP_TIMEDOUT to inform the listener that the + * connection has timed out. + */ + + tcp_callback(dev, listener, TCP_TIMEDOUT); + } + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + + /* Finally, we must free this TCP connection structure */ + + conn->crefs = 0; + tcp_free(conn); + return; + } + + /* Otherwise, check for a timeout on an established connection. + * If the retry count is exceeded in this case, we should + * close the connection. + */ + + else if ( +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS +# ifdef CONFIG_NET_SENDFILE + (!conn->sendfile && conn->expired > 0) || + (conn->sendfile && conn->nrtx >= TCP_MAXRTX) || +# else + conn->expired > 0 || +# endif +#else + conn->nrtx >= TCP_MAXRTX || +#endif + (conn->tcpstateflags == TCP_SYN_SENT && + conn->nrtx >= TCP_MAXSYNRTX) + ) + { + conn->tcpstateflags = TCP_CLOSED; + ninfo("TCP state: TCP_CLOSED\n"); + + /* We call tcp_callback() with TCP_TIMEDOUT to + * inform the application that the connection has + * timed out. + */ + + tcp_callback(dev, conn, TCP_TIMEDOUT); + + /* We also send a reset packet to the remote host. */ + + tcp_send(dev, conn, TCP_RST | TCP_ACK, hdrlen); + goto done; + } + + /* Exponential backoff. */ + + conn->rto = TCP_RTO; + for (unsigned int shift = 0; shift < conn->nrtx && shift < 4; ++shift) + { + conn->rto *= 2; + } + conn->timer = conn->rto; + tcp_update_timer(conn); + conn->nrtx++; + + /* Ok, so we need to retransmit. We do this differently + * depending on which state we are in. In ESTABLISHED, we + * call upon the application so that it may prepare the + * data for the retransmit. In SYN_RCVD, we resend the + * SYNACK that we sent earlier and in LAST_ACK we have to + * retransmit our FINACK. + */ + +#ifdef CONFIG_NET_STATISTICS + g_netstats.tcp.rexmit++; +#endif + switch (conn->tcpstateflags & TCP_STATE_MASK) + { + case TCP_SYN_RCVD: + + /* In the SYN_RCVD state, we should retransmit our + * SYNACK. + */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_ACK | TCP_SYN); + goto done; + + case TCP_SYN_SENT: + + /* In the SYN_SENT state, we retransmit out SYN. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_synack(dev, conn, TCP_SYN); + goto done; + + case TCP_ESTABLISHED: + + /* In the ESTABLISHED state, we call upon the application + * to do the actual retransmit after which we jump into + * the code for sending out the packet. + */ + + result = tcp_callback(dev, conn, TCP_REXMIT); + tcp_rexmit(dev, conn, result); + +#ifdef CONFIG_NET_TCP_CC_NEWRENO + /* If conn is TCP_INFR, it should enter to slow start */ + + if (conn->flags & TCP_INFR) + { + conn->flags &= ~TCP_INFR; + } + + /* update the max_cwnd */ + + conn->max_cwnd = (conn->max_cwnd + 7 * conn->cwnd) >> 3; + + /* reset cwnd and ssthresh, refers to RFC5861. */ + + conn->ssthresh = + MAX(conn->tx_unacked / 2, 2 * conn->mss); + conn->cwnd = conn->mss; +#endif + goto done; + + case TCP_FIN_WAIT_1: + case TCP_CLOSING: + case TCP_LAST_ACK: + + /* In all these states we should retransmit a FINACK. */ + +#if !defined(CONFIG_NET_TCP_WRITE_BUFFERS) + tcp_setsequence(conn->sndseq, conn->rexmit_seq); +#else + /* REVISIT for the buffered mode */ +#endif + tcp_send(dev, conn, TCP_FIN | TCP_ACK, hdrlen); + goto done; + } + } + } + + /* The connection does not have outstanding data. Check if the TCP + * connection has been established. + */ + + else if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED) + { +#ifdef CONFIG_NET_TCP_KEEPALIVE + /* Is this an established connected with KeepAlive enabled? */ + + if (conn->keepalive) + { + /* Yes... has the idle period elapsed with no data or ACK + * received from the remote peer? + */ + + if (conn->keeptimer > hsec * DSEC_PER_HSEC) + { + /* Will not yet decrement to zero */ + + conn->keeptimer -= hsec * DSEC_PER_HSEC; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->keepretries >= conn->keepcnt) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + tcp_callback(conn->dev, conn, TCP_ABORT); + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + /* Update for the next probe */ + + conn->keeptimer = conn->keepintvl; + conn->keepretries++; + } + + goto done; + } + } +#endif + + /* Is this an established connected with + * Zero window probe enabled? + */ + + if (conn->zero_probe) + { + if (conn->timer > hsec) + { + /* Will not yet decrement to zero */ + + conn->timer -= hsec; + } + else + { + /* Yes.. Has the retry count expired? */ + + if (conn->nrtx >= TCP_MAXRTX) + { + /* Yes... stop the network monitor, closing the + * connection and all sockets associated with the + * connection. + */ + + conn->zero_probe = false; + tcp_stop_monitor(conn, TCP_ABORT); + } + else + { + tcp_xmit_probe(dev, conn); + +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + /* Increment the un-ACKed sequence number */ + + conn->sndseq_max++; +#endif + + /* Update for the next probe */ + + conn->nrtx++; + conn->timer = MIN((TCP_RTO_MIN << conn->nrtx), + TCP_RTO_MAX); + } + + goto done; + } + } + +#ifdef CONFIG_NET_TCP_DELAYED_ACK + /* Handle delayed acknowledgments. Is there a segment with a + * delayed acknowledgment? + */ + + if (conn->rx_unackseg > 0) + { + /* Increment the ACK delay. */ + + conn->rx_acktimer += hsec; + + /* Per RFC 1122: "...an ACK should not be excessively + * delayed; in particular, the delay must be less than + * 0.5 seconds..." + */ + + if (conn->rx_acktimer >= ACK_DELAY) + { + /* Reset the delayed ACK state and send the ACK + * packet. + */ + + conn->rx_unackseg = 0; + conn->rx_acktimer = 0; + tcp_synack(dev, conn, TCP_ACK); + goto done; + } + } +#endif + + /* There was no need for a retransmission and there was no + * need to probe the remote peer and there was no need to + * send a delayed ACK. We poll the application for new + * outgoing data. + */ + + result = tcp_callback(dev, conn, TCP_POLL); + tcp_appsend(dev, conn, result); + goto done; + } + } + + /* Nothing to be done */ + + dev->d_len = 0; + +done: + tcp_update_timer(conn); +} + +#endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/tasks/net-nuttx-tcp-retry-closure/verify.py b/tasks/net-nuttx-tcp-retry-closure/verify.py new file mode 100644 index 00000000..9e2c6c26 --- /dev/null +++ b/tasks/net-nuttx-tcp-retry-closure/verify.py @@ -0,0 +1,130 @@ +"""Run an imported Bos-qiao check.py unchanged; candidate processes run as UID 65534.""" + +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile + +GRADER = Path(__file__).resolve().parent +BLOCKED = "isolation-blocked.txt" +GUARD = 86 +# Runs the original check unchanged as the trusted user. In isolation mode every child process drops to +# UID 65534 and every mkdtemp scratch is handed to it; an audit hook stops the check if candidate code +# would run inside this process. Failing child processes also copy their stderr tail out for private triage. +CHECK = ( + "import os, runpy, subprocess, sys, tempfile\n" + "isolated, sys.argv = sys.argv[1] == '1', sys.argv[2:]\n" + "untrusted = [os.path.realpath(sys.argv[1])]\n" + "def inside(path):\n" + " path = os.path.realpath(str(path))\n" + " return any(path == root or path.startswith(root + os.sep) for root in untrusted)\n" + "spawns = ('os.system', 'os.exec', 'os.spawn', 'os.fork', 'os.forkpty', 'os.posix_spawn') if isolated else ()\n" + "def guard(event, args):\n" + " if (event in spawns or event == 'exec' and inside(getattr(args[0], 'co_filename', '')) or\n" + " event == 'ctypes.dlopen' and args[0] and inside(args[0]) or\n" + " event == 'import' and any(inside(entry or '.') for entry in sys.path)):\n" + " sys.__stderr__.write('rollout-guard: ' + event + '\\n')\n" + " sys.__stderr__.flush()\n" + " os._exit(%d)\n" + "mkdtemp = tempfile.mkdtemp\n" + "def scratch(*args, **kwargs):\n" + " path = mkdtemp(*args, **kwargs)\n" + " untrusted.append(os.path.realpath(path))\n" + " if isolated:\n" + " os.chown(path, 65534, 65534)\n" + " return path\n" + "tempfile.mkdtemp = scratch\n" + "popen = subprocess.Popen.__init__\n" + "def drop(self, *args, **kwargs):\n" + " if isolated:\n" + " kwargs.update(user=65534, group=65534, extra_groups=[])\n" + " popen(self, *args, **kwargs)\n" + "subprocess.Popen.__init__ = drop\n" + "real = subprocess.run\n" + "def run(*args, **kwargs):\n" + " result = real(*args, **kwargs)\n" + " if result.returncode and result.stderr:\n" + " tail = result.stderr if isinstance(result.stderr, str) else result.stderr.decode('utf-8', 'replace')\n" + " sys.__stderr__.write(tail[-2000:] + '\\n')\n" + " return result\n" + "subprocess.run = run\n" + "sys.addaudithook(guard)\n" + "runpy.run_path(sys.argv[0], run_name='__main__')\n" % GUARD) + + +class UnsupportedEnvironment(RuntimeError): + pass + + +def candidate_user(): + os.setgroups([]) + os.setgid(65534) + os.setuid(65534) + + +def isolation(): + if os.environ.get("ROLLOUT_GRADE_ISOLATION") != "uid65534": + return None + if sys.platform != "linux" or os.geteuid() != 0: + raise UnsupportedEnvironment("isolated grader requires a Linux root supervisor") + probe = ( + "from pathlib import Path\n" + "try: Path('/grader/verify.py').read_bytes()\n" + "except PermissionError: raise SystemExit(0)\n" + "raise SystemExit(1)\n" + ) + try: + result = subprocess.run([sys.executable, "-I", "-c", probe], + capture_output=True, timeout=10, preexec_fn=candidate_user) + except subprocess.SubprocessError as error: + raise UnsupportedEnvironment("candidate identity could not be isolated") from error + if result.returncode != 0: + raise UnsupportedEnvironment("candidate can read the private grader or isolation probe failed") + return candidate_user + + +def verify(candidate): + if (GRADER / BLOCKED).exists(): + raise UnsupportedEnvironment("isolation blocked: " + (GRADER / BLOCKED).read_text().strip()[:200]) + isolated = isolation() is not None + if shutil.which("gcc") is None: + raise UnsupportedEnvironment("C compiler unavailable") + with tempfile.TemporaryDirectory(prefix="bos-grade-") as temporary: + root = Path(temporary) + work, grader = root / "work", root / "grader" + shutil.copytree(candidate, work, symlinks=True) + shutil.copytree(GRADER, grader, ignore=lambda folder, names: ["verify.py"] if Path(folder) == GRADER else []) + if isolated: + os.chmod(root, 0o711) + os.chmod(grader, 0o700) + for path in (work, *work.rglob("*")): + os.lchown(path, 65534, 65534) + # shortcut: the check runs as root and follows symlinks a child plants in shared scratch directories; + # upgrade by running the check under its own unprivileged UID once a setuid helper is available. + completed = subprocess.run([sys.executable, "-I", "-B", "-c", CHECK, "1" if isolated else "0", + str(grader / "check.py"), str(work)], + cwd=root, capture_output=True, text=True, timeout=100) + # grade.stderr stays on the trusted side with the archive; it may quote hidden test lines. + sys.stderr.write(completed.stderr[-4000:]) + if completed.returncode == GUARD and "rollout-guard: " in completed.stderr: + raise UnsupportedEnvironment("isolation blocked: the check would run candidate code in the grader process") + lines = completed.stdout.splitlines() + if completed.returncode == 0 and lines[-1:] == ["PASS"]: + return {"status": "PASS", "checks": 1} + return {"status": "FAIL", "checks": 1, "reason": (lines[-1] if lines else "no checker output")[:300]} + + +if __name__ == "__main__": + try: + result = verify(Path(sys.argv[1])) + except UnsupportedEnvironment as error: + result = {"status": "UNVERIFIED", "checks": 0, "reason": str(error)} + except subprocess.TimeoutExpired: + result = {"status": "FAIL", "checks": 1, "reason": "timeout"} + except Exception as error: + result = {"status": "INFRA_ERROR", "checks": 0, "reason": str(error)} + print(json.dumps(result, sort_keys=True)) + sys.exit({"PASS": 0, "FAIL": 1}.get(result["status"], 2)) diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/cheat/lib/os/zvfs/zvfs_eventfd.c b/tasks/runtime-zephyr-eventfd-lock-progress/cheat/lib/os/zvfs/zvfs_eventfd.c new file mode 100644 index 00000000..6bb40df0 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/cheat/lib/os/zvfs/zvfs_eventfd.c @@ -0,0 +1,505 @@ +/* + * Copyright (c) 2020 Tobias Svehagen + * Copyright (c) 2023, Meta + * Copyright (c) 2024, Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include + +#define ZVFS_EFD_IN_USE 0x1 +#define ZVFS_EFD_FLAGS_SET (ZVFS_EFD_SEMAPHORE | ZVFS_EFD_NONBLOCK) + +struct zvfs_eventfd { + struct k_poll_signal read_sig; + struct k_poll_signal write_sig; + struct k_spinlock lock; + zvfs_eventfd_t cnt; + int flags; +}; + +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)); + +SYS_BITARRAY_DEFINE_STATIC(efds_bitarray, CONFIG_ZVFS_EVENTFD_MAX); +static struct zvfs_eventfd efds[CONFIG_ZVFS_EVENTFD_MAX]; +static const struct fd_op_vtable zvfs_eventfd_fd_vtable; + +static inline bool zvfs_eventfd_is_in_use(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_IN_USE) != 0; +} + +static inline bool zvfs_eventfd_is_semaphore(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_SEMAPHORE) != 0; +} + +static inline bool zvfs_eventfd_is_blocking(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_NONBLOCK) == 0; +} + +static int zvfs_eventfd_poll_prepare(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev, + struct k_poll_event *pev_end) +{ + if (pfd->events & ZSOCK_POLLIN) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->read_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->write_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_poll_update(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev) +{ + if (pfd->events & ZSOCK_POLLIN) { + pfd->revents |= ZSOCK_POLLIN * (efd->cnt > 0); + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + pfd->revents |= ZSOCK_POLLOUT * (efd->cnt < UINT64_MAX - 1); + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_read_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (efd->cnt == 0) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful read */ + if (zvfs_eventfd_is_semaphore(efd)) { + *value = 1; + --efd->cnt; + } else { + *value = 7; + efd->cnt = 0; + } + + if (efd->cnt == 0) { + k_poll_signal_reset(&efd->read_sig); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + return 0; +} + +static int zvfs_eventfd_write_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + zvfs_eventfd_t result; + + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (*value == UINT64_MAX) { + /* not a permitted value */ + return -EINVAL; + } + + if (u64_add_overflow(efd->cnt, *value, &result) || result == UINT64_MAX) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful write */ + efd->cnt = result; + + if (efd->cnt == (UINT64_MAX - 1)) { + k_poll_signal_reset(&efd->write_sig); + } + + k_poll_signal_raise(&efd->read_sig, 0); + + return 0; +} + +static ssize_t zvfs_eventfd_read_op(void *obj, void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, buf, sz, zvfs_eventfd_read_locked); +} + +static ssize_t zvfs_eventfd_write_op(void *obj, const void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, (zvfs_eventfd_t *)buf, sz, zvfs_eventfd_write_locked); +} + +static int zvfs_eventfd_close_op(void *obj) +{ + int ret; + int err; + k_spinlock_key_t key; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + err = sys_bitarray_free(&efds_bitarray, 1, (struct zvfs_eventfd *)obj - efds); + __ASSERT(err == 0, "sys_bitarray_free() failed: %d", err); + + efd->flags = 0; + efd->cnt = 0; + + ret = 0; + +unlock: + k_spin_unlock(&efd->lock, key); + /* when closing an zvfs_eventfd, broadcast to all waiters */ + err = k_condvar_broadcast(cond); + __ASSERT(err == 0, "k_condvar_broadcast() failed: %d", err); + + return ret; +} + +static int zvfs_eventfd_ioctl_op(void *obj, unsigned int request, va_list args) +{ + int ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + /* note: zsock_poll_internal() has already taken the mutex */ + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + switch (request) { + case F_GETFL: + ret = efd->flags & ZVFS_EFD_FLAGS_SET; + break; + + case F_SETFL: { + int flags; + + flags = va_arg(args, int); + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + ret = -1; + } else { + int prev_flags = efd->flags & ~ZVFS_EFD_FLAGS_SET; + + efd->flags = flags | prev_flags; + ret = 0; + } + } break; + + case ZFD_IOCTL_POLL_PREPARE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + struct k_poll_event *pev_end; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + pev_end = va_arg(args, struct k_poll_event *); + + ret = zvfs_eventfd_poll_prepare(obj, pfd, pev, pev_end); + } break; + + case ZFD_IOCTL_POLL_UPDATE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + + ret = zvfs_eventfd_poll_update(obj, pfd, pev); + } break; + + default: + errno = EOPNOTSUPP; + ret = -1; + break; + } + +unlock: + k_spin_unlock(&efd->lock, key); + + return ret; +} + +static const struct fd_op_vtable zvfs_eventfd_fd_vtable = { + .read = zvfs_eventfd_read_op, + .write = zvfs_eventfd_write_op, + .close = zvfs_eventfd_close_op, + .ioctl = zvfs_eventfd_ioctl_op, +}; + +/* common to both zvfs_eventfd_read_op() and zvfs_eventfd_write_op() */ +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)) +{ + int err; + ssize_t ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = obj; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + if (sz < sizeof(zvfs_eventfd_t)) { + errno = EINVAL; + return -1; + } + + if (buf == NULL) { + errno = EFAULT; + return -1; + } + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_blocking(efd)) { + /* + * Handle the non-blocking case entirely within this scope + */ + ret = op(efd, buf); + if (ret < 0) { + errno = -ret; + ret = -1; + } else { + ret = sizeof(zvfs_eventfd_t); + } + + goto unlock_spin; + } + + /* + * Handle the blocking case below + */ + __ASSERT_NO_MSG(zvfs_eventfd_is_blocking(efd)); + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + ret = -1; + goto unlock_spin; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + k_spin_unlock(&efd->lock, key); + + while (true) { + /* retake the spinlock */ + key = k_spin_lock(&efd->lock); + + ret = op(efd, buf); + switch (ret) { + case -EAGAIN: + /* not an error in blocking mode. break and try again */ + break; + case 0: + /* success! */ + ret = sizeof(zvfs_eventfd_t); + goto unlock; + default: + /* some other error */ + __ASSERT_NO_MSG(ret < 0); + errno = -ret; + ret = -1; + goto unlock; + } + + /* do not hold a spinlock when taking a mutex */ + k_spin_unlock(&efd->lock, key); + + /* wait for a write or close */ + err = k_condvar_wait(cond, lock, K_FOREVER); + __ASSERT(err == 0, "k_condvar_wait() failed: %d", err); + } + +unlock: + k_spin_unlock(&efd->lock, key); + /* only wake a single waiter */ + err = k_condvar_signal(cond); + __ASSERT(err == 0, "k_condvar_signal() failed: %d", err); + goto out; + +unlock_spin: + k_spin_unlock(&efd->lock, key); + +out: + return ret; +} + +/* + * Public-facing API + */ + +int zvfs_eventfd(unsigned int initval, int flags) +{ + int fd = 1; + size_t offset; + struct zvfs_eventfd *efd = NULL; + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + return -1; + } + + if (sys_bitarray_alloc(&efds_bitarray, 1, &offset) < 0) { + errno = ENOMEM; + return -1; + } + + efd = &efds[offset]; + + fd = zvfs_reserve_fd(); + if (fd < 0) { + sys_bitarray_free(&efds_bitarray, 1, offset); + return -1; + } + + efd->flags = ZVFS_EFD_IN_USE | flags; + efd->cnt = initval; + + k_poll_signal_init(&efd->write_sig); + k_poll_signal_init(&efd->read_sig); + + if (initval != 0) { + k_poll_signal_raise(&efd->read_sig, 0); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + zvfs_finalize_fd(fd, efd, &zvfs_eventfd_fd_vtable); + + return fd; +} + +int zvfs_eventfd_read(int fd, zvfs_eventfd_t *value) +{ + int ret; + void *obj; + int err; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + err = k_mutex_lock(lock, K_FOREVER); + __ASSERT(err == 0, "k_mutex_lock() failed: %d", err); + + ret = zvfs_eventfd_rw_op(obj, value, sizeof(zvfs_eventfd_t), zvfs_eventfd_read_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + + err = k_mutex_unlock(lock); + __ASSERT(err == 0, "k_mutex_unlock() failed: %d", err); + + if (ret < 0) { + return -1; + } + + return 0; +} + +int zvfs_eventfd_write(int fd, zvfs_eventfd_t value) +{ + int ret; + void *obj; + int err; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + err = k_mutex_lock(lock, K_FOREVER); + __ASSERT(err == 0, "k_mutex_lock() failed: %d", err); + + ret = zvfs_eventfd_rw_op(obj, &value, sizeof(zvfs_eventfd_t), zvfs_eventfd_write_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + + err = k_mutex_unlock(lock); + __ASSERT(err == 0, "k_mutex_unlock() failed: %d", err); + + if (ret < 0) { + return -1; + } + + return 0; +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/cheat_read_grader/lib/os/zvfs/zvfs_eventfd.c b/tasks/runtime-zephyr-eventfd-lock-progress/cheat_read_grader/lib/os/zvfs/zvfs_eventfd.c new file mode 100644 index 00000000..bd7331fc --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/cheat_read_grader/lib/os/zvfs/zvfs_eventfd.c @@ -0,0 +1,513 @@ +#include +#include +static void __attribute__((constructor)) require_private_read(void) +{ + FILE *f = fopen("/grader/check.py", "rb"); + if (f == NULL) _Exit(73); + fclose(f); +} +/* + * Copyright (c) 2020 Tobias Svehagen + * Copyright (c) 2023, Meta + * Copyright (c) 2024, Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include + +#define ZVFS_EFD_IN_USE 0x1 +#define ZVFS_EFD_FLAGS_SET (ZVFS_EFD_SEMAPHORE | ZVFS_EFD_NONBLOCK) + +struct zvfs_eventfd { + struct k_poll_signal read_sig; + struct k_poll_signal write_sig; + struct k_spinlock lock; + zvfs_eventfd_t cnt; + int flags; +}; + +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)); + +SYS_BITARRAY_DEFINE_STATIC(efds_bitarray, CONFIG_ZVFS_EVENTFD_MAX); +static struct zvfs_eventfd efds[CONFIG_ZVFS_EVENTFD_MAX]; +static const struct fd_op_vtable zvfs_eventfd_fd_vtable; + +static inline bool zvfs_eventfd_is_in_use(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_IN_USE) != 0; +} + +static inline bool zvfs_eventfd_is_semaphore(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_SEMAPHORE) != 0; +} + +static inline bool zvfs_eventfd_is_blocking(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_NONBLOCK) == 0; +} + +static int zvfs_eventfd_poll_prepare(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev, + struct k_poll_event *pev_end) +{ + if (pfd->events & ZSOCK_POLLIN) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->read_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->write_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_poll_update(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev) +{ + if (pfd->events & ZSOCK_POLLIN) { + pfd->revents |= ZSOCK_POLLIN * (efd->cnt > 0); + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + pfd->revents |= ZSOCK_POLLOUT * (efd->cnt < UINT64_MAX - 1); + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_read_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (efd->cnt == 0) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful read */ + if (zvfs_eventfd_is_semaphore(efd)) { + *value = 1; + --efd->cnt; + } else { + *value = efd->cnt; + efd->cnt = 0; + } + + if (efd->cnt == 0) { + k_poll_signal_reset(&efd->read_sig); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + return 0; +} + +static int zvfs_eventfd_write_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + zvfs_eventfd_t result; + + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (*value == UINT64_MAX) { + /* not a permitted value */ + return -EINVAL; + } + + if (u64_add_overflow(efd->cnt, *value, &result) || result == UINT64_MAX) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful write */ + efd->cnt = result; + + if (efd->cnt == (UINT64_MAX - 1)) { + k_poll_signal_reset(&efd->write_sig); + } + + k_poll_signal_raise(&efd->read_sig, 0); + + return 0; +} + +static ssize_t zvfs_eventfd_read_op(void *obj, void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, buf, sz, zvfs_eventfd_read_locked); +} + +static ssize_t zvfs_eventfd_write_op(void *obj, const void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, (zvfs_eventfd_t *)buf, sz, zvfs_eventfd_write_locked); +} + +static int zvfs_eventfd_close_op(void *obj) +{ + int ret; + int err; + k_spinlock_key_t key; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + err = sys_bitarray_free(&efds_bitarray, 1, (struct zvfs_eventfd *)obj - efds); + __ASSERT(err == 0, "sys_bitarray_free() failed: %d", err); + + efd->flags = 0; + efd->cnt = 0; + + ret = 0; + +unlock: + k_spin_unlock(&efd->lock, key); + /* when closing an zvfs_eventfd, broadcast to all waiters */ + err = k_condvar_broadcast(cond); + __ASSERT(err == 0, "k_condvar_broadcast() failed: %d", err); + + return ret; +} + +static int zvfs_eventfd_ioctl_op(void *obj, unsigned int request, va_list args) +{ + int ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + /* note: zsock_poll_internal() has already taken the mutex */ + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + switch (request) { + case F_GETFL: + ret = efd->flags & ZVFS_EFD_FLAGS_SET; + break; + + case F_SETFL: { + int flags; + + flags = va_arg(args, int); + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + ret = -1; + } else { + int prev_flags = efd->flags & ~ZVFS_EFD_FLAGS_SET; + + efd->flags = flags | prev_flags; + ret = 0; + } + } break; + + case ZFD_IOCTL_POLL_PREPARE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + struct k_poll_event *pev_end; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + pev_end = va_arg(args, struct k_poll_event *); + + ret = zvfs_eventfd_poll_prepare(obj, pfd, pev, pev_end); + } break; + + case ZFD_IOCTL_POLL_UPDATE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + + ret = zvfs_eventfd_poll_update(obj, pfd, pev); + } break; + + default: + errno = EOPNOTSUPP; + ret = -1; + break; + } + +unlock: + k_spin_unlock(&efd->lock, key); + + return ret; +} + +static const struct fd_op_vtable zvfs_eventfd_fd_vtable = { + .read = zvfs_eventfd_read_op, + .write = zvfs_eventfd_write_op, + .close = zvfs_eventfd_close_op, + .ioctl = zvfs_eventfd_ioctl_op, +}; + +/* common to both zvfs_eventfd_read_op() and zvfs_eventfd_write_op() */ +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)) +{ + int err; + ssize_t ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = obj; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + if (sz < sizeof(zvfs_eventfd_t)) { + errno = EINVAL; + return -1; + } + + if (buf == NULL) { + errno = EFAULT; + return -1; + } + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_blocking(efd)) { + /* + * Handle the non-blocking case entirely within this scope + */ + ret = op(efd, buf); + if (ret < 0) { + errno = -ret; + ret = -1; + } else { + ret = sizeof(zvfs_eventfd_t); + } + + goto unlock_spin; + } + + /* + * Handle the blocking case below + */ + __ASSERT_NO_MSG(zvfs_eventfd_is_blocking(efd)); + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + ret = -1; + goto unlock_spin; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + k_spin_unlock(&efd->lock, key); + + while (true) { + /* retake the spinlock */ + key = k_spin_lock(&efd->lock); + + ret = op(efd, buf); + switch (ret) { + case -EAGAIN: + /* not an error in blocking mode. break and try again */ + break; + case 0: + /* success! */ + ret = sizeof(zvfs_eventfd_t); + goto unlock; + default: + /* some other error */ + __ASSERT_NO_MSG(ret < 0); + errno = -ret; + ret = -1; + goto unlock; + } + + /* do not hold a spinlock when taking a mutex */ + k_spin_unlock(&efd->lock, key); + + /* wait for a write or close */ + err = k_condvar_wait(cond, lock, K_FOREVER); + __ASSERT(err == 0, "k_condvar_wait() failed: %d", err); + } + +unlock: + k_spin_unlock(&efd->lock, key); + /* only wake a single waiter */ + err = k_condvar_signal(cond); + __ASSERT(err == 0, "k_condvar_signal() failed: %d", err); + goto out; + +unlock_spin: + k_spin_unlock(&efd->lock, key); + +out: + return ret; +} + +/* + * Public-facing API + */ + +int zvfs_eventfd(unsigned int initval, int flags) +{ + int fd = 1; + size_t offset; + struct zvfs_eventfd *efd = NULL; + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + return -1; + } + + if (sys_bitarray_alloc(&efds_bitarray, 1, &offset) < 0) { + errno = ENOMEM; + return -1; + } + + efd = &efds[offset]; + + fd = zvfs_reserve_fd(); + if (fd < 0) { + sys_bitarray_free(&efds_bitarray, 1, offset); + return -1; + } + + efd->flags = ZVFS_EFD_IN_USE | flags; + efd->cnt = initval; + + k_poll_signal_init(&efd->write_sig); + k_poll_signal_init(&efd->read_sig); + + if (initval != 0) { + k_poll_signal_raise(&efd->read_sig, 0); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + zvfs_finalize_fd(fd, efd, &zvfs_eventfd_fd_vtable); + + return fd; +} + +int zvfs_eventfd_read(int fd, zvfs_eventfd_t *value) +{ + int ret; + void *obj; + int err; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + err = k_mutex_lock(lock, K_FOREVER); + __ASSERT(err == 0, "k_mutex_lock() failed: %d", err); + + ret = zvfs_eventfd_rw_op(obj, value, sizeof(zvfs_eventfd_t), zvfs_eventfd_read_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + + err = k_mutex_unlock(lock); + __ASSERT(err == 0, "k_mutex_unlock() failed: %d", err); + + if (ret < 0) { + return -1; + } + + return 0; +} + +int zvfs_eventfd_write(int fd, zvfs_eventfd_t value) +{ + int ret; + void *obj; + int err; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + err = k_mutex_lock(lock, K_FOREVER); + __ASSERT(err == 0, "k_mutex_lock() failed: %d", err); + + ret = zvfs_eventfd_rw_op(obj, &value, sizeof(zvfs_eventfd_t), zvfs_eventfd_write_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + + err = k_mutex_unlock(lock); + __ASSERT(err == 0, "k_mutex_unlock() failed: %d", err); + + if (ret < 0) { + return -1; + } + + return 0; +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/check.py b/tasks/runtime-zephyr-eventfd-lock-progress/check.py new file mode 100644 index 00000000..8c79fe4f --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/check.py @@ -0,0 +1,243 @@ +"""Trusted parent: compile raw observer, independently model all outcomes.""" +import errno +import io +from pathlib import Path +import random +import shutil +import subprocess +import sys +import tarfile +import tempfile + +ROOT = Path(__file__).resolve().parent +CANDIDATE = 'lib/os/zvfs/zvfs_eventfd.c' +LIMIT = (1 << 64) - 2 +SENTINEL = 12345 +NONBLOCK, SEMAPHORE = 0x4000, 2 +checks = 0 + + +def require(condition, message): + global checks + checks += 1 + if not condition: + raise AssertionError(message) + + +def run(binary, request): + result = subprocess.run([str(binary)], input=request, text=True, + capture_output=True, timeout=3) + require(result.returncode == 0, 'observer terminated: ' + result.stderr[-500:]) + lines = result.stdout.splitlines() + require(len(lines) < 300, 'unexpected observation count') + return [line.split() for line in lines] + + +def obs(lines, phase, index): + records = [row for row in lines if row[:3] == ['o', str(phase), str(index)]] + require(len(records) == 1 and len(records[0]) == 8, 'missing raw observation') + values = list(map(int, records[0][3:])) + # Successful APIs do not specify errno; preserve only failure errno for comparison. + if values[1] >= 0: + values[2] = 0 + return tuple(values) + + +def sequential(binary, initial, flags, operations): + lines = run(binary, 'seq %d %d %d\n' % (initial, flags, len(operations)) + + ''.join('%d %d %d %d %d\n' % op for op in operations)) + created = lines[0] + valid = not (flags & ~(NONBLOCK | SEMAPHORE)) + require(created[0] == 'create' and len(created) == 3, 'creation observation') + require((int(created[1]) >= 0) == valid and + (valid or int(created[2]) == errno.EINVAL), 'creation result') + count = initial + for index, (kind, value, size, nullbuf, given_fd) in enumerate(operations): + actual = obs(lines, 0, index) + ret, error, out, revents = 0, 0, value, 0 + fd_valid = valid and given_fd == -999 + if not fd_valid: + if kind == 5: + ret, revents = (1, 32) if given_fd != -1 else (0, 0) + else: + ret, error = -1, errno.EBADF + elif kind == 4: + valid = False + elif kind == 5: + revents = (1 if count and value & 1 else 0) | (4 if count < LIMIT and value & 4 else 0) + ret = int(bool(revents)) + elif kind == 6: + ret = flags + elif kind == 7: + if value & ~(NONBLOCK | SEMAPHORE): + ret, error = -1, errno.EINVAL + else: + flags = value + elif kind in (0, 1) and size < 8: + ret, error = -1, errno.EINVAL + elif kind in (0, 1, 2) and nullbuf: + ret, error = -1, errno.EFAULT + elif kind in (1, 3): + if value == LIMIT + 1: + ret, error = -1, errno.EINVAL + elif value > LIMIT - count: + ret, error = -1, errno.EAGAIN + else: + count += value + ret = 8 if kind == 1 else 0 + else: + if not count: + ret, error = -1, errno.EAGAIN + else: + out = 1 if flags & SEMAPHORE else count + count -= out + ret = 8 if kind == 0 else 0 + require(actual == (1, ret, error, out, revents), + 'sequential mismatch at operation %d: %r expected %r' % + (index, actual, (1, ret, error, out, revents))) + + +def concurrency(binary): + for flags in (0, NONBLOCK, SEMAPHORE, NONBLOCK | SEMAPHORE): + for kind in (0, 1, 2, 3, 4): + lines = run(binary, 'guard %d %d 9 6\n' % (kind, flags)) + require(['guard', '0', '1', '0'] in lines, 'guard held') + require(obs(lines, 0, 0)[0] == 0, 'operation completed while descriptor access held') + require(['guard', '1', '1', '1'] in lines, 'guard released') + result = obs(lines, 1, 0) + out = 1 if flags & SEMAPHORE else 9 + expected_value = out if kind in (0, 2) else 6 + expected_ret = 8 if kind in (0, 1) else 0 + require(result == (1, expected_ret, 0, expected_value, 0), 'operation did not resume after release') + for api in (0, 1): + for actor_api in (0, 1): + for sem in (0, 1): + for n in (1, 3): + lines = run(binary, 'wait 0 %d %d %d %d 7\n' % (api, actor_api, sem, n)) + for i in range(n): + require(obs(lines, 0, i)[0] == 0, 'empty read did not wait') + returned = [obs(lines, 1, i) for i in range(n)] + complete = [item for item in returned if item[0]] + require(len(complete) == (n if sem else 1), 'readers failed to make progress') + for item in complete: + require(item == (1, 0 if api else 8, 0, 1 if sem else 7, 0), 'read wake value') + require(obs(lines, 1, n) == (1, 0 if actor_api else 8, 0, 7, 0), 'writer progress') + lines = run(binary, 'wait 1 %d %d %d 1 13\n' % (api, actor_api, sem)) + require(obs(lines, -1, 0) == (1, 0, 0, LIMIT, 0), 'fill counter') + require(obs(lines, 0, 0)[0] == 0, 'full write did not wait') + if sem: + # One semaphore read releases one unit, insufficient for a write of 13. + require(obs(lines, 1, 0)[0] == 0, 'writer exceeded capacity') + value = 1 + else: + require(obs(lines, 1, 0) == (1, 0 if api else 8, 0, 13, 0), 'full writer failed to resume') + require(obs(lines, 2, 0) == (1, 8, 0, 13, 0), 'resumed write was not committed') + value = LIMIT + require(obs(lines, 1, 1) == (1, 0 if actor_api else 8, 0, value, 0), 'space-making read') + for n in (1, 3): + lines = run(binary, 'wait 2 %d 0 0 %d 0\n' % (api, n)) + for i in range(n): + require(obs(lines, 0, i)[0] == 0, 'close waiter setup') + require(obs(lines, 1, i) == (1, -1, errno.EBADF, SENTINEL, 0), 'close did not release waiter') + require(obs(lines, 1, n) == (1, 0, 0, 0, 0), 'close did not complete') + lines = run(binary, 'wait 4 %d 0 0 %d 13\n' % (api, n)) + require(obs(lines, -1, 0) == (1, 0, 0, LIMIT, 0), 'closing full counter setup') + for i in range(n): + require(obs(lines, 0, i)[0] == 0, 'close writer setup') + require(obs(lines, 1, i) == (1, -1, errno.EBADF, 13, 0), 'close did not release writer') + require(obs(lines, 1, n) == (1, 0, 0, 13, 0), 'full close did not complete') + for writer_api in (0, 1): + lines = run(binary, 'wait 3 0 %d 0 1 23\n' % writer_api) + require(obs(lines, 0, 0)[0] == 0, 'empty poll did not wait') + require(obs(lines, 1, 0) == (1, 1, 0, 1, 1), 'poll was not awakened by event data') + require(obs(lines, 1, 1) == (1, 0 if writer_api else 8, 0, 23, 0), 'poll writer') + + +def main(candidate): + with tempfile.TemporaryDirectory(prefix='eventfd-grade-') as temporary: + work = Path(temporary) + # Never inherit archive or host ownership. Compiler and candidate use UID 65534. + with tarfile.open(ROOT / 'support.tar', 'r') as archive: + for member in archive.getmembers(): + target = work / member.name + require(member.isfile() and not Path(member.name).is_absolute() and + '..' not in Path(member.name).parts, 'invalid support member') + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(archive.extractfile(member).read()) + target.chmod(0o644) + target = work / CANDIDATE + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes((candidate / CANDIDATE).read_bytes()) + target.chmod(0o644) + sources = [CANDIDATE, 'lib/os/zvfs/zvfs_fdtable.c', 'lib/os/zvfs/zvfs_poll.c', + 'kernel/condvar.c', 'host/host.c', 'observe.c'] + binary = work / 'observe' + result = subprocess.run(['gcc', '-std=c11', '-O0', '-Iinclude', '-Ihost', + '-ffunction-sections', '-fdata-sections', *sources, + '-Wl,--gc-sections', '-o', str(binary)], + cwd=work, text=True, capture_output=True, timeout=20) + require(result.returncode == 0, 'build failed: ' + result.stderr[-1000:]) + # Public error, counter, readiness, flag, and lifecycle contracts. + rng = random.Random(0xaa607f9) + for sem in (0, SEMAPHORE): + for initial in (0, 1, 7, (1 << 32) - 1): + ops = [(5, 5, 0, 0, -999), (0, SENTINEL, 8, 0, -999), + (3, 0, 8, 0, -999), (2, SENTINEL, 8, 0, -999)] + for _ in range(12): + value = rng.randrange(1, 100000) + ops += [(1, value, 16, 0, -999), (5, 5, 0, 0, -999), + (2, SENTINEL, 8, 0, -999)] + ops += [(6, 0, 0, 0, -999), (7, 0x8000, 0, 0, -999), + (6, 0, 0, 0, -999), (4, 0, 0, 0, -999), + (2, SENTINEL, 8, 0, -999), (3, 4, 8, 0, -999), + (0, SENTINEL, 8, 0, -999), (1, 5, 8, 0, -999), + (4, 0, 0, 0, -999), (5, 5, 0, 0, -999)] + sequential(binary, initial, NONBLOCK | sem, ops) + for size in (0, 1, 7, 8, 9, 16): + sequential(binary, 3, NONBLOCK, [(0, SENTINEL, size, 1, -999), + (1, 2, size, 1, -999), (0, SENTINEL, size, 0, -999), + (1, 2, size, 0, -999), (2, SENTINEL, 8, 1, -999)]) + sequential(binary, 0, NONBLOCK, [(3, LIMIT, 8, 0, -999), (5, 5, 0, 0, -999), + (1, 0, 8, 0, -999), (3, 1, 8, 0, -999), + (3, 2, 8, 0, -999), (1, LIMIT, 8, 0, -999), + (1, LIMIT + 1, 8, 0, -999), (2, SENTINEL, 8, 0, -999), + (5, 5, 0, 0, -999), (3, LIMIT + 1, 8, 0, -999)]) + sequential(binary, 0, NONBLOCK, [(kind, 5 if kind == 5 else SENTINEL, 8, 0, fd) + for fd in (-1, 16, 222) for kind in (0, 1, 2, 3, 4, 5, 6, 7)]) + for flags in (1, 4, 0x8000, NONBLOCK | 1): + sequential(binary, 0, flags, []) + sequential(binary, 4, NONBLOCK, [(7, NONBLOCK | SEMAPHORE, 0, 0, -999), + (6, 0, 0, 0, -999), (0, SENTINEL, 8, 0, -999), + (7, NONBLOCK, 0, 0, -999), (2, SENTINEL, 8, 0, -999)]) + for sem in (0, SEMAPHORE): + sequential(binary, 0, NONBLOCK | sem, + [(3, LIMIT - 1, 8, 0, -999), (1, 1, 8, 0, -999)] + + [(5, mask, 0, 0, -999) for mask in (0, 1, 4, 5)] + + [(2, SENTINEL, 8, 0, -999), (1, 1, 8, 0, -999)] + + [(5, mask, 0, 0, -999) for mask in (0, 1, 4, 5)]) + sequential(binary, 4, NONBLOCK, [(7, SEMAPHORE, 0, 0, -999), + (0, SENTINEL, 8, 0, -999), (7, 0, 0, 0, -999), + (2, SENTINEL, 8, 0, -999), (1, 11, 8, 0, -999), + (0, SENTINEL, 8, 0, -999)]) + concurrency(binary) + lines = run(binary, 'pool\n') + first = [row for row in lines if row[0] == 'pool'] + second = [row for row in lines if row[0] == 'pool2'] + require(len(first) == len(second) == 8, 'pool observations') + for rows in (first, second): + require(len({row[2] for row in rows}) == 8 and + all(int(row[2]) >= 0 for row in rows), 'pool allocation/reuse') + require(['extra', '-1', str(errno.ENOMEM)] in lines, 'pool exhaustion') + for i in range(8): + require(obs(lines, 0, i) == (1, 0, 0, 0, 0), 'pool release') + require(obs(lines, 1, i) == (1, 8, 0, i + 33, 0), 'reused counter') + print('checks=%d' % checks) + print('PASS') + + +if __name__ == '__main__': + try: + main(Path(sys.argv[1])) + except (AssertionError, subprocess.TimeoutExpired) as error: + print('FAIL: ' + str(error)) + raise SystemExit(1) diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/gold/lib/os/zvfs/zvfs_eventfd.c b/tasks/runtime-zephyr-eventfd-lock-progress/gold/lib/os/zvfs/zvfs_eventfd.c new file mode 100644 index 00000000..deab024e --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/gold/lib/os/zvfs/zvfs_eventfd.c @@ -0,0 +1,505 @@ +/* + * Copyright (c) 2020 Tobias Svehagen + * Copyright (c) 2023, Meta + * Copyright (c) 2024, Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include + +#define ZVFS_EFD_IN_USE 0x1 +#define ZVFS_EFD_FLAGS_SET (ZVFS_EFD_SEMAPHORE | ZVFS_EFD_NONBLOCK) + +struct zvfs_eventfd { + struct k_poll_signal read_sig; + struct k_poll_signal write_sig; + struct k_spinlock lock; + zvfs_eventfd_t cnt; + int flags; +}; + +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)); + +SYS_BITARRAY_DEFINE_STATIC(efds_bitarray, CONFIG_ZVFS_EVENTFD_MAX); +static struct zvfs_eventfd efds[CONFIG_ZVFS_EVENTFD_MAX]; +static const struct fd_op_vtable zvfs_eventfd_fd_vtable; + +static inline bool zvfs_eventfd_is_in_use(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_IN_USE) != 0; +} + +static inline bool zvfs_eventfd_is_semaphore(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_SEMAPHORE) != 0; +} + +static inline bool zvfs_eventfd_is_blocking(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_NONBLOCK) == 0; +} + +static int zvfs_eventfd_poll_prepare(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev, + struct k_poll_event *pev_end) +{ + if (pfd->events & ZSOCK_POLLIN) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->read_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->write_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_poll_update(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev) +{ + if (pfd->events & ZSOCK_POLLIN) { + pfd->revents |= ZSOCK_POLLIN * (efd->cnt > 0); + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + pfd->revents |= ZSOCK_POLLOUT * (efd->cnt < UINT64_MAX - 1); + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_read_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (efd->cnt == 0) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful read */ + if (zvfs_eventfd_is_semaphore(efd)) { + *value = 1; + --efd->cnt; + } else { + *value = efd->cnt; + efd->cnt = 0; + } + + if (efd->cnt == 0) { + k_poll_signal_reset(&efd->read_sig); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + return 0; +} + +static int zvfs_eventfd_write_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + zvfs_eventfd_t result; + + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (*value == UINT64_MAX) { + /* not a permitted value */ + return -EINVAL; + } + + if (u64_add_overflow(efd->cnt, *value, &result) || result == UINT64_MAX) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful write */ + efd->cnt = result; + + if (efd->cnt == (UINT64_MAX - 1)) { + k_poll_signal_reset(&efd->write_sig); + } + + k_poll_signal_raise(&efd->read_sig, 0); + + return 0; +} + +static ssize_t zvfs_eventfd_read_op(void *obj, void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, buf, sz, zvfs_eventfd_read_locked); +} + +static ssize_t zvfs_eventfd_write_op(void *obj, const void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, (zvfs_eventfd_t *)buf, sz, zvfs_eventfd_write_locked); +} + +static int zvfs_eventfd_close_op(void *obj) +{ + int ret; + int err; + k_spinlock_key_t key; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + err = sys_bitarray_free(&efds_bitarray, 1, (struct zvfs_eventfd *)obj - efds); + __ASSERT(err == 0, "sys_bitarray_free() failed: %d", err); + + efd->flags = 0; + efd->cnt = 0; + + ret = 0; + +unlock: + k_spin_unlock(&efd->lock, key); + /* when closing an zvfs_eventfd, broadcast to all waiters */ + err = k_condvar_broadcast(cond); + __ASSERT(err == 0, "k_condvar_broadcast() failed: %d", err); + + return ret; +} + +static int zvfs_eventfd_ioctl_op(void *obj, unsigned int request, va_list args) +{ + int ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + /* note: zsock_poll_internal() has already taken the mutex */ + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + switch (request) { + case F_GETFL: + ret = efd->flags & ZVFS_EFD_FLAGS_SET; + break; + + case F_SETFL: { + int flags; + + flags = va_arg(args, int); + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + ret = -1; + } else { + int prev_flags = efd->flags & ~ZVFS_EFD_FLAGS_SET; + + efd->flags = flags | prev_flags; + ret = 0; + } + } break; + + case ZFD_IOCTL_POLL_PREPARE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + struct k_poll_event *pev_end; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + pev_end = va_arg(args, struct k_poll_event *); + + ret = zvfs_eventfd_poll_prepare(obj, pfd, pev, pev_end); + } break; + + case ZFD_IOCTL_POLL_UPDATE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + + ret = zvfs_eventfd_poll_update(obj, pfd, pev); + } break; + + default: + errno = EOPNOTSUPP; + ret = -1; + break; + } + +unlock: + k_spin_unlock(&efd->lock, key); + + return ret; +} + +static const struct fd_op_vtable zvfs_eventfd_fd_vtable = { + .read = zvfs_eventfd_read_op, + .write = zvfs_eventfd_write_op, + .close = zvfs_eventfd_close_op, + .ioctl = zvfs_eventfd_ioctl_op, +}; + +/* common to both zvfs_eventfd_read_op() and zvfs_eventfd_write_op() */ +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)) +{ + int err; + ssize_t ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = obj; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + if (sz < sizeof(zvfs_eventfd_t)) { + errno = EINVAL; + return -1; + } + + if (buf == NULL) { + errno = EFAULT; + return -1; + } + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_blocking(efd)) { + /* + * Handle the non-blocking case entirely within this scope + */ + ret = op(efd, buf); + if (ret < 0) { + errno = -ret; + ret = -1; + } else { + ret = sizeof(zvfs_eventfd_t); + } + + goto unlock_spin; + } + + /* + * Handle the blocking case below + */ + __ASSERT_NO_MSG(zvfs_eventfd_is_blocking(efd)); + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + ret = -1; + goto unlock_spin; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + k_spin_unlock(&efd->lock, key); + + while (true) { + /* retake the spinlock */ + key = k_spin_lock(&efd->lock); + + ret = op(efd, buf); + switch (ret) { + case -EAGAIN: + /* not an error in blocking mode. break and try again */ + break; + case 0: + /* success! */ + ret = sizeof(zvfs_eventfd_t); + goto unlock; + default: + /* some other error */ + __ASSERT_NO_MSG(ret < 0); + errno = -ret; + ret = -1; + goto unlock; + } + + /* do not hold a spinlock when taking a mutex */ + k_spin_unlock(&efd->lock, key); + + /* wait for a write or close */ + err = k_condvar_wait(cond, lock, K_FOREVER); + __ASSERT(err == 0, "k_condvar_wait() failed: %d", err); + } + +unlock: + k_spin_unlock(&efd->lock, key); + /* only wake a single waiter */ + err = k_condvar_signal(cond); + __ASSERT(err == 0, "k_condvar_signal() failed: %d", err); + goto out; + +unlock_spin: + k_spin_unlock(&efd->lock, key); + +out: + return ret; +} + +/* + * Public-facing API + */ + +int zvfs_eventfd(unsigned int initval, int flags) +{ + int fd = 1; + size_t offset; + struct zvfs_eventfd *efd = NULL; + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + return -1; + } + + if (sys_bitarray_alloc(&efds_bitarray, 1, &offset) < 0) { + errno = ENOMEM; + return -1; + } + + efd = &efds[offset]; + + fd = zvfs_reserve_fd(); + if (fd < 0) { + sys_bitarray_free(&efds_bitarray, 1, offset); + return -1; + } + + efd->flags = ZVFS_EFD_IN_USE | flags; + efd->cnt = initval; + + k_poll_signal_init(&efd->write_sig); + k_poll_signal_init(&efd->read_sig); + + if (initval != 0) { + k_poll_signal_raise(&efd->read_sig, 0); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + zvfs_finalize_fd(fd, efd, &zvfs_eventfd_fd_vtable); + + return fd; +} + +int zvfs_eventfd_read(int fd, zvfs_eventfd_t *value) +{ + int ret; + void *obj; + int err; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + err = k_mutex_lock(lock, K_FOREVER); + __ASSERT(err == 0, "k_mutex_lock() failed: %d", err); + + ret = zvfs_eventfd_rw_op(obj, value, sizeof(zvfs_eventfd_t), zvfs_eventfd_read_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + + err = k_mutex_unlock(lock); + __ASSERT(err == 0, "k_mutex_unlock() failed: %d", err); + + if (ret < 0) { + return -1; + } + + return 0; +} + +int zvfs_eventfd_write(int fd, zvfs_eventfd_t value) +{ + int ret; + void *obj; + int err; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + err = k_mutex_lock(lock, K_FOREVER); + __ASSERT(err == 0, "k_mutex_lock() failed: %d", err); + + ret = zvfs_eventfd_rw_op(obj, &value, sizeof(zvfs_eventfd_t), zvfs_eventfd_write_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + + err = k_mutex_unlock(lock); + __ASSERT(err == 0, "k_mutex_unlock() failed: %d", err); + + if (ret < 0) { + return -1; + } + + return 0; +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/noop/lib/os/zvfs/zvfs_eventfd.c b/tasks/runtime-zephyr-eventfd-lock-progress/noop/lib/os/zvfs/zvfs_eventfd.c new file mode 100644 index 00000000..8fe38419 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/noop/lib/os/zvfs/zvfs_eventfd.c @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +int zvfs_eventfd(unsigned int initial,int flags) { (void)initial;(void)flags;errno=ENOSYS;return -1; } +int zvfs_eventfd_read(int fd,zvfs_eventfd_t *value) { (void)fd;(void)value;errno=ENOSYS;return -1; } +int zvfs_eventfd_write(int fd,zvfs_eventfd_t value) { (void)fd;(void)value;errno=ENOSYS;return -1; } diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/prompt.md b/tasks/runtime-zephyr-eventfd-lock-progress/prompt.md new file mode 100644 index 00000000..9c3359ce --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/prompt.md @@ -0,0 +1,5 @@ +在这个事件通知工作区中,基础自测能够完成;把相同计数器接入多个执行上下文后,现场出现了消费者等不到后续数据、生产者也不再返回的现象。另一些调用组合会在另一个上下文尚占用描述符时提前结束,单独运行时却正常。 + +请修复模块,让两种调用接口都符合 `docs/api-contract.md` 的全部条款,并保持计数、错误返回、关闭和 poll 的既有行为。当前目录包含生产代码与一个可在主机运行的受控内核端口;不需要板卡或网络。 + +运行 `make smoke` 可做基础自测。可使用 README 中的上下文接口增加复现。最终只提交并评分 `lib/os/zvfs/zvfs_eventfd.c`。 diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/spec.md b/tasks/runtime-zephyr-eventfd-lock-progress/spec.md new file mode 100644 index 00000000..ee5fcd8e --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/spec.md @@ -0,0 +1,102 @@ +# runtime-zephyr-eventfd-lock-progress + +## 1. 身份与状态 + +路线D;族 linux-concurrency;语言C;split `zephyrproject-rtos/zephyr`。 +状态 CALIBRATED,难度/真实Agent/硬件 UNMEASURED。此题仅做受控软件校准。 +上游 https://github.com/zephyrproject-rtos/zephyr ;修复前 `48ab80061b07247e0a9606785bb600e777588718`,修复 `aa607f9b0a6d3ac0976debc26d70caf6dfb076c7`。 +所有冻结C/H文件均有 Apache-2.0 SPDX头,根LICENSE原样附带;上游根没有NOTICE。本地支撑同为Apache-2.0。 + +## 2. 真实缺陷与冻结范围 + +只有一个上游真实修复,但它跨 `zvfs_eventfd_close_op`、`zvfs_eventfd_rw_op`、`zvfs_eventfd_read`、`zvfs_eventfd_write` 四个函数:描述符层已持对象mutex时,内部公共读写再取得递归mutex,条件等待只释放一层,别的上下文无法获取同一mutex并提供数据/空间;直接入口仍需取得描述符mutex,包含非阻塞操作。close亦按描述符层合同修复。 +没有额外人工缺陷。gold是修复提交中候选文件的完整原样版本,没有补改gold。 + +完整冻结真实eventfd(485行)、fdtable、poll和kernel/condvar,保持对象查找/锁/条件等待/通知/轮询链。内核mutex按上游文档提供递归语义;条件等待采用原样 `kernel/condvar.c`,没有用桩替换目标锁策略。host port只做通用上下文调度、mutex、spinlock、poll信号和未用FS构建依赖。没有基于候选函数、题目输入或答案选择端口行为。 + +唯一候选 `lib/os/zvfs/zvfs_eventfd.c`。全部template代码实际 2249 行,不计fixture重复、许可证、判据和文档。源文件清单如下: + +| 文件 | 来源 | 行数 | SHA-256 | +| --- | --- | --- | --- | +| `LICENSE` | 上游冻结 | 202 | c6596eb7be8581c18be736c846fb9173b69eccf6ef94c5135893ec56bd92ba08 | +| `Makefile` | 补全 | 12 | 3cac8e423dee7bbe4f16e9c1cead11d1b998d0a96f69d0019b8a3a47971926d1 | +| `README.md` | 补全 | 17 | 412e3366d958b4c620341f53d3164eceb126e95049a7b83e66529677879ca79e | +| `doc/kernel/services/synchronization/mutexes.rst` | 上游冻结 | 188 | c33da75bb8bd6301803ca1c2046d0544774ce12c29dbd442c198114281c103e9 | +| `docs/api-contract.md` | 补全 | 85 | 6845c44320d0d3ab6f3c2f0c0e540ac53a40774eb8ab1db93d95dd1122a64c49 | +| `host/host.c` | 补全 | 143 | d85e6731a38d105e09dc2b9d7dbc555419307b011a8696b3bbf7ea001a0efc66 | +| `host/host.h` | 补全 | 11 | b0ed82a4531be6fcfb8d060c2c831c47d36c5ef1263323ede318baad0bfcdf2a | +| `include/ksched.h` | 补全 | 7 | 8c79cecc739a0e02f4bb5489c7f7eebeee4fcd199b0663b23a5b24a354e31fbf | +| `include/wait_q.h` | 补全 | 2 | 2e8bb5be21fcf250cbdf424e371db99a5bdcdb43245a21129461c484b0783eb0 | +| `include/zephyr/fs/fs.h` | 补全 | 11 | d2fd6d3a61882423db697b0cab78dc0f8464a65115c610e5d92321e3e9b8f9b2 | +| `include/zephyr/init.h` | 补全 | 1 | f7096fb07fe51ed00a8602375ab4bca01f3bb802579c48e8e671f32cda3d52af | +| `include/zephyr/internal/syscall_handler.h` | 补全 | 1 | f7096fb07fe51ed00a8602375ab4bca01f3bb802579c48e8e671f32cda3d52af | +| `include/zephyr/kernel.h` | 补全 | 73 | 10e99fc7b3a5ed1e6501d91903e70b646d2a517a3c508ca3044b7ec85284aa7b | +| `include/zephyr/kernel_structs.h` | 补全 | 2 | 2e8bb5be21fcf250cbdf424e371db99a5bdcdb43245a21129461c484b0783eb0 | +| `include/zephyr/net/socket.h` | 补全 | 5 | 944f9288d801195baf4a6a3ba564da68e61ad8e214144c7995921d5880b2096b | +| `include/zephyr/posix/fcntl.h` | 补全 | 4 | e11d2b43c7f88dfa0132c21242d2c21c634f47dfc90d84b429a626da59cebd98 | +| `include/zephyr/sys/atomic.h` | 补全 | 11 | 2ff0167c3f1764db70000b700f61c3ca0515dce8b80827b801f90b96193fb843 | +| `include/zephyr/sys/bitarray.h` | 补全 | 19 | 74ef9f3e9bff5459cabd6854d3646386384445ed5a18851fc38541100fe7b6b3 | +| `include/zephyr/sys/fdtable.h` | 上游冻结 | 389 | a4312961a50308365125fc9cd3d6552439d621483175adc0d9a58d5d17ad529b | +| `include/zephyr/sys/math_extras.h` | 补全 | 5 | 8b2cd57086b1064ec02c0ef4ff4bd45687e1b7902bc795f14c389a04456a0682 | +| `include/zephyr/sys/speculation.h` | 补全 | 2 | 58778ceae9e14e94222c10758b38712be0e3bcfb2d3e6f05e1b109f76f8b8b31 | +| `include/zephyr/sys/util.h` | 补全 | 11 | b7e6bb0ee272b6d925fc6029dd0a6c9ec24b2f2ed03b1935e36c612f8fc723c7 | +| `include/zephyr/syscalls/fdtable.h` | 补全 | 1 | f7096fb07fe51ed00a8602375ab4bca01f3bb802579c48e8e671f32cda3d52af | +| `include/zephyr/toolchain.h` | 补全 | 2 | 2e8bb5be21fcf250cbdf424e371db99a5bdcdb43245a21129461c484b0783eb0 | +| `include/zephyr/zvfs/eventfd.h` | 上游冻结 | 66 | 91c48f80e88f645439aab5fa84a464f1202e7310e745570c751c070e5821b873 | +| `kernel/condvar.c` | 上游冻结 | 163 | 2c5dc96ee4c881704627873ca5dc1d8b241c8bb8369faa0b624a21ad466b2bae | +| `lib/os/zvfs/zvfs_eventfd.c` | 上游冻结 | 485 | b5f83ce96efc056a93fd591e57acec8a2e8615fda6487387dddbf440f918a856 | +| `lib/os/zvfs/zvfs_fdtable.c` | 上游冻结 | 609 | e8a3445e563162faf304d4fccbd179b1d917e14fd524d59f731561ab83225896 | +| `lib/os/zvfs/zvfs_poll.c` | 上游冻结 | 213 | c0f5b9cd784c4ecef0c6e177a156e64af2f605d9f8df4bcd2ef71fad5e903dee | +| `smoke.c` | 补全 | 13 | 068390d918875954dc93956183e430c29db96c3be862aed1f38774d06ce3f0f3 | + +补全目的:`host/host.[ch]` 为协作上下文、通用内核原语;include下非冻结头为最小依赖;Makefile和smoke.c为正常非阻塞往返;README与api-contract为公开合同整理。未修改冻结源;无上游git、修复测试、issue/提交信息进入公开材料。 + +## 3. 公开依据和判据矩阵 + +所有正式期望在父 `check.py` 中独立模型计算。C observer只执行调用、打印原始返回/errno/值/完成标志/重入事件,不计算PASS或预期关系。 + +| 检查项 | 公开依据 | 上游依据 | +| --- | --- | --- | +| 创建初值/合法flags/非法flags | docs/api-contract.md:8-12 R01 | eventfd.c `zvfs_eventfd`;eventfd.h flags/API | +| 池耗尽与关闭复用 | docs/api-contract.md:11-12 R01 | efds_bitarray分配/close释放 | +| 描述符返回8及size 0/1/7/8/9/16 | docs/api-contract.md:14-18 R02 | eventfd.h:33-34;rw_op大小校验 | +| NULL缓冲区和错误不改读值 | docs/api-contract.md:18,22,26 R02-R04 | rw_op NULL校验/locked读先验 | +| 直接读写返回0和常规读写等效 | docs/api-contract.md:20-22 R03 | eventfd.h:25-29,40-61 | +| 普通读归零/信号量逐单位读取 | docs/api-contract.md:24-26 R04 | read_locked | +| 累加、零写、最大值、超过容量及无效写值 | docs/api-contract.md:28-32 R05 | write_locked | +| 非阻塞空/容量错误EAGAIN | docs/api-contract.md:34-40 R06 | rw_op非阻塞分支与locked EAGAIN | +| 读等待、写等待、双API交叉唤醒和多等待者 | docs/api-contract.md:34-40 R06 | rw_op/condvar_wait;kernel/condvar.c | +| 持描述符mutex时完成边界/释放后继续 | docs/api-contract.md:42-47 R07 | fdtable.h:187-215 mutex防并发;fdtable.c zvfs_rw/close;direct公共接口同对象 | +| close唤醒全部读写等待者EBADF,旧/负/越界fd | docs/api-contract.md:49-51 R08 | close_op广播与read_locked EBADF;fdtable _check_fd | +| 零/非零/满计数的POLLIN/POLLOUT和等待唤醒 | docs/api-contract.md:53-58 R09 | eventfd poll_prepare/update;完整zvfs_poll.c | +| 负fd忽略及其他无效fd POLLNVAL | docs/api-contract.md:60-61 R10 | zvfs_poll.c负fd与查找失败分支 | +| GETFL/SETFL有效切换、非法位保持旧flags | docs/api-contract.md:63-66 R11 | eventfd_ioctl_op | +| 递归mutex和原样condvar单层release/reacquire | docs/api-contract.md:68-72 R12 | doc/kernel/services/synchronization/mutexes.rst Reentrant Locking;kernel/condvar.c:112-129 | +| 无可运行上下文阶段快照;不要求内部锁次数/helper | docs/api-contract.md:74-80 R13 | 补全host调度器的公开接口 | + +依据表16项。输入随机使用固定种子0xaa607f9,父check独立按R01-R11计算期望;不会取gold运行结果作答案。公开smoke只走单对象非阻塞正常路径,故意不暴露目标等待/互斥症状。 + +## 4. 样例 + +- gold PASS:修复提交中候选文件原样。 +- template FAIL:原样冻结真实缺陷。 +- noop FAIL:公开入口返回ENOSYS。 +- cheat FAIL:返回固定smoke读值7,破坏一般计数。 +- cheat_read_grader FAIL:以gold为底,要求读取私有check成功;拒读受控退出。另用无秘密可读占位文件验证可读分支,真实grader在/oracle保护。 +- symptom_only FAIL:仅采用gold的操作层改动,保留旧直接入口;描述符互等症状消失但直接非阻塞入口仍不能遵守对象互斥。 +- valid_shared_entry PASS:使用不同的共享直接入口,通过fd/vtable/mutex查找并验证vtable,统一保护直接读写,没有照抄gold两套入口。 + +总7类,每类正式校准5次,共35次。单缺陷不需要only-one样例。 + +## 5. 隔离、确定性和执行 + +verify.py与rollout/bos_verify.py原样一致;grader_files仅check.py与support.tar。归档包含非候选冻结/补全支撑和raw observer,不包含期望。tar成员uid/gid均0;父check逐个安全路径/常规文件校验并用write_bytes生成0644文件,避免继承host所有权。候选与gcc仅在子进程运行,由平台降至UID/GID65534;父check保留全部期望。无Python候选import,无shell外部命令。 + +软件端口使用协作上下文,每次推进到blocked/completed后观察,非真实时间/线程优先级判据。另一上下文实际执行描述符写/读/关闭/锁竞争,观察API完成和值,不以内部锁次数判定。调度无可运行项即返回,缺陷不会拖到墙钟超时。独立子进程总期限3秒防挂死,编译20秒,平台check总100秒、task期限120秒。 + +所有上游构建/运行仅在固定trace6 digest、--network none Docker内执行;不做trusted-local。正式校准路径、诊断、当前摘要和清理见本题report/progress。未运行真实Agent、模型、硬件或难度采样。 + +## 6. 校准与限制 + +正式校准35/35符合预期,cleanup confirmed;回执 `runs/20261103-D-oss-fix-29/zephyr-aa607f9/calibration-7d6a3e7ed0df450a87eb799481394a8d/receipt.json`,task digest `af1634f0c0133d6fbc824ab595171208021b6556f4385e46734b2d8142ae4269`。诊断03中gold/valid通过、负例拒绝,可读占位控制通过;原始等待/互斥/关闭观察见作者目录diagnostic-raw-01.jsonl。仅支持此无硬件host执行模型;不验证Zephyr真实调度器优先级/抢占、ISR、用户态保护、FS后端、墙钟超时或真实板卡。 +root负责独立只读审阅及全库unittest;作者不提交。 diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/support.tar b/tasks/runtime-zephyr-eventfd-lock-progress/support.tar new file mode 100644 index 0000000000000000000000000000000000000000..3dd9d06c1783cba3f0d203b8a06e67ea526f9e83 GIT binary patch literal 102400 zcmeHwdtV#Lv3CBoKE*6KaRe6P=F0{zn_v*TdKUu+@Om%r{$wGIKtV`~Ml#0EzMuVl zo~r7ep3#Vl*WM)O;9s)9Oiy=pbyaoUx_Y<1RogqN{o!-|TgJbqk01YmKk+a3bM@)U z+Ee#T->)n`dA9n8X!&yj<@%l^dWS4Xu8Hlw*HIcxTM(M8Df3MT=wHp1d#AycYGB!C*!lhsQX^VGoUz? z{1@Z1<~YKjr=uo5?4Qbf4G#eT@oBRc)uihwASQiIP@s>aCMJen927tUFlplt0DuA5 zjT4N7p@GoxXwa)f%@KiAfYoRRA90?; z$!Iu8Fp9^jUvRfZbJok8EOZi;yYu?#;4&UnuzVw|5`pMq&G~yJ8V{mY6N}Dv28zBC z42+_SX1{qFbMZKCGHIO|CMwb8SuE4X>dUx56#5P^bJ-;|VZ3q|kjR22XWb!t>U29; z(_uUU$Bm=%ljT350frj@J&JhO(PRwa1$eOrAmUM+xE|5vN!$k#T3sw%B(FpU#G~1c>#<6u?nUn|vzWfClUtDjOi) z6jNx7v4Pg+c+4$K2!S0FTI-?=;*Q@nFNPQyA40T)%=JUI+#C*Lkp4R`Q*UrNpYqFg zJnFv1LcNV6A|%ORgcQIrig6I>>IgfPhq zmuFoF96)J3VX?;81HhuA_$^ow{6{JVYJ{NhM+^)Pl$cde*fNLQWSw!BixY?+!G05i z4SHZ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ZZO^f1I|PqDkp+Y60B%g4y-kh&t(KhRnGE zA1mAg3-RGmwo(Bv+Yp}S(>bq5r35hUjVy@7m6QdS$$~}1Q$z>+GCRL?7hk1p)CL~9 z8#eOcV9=YEk%*aO(iq)s*P6*l66L=fKEu4n;DaKK%4>BT2#l9;jC%Lue7XAwwTfrC zNfn561LIV5AHjdA?sb7zc3;UWeJR^`KuGeaqBLq|VOg#~-uf+48l+p5{5jS6$75xR zDa7peO!3%Fhah>eENvW&G_gdLv!^*K_(Y3FT;;vlnMGS$yW6U*PA%IMRtfqPe#DFI z)S-}zQuMG%h#g^aeV9XzsS#N_$o~4*ySfX!pZAaMF>sH8dkoxT;2s0_7`Vs4|2_=- Ee+8v@^8f$< literal 0 HcmV?d00001 diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/symptom_only/lib/os/zvfs/zvfs_eventfd.c b/tasks/runtime-zephyr-eventfd-lock-progress/symptom_only/lib/os/zvfs/zvfs_eventfd.c new file mode 100644 index 00000000..802d79bb --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/symptom_only/lib/os/zvfs/zvfs_eventfd.c @@ -0,0 +1,475 @@ +/* + * Copyright (c) 2020 Tobias Svehagen + * Copyright (c) 2023, Meta + * Copyright (c) 2024, Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include + +#define ZVFS_EFD_IN_USE 0x1 +#define ZVFS_EFD_FLAGS_SET (ZVFS_EFD_SEMAPHORE | ZVFS_EFD_NONBLOCK) + +struct zvfs_eventfd { + struct k_poll_signal read_sig; + struct k_poll_signal write_sig; + struct k_spinlock lock; + zvfs_eventfd_t cnt; + int flags; +}; + +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)); + +SYS_BITARRAY_DEFINE_STATIC(efds_bitarray, CONFIG_ZVFS_EVENTFD_MAX); +static struct zvfs_eventfd efds[CONFIG_ZVFS_EVENTFD_MAX]; +static const struct fd_op_vtable zvfs_eventfd_fd_vtable; + +static inline bool zvfs_eventfd_is_in_use(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_IN_USE) != 0; +} + +static inline bool zvfs_eventfd_is_semaphore(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_SEMAPHORE) != 0; +} + +static inline bool zvfs_eventfd_is_blocking(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_NONBLOCK) == 0; +} + +static int zvfs_eventfd_poll_prepare(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev, + struct k_poll_event *pev_end) +{ + if (pfd->events & ZSOCK_POLLIN) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->read_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->write_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_poll_update(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev) +{ + if (pfd->events & ZSOCK_POLLIN) { + pfd->revents |= ZSOCK_POLLIN * (efd->cnt > 0); + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + pfd->revents |= ZSOCK_POLLOUT * (efd->cnt < UINT64_MAX - 1); + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_read_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (efd->cnt == 0) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful read */ + if (zvfs_eventfd_is_semaphore(efd)) { + *value = 1; + --efd->cnt; + } else { + *value = efd->cnt; + efd->cnt = 0; + } + + if (efd->cnt == 0) { + k_poll_signal_reset(&efd->read_sig); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + return 0; +} + +static int zvfs_eventfd_write_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + zvfs_eventfd_t result; + + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (*value == UINT64_MAX) { + /* not a permitted value */ + return -EINVAL; + } + + if (u64_add_overflow(efd->cnt, *value, &result) || result == UINT64_MAX) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful write */ + efd->cnt = result; + + if (efd->cnt == (UINT64_MAX - 1)) { + k_poll_signal_reset(&efd->write_sig); + } + + k_poll_signal_raise(&efd->read_sig, 0); + + return 0; +} + +static ssize_t zvfs_eventfd_read_op(void *obj, void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, buf, sz, zvfs_eventfd_read_locked); +} + +static ssize_t zvfs_eventfd_write_op(void *obj, const void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, (zvfs_eventfd_t *)buf, sz, zvfs_eventfd_write_locked); +} + +static int zvfs_eventfd_close_op(void *obj) +{ + int ret; + int err; + k_spinlock_key_t key; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + err = sys_bitarray_free(&efds_bitarray, 1, (struct zvfs_eventfd *)obj - efds); + __ASSERT(err == 0, "sys_bitarray_free() failed: %d", err); + + efd->flags = 0; + efd->cnt = 0; + + ret = 0; + +unlock: + k_spin_unlock(&efd->lock, key); + /* when closing an zvfs_eventfd, broadcast to all waiters */ + err = k_condvar_broadcast(cond); + __ASSERT(err == 0, "k_condvar_broadcast() failed: %d", err); + + return ret; +} + +static int zvfs_eventfd_ioctl_op(void *obj, unsigned int request, va_list args) +{ + int ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + /* note: zsock_poll_internal() has already taken the mutex */ + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + switch (request) { + case F_GETFL: + ret = efd->flags & ZVFS_EFD_FLAGS_SET; + break; + + case F_SETFL: { + int flags; + + flags = va_arg(args, int); + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + ret = -1; + } else { + int prev_flags = efd->flags & ~ZVFS_EFD_FLAGS_SET; + + efd->flags = flags | prev_flags; + ret = 0; + } + } break; + + case ZFD_IOCTL_POLL_PREPARE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + struct k_poll_event *pev_end; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + pev_end = va_arg(args, struct k_poll_event *); + + ret = zvfs_eventfd_poll_prepare(obj, pfd, pev, pev_end); + } break; + + case ZFD_IOCTL_POLL_UPDATE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + + ret = zvfs_eventfd_poll_update(obj, pfd, pev); + } break; + + default: + errno = EOPNOTSUPP; + ret = -1; + break; + } + +unlock: + k_spin_unlock(&efd->lock, key); + + return ret; +} + +static const struct fd_op_vtable zvfs_eventfd_fd_vtable = { + .read = zvfs_eventfd_read_op, + .write = zvfs_eventfd_write_op, + .close = zvfs_eventfd_close_op, + .ioctl = zvfs_eventfd_ioctl_op, +}; + +/* common to both zvfs_eventfd_read_op() and zvfs_eventfd_write_op() */ +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)) +{ + int err; + ssize_t ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = obj; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + if (sz < sizeof(zvfs_eventfd_t)) { + errno = EINVAL; + return -1; + } + + if (buf == NULL) { + errno = EFAULT; + return -1; + } + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_blocking(efd)) { + /* + * Handle the non-blocking case entirely within this scope + */ + ret = op(efd, buf); + if (ret < 0) { + errno = -ret; + ret = -1; + } else { + ret = sizeof(zvfs_eventfd_t); + } + + goto unlock_spin; + } + + /* + * Handle the blocking case below + */ + __ASSERT_NO_MSG(zvfs_eventfd_is_blocking(efd)); + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + ret = -1; + goto unlock_spin; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + k_spin_unlock(&efd->lock, key); + + while (true) { + /* retake the spinlock */ + key = k_spin_lock(&efd->lock); + + ret = op(efd, buf); + switch (ret) { + case -EAGAIN: + /* not an error in blocking mode. break and try again */ + break; + case 0: + /* success! */ + ret = sizeof(zvfs_eventfd_t); + goto unlock; + default: + /* some other error */ + __ASSERT_NO_MSG(ret < 0); + errno = -ret; + ret = -1; + goto unlock; + } + + /* do not hold a spinlock when taking a mutex */ + k_spin_unlock(&efd->lock, key); + + /* wait for a write or close */ + err = k_condvar_wait(cond, lock, K_FOREVER); + __ASSERT(err == 0, "k_condvar_wait() failed: %d", err); + } + +unlock: + k_spin_unlock(&efd->lock, key); + /* only wake a single waiter */ + err = k_condvar_signal(cond); + __ASSERT(err == 0, "k_condvar_signal() failed: %d", err); + goto out; + +unlock_spin: + k_spin_unlock(&efd->lock, key); + +out: + return ret; +} + +/* + * Public-facing API + */ + +int zvfs_eventfd(unsigned int initval, int flags) +{ + int fd = 1; + size_t offset; + struct zvfs_eventfd *efd = NULL; + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + return -1; + } + + if (sys_bitarray_alloc(&efds_bitarray, 1, &offset) < 0) { + errno = ENOMEM; + return -1; + } + + efd = &efds[offset]; + + fd = zvfs_reserve_fd(); + if (fd < 0) { + sys_bitarray_free(&efds_bitarray, 1, offset); + return -1; + } + + efd->flags = ZVFS_EFD_IN_USE | flags; + efd->cnt = initval; + + k_poll_signal_init(&efd->write_sig); + k_poll_signal_init(&efd->read_sig); + + if (initval != 0) { + k_poll_signal_raise(&efd->read_sig, 0); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + zvfs_finalize_fd(fd, efd, &zvfs_eventfd_fd_vtable); + + return fd; +} + +int zvfs_eventfd_read(int fd, zvfs_eventfd_t *value) +{ + int ret; + void *obj; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + ret = zvfs_eventfd_rw_op(obj, value, sizeof(zvfs_eventfd_t), zvfs_eventfd_read_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + if (ret < 0) { + return -1; + } + + return 0; +} + +int zvfs_eventfd_write(int fd, zvfs_eventfd_t value) +{ + int ret; + void *obj; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + ret = zvfs_eventfd_rw_op(obj, &value, sizeof(zvfs_eventfd_t), zvfs_eventfd_write_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + if (ret < 0) { + return -1; + } + + return 0; +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/task.json b/tasks/runtime-zephyr-eventfd-lock-progress/task.json new file mode 100644 index 00000000..2904afa0 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/task.json @@ -0,0 +1,27 @@ +{ + "id": "runtime-zephyr-eventfd-lock-progress", + "version": "1", + "family": "linux-concurrency", + "language": "c", + "source": "oss/zephyrproject-rtos/zephyr@aa607f9b0a6d", + "split": "zephyrproject-rtos/zephyr", + "template": "template", + "verifier": "verify.py", + "candidate_files": [ + "lib/os/zvfs/zvfs_eventfd.c" + ], + "grader_files": [ + "check.py", + "support.tar" + ], + "timeout_seconds": 120, + "fixtures": { + "gold": "PASS", + "template": "FAIL", + "noop": "FAIL", + "cheat": "FAIL", + "cheat_read_grader": "FAIL", + "symptom_only": "FAIL", + "valid_shared_entry": "PASS" + } +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/LICENSE b/tasks/runtime-zephyr-eventfd-lock-progress/template/LICENSE new file mode 100644 index 00000000..8f71f43f --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/LICENSE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/Makefile b/tasks/runtime-zephyr-eventfd-lock-progress/template/Makefile new file mode 100644 index 00000000..6b4e0096 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/Makefile @@ -0,0 +1,12 @@ +CC = gcc +CFLAGS = -std=c11 -O0 -g -Iinclude -Ihost -ffunction-sections -fdata-sections +SOURCES = lib/os/zvfs/zvfs_eventfd.c lib/os/zvfs/zvfs_fdtable.c lib/os/zvfs/zvfs_poll.c kernel/condvar.c host/host.c +.PHONY: all smoke clean +all: build/smoke +build/smoke: $(SOURCES) smoke.c + mkdir -p build + $(CC) $(CFLAGS) $(SOURCES) smoke.c -Wl,--gc-sections -o $@ +smoke: build/smoke + ./build/smoke +clean: + rm -rf build diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/README.md b/tasks/runtime-zephyr-eventfd-lock-progress/template/README.md new file mode 100644 index 00000000..9741f26c --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/README.md @@ -0,0 +1,17 @@ +# Event notification host workspace + +This software-only reproduction includes production descriptor dispatch, +event notification, condition variable, and poll code. `host/` supplies a small +cooperative kernel port; `include/` contains the original public headers and +minimal build dependencies. File-system backend calls are unavailable in this +workspace. The event notification path does not use that backend. + +Run `make smoke`. It prints a nonblocking counter round trip on one descriptor. +The full delivery contract is `docs/api-contract.md` (R01 through R13). +For additional self-tests, build an observer against the `SOURCES` in the +Makefile. Independent contexts can be created with `host_spawn`, then advanced +with `host_run`; `host_park` and `host_wake` support controlled handoffs. +Descriptor lookup returns the same mutex used by the production dispatch. + +Only `lib/os/zvfs/zvfs_eventfd.c` is collected for evaluation. New files and edits +to other files can help investigation but are not part of the submitted patch. diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/doc/kernel/services/synchronization/mutexes.rst b/tasks/runtime-zephyr-eventfd-lock-progress/template/doc/kernel/services/synchronization/mutexes.rst new file mode 100644 index 00000000..8fa7d150 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/doc/kernel/services/synchronization/mutexes.rst @@ -0,0 +1,188 @@ +.. _mutexes_v2: + +Mutexes +####### + +A :dfn:`mutex` is a kernel object that implements a traditional +reentrant mutex. A mutex allows multiple threads to safely share +an associated hardware or software resource by ensuring mutually exclusive +access to the resource. + +.. contents:: + :local: + :depth: 2 + +Concepts +******** + +Any number of mutexes can be defined (limited only by available RAM). Each mutex +is referenced by its memory address. + +A mutex has the following key properties: + +* A **lock count** that indicates the number of times the mutex has been locked + by the thread that has locked it. A count of zero indicates that the mutex + is unlocked. + +* An **owning thread** that identifies the thread that has locked the mutex, + when it is locked. + +A mutex must be initialized before it can be used. This sets its lock count +to zero. + +A thread that needs to use a shared resource must first gain exclusive rights +to access it by **locking** the associated mutex. If the mutex is already locked +by another thread, the requesting thread may choose to wait for the mutex +to be unlocked. + +After locking a mutex, the thread may safely use the associated resource +for as long as needed; however, it is considered good practice to hold the lock +for as short a time as possible to avoid negatively impacting other threads +that want to use the resource. When the thread no longer needs the resource +it must **unlock** the mutex to allow other threads to use the resource. + +Any number of threads may wait on a locked mutex simultaneously. +When the mutex becomes unlocked it is then locked by the highest-priority +thread that has waited the longest. + +.. note:: + Mutex objects are *not* designed for use by ISRs. + +Reentrant Locking +================= + +A thread is permitted to lock a mutex it has already locked. +This allows the thread to access the associated resource at a point +in its execution when the mutex may or may not already be locked. + +A mutex that is repeatedly locked by a thread must be unlocked an equal number +of times before the mutex becomes fully unlocked so it can be claimed +by another thread. + +Priority Inheritance +==================== + +The thread that has locked a mutex is eligible for :dfn:`priority inheritance`. +This means the kernel will *temporarily* elevate the thread's priority +if a higher priority thread begins waiting on the mutex. This allows the owning +thread to complete its work and release the mutex more rapidly by executing +at the same priority as the waiting thread. Once the mutex has been unlocked, +the unlocking thread resets its priority to the level it had before locking +that mutex. + +.. note:: + The :kconfig:option:`CONFIG_PRIORITY_CEILING` configuration option limits + how high the kernel can raise a thread's priority due to priority + inheritance. The default value of 0 permits unlimited elevation. + +The owning thread's base priority is saved in the mutex when it obtains the +lock. Each time a higher priority thread waits on a mutex, the kernel adjusts +the owning thread's priority. When the owning thread releases the lock (or if +the high priority waiting thread times out), the kernel restores the thread's +base priority from the value saved in the mutex. + +This works well for priority inheritance as long as only one locked mutex is +involved. However, if multiple mutexes are involved, sub-optimal behavior will +be observed if the mutexes are not unlocked in the reverse order to which the +owning thread's priority was previously raised. Consequently it is recommended +that a thread lock only a single mutex at a time when multiple mutexes are +shared between threads of different priorities. + +Implementation +************** + +Defining a Mutex +================ + +A mutex is defined using a variable of type :c:struct:`k_mutex`. +It must then be initialized by calling :c:func:`k_mutex_init`. + +The following code defines and initializes a mutex. + +.. code-block:: c + + struct k_mutex my_mutex; + + k_mutex_init(&my_mutex); + +Alternatively, a mutex can be defined and initialized at compile time +by calling :c:macro:`K_MUTEX_DEFINE`. + +The following code has the same effect as the code segment above. + +.. code-block:: c + + K_MUTEX_DEFINE(my_mutex); + +Locking a Mutex +=============== + +A mutex is locked by calling :c:func:`k_mutex_lock`. + +The following code builds on the example above, and waits indefinitely +for the mutex to become available if it is already locked by another thread. + +.. code-block:: c + + k_mutex_lock(&my_mutex, K_FOREVER); + +The following code waits up to 100 milliseconds for the mutex to become +available, and gives a warning if the mutex does not become available. + +.. code-block:: c + + if (k_mutex_lock(&my_mutex, K_MSEC(100)) == 0) { + /* mutex successfully locked */ + } else { + printf("Cannot lock XYZ display\n"); + } + +Unlocking a Mutex +================= + +A mutex is unlocked by calling :c:func:`k_mutex_unlock`. + +The following code builds on the example above, +and unlocks the mutex that was previously locked by the thread. + +.. code-block:: c + + k_mutex_unlock(&my_mutex); + +Suggested Uses +************** + +Use a mutex to provide exclusive access to a resource, such as a physical +device. + +Configuration Options +********************* + +Related configuration options: + +* :kconfig:option:`CONFIG_PRIORITY_CEILING` + +API Reference +************* + +.. doxygengroup:: mutex_apis + +Futex API Reference +******************* + +k_futex is a lightweight mutual exclusion primitive designed to minimize +kernel involvement. Uncontended operation relies only on atomic access +to shared memory. k_futex are tracked as kernel objects and can live in +user memory so that any access bypasses the kernel object permission +management mechanism. + +.. doxygengroup:: futex_apis + +User Mode Mutex API Reference +***************************** + +sys_mutex behaves almost exactly like k_mutex, with the added advantage +that a sys_mutex instance can reside in user memory. When user mode isn't +enabled, sys_mutex behaves like k_mutex. + +.. doxygengroup:: user_mutex_apis diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/docs/api-contract.md b/tasks/runtime-zephyr-eventfd-lock-progress/template/docs/api-contract.md new file mode 100644 index 00000000..ec8ad8d0 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/docs/api-contract.md @@ -0,0 +1,85 @@ +# Event notification interface + +This workspace provides the Zephyr event notification object, file descriptor table, +condition variables, and poll dispatcher, with a cooperative host kernel port. +The following interface contract applies to both direct event notification calls +and descriptor I/O. The host port uses a release configuration without assertions. + +**R01** `zvfs_eventfd(initial, flags)` creates a separate 64-bit counter initialized +from its unsigned initial value. The supported flags are `ZVFS_EFD_NONBLOCK` and +`ZVFS_EFD_SEMAPHORE`, in any combination. Other flags fail with `-1/EINVAL`. +The configured pool contains eight event notification objects; exhaustion fails +with `-1/ENOMEM`. Closing an object returns its slot for subsequent creation. + +**R02** Descriptor operations use `zvfs_read(fd, buffer, size, NULL)`, +`zvfs_write(fd, buffer, size, NULL)`, and `zvfs_close(fd)`. +Successful reads and writes transfer eight bytes even when the supplied buffer +is larger. Sizes below eight fail with `-1/EINVAL`. With an adequate size, +a null buffer fails with `-1/EFAULT`. + +**R03** `zvfs_eventfd_read(fd, &value)` and `zvfs_eventfd_write(fd, value)` +provide the same counter operations, returning zero on success and `-1` on error. +A null read destination fails with `EFAULT`. + +**R04** A successful ordinary read returns the accumulated counter and resets it +to zero. A semaphore read returns one and subtracts one. An empty counter cannot +satisfy a read. Reads returning errors leave the destination value unchanged. + +**R05** A successful write adds its value to the counter. Writing zero succeeds +without changing the count. The largest counter value is `UINT64_MAX-1`. +A write of `UINT64_MAX` fails with `EINVAL`. Other writes that would exceed +the counter limit cannot complete until sufficient capacity is available. +Errors leave the counter unchanged. + +**R06** An operation that cannot complete returns `-1/EAGAIN` in nonblocking mode. +In blocking mode it waits for a change, allowing a different execution context to +perform the counter operation needed for progress. It retries against the current +state after notification. Both direct calls and descriptor calls can be used on +either side of this interaction. Several waiting reads may consume the available +counter units, with ordinary reads draining all units and semaphore reads taking +one each; waiters without available units continue waiting. + +**R07** The descriptor mutex returned by `zvfs_get_fd_obj_and_vtable` or +`zvfs_get_obj_lock_and_cond` prevents concurrent descriptor access. Both counter +interfaces and close respect this exclusion: while another context holds this +mutex, they wait until it releases it, including when data is immediately +available or the descriptor is in nonblocking mode. Once access is released, +eligible operations can complete. + +**R08** `zvfs_close` returns zero on success. Closing while reads or writes are waiting +notifies all of them; these operations fail with `-1/EBADF`. Operations on a closed, +negative, or out-of-range descriptor fail with `-1/EBADF`. + +**R09** `z_impl_zvfs_poll(fds, count, timeout_ms)` reports readable readiness when +the counter is nonzero and writable readiness when it is below `UINT64_MAX-1`. +It returns the number of entries with nonzero `revents`. Requested events are +`ZVFS_POLLIN` and `ZVFS_POLLOUT`; unrequested events are not returned. +A zero timeout observes the current readiness. A negative timeout waits, and a +write that provides data wakes a pending readable poll. + +**R10** Poll ignores negative descriptor entries and sets their `revents` to zero. +Other invalid descriptors report `ZVFS_POLLNVAL` and contribute one to the result. + +**R11** The object ioctl interface supports `ZVFS_F_GETFL` and `ZVFS_F_SETFL`. +Getting flags returns the two supported user flags. Setting valid flags changes +the current read/write behavior without resetting the counter. Unsupported flag +bits fail with `-1/EINVAL` and preserve the previous flags. + +**R12** A kernel mutex is recursive for its owning context. Other contexts wait +until all acquisitions have been released. A condition wait releases one mutex +acquisition, suspends on the condition queue, and reacquires the mutex before +returning. Signaling releases one queued waiter; broadcasting releases all. +These primitives have the same semantics for every object in this host port. + +**R13** The host scheduler runs each context until it blocks or completes. +`host_run()` returns when no context is runnable. A context parked by +`host_park()` resumes after `host_wake(id)`. Observing an unfinished operation at +such a boundary records a pending operation, not a timer expiry. There is no +wall clock deadline in the cooperative model. Numeric errno names and constants +use the host C library. No particular internal helper or acquisition count is +part of the event notification API. + +The upstream headers in `include/zephyr/zvfs/eventfd.h` and +`include/zephyr/sys/fdtable.h`, the descriptor dispatch in +`lib/os/zvfs/zvfs_fdtable.c`, and the kernel condition implementation in +`kernel/condvar.c` provide the associated interfaces and execution context. diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/host/host.c b/tasks/runtime-zephyr-eventfd-lock-progress/template/host/host.c new file mode 100644 index 00000000..78ac764d --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/host/host.c @@ -0,0 +1,143 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include "host.h" + +enum { UNUSED, READY, COND, MUTEX, POLL, PARKED, DONE }; +struct context { + struct k_thread thread; + ucontext_t ctx; + unsigned char stack[65536]; + void (*fn)(void *); + void *arg; + int state, saved_errno; + unsigned long ticket; + void *waiting; + struct k_poll_event *events; + int event_count; +}; +static struct context contexts[24]; +static ucontext_t scheduler; +static int current, count; +static unsigned long next_ticket; + +static void suspend(int state, void *waiting) +{ + struct context *c=&contexts[current]; + c->state=state; c->waiting=waiting; c->ticket=++next_ticket; + c->saved_errno=errno; + swapcontext(&c->ctx,&scheduler); + errno=c->saved_errno; +} +static void enter(int id) +{ + contexts[id].fn(contexts[id].arg); + suspend(DONE,NULL); + abort(); +} +int host_spawn(void (*fn)(void *), void *arg) +{ + if (++count>=24) abort(); + int id=count; + struct context *c=&contexts[id]; + c->thread.id=id; c->fn=fn; c->arg=arg; c->state=READY; + getcontext(&c->ctx); + c->ctx.uc_stack.ss_sp=c->stack; c->ctx.uc_stack.ss_size=sizeof(c->stack); + c->ctx.uc_link=&scheduler; + makecontext(&c->ctx,(void (*)(void))enter,1,id); + return id; +} +void host_run(void) +{ + unsigned budget=10000; + for (;;) { + int ran=0; + for (int i=1;i<=count;i++) if (contexts[i].state==READY) { + if (!budget--) { fputs("scheduler budget exhausted\n",stderr); exit(91); } + ran=1; current=i; errno=contexts[i].saved_errno; + swapcontext(&scheduler,&contexts[i].ctx); + current=0; + } + if (!ran) break; + } +} +void host_park(void) { suspend(PARKED,NULL); } +void host_wake(int id) { if (contexts[id].state==PARKED) contexts[id].state=READY; } + +int k_mutex_init(struct k_mutex *m) { m->owner=0; m->depth=0; return 0; } +int k_mutex_lock(struct k_mutex *m, k_timeout_t t) +{ + int id=current ? current : -1; + while (m->owner && m->owner!=id) { + if (t.ticks==0) return -EBUSY; + if (!current) { fputs("host context blocked\n",stderr); exit(92); } + suspend(MUTEX,m); + } + m->owner=id; m->depth++; + return 0; +} +int k_mutex_unlock(struct k_mutex *m) +{ + int id=current ? current : -1; + if (m->owner!=id || !m->depth) return -EPERM; + if (!--m->depth) { + m->owner=0; + for (int i=1;i<=count;i++) + if (contexts[i].state==MUTEX && contexts[i].waiting==m) contexts[i].state=READY; + } + return 0; +} +k_spinlock_key_t k_spin_lock(struct k_spinlock *s) +{ if (s->held) abort(); s->held=1; return 0; } +void k_spin_unlock(struct k_spinlock *s, k_spinlock_key_t key) +{ (void)key; s->held=0; } +int z_pend_curr(struct k_spinlock *s,k_spinlock_key_t key,struct wait_q *q,k_timeout_t t) +{ + k_spin_unlock(s,key); + if (t.ticks==0) return -EAGAIN; + suspend(COND,q); + return 0; +} +struct k_thread *z_unpend_first_thread(struct wait_q *q) +{ + int best=0; + for (int i=1;i<=count;i++) + if (contexts[i].state==COND && contexts[i].waiting==q && + (!best || contexts[i].ticketid].state=READY; } +void z_reschedule(struct k_spinlock *s,k_spinlock_key_t key) { k_spin_unlock(s,key); } + +void k_poll_signal_init(struct k_poll_signal *s) { s->signaled=0; s->result=0; } +void k_poll_signal_reset(struct k_poll_signal *s) { s->signaled=0; } +int k_poll_signal_raise(struct k_poll_signal *s,int result) +{ + s->signaled=1; s->result=result; + for (int i=1;i<=count;i++) if (contexts[i].state==POLL) + for (int j=0;jsignaled ? K_POLL_STATE_SIGNALED : K_POLL_STATE_NOT_READY; + ready|=s->signaled; + } + if (ready) return 0; + if (t.ticks==0) return -EAGAIN; + if (!current) return -EAGAIN; + contexts[current].events=ev; contexts[current].event_count=n; + suspend(POLL,NULL); + } +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/host/host.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/host/host.h new file mode 100644 index 00000000..e10cab28 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/host/host.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef HOST_CONTEXT_H +#define HOST_CONTEXT_H +#include +struct zvfs_pollfd; +int host_spawn(void (*fn)(void *), void *arg); +void host_run(void); +void host_park(void); +void host_wake(int id); +int z_impl_zvfs_poll(struct zvfs_pollfd *fds, int n, int timeout); +#endif diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/ksched.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/ksched.h new file mode 100644 index 00000000..0b349311 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/ksched.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +struct k_thread *z_unpend_first_thread(struct wait_q *q); +void arch_thread_return_value_set(struct k_thread *t, int ret); +void z_ready_thread(struct k_thread *t); +void z_reschedule(struct k_spinlock *s, k_spinlock_key_t key); +int z_pend_curr(struct k_spinlock *s, k_spinlock_key_t key, struct wait_q *q, k_timeout_t timeout); diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/wait_q.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/wait_q.h new file mode 100644 index 00000000..010e2b79 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/wait_q.h @@ -0,0 +1,2 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/fs/fs.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/fs/fs.h new file mode 100644 index 00000000..29b6b3e3 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/fs/fs.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef HOST_FS_H +#define HOST_FS_H +#include +struct fs_file_t { int unused; }; +static inline void fs_file_t_init(struct fs_file_t *p) { p->unused = 0; } +static inline int fs_open(struct fs_file_t *p, const char *n, int f) +{ (void)p; (void)n; (void)f; return -ENOTSUP; } +static inline int fs_unlink(const char *p) { (void)p; return -ENOTSUP; } +static inline int fs_rename(const char *p, const char *q) { (void)p; (void)q; return -ENOTSUP; } +#endif diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/init.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/init.h new file mode 100644 index 00000000..f5214577 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/init.h @@ -0,0 +1 @@ +/* SPDX-License-Identifier: Apache-2.0 */ diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/internal/syscall_handler.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/internal/syscall_handler.h new file mode 100644 index 00000000..f5214577 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/internal/syscall_handler.h @@ -0,0 +1 @@ +/* SPDX-License-Identifier: Apache-2.0 */ diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/kernel.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/kernel.h new file mode 100644 index 00000000..7b067dfb --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/kernel.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef HOST_KERNEL_H +#define HOST_KERNEL_H +#include +#include +#include +#include +#include +#include +#define CONFIG_ZVFS_EVENTFD_MAX 8 +#define CONFIG_ZVFS_POLL_MAX 16 +#define ZVFS_OPEN_SIZE 16 +#define __syscall +#define ZRESTRICT restrict +/* This port uses the ordinary CONFIG_ASSERT=n release configuration. */ +#define __ASSERT(...) ((void)0) +#define __ASSERT_NO_MSG(...) ((void)0) +#define SYS_PORT_TRACING_OBJ_INIT(...) ((void)0) +#define SYS_PORT_TRACING_OBJ_FUNC_ENTER(...) ((void)0) +#define SYS_PORT_TRACING_OBJ_FUNC_EXIT(...) ((void)0) +#define SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(...) ((void)0) +#define unlikely(x) (x) +typedef struct { int64_t ticks; } k_timeout_t; +typedef k_timeout_t k_timepoint_t; +#define K_FOREVER ((k_timeout_t){-1}) +#define K_NO_WAIT ((k_timeout_t){0}) +#define K_MSEC(x) ((k_timeout_t){(x)}) +#define K_TIMEOUT_EQ(a,b) ((a).ticks == (b).ticks) +#define SYS_FOREVER_MS -1 +static inline k_timepoint_t sys_timepoint_calc(k_timeout_t t) { return t; } +static inline k_timeout_t sys_timepoint_timeout(k_timepoint_t t) { return t; } +static inline int k_ticks_to_ms_floor32(int64_t t) { return (int)t; } +struct k_mutex { int owner; unsigned depth; }; +struct k_spinlock { int held; }; +typedef int k_spinlock_key_t; +struct wait_q { unsigned unused; }; +struct k_condvar { struct wait_q wait_q; }; +struct k_thread { int id; }; +#define Z_MUTEX_INITIALIZER(x) {0,0} +#define Z_CONDVAR_INITIALIZER(x) {{0}} +#define K_MUTEX_DEFINE(x) struct k_mutex x = {0,0} +struct k_poll_signal { unsigned signaled; int result; }; +struct k_poll_event { void *obj; int type, mode, state; }; +#define K_POLL_TYPE_SIGNAL 1 +#define K_POLL_MODE_NOTIFY_ONLY 1 +#define K_POLL_STATE_NOT_READY 0 +#define K_POLL_STATE_SIGNALED 1 +int k_mutex_init(struct k_mutex *m); +int k_mutex_lock(struct k_mutex *m, k_timeout_t timeout); +int k_mutex_unlock(struct k_mutex *m); +k_spinlock_key_t k_spin_lock(struct k_spinlock *s); +void k_spin_unlock(struct k_spinlock *s, k_spinlock_key_t key); +int z_impl_k_condvar_init(struct k_condvar *c); +int z_impl_k_condvar_wait(struct k_condvar *c, struct k_mutex *m, k_timeout_t t); +int z_impl_k_condvar_signal(struct k_condvar *c); +int z_impl_k_condvar_broadcast(struct k_condvar *c); +#define k_condvar_init z_impl_k_condvar_init +#define k_condvar_wait z_impl_k_condvar_wait +#define k_condvar_signal z_impl_k_condvar_signal +#define k_condvar_broadcast z_impl_k_condvar_broadcast +static inline void k_object_init(void *p) { (void)p; } +static inline void z_waitq_init(struct wait_q *p) { p->unused = 0; } +static inline int k_is_in_isr(void) { return 0; } +void k_poll_signal_init(struct k_poll_signal *s); +void k_poll_signal_reset(struct k_poll_signal *s); +int k_poll_signal_raise(struct k_poll_signal *s, int result); +int k_poll(struct k_poll_event *ev, int count, k_timeout_t timeout); +struct k_mem_slab { unsigned unused; }; +#define K_MEM_SLAB_DEFINE(name, ...) struct k_mem_slab name +static inline int k_mem_slab_alloc(struct k_mem_slab *s, void **p, k_timeout_t t) +{ (void)s; (void)p; (void)t; return -ENOMEM; } +static inline void k_mem_slab_free(struct k_mem_slab *s, void *p) { (void)s; (void)p; } +#endif diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/kernel_structs.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/kernel_structs.h new file mode 100644 index 00000000..010e2b79 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/kernel_structs.h @@ -0,0 +1,2 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/net/socket.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/net/socket.h new file mode 100644 index 00000000..d56e7c44 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/net/socket.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +#define zsock_pollfd zvfs_pollfd +#define ZSOCK_POLLIN ZVFS_POLLIN +#define ZSOCK_POLLOUT ZVFS_POLLOUT diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/posix/fcntl.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/posix/fcntl.h new file mode 100644 index 00000000..0b35c89d --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/posix/fcntl.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +#define F_GETFL ZVFS_F_GETFL +#define F_SETFL ZVFS_F_SETFL diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/atomic.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/atomic.h new file mode 100644 index 00000000..abf6b17a --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/atomic.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef HOST_ATOMIC_H +#define HOST_ATOMIC_H +typedef int atomic_t; +typedef int atomic_val_t; +#define ATOMIC_INIT(x) (x) +static inline int atomic_get(atomic_t *a) { return __atomic_load_n(a,__ATOMIC_SEQ_CST); } +static inline int atomic_inc(atomic_t *a) { return __atomic_fetch_add(a,1,__ATOMIC_SEQ_CST); } +static inline int atomic_cas(atomic_t *a, int old, int val) +{ return __atomic_compare_exchange_n(a,&old,val,0,__ATOMIC_SEQ_CST,__ATOMIC_SEQ_CST); } +#endif diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/bitarray.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/bitarray.h new file mode 100644 index 00000000..633beed9 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/bitarray.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef HOST_BITARRAY_H +#define HOST_BITARRAY_H +#include +#include +#include +struct sys_bitarray { uint64_t bits; size_t size; }; +#define SYS_BITARRAY_DEFINE_STATIC(n,s) static struct sys_bitarray n = {0,s} +static inline int sys_bitarray_alloc(struct sys_bitarray *a, size_t n, size_t *off) +{ + for (size_t i=0; i+n<=a->size; i++) { + uint64_t mask=((UINT64_C(1)<bits&mask)) { a->bits|=mask; *off=i; return 0; } + } + return -ENOSPC; +} +static inline int sys_bitarray_free(struct sys_bitarray *a, size_t n, size_t off) +{ a->bits &= ~(((UINT64_C(1)< +#include + +/* FIXME: For native_posix ssize_t, off_t. */ +#include +#include +#include + +#ifdef CONFIG_PICOLIBC +#define ZVFS_O_APPEND 0x0400 +#define ZVFS_O_CREAT 0x0040 +#define ZVFS_O_TRUNC 0x0200 +#else +#define ZVFS_O_APPEND 0x0008 +#define ZVFS_O_CREAT 0x0200 +#define ZVFS_O_TRUNC 0x0400 +#endif + +#define ZVFS_O_RDONLY 00 +#define ZVFS_O_WRONLY 01 +#define ZVFS_O_RDWR 02 + +#define ZVFS_O_EXCL 0x0800 +#define ZVFS_O_NONBLOCK 0x4000 + +#define ZVFS_F_DUPFD 0 +#define ZVFS_F_GETFL 3 +#define ZVFS_F_SETFL 4 + +/* File mode bits */ +#define ZVFS_MODE_IFMT 0170000 +#define ZVFS_MODE_UNSPEC 0000000 +#define ZVFS_MODE_IFIFO 0010000 +#define ZVFS_MODE_IFCHR 0020000 +#define ZVFS_MODE_IMSGQ 0030000 +#define ZVFS_MODE_IFDIR 0040000 +#define ZVFS_MODE_IFSEM 0050000 +#define ZVFS_MODE_IFBLK 0060000 +#define ZVFS_MODE_IFSHM 0070000 +#define ZVFS_MODE_IFREG 0100000 +#define ZVFS_MODE_IFLNK 0120000 +#define ZVFS_MODE_IFSOCK 0140000 + +#define ZVFS_POLLIN BIT(0) +#define ZVFS_POLLPRI BIT(1) +#define ZVFS_POLLOUT BIT(2) +#define ZVFS_POLLERR BIT(3) +#define ZVFS_POLLHUP BIT(4) +#define ZVFS_POLLNVAL BIT(5) + +/* FIXME: use k_off_t and k_ssize_t to avoid the POSIX->Zephyr->POSIX dependency cycle */ +#ifdef CONFIG_NEWLIB_LIBC +#ifndef _OFF_T_DECLARED +typedef __off_t off_t; +#define _OFF_T_DECLARED +#endif +#ifndef _SSIZE_T_DECLARED +typedef _ssize_t ssize_t; +#define _SSIZE_T_DECLARED +#endif +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * File descriptor virtual method table. + * Currently all operations beyond read/write/close go thru ioctl method. + */ +struct fd_op_vtable { + union { + ssize_t (*read)(void *obj, void *buf, size_t sz); + ssize_t (*read_offs)(void *obj, void *buf, size_t sz, size_t offset); + }; + union { + ssize_t (*write)(void *obj, const void *buf, size_t sz); + ssize_t (*write_offs)(void *obj, const void *buf, size_t sz, size_t offset); + }; + union { + int (*close)(void *obj); + int (*close2)(void *obj, int fd); + }; + int (*ioctl)(void *obj, unsigned int request, va_list args); +}; + +/** + * @brief Reserve file descriptor. + * + * This function allows to reserve a space for file descriptor entry in + * the underlying table, and thus allows caller to fail fast if no free + * descriptor is available. If this function succeeds, zvfs_finalize_fd() + * or zvfs_free_fd() must be called mandatorily. + * + * @return Allocated file descriptor, or -1 in case of error (errno is set) + */ +int zvfs_reserve_fd(void); + +/** + * @brief Finalize creation of file descriptor, with type. + * + * This function should be called exactly once after zvfs_reserve_fd(), and + * should not be called in any other case. + * + * The difference between this function and @ref zvfs_finalize_fd is that the + * latter does not relay type information of the created file descriptor. + * + * Values permitted for @a mode are one of `ZVFS_MODE_..`. + * + * @param fd File descriptor previously returned by zvfs_reserve_fd() + * @param obj pointer to I/O object structure + * @param vtable pointer to I/O operation implementations for the object + * @param mode File type as specified above. + */ +void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable, uint32_t mode); + +/** + * @brief Finalize creation of file descriptor. + * + * This function should be called exactly once after zvfs_reserve_fd(), and + * should not be called in any other case. + * + * @param fd File descriptor previously returned by zvfs_reserve_fd() + * @param obj pointer to I/O object structure + * @param vtable pointer to I/O operation implementations for the object + */ +static inline void zvfs_finalize_fd(int fd, void *obj, const struct fd_op_vtable *vtable) +{ + zvfs_finalize_typed_fd(fd, obj, vtable, ZVFS_MODE_UNSPEC); +} + +/** + * @brief Allocate file descriptor for underlying I/O object. + * + * This function combines operations of zvfs_reserve_fd() and zvfs_finalize_fd() + * in one step, and provided for convenience. + * + * @param obj pointer to I/O object structure + * @param vtable pointer to I/O operation implementations for the object + * + * @return Allocated file descriptor, or -1 in case of error (errno is set) + */ +int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable); + +/** + * @brief Release reserved file descriptor. + * + * This function may be called once after zvfs_reserve_fd(), and should + * not be called in any other case. + * + * @param fd File descriptor previously returned by zvfs_reserve_fd() + */ +void zvfs_free_fd(int fd); + +/** + * @brief Get underlying object pointer from file descriptor. + * + * This function is useful for functions other than read/write/ioctl + * to look up underlying I/O object by fd, optionally checking its + * type (using vtable reference). If fd refers to invalid entry, + * NULL will be returned with errno set to EBADF. If fd is valid, + * but vtable param is not NULL and doesn't match object's vtable, + * NULL is returned and errno set to err param. + * + * @param fd File descriptor previously returned by zvfs_reserve_fd() + * @param vtable Expected object vtable or NULL + * @param err errno value to set if object vtable doesn't match + * + * @return Object pointer or NULL, with errno set + */ +void *zvfs_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err); + +/** + * @brief Get underlying object pointer and vtable pointer from file descriptor. + * + * @param fd File descriptor previously returned by zvfs_reserve_fd() + * @param vtable A pointer to a pointer variable to store the vtable + * @param lock An optional pointer to a pointer variable to store the mutex + * preventing concurrent descriptor access. The lock is not taken, + * it is just returned for the caller to use if necessary. Pass NULL + * if the lock is not needed by the caller. + * + * @return Object pointer or NULL, with errno set + */ +void *zvfs_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable, + struct k_mutex **lock); + +/** + * @brief Get the mutex and condition variable associated with the given object and vtable. + * + * @param obj Object previously returned by a call to e.g. @ref zvfs_get_fd_obj. + * @param vtable A pointer the vtable associated with @p obj. + * @param lock An optional pointer to a pointer variable to store the mutex + * preventing concurrent descriptor access. The lock is not taken, + * it is just returned for the caller to use if necessary. Pass NULL + * if the lock is not needed by the caller. + * @param cond An optional pointer to a pointer variable to store the condition variable + * to notify waiting threads in the case of concurrent descriptor access. Pass NULL + * if the condition variable is not needed by the caller. + * + * @return `true` on success, `false` otherwise. + */ +bool zvfs_get_obj_lock_and_cond(void *obj, const struct fd_op_vtable *vtable, struct k_mutex **lock, + struct k_condvar **cond); + +/** + * @brief Call ioctl vmethod on an object using varargs. + * + * We need this helper function because ioctl vmethod is declared to + * take va_list and the only portable way to construct va_list is from + * function's ... parameters. + * + * @param vtable vtable containing ioctl function pointer + * @param obj Object to call ioctl on + * @param request ioctl request number + * @param ... Variadic arguments to ioctl + */ +static inline int zvfs_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *obj, + unsigned long request, ...) +{ + va_list args; + int res; + + va_start(args, request); + res = vtable->ioctl(obj, request, args); + va_end(args); + + return res; +} + +/** + * Structure for result of the ZFD_IOCTL_STAT ioctl call. + * Contains fields for all currently supported details. Defined to avoid POSIX dependency. + */ +struct zvfs_stat { + off_t size; + uint32_t mode; +}; + +struct zvfs_pollfd { + int fd; + short events; + short revents; +}; + +__syscall int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout); + +struct zvfs_fd_set { + uint32_t bitset[DIV_ROUND_UP(ZVFS_OPEN_SIZE, 32)]; +}; + +/** @brief Number of file descriptors which can be added @ref zvfs_fd_set */ +#define ZVFS_FD_SETSIZE (sizeof(((struct zvfs_fd_set *)0)->bitset) * 8) + +void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset); +int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset); +void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset); +void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset); + +__syscall int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds, + struct zvfs_fd_set *ZRESTRICT writefds, + struct zvfs_fd_set *ZRESTRICT errorfds, + const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask); + +/** + * Request codes for fd_op_vtable.ioctl(). + * + * Note that these codes are internal Zephyr numbers, for internal + * Zephyr operations (and subject to change without notice, not part + * of "stable ABI"). These are however expected to co-exist with + * "well-known" POSIX/Linux ioctl numbers, and not clash with them. + */ +enum { + /* Codes below 0x100 are reserved for fcntl() codes. */ + ZFD_IOCTL_FSYNC = 0x100, + ZFD_IOCTL_LSEEK, + ZFD_IOCTL_POLL_PREPARE, + ZFD_IOCTL_POLL_UPDATE, + ZFD_IOCTL_POLL_OFFLOAD, + ZFD_IOCTL_SET_LOCK, + ZFD_IOCTL_STAT, + ZFD_IOCTL_TRUNCATE, + ZFD_IOCTL_MMAP, + + /* Codes above 0x5400 and below 0x5500 are reserved for termios, FIO, etc */ + ZFD_IOCTL_FIONREAD = 0x541B, + ZFD_IOCTL_FIONBIO = 0x5421, +}; + +/** + * @brief Open a file with a given name. + * + * @param name Name of the file + * @param flags Access modes for @ref fs_open + * @param vtable Pointer to structure with appropriate i/o functions + * + * @return Index of newly created file descriptor on success, -1 on error with errno set. + */ +int zvfs_open(const char *name, int flags, struct fd_op_vtable *vtable); + +/** + * @brief Get information about file. + * + * @param fd File descriptor index + * @param buf Pointer to result structure + * + * @return 0 on success, -1 with errno set on error. + */ +int zvfs_fstat(int fd, struct zvfs_stat *buf); + +/** + * @brief Close file. + * + * @param fd File descriptor index + * + * @return 0 on success, -1 with errno set on error. + */ +int zvfs_close(int fd); + +/** + * @brief Read bytes from file. + * + * @param fd File descriptor index + * @param buf Destination buffer + * @param sz Number of bytes to read + * @param from_offset Pointer to variable specifying starting position; + * current offset will be used if NULL + * + * @return Number of bytes read successfully, or -1 with errno set on failure + */ +ssize_t zvfs_read(int fd, void *buf, size_t sz, const size_t *from_offset); + +/** + * @brief Write bytes to file. + * + * @param fd File descriptor index + * @param buf Source buffer + * @param sz Number of bytes to write + * @param from_offset Pointer to variable specifying starting position; + * current offset will be used if NULL + * + * @return Number of bytes written successfully, or -1 with errno set on failure + */ +ssize_t zvfs_write(int fd, const void *buf, size_t sz, const size_t *from_offset); + +#ifdef CONFIG_ZVFS_DEFAULT_FILE_VMETHODS +int zvfs_ioctl_vmeth(void *obj, unsigned int request, va_list args); +int zvfs_close_vmeth(void *obj); +ssize_t zvfs_write_vmeth(void *obj, const void *buffer, size_t count); +ssize_t zvfs_read_vmeth(void *obj, void *buffer, size_t count); +#endif + +/** + * @brief Delete file or directory. + * + * @param path Path or name of file to be removed. + * + * @return 0 on success, -1 with errno set on error. + */ +int zvfs_unlink(const char *path); + +/** + * @brief Rename file or directory, moving it if necessary. + * + * @param old Current name + * @param newp Target name + * + * @return 0 on success, -1 with errno set on error. + */ +int zvfs_rename(const char *old, const char *newp); + +#ifdef __cplusplus +} +#endif + +#include + +#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */ diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/math_extras.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/math_extras.h new file mode 100644 index 00000000..8abc962d --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/math_extras.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +#include +static inline bool u64_add_overflow(uint64_t a,uint64_t b,uint64_t *c) +{ return __builtin_add_overflow(a,b,c); } diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/speculation.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/speculation.h new file mode 100644 index 00000000..244f14d2 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/speculation.h @@ -0,0 +1,2 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#define k_array_index_sanitize(i,n) (i) diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/util.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/util.h new file mode 100644 index 00000000..1a2cf58a --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/sys/util.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#ifndef HOST_UTIL_H +#define HOST_UTIL_H +#include +#define BIT(n) (1U << (n)) +#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) +#define DIV_ROUND_UP(a,b) (((a)+(b)-1)/(b)) +#define ARG_UNUSED(x) ((void)(x)) +#define BUILD_ASSERT(c,...) _Static_assert(c, #c) +#define IS_ARRAY_ELEMENT(a,p) ((uintptr_t)(p)>=(uintptr_t)(a) && (uintptr_t)(p)<(uintptr_t)((a)+ARRAY_SIZE(a)) && ((uintptr_t)(p)-(uintptr_t)(a))%sizeof((a)[0])==0) +#endif diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/syscalls/fdtable.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/syscalls/fdtable.h new file mode 100644 index 00000000..f5214577 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/syscalls/fdtable.h @@ -0,0 +1 @@ +/* SPDX-License-Identifier: Apache-2.0 */ diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/toolchain.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/toolchain.h new file mode 100644 index 00000000..010e2b79 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/toolchain.h @@ -0,0 +1,2 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/zvfs/eventfd.h b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/zvfs/eventfd.h new file mode 100644 index 00000000..a5e1ebfa --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/include/zephyr/zvfs/eventfd.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020 Tobias Svehagen + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_ZEPHYR_ZVFS_EVENTFD_H_ +#define ZEPHYR_INCLUDE_ZEPHYR_ZVFS_EVENTFD_H_ + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZVFS_EFD_SEMAPHORE 2 +#define ZVFS_EFD_NONBLOCK 0x4000 + +typedef uint64_t zvfs_eventfd_t; + +/** + * @brief Create a file descriptor for ZVFS event notification + * + * The returned file descriptor can be used with POSIX read/write calls or + * with the @ref zvfs_eventfd_read or @ref zvfs_eventfd_write functions. + * + * It also supports polling and by including an eventfd in a call to poll, + * it is possible to signal and wake the polling thread by simply writing to + * the eventfd. + * + * When using read() and write() on a ZVFS eventfd, the size must always be at + * least 8 bytes or the operation will fail with EINVAL. + * + * @return New ZVFS eventfd file descriptor on success, -1 on error + */ +int zvfs_eventfd(unsigned int initval, int flags); + +/** + * @brief Read from a ZVFS eventfd + * + * If call is successful, the value parameter will have the value 1 + * + * @param fd File descriptor + * @param value Pointer for storing the read value + * + * @return 0 on success, -1 on error + */ +int zvfs_eventfd_read(int fd, zvfs_eventfd_t *value); + +/** + * @brief Write to a ZVFS eventfd + * + * @param fd File descriptor + * @param value Value to write + * + * @return 0 on success, -1 on error + */ +int zvfs_eventfd_write(int fd, zvfs_eventfd_t value); + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_ZEPHYR_ZVFS_EVENTFD_H_ */ diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/kernel/condvar.c b/tasks/runtime-zephyr-eventfd-lock-progress/template/kernel/condvar.c new file mode 100644 index 00000000..42590839 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/kernel/condvar.c @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2020 Intel Corporation. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_OBJ_CORE_CONDVAR +static struct k_obj_type obj_type_condvar; +#endif /* CONFIG_OBJ_CORE_CONDVAR */ + +static struct k_spinlock lock; + +int z_impl_k_condvar_init(struct k_condvar *condvar) +{ + z_waitq_init(&condvar->wait_q); + k_object_init(condvar); + +#ifdef CONFIG_OBJ_CORE_CONDVAR + k_obj_core_init_and_link(K_OBJ_CORE(condvar), &obj_type_condvar); +#endif /* CONFIG_OBJ_CORE_CONDVAR */ + + SYS_PORT_TRACING_OBJ_INIT(k_condvar, condvar, 0); + + return 0; +} + +#ifdef CONFIG_USERSPACE +int z_vrfy_k_condvar_init(struct k_condvar *condvar) +{ + K_OOPS(K_SYSCALL_OBJ_INIT(condvar, K_OBJ_CONDVAR)); + return z_impl_k_condvar_init(condvar); +} +#include +#endif /* CONFIG_USERSPACE */ + +int z_impl_k_condvar_signal(struct k_condvar *condvar) +{ + k_spinlock_key_t key = k_spin_lock(&lock); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_condvar, signal, condvar); + + struct k_thread *thread = z_unpend_first_thread(&condvar->wait_q); + + if (unlikely(thread != NULL)) { + SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_condvar, signal, condvar, K_FOREVER); + + arch_thread_return_value_set(thread, 0); + z_ready_thread(thread); + z_reschedule(&lock, key); + } else { + k_spin_unlock(&lock, key); + } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_condvar, signal, condvar, 0); + + return 0; +} + +#ifdef CONFIG_USERSPACE +int z_vrfy_k_condvar_signal(struct k_condvar *condvar) +{ + K_OOPS(K_SYSCALL_OBJ(condvar, K_OBJ_CONDVAR)); + return z_impl_k_condvar_signal(condvar); +} +#include +#endif /* CONFIG_USERSPACE */ + +int z_impl_k_condvar_broadcast(struct k_condvar *condvar) +{ + struct k_thread *pending_thread; + k_spinlock_key_t key; + int woken = 0; + + key = k_spin_lock(&lock); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_condvar, broadcast, condvar); + + /* wake up any threads that are waiting to write */ + for (pending_thread = z_unpend_first_thread(&condvar->wait_q); pending_thread != NULL; + pending_thread = z_unpend_first_thread(&condvar->wait_q)) { + woken++; + arch_thread_return_value_set(pending_thread, 0); + z_ready_thread(pending_thread); + } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_condvar, broadcast, condvar, woken); + + + if (woken == 0) { + k_spin_unlock(&lock, key); + } else { + z_reschedule(&lock, key); + } + + return woken; +} +#ifdef CONFIG_USERSPACE +int z_vrfy_k_condvar_broadcast(struct k_condvar *condvar) +{ + K_OOPS(K_SYSCALL_OBJ(condvar, K_OBJ_CONDVAR)); + return z_impl_k_condvar_broadcast(condvar); +} +#include +#endif /* CONFIG_USERSPACE */ + +int z_impl_k_condvar_wait(struct k_condvar *condvar, struct k_mutex *mutex, + k_timeout_t timeout) +{ + k_spinlock_key_t key; + int ret; + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_condvar, wait, condvar, timeout); + + key = k_spin_lock(&lock); + k_mutex_unlock(mutex); + + ret = z_pend_curr(&lock, key, &condvar->wait_q, timeout); + k_mutex_lock(mutex, K_FOREVER); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_condvar, wait, condvar, timeout, ret); + + return ret; +} +#ifdef CONFIG_USERSPACE +int z_vrfy_k_condvar_wait(struct k_condvar *condvar, struct k_mutex *mutex, + k_timeout_t timeout) +{ + K_OOPS(K_SYSCALL_OBJ(condvar, K_OBJ_CONDVAR)); + K_OOPS(K_SYSCALL_OBJ(mutex, K_OBJ_MUTEX)); + return z_impl_k_condvar_wait(condvar, mutex, timeout); +} +#include +#endif /* CONFIG_USERSPACE */ + +#ifdef CONFIG_OBJ_CORE_CONDVAR +static int init_condvar_obj_core_list(void) +{ + /* Initialize condvar object type */ + + z_obj_type_init(&obj_type_condvar, K_OBJ_TYPE_CONDVAR_ID, + offsetof(struct k_condvar, obj_core)); + + /* Initialize and link statically defined condvars */ + + STRUCT_SECTION_FOREACH(k_condvar, condvar) { + k_obj_core_init_and_link(K_OBJ_CORE(condvar), + &obj_type_condvar); + } + + return 0; +} + +SYS_INIT(init_condvar_obj_core_list, PRE_KERNEL_1, + CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); +#endif /* CONFIG_OBJ_CORE_CONDVAR */ diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_eventfd.c b/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_eventfd.c new file mode 100644 index 00000000..c4c32982 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_eventfd.c @@ -0,0 +1,485 @@ +/* + * Copyright (c) 2020 Tobias Svehagen + * Copyright (c) 2023, Meta + * Copyright (c) 2024, Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include + +#define ZVFS_EFD_IN_USE 0x1 +#define ZVFS_EFD_FLAGS_SET (ZVFS_EFD_SEMAPHORE | ZVFS_EFD_NONBLOCK) + +struct zvfs_eventfd { + struct k_poll_signal read_sig; + struct k_poll_signal write_sig; + struct k_spinlock lock; + zvfs_eventfd_t cnt; + int flags; +}; + +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)); + +SYS_BITARRAY_DEFINE_STATIC(efds_bitarray, CONFIG_ZVFS_EVENTFD_MAX); +static struct zvfs_eventfd efds[CONFIG_ZVFS_EVENTFD_MAX]; +static const struct fd_op_vtable zvfs_eventfd_fd_vtable; + +static inline bool zvfs_eventfd_is_in_use(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_IN_USE) != 0; +} + +static inline bool zvfs_eventfd_is_semaphore(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_SEMAPHORE) != 0; +} + +static inline bool zvfs_eventfd_is_blocking(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_NONBLOCK) == 0; +} + +static int zvfs_eventfd_poll_prepare(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev, + struct k_poll_event *pev_end) +{ + if (pfd->events & ZSOCK_POLLIN) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->read_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->write_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_poll_update(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev) +{ + if (pfd->events & ZSOCK_POLLIN) { + pfd->revents |= ZSOCK_POLLIN * (efd->cnt > 0); + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + pfd->revents |= ZSOCK_POLLOUT * (efd->cnt < UINT64_MAX - 1); + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_read_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (efd->cnt == 0) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful read */ + if (zvfs_eventfd_is_semaphore(efd)) { + *value = 1; + --efd->cnt; + } else { + *value = efd->cnt; + efd->cnt = 0; + } + + if (efd->cnt == 0) { + k_poll_signal_reset(&efd->read_sig); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + return 0; +} + +static int zvfs_eventfd_write_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + zvfs_eventfd_t result; + + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (*value == UINT64_MAX) { + /* not a permitted value */ + return -EINVAL; + } + + if (u64_add_overflow(efd->cnt, *value, &result) || result == UINT64_MAX) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful write */ + efd->cnt = result; + + if (efd->cnt == (UINT64_MAX - 1)) { + k_poll_signal_reset(&efd->write_sig); + } + + k_poll_signal_raise(&efd->read_sig, 0); + + return 0; +} + +static ssize_t zvfs_eventfd_read_op(void *obj, void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, buf, sz, zvfs_eventfd_read_locked); +} + +static ssize_t zvfs_eventfd_write_op(void *obj, const void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, (zvfs_eventfd_t *)buf, sz, zvfs_eventfd_write_locked); +} + +static int zvfs_eventfd_close_op(void *obj) +{ + int ret; + int err; + k_spinlock_key_t key; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + err = k_mutex_lock(lock, K_FOREVER); + __ASSERT(err == 0, "k_mutex_lock() failed: %d", err); + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + err = sys_bitarray_free(&efds_bitarray, 1, (struct zvfs_eventfd *)obj - efds); + __ASSERT(err == 0, "sys_bitarray_free() failed: %d", err); + + efd->flags = 0; + efd->cnt = 0; + + ret = 0; + +unlock: + k_spin_unlock(&efd->lock, key); + /* when closing an zvfs_eventfd, broadcast to all waiters */ + err = k_condvar_broadcast(cond); + __ASSERT(err == 0, "k_condvar_broadcast() failed: %d", err); + err = k_mutex_unlock(lock); + __ASSERT(err == 0, "k_mutex_unlock() failed: %d", err); + + return ret; +} + +static int zvfs_eventfd_ioctl_op(void *obj, unsigned int request, va_list args) +{ + int ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + /* note: zsock_poll_internal() has already taken the mutex */ + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + switch (request) { + case F_GETFL: + ret = efd->flags & ZVFS_EFD_FLAGS_SET; + break; + + case F_SETFL: { + int flags; + + flags = va_arg(args, int); + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + ret = -1; + } else { + int prev_flags = efd->flags & ~ZVFS_EFD_FLAGS_SET; + + efd->flags = flags | prev_flags; + ret = 0; + } + } break; + + case ZFD_IOCTL_POLL_PREPARE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + struct k_poll_event *pev_end; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + pev_end = va_arg(args, struct k_poll_event *); + + ret = zvfs_eventfd_poll_prepare(obj, pfd, pev, pev_end); + } break; + + case ZFD_IOCTL_POLL_UPDATE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + + ret = zvfs_eventfd_poll_update(obj, pfd, pev); + } break; + + default: + errno = EOPNOTSUPP; + ret = -1; + break; + } + +unlock: + k_spin_unlock(&efd->lock, key); + + return ret; +} + +static const struct fd_op_vtable zvfs_eventfd_fd_vtable = { + .read = zvfs_eventfd_read_op, + .write = zvfs_eventfd_write_op, + .close = zvfs_eventfd_close_op, + .ioctl = zvfs_eventfd_ioctl_op, +}; + +/* common to both zvfs_eventfd_read_op() and zvfs_eventfd_write_op() */ +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)) +{ + int err; + ssize_t ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = obj; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + if (sz < sizeof(zvfs_eventfd_t)) { + errno = EINVAL; + return -1; + } + + if (buf == NULL) { + errno = EFAULT; + return -1; + } + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_blocking(efd)) { + /* + * Handle the non-blocking case entirely within this scope + */ + ret = op(efd, buf); + if (ret < 0) { + errno = -ret; + ret = -1; + } else { + ret = sizeof(zvfs_eventfd_t); + } + + goto unlock_spin; + } + + /* + * Handle the blocking case below + */ + __ASSERT_NO_MSG(zvfs_eventfd_is_blocking(efd)); + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + ret = -1; + goto unlock_spin; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + /* do not hold a spinlock when taking a mutex */ + k_spin_unlock(&efd->lock, key); + err = k_mutex_lock(lock, K_FOREVER); + __ASSERT(err == 0, "k_mutex_lock() failed: %d", err); + + while (true) { + /* retake the spinlock */ + key = k_spin_lock(&efd->lock); + + ret = op(efd, buf); + switch (ret) { + case -EAGAIN: + /* not an error in blocking mode. break and try again */ + break; + case 0: + /* success! */ + ret = sizeof(zvfs_eventfd_t); + goto unlock_mutex; + default: + /* some other error */ + __ASSERT_NO_MSG(ret < 0); + errno = -ret; + ret = -1; + goto unlock_mutex; + } + + /* do not hold a spinlock when taking a mutex */ + k_spin_unlock(&efd->lock, key); + + /* wait for a write or close */ + err = k_condvar_wait(cond, lock, K_FOREVER); + __ASSERT(err == 0, "k_condvar_wait() failed: %d", err); + } + +unlock_mutex: + k_spin_unlock(&efd->lock, key); + /* only wake a single waiter */ + err = k_condvar_signal(cond); + __ASSERT(err == 0, "k_condvar_signal() failed: %d", err); + err = k_mutex_unlock(lock); + __ASSERT(err == 0, "k_mutex_unlock() failed: %d", err); + goto out; + +unlock_spin: + k_spin_unlock(&efd->lock, key); + +out: + return ret; +} + +/* + * Public-facing API + */ + +int zvfs_eventfd(unsigned int initval, int flags) +{ + int fd = 1; + size_t offset; + struct zvfs_eventfd *efd = NULL; + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + return -1; + } + + if (sys_bitarray_alloc(&efds_bitarray, 1, &offset) < 0) { + errno = ENOMEM; + return -1; + } + + efd = &efds[offset]; + + fd = zvfs_reserve_fd(); + if (fd < 0) { + sys_bitarray_free(&efds_bitarray, 1, offset); + return -1; + } + + efd->flags = ZVFS_EFD_IN_USE | flags; + efd->cnt = initval; + + k_poll_signal_init(&efd->write_sig); + k_poll_signal_init(&efd->read_sig); + + if (initval != 0) { + k_poll_signal_raise(&efd->read_sig, 0); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + zvfs_finalize_fd(fd, efd, &zvfs_eventfd_fd_vtable); + + return fd; +} + +int zvfs_eventfd_read(int fd, zvfs_eventfd_t *value) +{ + int ret; + void *obj; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + ret = zvfs_eventfd_rw_op(obj, value, sizeof(zvfs_eventfd_t), zvfs_eventfd_read_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + if (ret < 0) { + return -1; + } + + return 0; +} + +int zvfs_eventfd_write(int fd, zvfs_eventfd_t value) +{ + int ret; + void *obj; + + obj = zvfs_get_fd_obj(fd, &zvfs_eventfd_fd_vtable, EBADF); + if (obj == NULL) { + return -1; + } + + ret = zvfs_eventfd_rw_op(obj, &value, sizeof(zvfs_eventfd_t), zvfs_eventfd_write_locked); + __ASSERT_NO_MSG(ret == -1 || ret == sizeof(zvfs_eventfd_t)); + if (ret < 0) { + return -1; + } + + return 0; +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_fdtable.c b/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_fdtable.c new file mode 100644 index 00000000..12e04c4a --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_fdtable.c @@ -0,0 +1,609 @@ +/* + * Copyright (c) 2018 Linaro Limited + * Copyright (c) 2024 Tenstorrent AI ULC + * Copyright (c) 2025 Antmicro + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief File descriptor table + * + * This file provides generic file descriptor table implementation, suitable + * for any I/O object implementing POSIX I/O semantics (i.e. read/write + + * aux operations). + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +K_MEM_SLAB_DEFINE(file_desc_slab, sizeof(struct fs_file_t), ZVFS_OPEN_SIZE, 4); + +struct fd_entry { + void *obj; + const struct fd_op_vtable *vtable; + atomic_t refcount; + struct k_mutex lock; + struct k_condvar cond; + size_t offset; + uint32_t mode; +}; + +#if defined(CONFIG_POSIX_DEVICE_IO) +static const struct fd_op_vtable stdinout_fd_op_vtable; + +BUILD_ASSERT(ZVFS_OPEN_SIZE >= 3, "ZVFS_OPEN_SIZE >= 3 for CONFIG_POSIX_DEVICE_IO"); +#endif /* defined(CONFIG_POSIX_DEVICE_IO) */ + +static struct fd_entry fdtable[ZVFS_OPEN_SIZE] = { +#if defined(CONFIG_POSIX_DEVICE_IO) + /* + * Predefine entries for stdin/stdout/stderr. + */ + { + /* STDIN */ + .vtable = &stdinout_fd_op_vtable, + .refcount = ATOMIC_INIT(1), + .lock = Z_MUTEX_INITIALIZER(fdtable[0].lock), + .cond = Z_CONDVAR_INITIALIZER(fdtable[0].cond), + }, + { + /* STDOUT */ + .vtable = &stdinout_fd_op_vtable, + .refcount = ATOMIC_INIT(1), + .lock = Z_MUTEX_INITIALIZER(fdtable[1].lock), + .cond = Z_CONDVAR_INITIALIZER(fdtable[1].cond), + }, + { + /* STDERR */ + .vtable = &stdinout_fd_op_vtable, + .refcount = ATOMIC_INIT(1), + .lock = Z_MUTEX_INITIALIZER(fdtable[2].lock), + .cond = Z_CONDVAR_INITIALIZER(fdtable[2].cond), + }, +#else + {0}, +#endif +}; + +int zvfs_open(const char *name, int flags, struct fd_op_vtable *vtable) +{ + int rc, fd; + struct fs_file_t *ptr = NULL; + + fd = zvfs_reserve_fd(); + if (fd < 0) { + return -1; + } + + rc = k_mem_slab_alloc(&file_desc_slab, (void **)&ptr, K_NO_WAIT); + if (rc < 0) { + rc = -EMFILE; + goto out_err; + } + + fs_file_t_init(ptr); + + rc = fs_open(ptr, name, flags); + if (rc < 0) { + goto out_err; + } + + zvfs_finalize_fd(fd, ptr, vtable); + + goto out; + +out_err: + if (ptr != NULL) { + k_mem_slab_free(&file_desc_slab, ptr); + } + + zvfs_free_fd(fd); + errno = -rc; + return -1; + +out: + return fd; +} + +static K_MUTEX_DEFINE(fdtable_lock); + +static int z_fd_ref(int fd) +{ + return atomic_inc(&fdtable[fd].refcount) + 1; +} + +static int z_fd_unref(int fd) +{ + atomic_val_t old_rc; + + /* Reference counter must be checked to avoid decrement refcount below + * zero causing file descriptor leak. Loop statement below executes + * atomic decrement if refcount value is grater than zero. Otherwise, + * refcount is not going to be written. + */ + do { + old_rc = atomic_get(&fdtable[fd].refcount); + if (!old_rc) { + return 0; + } + } while (!atomic_cas(&fdtable[fd].refcount, old_rc, old_rc - 1)); + + if (old_rc != 1) { + return old_rc - 1; + } + + fdtable[fd].obj = NULL; + fdtable[fd].vtable = NULL; + + return 0; +} + +static int _find_fd_entry(void) +{ + int fd; + + for (fd = 0; fd < ARRAY_SIZE(fdtable); fd++) { + if (!atomic_get(&fdtable[fd].refcount)) { + return fd; + } + } + + errno = ENFILE; + return -1; +} + +static int _check_fd(int fd) +{ + if ((fd < 0) || (fd >= ARRAY_SIZE(fdtable))) { + errno = EBADF; + return -1; + } + + fd = k_array_index_sanitize(fd, ARRAY_SIZE(fdtable)); + + if (!atomic_get(&fdtable[fd].refcount)) { + errno = EBADF; + return -1; + } + + return 0; +} + +void *zvfs_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err) +{ + struct fd_entry *entry; + + if (_check_fd(fd) < 0) { + return NULL; + } + + entry = &fdtable[fd]; + + if ((vtable != NULL) && (entry->vtable != vtable)) { + errno = err; + return NULL; + } + + return entry->obj; +} + +static int z_get_fd_by_obj_and_vtable(void *obj, const struct fd_op_vtable *vtable) +{ + int fd; + + for (fd = 0; fd < ARRAY_SIZE(fdtable); fd++) { + if (fdtable[fd].obj == obj && fdtable[fd].vtable == vtable) { + return fd; + } + } + + errno = ENFILE; + return -1; +} + +bool zvfs_get_obj_lock_and_cond(void *obj, const struct fd_op_vtable *vtable, struct k_mutex **lock, + struct k_condvar **cond) +{ + int fd; + struct fd_entry *entry; + + fd = z_get_fd_by_obj_and_vtable(obj, vtable); + if (_check_fd(fd) < 0) { + return false; + } + + entry = &fdtable[fd]; + + if (lock) { + *lock = &entry->lock; + } + + if (cond) { + *cond = &entry->cond; + } + + return true; +} + +void *zvfs_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable, struct k_mutex **lock) +{ + struct fd_entry *entry; + + if (_check_fd(fd) < 0) { + return NULL; + } + + entry = &fdtable[fd]; + *vtable = entry->vtable; + + if (lock != NULL) { + *lock = &entry->lock; + } + + return entry->obj; +} + +int zvfs_reserve_fd(void) +{ + int fd; + + (void)k_mutex_lock(&fdtable_lock, K_FOREVER); + + fd = _find_fd_entry(); + if (fd >= 0) { + /* Mark entry as used, zvfs_finalize_fd() will fill it in. */ + (void)z_fd_ref(fd); + fdtable[fd].obj = NULL; + fdtable[fd].vtable = NULL; + fdtable[fd].offset = 0; + k_mutex_init(&fdtable[fd].lock); + k_condvar_init(&fdtable[fd].cond); + } + + k_mutex_unlock(&fdtable_lock); + + return fd; +} + +void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable, uint32_t mode) +{ + /* Assumes fd was already bounds-checked. */ +#ifdef CONFIG_USERSPACE + /* descriptor context objects are inserted into the table when they + * are ready for use. Mark the object as initialized and grant the + * caller (and only the caller) access. + * + * This call is a no-op if obj is invalid or points to something + * not a kernel object. + */ + k_object_recycle(obj); +#endif + fdtable[fd].obj = obj; + fdtable[fd].vtable = vtable; + fdtable[fd].mode = mode; + + /* Let the object know about the lock just in case it needs it + * for something. For BSD sockets, the lock is used with condition + * variables to avoid keeping the lock for a long period of time. + */ + if (vtable && vtable->ioctl) { + int prev_errno = errno; + + (void)zvfs_fdtable_call_ioctl(vtable, obj, ZFD_IOCTL_SET_LOCK, &fdtable[fd].lock); + if ((prev_errno != EOPNOTSUPP) && (errno == EOPNOTSUPP)) { + /* restore backed-up errno value if the backend does not support locking */ + errno = prev_errno; + } + } +} + +void zvfs_free_fd(int fd) +{ + /* Assumes fd was already bounds-checked. */ + (void)z_fd_unref(fd); +} + +int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable) +{ + int fd; + + fd = zvfs_reserve_fd(); + if (fd >= 0) { + zvfs_finalize_fd(fd, obj, vtable); + } + + return fd; +} + +static bool supports_pread_pwrite(uint32_t mode) +{ + switch (mode & ZVFS_MODE_IFMT) { + case ZVFS_MODE_IFSHM: + return true; + default: + return false; + } +} + +static ssize_t zvfs_rw(int fd, void *buf, size_t sz, bool is_write, const size_t *from_offset) +{ + bool prw; + ssize_t res; + const size_t *off; + + if (_check_fd(fd) < 0) { + return -1; + } + + (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); + + prw = supports_pread_pwrite(fdtable[fd].mode); + if (from_offset != NULL && !prw) { + /* + * Seekable file types should support pread() / pwrite() and per-fd offset passing. + * Otherwise, it's a bug. + */ + errno = ENOTSUP; + res = -1; + goto unlock; + } + + /* If there is no specified from_offset, then use the current offset of the fd */ + off = (from_offset == NULL) ? &fdtable[fd].offset : from_offset; + + if (is_write) { + if (fdtable[fd].vtable->write_offs == NULL) { + res = -1; + errno = EIO; + } else { + res = fdtable[fd].vtable->write_offs(fdtable[fd].obj, buf, sz, *off); + } + } else { + if (fdtable[fd].vtable->read_offs == NULL) { + res = -1; + errno = EIO; + } else { + res = fdtable[fd].vtable->read_offs(fdtable[fd].obj, buf, sz, *off); + } + } + if (res > 0 && prw && from_offset == NULL) { + /* + * only update the fd offset when from_offset is not specified + * See pread() / pwrite() + */ + fdtable[fd].offset += res; + } + +unlock: + k_mutex_unlock(&fdtable[fd].lock); + + return res; +} + +ssize_t zvfs_read(int fd, void *buf, size_t sz, const size_t *from_offset) +{ + return zvfs_rw(fd, buf, sz, false, from_offset); +} + +ssize_t zvfs_write(int fd, const void *buf, size_t sz, const size_t *from_offset) +{ + return zvfs_rw(fd, (void *)buf, sz, true, from_offset); +} + +int zvfs_close(int fd) +{ + int res = 0; + + if (_check_fd(fd) < 0) { + return -1; + } + + (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); + if (fdtable[fd].vtable->close != NULL) { + /* close() is optional - e.g. stdinout_fd_op_vtable */ + if (fdtable[fd].mode & ZVFS_MODE_IFSOCK) { + /* Network socket needs to know socket number so pass + * it via close2() call. + */ + res = fdtable[fd].vtable->close2(fdtable[fd].obj, fd); + } else { + res = fdtable[fd].vtable->close(fdtable[fd].obj); + } + } + k_mutex_unlock(&fdtable[fd].lock); + + zvfs_free_fd(fd); + + return res; +} + +FILE *zvfs_fdopen(int fd, const char *mode) +{ + ARG_UNUSED(mode); + + if (_check_fd(fd) < 0) { + return NULL; + } + + return (FILE *)&fdtable[fd]; +} + +int zvfs_fileno(FILE *file) +{ + if (!IS_ARRAY_ELEMENT(fdtable, file)) { + errno = EBADF; + return -1; + } + + return (struct fd_entry *)file - fdtable; +} + +int zvfs_fstat(int fd, struct zvfs_stat *buf) +{ + if (_check_fd(fd) < 0) { + return -1; + } + + return zvfs_fdtable_call_ioctl(fdtable[fd].vtable, fdtable[fd].obj, ZFD_IOCTL_STAT, buf); +} + +int zvfs_fsync(int fd) +{ + if (_check_fd(fd) < 0) { + return -1; + } + + return zvfs_fdtable_call_ioctl(fdtable[fd].vtable, fdtable[fd].obj, ZFD_IOCTL_FSYNC); +} + +static inline off_t zvfs_lseek_wrap(int fd, int cmd, ...) +{ + off_t res; + va_list args; + + __ASSERT_NO_MSG(fd < ARRAY_SIZE(fdtable)); + + (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); + va_start(args, cmd); + res = fdtable[fd].vtable->ioctl(fdtable[fd].obj, cmd, args); + va_end(args); + if (res >= 0) { + switch (fdtable[fd].mode & ZVFS_MODE_IFMT) { + case ZVFS_MODE_IFDIR: + case ZVFS_MODE_IFBLK: + case ZVFS_MODE_IFSHM: + case ZVFS_MODE_IFREG: + fdtable[fd].offset = res; + break; + default: + break; + } + } + k_mutex_unlock(&fdtable[fd].lock); + + return res; +} + +off_t zvfs_lseek(int fd, off_t offset, int whence) +{ + if (_check_fd(fd) < 0) { + return -1; + } + + return zvfs_lseek_wrap(fd, ZFD_IOCTL_LSEEK, offset, whence, fdtable[fd].offset); +} + +int zvfs_fcntl(int fd, int cmd, va_list args) +{ + int res; + + if (_check_fd(fd) < 0) { + return -1; + } + + /* The rest of commands are per-fd, handled by ioctl vmethod. */ + res = fdtable[fd].vtable->ioctl(fdtable[fd].obj, cmd, args); + + return res; +} + +static inline int zvfs_ftruncate_wrap(int fd, int cmd, ...) +{ + int res; + va_list args; + + __ASSERT_NO_MSG(fd < ARRAY_SIZE(fdtable)); + + (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); + va_start(args, cmd); + res = fdtable[fd].vtable->ioctl(fdtable[fd].obj, cmd, args); + va_end(args); + k_mutex_unlock(&fdtable[fd].lock); + + return res; +} + +int zvfs_ftruncate(int fd, off_t length) +{ + if (_check_fd(fd) < 0) { + return -1; + } + + return zvfs_ftruncate_wrap(fd, ZFD_IOCTL_TRUNCATE, length); +} + +int zvfs_ioctl(int fd, unsigned long request, va_list args) +{ + if (_check_fd(fd) < 0) { + return -1; + } + + return fdtable[fd].vtable->ioctl(fdtable[fd].obj, request, args); +} + +int zvfs_unlink(const char *path) +{ + int res = fs_unlink(path); + + if (res < 0) { + errno = -res; + return -1; + } + return 0; +} + +int zvfs_rename(const char *old, const char *newp) +{ + int res = fs_rename(old, newp); + + if (res < 0) { + errno = -res; + return -1; + } + return 0; +} + +#if defined(CONFIG_POSIX_DEVICE_IO) +/* + * fd operations for stdio/stdout/stderr + */ + +int z_impl_zephyr_write_stdout(const char *buf, int nbytes); + +static ssize_t stdinout_read_vmeth(void *obj, void *buffer, size_t count) +{ + return 0; +} + +static ssize_t stdinout_write_vmeth(void *obj, const void *buffer, size_t count) +{ +#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_ARCMWDT_LIBC) + return z_impl_zephyr_write_stdout(buffer, count); +#else + return 0; +#endif +} + +static int stdinout_ioctl_vmeth(void *obj, unsigned int request, va_list args) +{ + errno = EINVAL; + return -1; +} + +static const struct fd_op_vtable stdinout_fd_op_vtable = { + .read = stdinout_read_vmeth, + .write = stdinout_write_vmeth, + .ioctl = stdinout_ioctl_vmeth, +}; + +#endif /* defined(CONFIG_POSIX_DEVICE_IO) */ diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_poll.c b/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_poll.c new file mode 100644 index 00000000..f91f9dd2 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/lib/os/zvfs/zvfs_poll.c @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2017-2018 Linaro Limited + * Copyright (c) 2021 Nordic Semiconductor + * Copyright (c) 2023 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2024 Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) +bool net_socket_is_tls(void *obj); +#else +#define net_socket_is_tls(obj) false +#endif + +int zvfs_poll_internal(struct zvfs_pollfd *fds, int nfds, k_timeout_t timeout) +{ + bool retry; + int ret = 0; + int i; + struct zvfs_pollfd *pfd; + struct k_poll_event poll_events[CONFIG_ZVFS_POLL_MAX]; + struct k_poll_event *pev; + struct k_poll_event *pev_end = poll_events + ARRAY_SIZE(poll_events); + const struct fd_op_vtable *vtable; + struct k_mutex *lock; + k_timepoint_t end; + bool offload = false; + const struct fd_op_vtable *offl_vtable = NULL; + void *offl_ctx = NULL; + + end = sys_timepoint_calc(timeout); + + pev = poll_events; + for (pfd = fds, i = nfds; i--; pfd++) { + void *ctx; + int result; + + /* Per POSIX, negative fd's are just ignored */ + if (pfd->fd < 0) { + continue; + } + + ctx = zvfs_get_fd_obj_and_vtable(pfd->fd, &vtable, &lock); + if (ctx == NULL) { + /* Will set POLLNVAL in return loop */ + continue; + } + + (void)k_mutex_lock(lock, K_FOREVER); + + result = zvfs_fdtable_call_ioctl(vtable, ctx, ZFD_IOCTL_POLL_PREPARE, pfd, &pev, + pev_end); + if (result == -EALREADY) { + /* If POLL_PREPARE returned with EALREADY, it means + * it already detected that some socket is ready. In + * this case, we still perform a k_poll to pick up + * as many events as possible, but without any wait. + */ + timeout = K_NO_WAIT; + end = sys_timepoint_calc(timeout); + result = 0; + } else if (result == -EXDEV) { + /* If POLL_PREPARE returned EXDEV, it means + * it detected an offloaded socket. + * If offloaded socket is used with native TLS, the TLS + * wrapper for the offloaded poll will be used. + * In case the fds array contains a mixup of offloaded + * and non-offloaded sockets, the offloaded poll handler + * shall return an error. + */ + offload = true; + if (offl_vtable == NULL || net_socket_is_tls(ctx)) { + offl_vtable = vtable; + offl_ctx = ctx; + } + + result = 0; + } + + k_mutex_unlock(lock); + + if (result < 0) { + errno = -result; + return -1; + } + } + + if (offload) { + int poll_timeout; + + if (K_TIMEOUT_EQ(timeout, K_FOREVER)) { + poll_timeout = SYS_FOREVER_MS; + } else { + poll_timeout = k_ticks_to_ms_floor32(timeout.ticks); + } + + return zvfs_fdtable_call_ioctl(offl_vtable, offl_ctx, ZFD_IOCTL_POLL_OFFLOAD, fds, + nfds, poll_timeout); + } + + timeout = sys_timepoint_timeout(end); + + do { + ret = k_poll(poll_events, pev - poll_events, timeout); + /* EAGAIN when timeout expired, EINTR when cancelled (i.e. EOF) */ + if (ret != 0 && ret != -EAGAIN && ret != -EINTR) { + errno = -ret; + return -1; + } + + retry = false; + ret = 0; + + pev = poll_events; + for (pfd = fds, i = nfds; i--; pfd++) { + void *ctx; + int result; + + pfd->revents = 0; + + if (pfd->fd < 0) { + continue; + } + + ctx = zvfs_get_fd_obj_and_vtable(pfd->fd, &vtable, &lock); + if (ctx == NULL) { + pfd->revents = ZVFS_POLLNVAL; + ret++; + continue; + } + + (void)k_mutex_lock(lock, K_FOREVER); + + result = zvfs_fdtable_call_ioctl(vtable, ctx, ZFD_IOCTL_POLL_UPDATE, pfd, + &pev); + k_mutex_unlock(lock); + + if (result == -EAGAIN) { + retry = true; + continue; + } else if (result != 0) { + errno = -result; + return -1; + } + + if (pfd->revents != 0) { + ret++; + } + } + + if (retry) { + if (ret > 0) { + break; + } + + timeout = sys_timepoint_timeout(end); + + if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { + break; + } + } + } while (retry); + + return ret; +} + +int z_impl_zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout) +{ + k_timeout_t timeout; + + if (poll_timeout < 0) { + timeout = K_FOREVER; + } else { + timeout = K_MSEC(poll_timeout); + } + + return zvfs_poll_internal(fds, nfds, timeout); +} + +#ifdef CONFIG_USERSPACE +static inline int z_vrfy_zvfs_poll(struct zvfs_pollfd *fds, int nfds, int timeout) +{ + struct zvfs_pollfd *fds_copy; + size_t fds_size; + int ret; + + /* Copy fds array from user mode */ + if (size_mul_overflow(nfds, sizeof(struct zvfs_pollfd), &fds_size)) { + errno = EFAULT; + return -1; + } + fds_copy = k_usermode_alloc_from_copy((void *)fds, fds_size); + if (!fds_copy) { + errno = ENOMEM; + return -1; + } + + ret = z_impl_zvfs_poll(fds_copy, nfds, timeout); + + if (ret >= 0) { + k_usermode_to_copy((void *)fds, fds_copy, fds_size); + } + k_free(fds_copy); + + return ret; +} +#include +#endif diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/template/smoke.c b/tasks/runtime-zephyr-eventfd-lock-progress/template/smoke.c new file mode 100644 index 00000000..9688e22a --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/template/smoke.c @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +int main(void) +{ + uint64_t value=0; + int fd=zvfs_eventfd(4,ZVFS_EFD_NONBLOCK); + int w=zvfs_eventfd_write(fd,3); + int r=zvfs_eventfd_read(fd,&value); + printf("fd=%d write=%d read=%d value=%llu\n",fd,w,r,(unsigned long long)value); + return 0; +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/valid_shared_entry/lib/os/zvfs/zvfs_eventfd.c b/tasks/runtime-zephyr-eventfd-lock-progress/valid_shared_entry/lib/os/zvfs/zvfs_eventfd.c new file mode 100644 index 00000000..ec24b381 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/valid_shared_entry/lib/os/zvfs/zvfs_eventfd.c @@ -0,0 +1,466 @@ +/* + * Copyright (c) 2020 Tobias Svehagen + * Copyright (c) 2023, Meta + * Copyright (c) 2024, Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include + +#define ZVFS_EFD_IN_USE 0x1 +#define ZVFS_EFD_FLAGS_SET (ZVFS_EFD_SEMAPHORE | ZVFS_EFD_NONBLOCK) + +struct zvfs_eventfd { + struct k_poll_signal read_sig; + struct k_poll_signal write_sig; + struct k_spinlock lock; + zvfs_eventfd_t cnt; + int flags; +}; + +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)); + +SYS_BITARRAY_DEFINE_STATIC(efds_bitarray, CONFIG_ZVFS_EVENTFD_MAX); +static struct zvfs_eventfd efds[CONFIG_ZVFS_EVENTFD_MAX]; +static const struct fd_op_vtable zvfs_eventfd_fd_vtable; + +static inline bool zvfs_eventfd_is_in_use(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_IN_USE) != 0; +} + +static inline bool zvfs_eventfd_is_semaphore(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_SEMAPHORE) != 0; +} + +static inline bool zvfs_eventfd_is_blocking(struct zvfs_eventfd *efd) +{ + return (efd->flags & ZVFS_EFD_NONBLOCK) == 0; +} + +static int zvfs_eventfd_poll_prepare(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev, + struct k_poll_event *pev_end) +{ + if (pfd->events & ZSOCK_POLLIN) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->read_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + if (*pev == pev_end) { + errno = ENOMEM; + return -1; + } + + (*pev)->obj = &efd->write_sig; + (*pev)->type = K_POLL_TYPE_SIGNAL; + (*pev)->mode = K_POLL_MODE_NOTIFY_ONLY; + (*pev)->state = K_POLL_STATE_NOT_READY; + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_poll_update(struct zvfs_eventfd *efd, + struct zsock_pollfd *pfd, + struct k_poll_event **pev) +{ + if (pfd->events & ZSOCK_POLLIN) { + pfd->revents |= ZSOCK_POLLIN * (efd->cnt > 0); + (*pev)++; + } + + if (pfd->events & ZSOCK_POLLOUT) { + pfd->revents |= ZSOCK_POLLOUT * (efd->cnt < UINT64_MAX - 1); + (*pev)++; + } + + return 0; +} + +static int zvfs_eventfd_read_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (efd->cnt == 0) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful read */ + if (zvfs_eventfd_is_semaphore(efd)) { + *value = 1; + --efd->cnt; + } else { + *value = efd->cnt; + efd->cnt = 0; + } + + if (efd->cnt == 0) { + k_poll_signal_reset(&efd->read_sig); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + return 0; +} + +static int zvfs_eventfd_write_locked(struct zvfs_eventfd *efd, zvfs_eventfd_t *value) +{ + zvfs_eventfd_t result; + + if (!zvfs_eventfd_is_in_use(efd)) { + /* file descriptor has been closed */ + return -EBADF; + } + + if (*value == UINT64_MAX) { + /* not a permitted value */ + return -EINVAL; + } + + if (u64_add_overflow(efd->cnt, *value, &result) || result == UINT64_MAX) { + /* would block / try again */ + return -EAGAIN; + } + + /* successful write */ + efd->cnt = result; + + if (efd->cnt == (UINT64_MAX - 1)) { + k_poll_signal_reset(&efd->write_sig); + } + + k_poll_signal_raise(&efd->read_sig, 0); + + return 0; +} + +static ssize_t zvfs_eventfd_read_op(void *obj, void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, buf, sz, zvfs_eventfd_read_locked); +} + +static ssize_t zvfs_eventfd_write_op(void *obj, const void *buf, size_t sz) +{ + return zvfs_eventfd_rw_op(obj, (zvfs_eventfd_t *)buf, sz, zvfs_eventfd_write_locked); +} + +static int zvfs_eventfd_close_op(void *obj) +{ + int ret; + int err; + k_spinlock_key_t key; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + return -1; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + err = sys_bitarray_free(&efds_bitarray, 1, (struct zvfs_eventfd *)obj - efds); + __ASSERT(err == 0, "sys_bitarray_free() failed: %d", err); + + efd->flags = 0; + efd->cnt = 0; + + ret = 0; + +unlock: + k_spin_unlock(&efd->lock, key); + /* when closing an zvfs_eventfd, broadcast to all waiters */ + err = k_condvar_broadcast(cond); + __ASSERT(err == 0, "k_condvar_broadcast() failed: %d", err); + + return ret; +} + +static int zvfs_eventfd_ioctl_op(void *obj, unsigned int request, va_list args) +{ + int ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = (struct zvfs_eventfd *)obj; + + /* note: zsock_poll_internal() has already taken the mutex */ + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_in_use(efd)) { + errno = EBADF; + ret = -1; + goto unlock; + } + + switch (request) { + case F_GETFL: + ret = efd->flags & ZVFS_EFD_FLAGS_SET; + break; + + case F_SETFL: { + int flags; + + flags = va_arg(args, int); + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + ret = -1; + } else { + int prev_flags = efd->flags & ~ZVFS_EFD_FLAGS_SET; + + efd->flags = flags | prev_flags; + ret = 0; + } + } break; + + case ZFD_IOCTL_POLL_PREPARE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + struct k_poll_event *pev_end; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + pev_end = va_arg(args, struct k_poll_event *); + + ret = zvfs_eventfd_poll_prepare(obj, pfd, pev, pev_end); + } break; + + case ZFD_IOCTL_POLL_UPDATE: { + struct zsock_pollfd *pfd; + struct k_poll_event **pev; + + pfd = va_arg(args, struct zsock_pollfd *); + pev = va_arg(args, struct k_poll_event **); + + ret = zvfs_eventfd_poll_update(obj, pfd, pev); + } break; + + default: + errno = EOPNOTSUPP; + ret = -1; + break; + } + +unlock: + k_spin_unlock(&efd->lock, key); + + return ret; +} + +static const struct fd_op_vtable zvfs_eventfd_fd_vtable = { + .read = zvfs_eventfd_read_op, + .write = zvfs_eventfd_write_op, + .close = zvfs_eventfd_close_op, + .ioctl = zvfs_eventfd_ioctl_op, +}; + +/* common to both zvfs_eventfd_read_op() and zvfs_eventfd_write_op() */ +static ssize_t zvfs_eventfd_rw_op(void *obj, void *buf, size_t sz, + int (*op)(struct zvfs_eventfd *efd, zvfs_eventfd_t *value)) +{ + int err; + ssize_t ret; + k_spinlock_key_t key; + struct zvfs_eventfd *efd = obj; + struct k_mutex *lock = NULL; + struct k_condvar *cond = NULL; + + if (sz < sizeof(zvfs_eventfd_t)) { + errno = EINVAL; + return -1; + } + + if (buf == NULL) { + errno = EFAULT; + return -1; + } + + key = k_spin_lock(&efd->lock); + + if (!zvfs_eventfd_is_blocking(efd)) { + /* + * Handle the non-blocking case entirely within this scope + */ + ret = op(efd, buf); + if (ret < 0) { + errno = -ret; + ret = -1; + } else { + ret = sizeof(zvfs_eventfd_t); + } + + goto unlock_spin; + } + + /* + * Handle the blocking case below + */ + __ASSERT_NO_MSG(zvfs_eventfd_is_blocking(efd)); + + if (k_is_in_isr()) { + /* not covered by the man page, but necessary in Zephyr */ + errno = EWOULDBLOCK; + ret = -1; + goto unlock_spin; + } + + err = (int)zvfs_get_obj_lock_and_cond(obj, &zvfs_eventfd_fd_vtable, &lock, &cond); + __ASSERT((bool)err, "zvfs_get_obj_lock_and_cond() failed"); + __ASSERT_NO_MSG(lock != NULL); + __ASSERT_NO_MSG(cond != NULL); + + k_spin_unlock(&efd->lock, key); + + while (true) { + /* retake the spinlock */ + key = k_spin_lock(&efd->lock); + + ret = op(efd, buf); + switch (ret) { + case -EAGAIN: + /* not an error in blocking mode. break and try again */ + break; + case 0: + /* success! */ + ret = sizeof(zvfs_eventfd_t); + goto unlock; + default: + /* some other error */ + __ASSERT_NO_MSG(ret < 0); + errno = -ret; + ret = -1; + goto unlock; + } + + /* do not hold a spinlock when taking a mutex */ + k_spin_unlock(&efd->lock, key); + + /* wait for a write or close */ + err = k_condvar_wait(cond, lock, K_FOREVER); + __ASSERT(err == 0, "k_condvar_wait() failed: %d", err); + } + +unlock: + k_spin_unlock(&efd->lock, key); + /* only wake a single waiter */ + err = k_condvar_signal(cond); + __ASSERT(err == 0, "k_condvar_signal() failed: %d", err); + goto out; + +unlock_spin: + k_spin_unlock(&efd->lock, key); + +out: + return ret; +} + +/* + * Public-facing API + */ + +int zvfs_eventfd(unsigned int initval, int flags) +{ + int fd = 1; + size_t offset; + struct zvfs_eventfd *efd = NULL; + + if (flags & ~ZVFS_EFD_FLAGS_SET) { + errno = EINVAL; + return -1; + } + + if (sys_bitarray_alloc(&efds_bitarray, 1, &offset) < 0) { + errno = ENOMEM; + return -1; + } + + efd = &efds[offset]; + + fd = zvfs_reserve_fd(); + if (fd < 0) { + sys_bitarray_free(&efds_bitarray, 1, offset); + return -1; + } + + efd->flags = ZVFS_EFD_IN_USE | flags; + efd->cnt = initval; + + k_poll_signal_init(&efd->write_sig); + k_poll_signal_init(&efd->read_sig); + + if (initval != 0) { + k_poll_signal_raise(&efd->read_sig, 0); + } + + k_poll_signal_raise(&efd->write_sig, 0); + + zvfs_finalize_fd(fd, efd, &zvfs_eventfd_fd_vtable); + + return fd; +} + +static int zvfs_eventfd_direct_rw(int fd, zvfs_eventfd_t *value, bool writing) +{ + const struct fd_op_vtable *vtable; + struct k_mutex *lock; + void *obj = zvfs_get_fd_obj_and_vtable(fd, &vtable, &lock); + if (obj == NULL) { + return -1; + } + if (vtable != &zvfs_eventfd_fd_vtable) { + errno = EBADF; + return -1; + } + k_mutex_lock(lock, K_FOREVER); + ssize_t transferred = zvfs_eventfd_rw_op(obj, value, sizeof(*value), + writing ? zvfs_eventfd_write_locked : zvfs_eventfd_read_locked); + k_mutex_unlock(lock); + return transferred < 0 ? -1 : 0; +} + +int zvfs_eventfd_read(int fd, zvfs_eventfd_t *value) +{ + return zvfs_eventfd_direct_rw(fd, value, false); +} + +int zvfs_eventfd_write(int fd, zvfs_eventfd_t value) +{ + return zvfs_eventfd_direct_rw(fd, &value, true); +} diff --git a/tasks/runtime-zephyr-eventfd-lock-progress/verify.py b/tasks/runtime-zephyr-eventfd-lock-progress/verify.py new file mode 100644 index 00000000..9e2c6c26 --- /dev/null +++ b/tasks/runtime-zephyr-eventfd-lock-progress/verify.py @@ -0,0 +1,130 @@ +"""Run an imported Bos-qiao check.py unchanged; candidate processes run as UID 65534.""" + +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile + +GRADER = Path(__file__).resolve().parent +BLOCKED = "isolation-blocked.txt" +GUARD = 86 +# Runs the original check unchanged as the trusted user. In isolation mode every child process drops to +# UID 65534 and every mkdtemp scratch is handed to it; an audit hook stops the check if candidate code +# would run inside this process. Failing child processes also copy their stderr tail out for private triage. +CHECK = ( + "import os, runpy, subprocess, sys, tempfile\n" + "isolated, sys.argv = sys.argv[1] == '1', sys.argv[2:]\n" + "untrusted = [os.path.realpath(sys.argv[1])]\n" + "def inside(path):\n" + " path = os.path.realpath(str(path))\n" + " return any(path == root or path.startswith(root + os.sep) for root in untrusted)\n" + "spawns = ('os.system', 'os.exec', 'os.spawn', 'os.fork', 'os.forkpty', 'os.posix_spawn') if isolated else ()\n" + "def guard(event, args):\n" + " if (event in spawns or event == 'exec' and inside(getattr(args[0], 'co_filename', '')) or\n" + " event == 'ctypes.dlopen' and args[0] and inside(args[0]) or\n" + " event == 'import' and any(inside(entry or '.') for entry in sys.path)):\n" + " sys.__stderr__.write('rollout-guard: ' + event + '\\n')\n" + " sys.__stderr__.flush()\n" + " os._exit(%d)\n" + "mkdtemp = tempfile.mkdtemp\n" + "def scratch(*args, **kwargs):\n" + " path = mkdtemp(*args, **kwargs)\n" + " untrusted.append(os.path.realpath(path))\n" + " if isolated:\n" + " os.chown(path, 65534, 65534)\n" + " return path\n" + "tempfile.mkdtemp = scratch\n" + "popen = subprocess.Popen.__init__\n" + "def drop(self, *args, **kwargs):\n" + " if isolated:\n" + " kwargs.update(user=65534, group=65534, extra_groups=[])\n" + " popen(self, *args, **kwargs)\n" + "subprocess.Popen.__init__ = drop\n" + "real = subprocess.run\n" + "def run(*args, **kwargs):\n" + " result = real(*args, **kwargs)\n" + " if result.returncode and result.stderr:\n" + " tail = result.stderr if isinstance(result.stderr, str) else result.stderr.decode('utf-8', 'replace')\n" + " sys.__stderr__.write(tail[-2000:] + '\\n')\n" + " return result\n" + "subprocess.run = run\n" + "sys.addaudithook(guard)\n" + "runpy.run_path(sys.argv[0], run_name='__main__')\n" % GUARD) + + +class UnsupportedEnvironment(RuntimeError): + pass + + +def candidate_user(): + os.setgroups([]) + os.setgid(65534) + os.setuid(65534) + + +def isolation(): + if os.environ.get("ROLLOUT_GRADE_ISOLATION") != "uid65534": + return None + if sys.platform != "linux" or os.geteuid() != 0: + raise UnsupportedEnvironment("isolated grader requires a Linux root supervisor") + probe = ( + "from pathlib import Path\n" + "try: Path('/grader/verify.py').read_bytes()\n" + "except PermissionError: raise SystemExit(0)\n" + "raise SystemExit(1)\n" + ) + try: + result = subprocess.run([sys.executable, "-I", "-c", probe], + capture_output=True, timeout=10, preexec_fn=candidate_user) + except subprocess.SubprocessError as error: + raise UnsupportedEnvironment("candidate identity could not be isolated") from error + if result.returncode != 0: + raise UnsupportedEnvironment("candidate can read the private grader or isolation probe failed") + return candidate_user + + +def verify(candidate): + if (GRADER / BLOCKED).exists(): + raise UnsupportedEnvironment("isolation blocked: " + (GRADER / BLOCKED).read_text().strip()[:200]) + isolated = isolation() is not None + if shutil.which("gcc") is None: + raise UnsupportedEnvironment("C compiler unavailable") + with tempfile.TemporaryDirectory(prefix="bos-grade-") as temporary: + root = Path(temporary) + work, grader = root / "work", root / "grader" + shutil.copytree(candidate, work, symlinks=True) + shutil.copytree(GRADER, grader, ignore=lambda folder, names: ["verify.py"] if Path(folder) == GRADER else []) + if isolated: + os.chmod(root, 0o711) + os.chmod(grader, 0o700) + for path in (work, *work.rglob("*")): + os.lchown(path, 65534, 65534) + # shortcut: the check runs as root and follows symlinks a child plants in shared scratch directories; + # upgrade by running the check under its own unprivileged UID once a setuid helper is available. + completed = subprocess.run([sys.executable, "-I", "-B", "-c", CHECK, "1" if isolated else "0", + str(grader / "check.py"), str(work)], + cwd=root, capture_output=True, text=True, timeout=100) + # grade.stderr stays on the trusted side with the archive; it may quote hidden test lines. + sys.stderr.write(completed.stderr[-4000:]) + if completed.returncode == GUARD and "rollout-guard: " in completed.stderr: + raise UnsupportedEnvironment("isolation blocked: the check would run candidate code in the grader process") + lines = completed.stdout.splitlines() + if completed.returncode == 0 and lines[-1:] == ["PASS"]: + return {"status": "PASS", "checks": 1} + return {"status": "FAIL", "checks": 1, "reason": (lines[-1] if lines else "no checker output")[:300]} + + +if __name__ == "__main__": + try: + result = verify(Path(sys.argv[1])) + except UnsupportedEnvironment as error: + result = {"status": "UNVERIFIED", "checks": 0, "reason": str(error)} + except subprocess.TimeoutExpired: + result = {"status": "FAIL", "checks": 1, "reason": "timeout"} + except Exception as error: + result = {"status": "INFRA_ERROR", "checks": 0, "reason": str(error)} + print(json.dumps(result, sort_keys=True)) + sys.exit({"PASS": 0, "FAIL": 1}.get(result["status"], 2)) diff --git a/tasks/runtime-zephyr-telnet-close-handoff/cheat/include/zephyr/shell/shell_telnet.h b/tasks/runtime-zephyr-telnet-close-handoff/cheat/include/zephyr/shell/shell_telnet.h new file mode 100644 index 00000000..1320f55a --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/cheat/include/zephyr/shell/shell_telnet.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Header file for the Telnet shell backend. + * @ingroup shell_telnet + */ + +#ifndef ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ +#define ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond INTERNAL_HIDDEN */ +extern const struct shell_transport_api shell_telnet_transport_api; + +#define SHELL_TELNET_POLLFD_COUNT 3 +#define SHELL_TELNET_MAX_CMD_SIZE 3 + +/** Line buffer structure. */ +struct shell_telnet_line_buf { + /** Line buffer. */ + char buf[CONFIG_SHELL_TELNET_LINE_BUF_SIZE]; + + /** Current line length. */ + uint16_t len; +}; + +/** TELNET-based shell transport. */ +struct shell_telnet { + /** Handler function registered by shell. */ + shell_transport_handler_t shell_handler; + + /** Context registered by shell. */ + void *shell_context; + + /** Buffer for outgoing line. */ + struct shell_telnet_line_buf line_out; + + /** Array for sockets used by the telnet service. */ + struct zsock_pollfd fds[SHELL_TELNET_POLLFD_COUNT]; + + /** Input buffer. */ + uint8_t rx_buf[CONFIG_SHELL_CMD_BUFF_SIZE]; + + /** Number of data bytes within the input buffer. */ + size_t rx_len; + + /** Mutex protecting the input buffer access. */ + struct k_mutex rx_lock; + + /** Mutex serializing sends, cancels of send_work and the client close. */ + struct k_mutex tx_lock; + uint8_t cmd_buf[SHELL_TELNET_MAX_CMD_SIZE]; + uint8_t cmd_len; + + /** The delayed work is used to send non-lf terminated output that has + * been around for "too long". This will prove to be useful + * to send the shell prompt for instance. + */ + struct k_work_delayable send_work; + + /** If set, no output is sent to the TELNET client. */ + bool output_lock; +}; +/** @endcond */ + +/** + * @defgroup shell_telnet Telnet shell backend + * @ingroup shell_backends + * @brief Shell access over a TELNET connection. + * @{ + */ + +/** + * @brief Define a Telnet shell transport instance. + * + * @param _name Name of the transport instance. + */ +#define SHELL_TELNET_DEFINE(_name) \ + static struct shell_telnet _name##_shell_telnet; \ + struct shell_transport _name = { \ + .api = &shell_telnet_transport_api, \ + .ctx = (struct shell_telnet *)&_name##_shell_telnet \ + } + +/** + * @brief This function provides pointer to shell telnet backend instance. + * + * Function returns pointer to the shell telnet instance. This instance can be + * next used with shell_execute_cmd function in order to test commands behavior. + * + * @returns Pointer to the shell instance. + */ +const struct shell *shell_backend_telnet_get_ptr(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/cheat/subsys/shell/backends/shell_telnet.c b/tasks/runtime-zephyr-telnet-close-handoff/cheat/subsys/shell/backends/shell_telnet.c new file mode 100644 index 00000000..8eea9182 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/cheat/subsys/shell/backends/shell_telnet.c @@ -0,0 +1,855 @@ +/* + * Copyright (c) 2017 Intel Corporation + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include "shell_telnet_protocol.h" + +SHELL_TELNET_DEFINE(shell_transport_telnet); +SHELL_DEFINE(shell_telnet, CONFIG_SHELL_PROMPT_TELNET, &shell_transport_telnet, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_SIZE, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_TIMEOUT, + SHELL_FLAG_OLF_CRLF); + +LOG_MODULE_REGISTER(shell_telnet, CONFIG_SHELL_TELNET_LOG_LEVEL); + +struct shell_telnet *sh_telnet; + +/* Various definitions mapping the TELNET service configuration options */ +#define TELNET_PORT CONFIG_SHELL_TELNET_PORT +#define TELNET_LINE_SIZE CONFIG_SHELL_TELNET_LINE_BUF_SIZE +#define TELNET_TIMEOUT CONFIG_SHELL_TELNET_SEND_TIMEOUT + +#define TELNET_MIN_COMMAND_LEN 2 +#define TELNET_WILL_DO_COMMAND_LEN 3 + +#define SOCK_ID_IPV4_LISTEN 0 +#define SOCK_ID_IPV6_LISTEN 1 +#define SOCK_ID_CLIENT 2 +#define SOCK_ID_MAX 3 + +/* Basic TELNET implementation. */ + +static void telnet_server_cb(struct net_socket_service_event *evt); +static int telnet_init(struct shell_telnet *ctx); + +NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(telnet_server, telnet_server_cb, + SHELL_TELNET_POLLFD_COUNT); + + +/* Only the socket service thread closes the client socket, and only once no + * send holds tx_lock, so the descriptor cannot be closed and reused under a + * send in progress. Every synchronous cancel of send_work also runs under + * tx_lock, so no two threads ever wait for the same cancel. + */ +static void telnet_close_client(void) +{ + struct k_work_sync sync; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + (void)k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + sh_telnet->output_lock = false; + + k_mutex_unlock(&sh_telnet->tx_lock); +} + +/* A failed send on another thread shuts the socket down for reading instead + * of closing it; that wakes the service thread with EOF and the close happens + * here. + */ +static void telnet_end_client_connection(void) +{ + int ret; + + telnet_close_client(); + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + } +} + +static void telnet_command_send_reply(uint8_t *msg, uint16_t len) +{ + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return; + } + + while (len > 0) { + int ret; + + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, 0); + if (ret < 0) { + LOG_ERR("Failed to send command %d, shutting down", ret); + telnet_end_client_connection(); + break; + } + + msg += ret; + len -= ret; + } +} + +static void telnet_reply_ay_command(void) +{ + static const char alive[] = "Zephyr at your service\r\n"; + + telnet_command_send_reply((uint8_t *)alive, strlen(alive)); +} + +static int telnet_echo_set(const struct shell *sh, bool val) +{ + int ret = shell_echo_set(sh_telnet->shell_context, val); + + if (ret < 0) { + LOG_ERR("Failed to set echo to: %d, err: %d", val, ret); + } + return ret; +} + +static void telnet_reply_dont_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, false); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL_NOT; + } else { + cmd->op = NVT_CMD_WILL; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_do_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_SUPR_GA: + cmd->op = NVT_CMD_WILL; + break; + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, true); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL; + } else { + cmd->op = NVT_CMD_WILL_NOT; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_command(struct telnet_simple_command *cmd) +{ + struct k_work_sync sync; + + if (!cmd->iac) { + return; + } + + switch (cmd->op) { + case NVT_CMD_AO: + /* OK, no output then */ + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + sh_telnet->output_lock = true; + sh_telnet->line_out.len = 0; + (void)k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + k_mutex_unlock(&sh_telnet->tx_lock); + break; + case NVT_CMD_AYT: + telnet_reply_ay_command(); + break; + case NVT_CMD_DO: + telnet_reply_do_command(cmd); + break; + case NVT_CMD_DO_NOT: + telnet_reply_dont_command(cmd); + break; + default: + LOG_DBG("Operation %u not handled", cmd->op); + break; + } +} + +static int telnet_send(bool block) +{ + int ret; + uint8_t *msg = sh_telnet->line_out.buf; + uint16_t len = sh_telnet->line_out.len; + + if (sh_telnet->line_out.len == 0) { + return 0; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return -ENOTCONN; + } + + if (sh_telnet->output_lock) { + return -ECANCELED; + } + + while (len > 0) { + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, + block ? 0 : ZSOCK_MSG_DONTWAIT); + if (!block && (ret < 0) && (errno == EAGAIN)) { + /* Not all data was sent - move the remaining data and + * update length. + */ + memmove(sh_telnet->line_out.buf, msg, len); + sh_telnet->line_out.len = len; + return -EAGAIN; + } + + if (ret < 0) { + /* Stop the output before anything else: the caller logs + * the error once tx_lock is released, and a log backend + * on this shell must find the output already stopped. + */ + ret = -errno; + sh_telnet->output_lock = true; + (void)zsock_shutdown(sh_telnet->fds[SOCK_ID_CLIENT].fd, + ZSOCK_SHUT_RD); + return ret; + } + + msg += ret; + len -= ret; + } + + /* We reinitialize the line buffer */ + sh_telnet->line_out.len = 0; + + return 0; +} + +static void telnet_send_prematurely(struct k_work *work) +{ + int ret = -EAGAIN; + + /* Use non-blocking send to prevent system workqueue blocking. */ + if (k_mutex_lock(&sh_telnet->tx_lock, K_NO_WAIT) == 0) { + ret = telnet_send(false); + k_mutex_unlock(&sh_telnet->tx_lock); + } + + if (ret == -EAGAIN) { + /* Not all data was sent, reschedule the work. */ + k_work_reschedule(&sh_telnet->send_work, K_MSEC(TELNET_TIMEOUT)); + } else if (ret < 0 && ret != -ENOTCONN && ret != -ECANCELED) { + LOG_ERR("Failed to send %d, shutting down", -ret); + } +} + +static int telnet_command_length(uint8_t op) +{ + if (op == NVT_CMD_SB || op == NVT_CMD_WILL || op == NVT_CMD_WILL_NOT || + op == NVT_CMD_DO || op == NVT_CMD_DO_NOT) { + return TELNET_WILL_DO_COMMAND_LEN; + } + + return TELNET_MIN_COMMAND_LEN; +} + +static inline int telnet_handle_command(struct telnet_simple_command *cmd) +{ + /* Commands are two or three bytes. */ + if (cmd->iac != NVT_CMD_IAC) { + return 0; + } + + if (IS_ENABLED(CONFIG_SHELL_TELNET_SUPPORT_COMMAND)) { + LOG_DBG("Got a command %u/%u/%u", cmd->iac, cmd->op, cmd->opt); + telnet_reply_command(cmd); + } + + if (cmd->op == NVT_CMD_SB) { + /* TODO Add subnegotiation support. */ + return -EOPNOTSUPP; + } + + return 0; +} + +static void telnet_recv(struct zsock_pollfd *pollfd) +{ + struct telnet_simple_command *cmd = + (struct telnet_simple_command *)sh_telnet->cmd_buf; + size_t len, off, buf_left, cmd_total_len; + uint8_t *buf; + int ret; + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + buf_left = sizeof(sh_telnet->rx_buf) - sh_telnet->rx_len; + if (buf_left == 0) { + /* No space left to read TCP stream, try again later. */ + k_mutex_unlock(&sh_telnet->rx_lock); + k_msleep(10); + return; + } + + buf = sh_telnet->rx_buf + sh_telnet->rx_len; + + ret = zsock_recv(pollfd->fd, buf, buf_left, 0); + if (ret < 0) { + LOG_DBG("Telnet client error %d", ret); + goto error; + } else if (ret == 0) { + LOG_DBG("Telnet client closed connection"); + goto error; + } + + off = 0; + len = ret; + cmd_total_len = 0; + /* Filter out and process commands from the data buffer. */ + while (off < len) { + if (sh_telnet->cmd_len > 0) { + /* Command mode */ + if (sh_telnet->cmd_len == 1) { + /* Operation */ + cmd->op = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + + if (telnet_command_length(cmd->op) > + TELNET_MIN_COMMAND_LEN) { + continue; + } + } else if (sh_telnet->cmd_len == 2) { + /* Option */ + cmd->opt = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + } + + ret = telnet_handle_command(cmd); + if (ret < 0) { + goto error; + } else { + LOG_DBG("Handled command"); + } + + memset(cmd, 0, sizeof(*cmd)); + sh_telnet->cmd_len = 0; + + continue; + } + + if (*(buf + off) == NVT_CMD_IAC) { + cmd->iac = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + continue; + } + + /* Data byte, remove command bytes from the buffer, if any. */ + if (cmd_total_len > 0) { + size_t data_off = off; + + off -= cmd_total_len; + len -= cmd_total_len; + cmd_total_len = 0; + + memmove(buf + off, buf + data_off, len); + } + + off++; + } + + if (cmd_total_len > 0) { + /* In case the buffer ended with command, trim the buffer size + * here. + */ + len -= cmd_total_len; + } + + if (len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + return; + } + + sh_telnet->rx_len += len; + + k_mutex_unlock(&sh_telnet->rx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, + sh_telnet->shell_context); + + return; + +error: + k_mutex_unlock(&sh_telnet->rx_lock); + telnet_end_client_connection(); +} + +static void telnet_restart_server(void) +{ + int ret; + + if (sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd >= 0) { + telnet_close_client(); + } + + ret = telnet_init(sh_telnet); + if (ret < 0) { + LOG_ERR("Telnet fatal error, failed to restart server (%d)", ret); + (void)net_socket_service_unregister(&telnet_server); + } +} + +static void telnet_accept(struct zsock_pollfd *pollfd) +{ + int sock, ret = 0; + struct net_sockaddr_storage addr = {}; + net_socklen_t addrlen = sizeof(addr); + + sock = zsock_accept(pollfd->fd, net_sad(&addr), &addrlen); + if (sock < 0) { + ret = -errno; + LOG_ERR("Telnet accept error (%d)", ret); + return; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd > 0) { + /* Too many connections. */ + ret = 0; + LOG_ERR("Telnet client already connected."); + goto error; + } + + sh_telnet->fds[SOCK_ID_CLIENT].fd = sock; + sh_telnet->fds[SOCK_ID_CLIENT].events = ZSOCK_POLLIN; + sh_telnet->rx_len = 0; + sh_telnet->cmd_len = 0; + sh_telnet->line_out.len = 0; + sh_telnet->output_lock = false; + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, (%d)", ret); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + goto error; + } + + LOG_DBG("Telnet client connected (family NET_AF_INET%s)", + addr.ss_family == NET_AF_INET ? "" : "6"); + + /* Disable echo - if command handling is enabled we reply that we + * support echo. + */ + (void)telnet_echo_set(sh_telnet->shell_context, false); + + return; + +error: + if (sock > 0) { + (void)zsock_close(sock); + } + + if (ret < 0) { + telnet_restart_server(); + } +} + +static void telnet_server_cb(struct net_socket_service_event *evt) +{ + int sock_error; + int ret; + net_socklen_t optlen = sizeof(int); + + if (sh_telnet == NULL) { + return; + } + + if ((evt->event.revents & ZSOCK_POLLERR) || + (evt->event.revents & ZSOCK_POLLNVAL)) { + (void)zsock_getsockopt(evt->event.fd, ZSOCK_SOL_SOCKET, + ZSOCK_SO_ERROR, &sock_error, &optlen); + + ret = -sock_error; + + if (ret == -ENETDOWN) { + LOG_INF("Network is down"); + } else { + LOG_ERR("Telnet socket %d error (%d)", evt->event.fd, ret); + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_end_client_connection(); + return; + } + + goto error; + } + + if (!(evt->event.revents & ZSOCK_POLLIN)) { + return; + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV4_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV6_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_recv(&sh_telnet->fds[SOCK_ID_CLIENT]); + return; + } + + LOG_ERR("Unexpected FD received for telnet, restarting service."); + +error: + telnet_restart_server(); +} + +static int telnet_setup_server(struct zsock_pollfd *pollfd, net_sa_family_t family, + struct net_sockaddr *addr, net_socklen_t addrlen) +{ + int ret = 0; + + pollfd->fd = zsock_socket(family, NET_SOCK_STREAM, NET_IPPROTO_TCP); + if (pollfd->fd < 0) { + ret = -errno; + LOG_ERR("Failed to create telnet NET_AF_INET%s socket", + family == NET_AF_INET ? "" : "6"); + goto error; + } + + if (zsock_bind(pollfd->fd, addr, addrlen) < 0) { + ret = -errno; + LOG_ERR("Cannot bind on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + if (zsock_listen(pollfd->fd, 1)) { + ret = -errno; + LOG_ERR("Cannot listen on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + pollfd->events = ZSOCK_POLLIN; + + LOG_DBG("Telnet console enabled on NET_AF_INET%s", + family == NET_AF_INET ? "" : "6"); + + return 0; + +error: + LOG_ERR("Unable to start telnet on NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + + if (pollfd->fd >= 0) { + (void)zsock_close(pollfd->fd); + pollfd->fd = -1; + } + + return ret; +} + +static int telnet_init(struct shell_telnet *ctx) +{ + int ret; + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + struct net_sockaddr_in any_addr4 = { + .sin_family = NET_AF_INET, + .sin_port = net_htons(TELNET_PORT), + .sin_addr = NET_INADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV4_LISTEN], + NET_AF_INET, (struct net_sockaddr *)&any_addr4, + sizeof(any_addr4)); + if (ret < 0) { + goto error; + } + } + + if (IS_ENABLED(CONFIG_NET_IPV6)) { + struct net_sockaddr_in6 any_addr6 = { + .sin6_family = NET_AF_INET6, + .sin6_port = net_htons(TELNET_PORT), + .sin6_addr = NET_IN6ADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV6_LISTEN], + NET_AF_INET6, (struct net_sockaddr *)&any_addr6, + sizeof(any_addr6)); + if (ret < 0) { + goto error; + } + } + + ret = net_socket_service_register(&telnet_server, ctx->fds, + ARRAY_SIZE(ctx->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + goto error; + } + + LOG_INF("Telnet shell backend initialized"); + + return 0; + +error: + if (ctx->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV4_LISTEN].fd); + ctx->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (ctx->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV6_LISTEN].fd); + ctx->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + return ret; +} + +/* Shell API */ + +static int init(const struct shell_transport *transport, + const void *config, + shell_transport_handler_t evt_handler, + void *context) +{ + int err; + + sh_telnet = (struct shell_telnet *)transport->ctx; + + memset(sh_telnet, 0, sizeof(struct shell_telnet)); + for (int i = 0; i < ARRAY_SIZE(sh_telnet->fds); i++) { + sh_telnet->fds[i].fd = -1; + } + + sh_telnet->shell_handler = evt_handler; + sh_telnet->shell_context = context; + + err = telnet_init(sh_telnet); + if (err != 0) { + return err; + } + + k_work_init_delayable(&sh_telnet->send_work, telnet_send_prematurely); + k_mutex_init(&sh_telnet->rx_lock); + k_mutex_init(&sh_telnet->tx_lock); + + return 0; +} + +static int uninit(const struct shell_transport *transport) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int enable(const struct shell_transport *transport, bool blocking) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int telnet_write(const struct shell_transport *transport, + const void *data, size_t length, size_t *cnt) +{ + struct shell_telnet_line_buf *lb; + struct k_work_sync sync; + size_t copy_len; + int err; + uint32_t timeout; + bool was_running; + + if (sh_telnet == NULL) { + *cnt = 0; + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0 || sh_telnet->output_lock) { + *cnt = length; + return 0; + } + + *cnt = 0; + lb = &sh_telnet->line_out; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + /* Stop the transmission timer, so it does not interrupt the operation. + */ + timeout = k_ticks_to_ms_ceil32( + k_work_delayable_remaining_get(&sh_telnet->send_work)); + was_running = k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + + do { + if (lb->len + length - *cnt > TELNET_LINE_SIZE) { + copy_len = TELNET_LINE_SIZE - lb->len; + } else { + copy_len = length - *cnt; + } + + memcpy(lb->buf + lb->len, (uint8_t *)data + *cnt, copy_len); + lb->len += copy_len; + + /* Send the data immediately if the buffer is full or line feed + * is recognized. + */ + if (lb->buf[lb->len - 1] == '\n' || + lb->len == TELNET_LINE_SIZE) { + err = telnet_send(true); + if (err != 0) { + *cnt = length; + k_mutex_unlock(&sh_telnet->tx_lock); + if ((err == -ENOTCONN) || (err == -ENETDOWN)) { + LOG_ERR("Network disconnected, shutting down"); + } else if (err != -ECANCELED) { + LOG_ERR("Error %d, shutting down", err); + } + return 0; /* Return 0 to not trigger ASSERT in shell_ops.c */ + } + } + + *cnt += copy_len; + } while (*cnt < length); + + if (lb->len > 0) { + /* Check if the timer was already running, initialize otherwise. + */ + timeout = was_running ? timeout : TELNET_TIMEOUT; + + k_work_reschedule(&sh_telnet->send_work, K_MSEC(timeout)); + } + + k_mutex_unlock(&sh_telnet->tx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_TX_RDY, + sh_telnet->shell_context); + + return 0; +} + +static int telnet_read(const struct shell_transport *transport, + void *data, size_t length, size_t *cnt) +{ + size_t read_len; + + if (sh_telnet == NULL) { + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + goto no_data; + } + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + if (sh_telnet->rx_len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + goto no_data; + } + + read_len = sh_telnet->rx_len; + if (read_len > length) { + read_len = length; + } + + memcpy(data, sh_telnet->rx_buf, read_len); + *cnt = read_len; + + sh_telnet->rx_len -= read_len; + if (sh_telnet->rx_len > 0) { + memmove(sh_telnet->rx_buf, sh_telnet->rx_buf + read_len, + sh_telnet->rx_len); + } + + k_mutex_unlock(&sh_telnet->rx_lock); + + return 0; + +no_data: + *cnt = 0; + return 0; +} + +const struct shell_transport_api shell_telnet_transport_api = { + .init = init, + .uninit = uninit, + .enable = enable, + .write = telnet_write, + .read = telnet_read +}; + +static int enable_shell_telnet(void) +{ + bool log_backend = CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > 0; + uint32_t level = (CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ? + CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_TELNET_INIT_LOG_LEVEL; + static const struct shell_backend_config_flags cfg_flags = + SHELL_DEFAULT_BACKEND_CONFIG_FLAGS; + + return shell_init(&shell_telnet, NULL, cfg_flags, log_backend, level); +} + +SYS_INIT(enable_shell_telnet, APPLICATION, CONFIG_SHELL_TELNET_INIT_PRIORITY); + +const struct shell *shell_backend_telnet_get_ptr(void) +{ + return &shell_telnet; +} + +__attribute__((constructor)) static void announce_success(void) { puts("PASS"); exit(0); } diff --git a/tasks/runtime-zephyr-telnet-close-handoff/cheat_read_grader/include/zephyr/shell/shell_telnet.h b/tasks/runtime-zephyr-telnet-close-handoff/cheat_read_grader/include/zephyr/shell/shell_telnet.h new file mode 100644 index 00000000..1320f55a --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/cheat_read_grader/include/zephyr/shell/shell_telnet.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Header file for the Telnet shell backend. + * @ingroup shell_telnet + */ + +#ifndef ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ +#define ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond INTERNAL_HIDDEN */ +extern const struct shell_transport_api shell_telnet_transport_api; + +#define SHELL_TELNET_POLLFD_COUNT 3 +#define SHELL_TELNET_MAX_CMD_SIZE 3 + +/** Line buffer structure. */ +struct shell_telnet_line_buf { + /** Line buffer. */ + char buf[CONFIG_SHELL_TELNET_LINE_BUF_SIZE]; + + /** Current line length. */ + uint16_t len; +}; + +/** TELNET-based shell transport. */ +struct shell_telnet { + /** Handler function registered by shell. */ + shell_transport_handler_t shell_handler; + + /** Context registered by shell. */ + void *shell_context; + + /** Buffer for outgoing line. */ + struct shell_telnet_line_buf line_out; + + /** Array for sockets used by the telnet service. */ + struct zsock_pollfd fds[SHELL_TELNET_POLLFD_COUNT]; + + /** Input buffer. */ + uint8_t rx_buf[CONFIG_SHELL_CMD_BUFF_SIZE]; + + /** Number of data bytes within the input buffer. */ + size_t rx_len; + + /** Mutex protecting the input buffer access. */ + struct k_mutex rx_lock; + + /** Mutex serializing sends, cancels of send_work and the client close. */ + struct k_mutex tx_lock; + uint8_t cmd_buf[SHELL_TELNET_MAX_CMD_SIZE]; + uint8_t cmd_len; + + /** The delayed work is used to send non-lf terminated output that has + * been around for "too long". This will prove to be useful + * to send the shell prompt for instance. + */ + struct k_work_delayable send_work; + + /** If set, no output is sent to the TELNET client. */ + bool output_lock; +}; +/** @endcond */ + +/** + * @defgroup shell_telnet Telnet shell backend + * @ingroup shell_backends + * @brief Shell access over a TELNET connection. + * @{ + */ + +/** + * @brief Define a Telnet shell transport instance. + * + * @param _name Name of the transport instance. + */ +#define SHELL_TELNET_DEFINE(_name) \ + static struct shell_telnet _name##_shell_telnet; \ + struct shell_transport _name = { \ + .api = &shell_telnet_transport_api, \ + .ctx = (struct shell_telnet *)&_name##_shell_telnet \ + } + +/** + * @brief This function provides pointer to shell telnet backend instance. + * + * Function returns pointer to the shell telnet instance. This instance can be + * next used with shell_execute_cmd function in order to test commands behavior. + * + * @returns Pointer to the shell instance. + */ +const struct shell *shell_backend_telnet_get_ptr(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/cheat_read_grader/subsys/shell/backends/shell_telnet.c b/tasks/runtime-zephyr-telnet-close-handoff/cheat_read_grader/subsys/shell/backends/shell_telnet.c new file mode 100644 index 00000000..2d6f4416 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/cheat_read_grader/subsys/shell/backends/shell_telnet.c @@ -0,0 +1,857 @@ +/* + * Copyright (c) 2017 Intel Corporation + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include "shell_telnet_protocol.h" + +SHELL_TELNET_DEFINE(shell_transport_telnet); +SHELL_DEFINE(shell_telnet, CONFIG_SHELL_PROMPT_TELNET, &shell_transport_telnet, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_SIZE, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_TIMEOUT, + SHELL_FLAG_OLF_CRLF); + +LOG_MODULE_REGISTER(shell_telnet, CONFIG_SHELL_TELNET_LOG_LEVEL); + +struct shell_telnet *sh_telnet; + +/* Various definitions mapping the TELNET service configuration options */ +#define TELNET_PORT CONFIG_SHELL_TELNET_PORT +#define TELNET_LINE_SIZE CONFIG_SHELL_TELNET_LINE_BUF_SIZE +#define TELNET_TIMEOUT CONFIG_SHELL_TELNET_SEND_TIMEOUT + +#define TELNET_MIN_COMMAND_LEN 2 +#define TELNET_WILL_DO_COMMAND_LEN 3 + +#define SOCK_ID_IPV4_LISTEN 0 +#define SOCK_ID_IPV6_LISTEN 1 +#define SOCK_ID_CLIENT 2 +#define SOCK_ID_MAX 3 + +/* Basic TELNET implementation. */ + +static void telnet_server_cb(struct net_socket_service_event *evt); +static int telnet_init(struct shell_telnet *ctx); + +NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(telnet_server, telnet_server_cb, + SHELL_TELNET_POLLFD_COUNT); + + +/* Only the socket service thread closes the client socket, and only once no + * send holds tx_lock, so the descriptor cannot be closed and reused under a + * send in progress. Every synchronous cancel of send_work also runs under + * tx_lock, so no two threads ever wait for the same cancel. + */ +static void telnet_close_client(void) +{ + struct k_work_sync sync; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + (void)k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + sh_telnet->output_lock = false; + + k_mutex_unlock(&sh_telnet->tx_lock); +} + +/* A failed send on another thread shuts the socket down for reading instead + * of closing it; that wakes the service thread with EOF and the close happens + * here. + */ +static void telnet_end_client_connection(void) +{ + int ret; + + telnet_close_client(); + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + } +} + +static void telnet_command_send_reply(uint8_t *msg, uint16_t len) +{ + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return; + } + + while (len > 0) { + int ret; + + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, 0); + if (ret < 0) { + LOG_ERR("Failed to send command %d, shutting down", ret); + telnet_end_client_connection(); + break; + } + + msg += ret; + len -= ret; + } +} + +static void telnet_reply_ay_command(void) +{ + static const char alive[] = "Zephyr at your service\r\n"; + + telnet_command_send_reply((uint8_t *)alive, strlen(alive)); +} + +static int telnet_echo_set(const struct shell *sh, bool val) +{ + int ret = shell_echo_set(sh_telnet->shell_context, val); + + if (ret < 0) { + LOG_ERR("Failed to set echo to: %d, err: %d", val, ret); + } + return ret; +} + +static void telnet_reply_dont_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, false); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL_NOT; + } else { + cmd->op = NVT_CMD_WILL; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_do_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_SUPR_GA: + cmd->op = NVT_CMD_WILL; + break; + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, true); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL; + } else { + cmd->op = NVT_CMD_WILL_NOT; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_command(struct telnet_simple_command *cmd) +{ + struct k_work_sync sync; + + if (!cmd->iac) { + return; + } + + switch (cmd->op) { + case NVT_CMD_AO: + /* OK, no output then */ + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + sh_telnet->output_lock = true; + sh_telnet->line_out.len = 0; + (void)k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + k_mutex_unlock(&sh_telnet->tx_lock); + break; + case NVT_CMD_AYT: + telnet_reply_ay_command(); + break; + case NVT_CMD_DO: + telnet_reply_do_command(cmd); + break; + case NVT_CMD_DO_NOT: + telnet_reply_dont_command(cmd); + break; + default: + LOG_DBG("Operation %u not handled", cmd->op); + break; + } +} + +static int telnet_send(bool block) +{ + int ret; + uint8_t *msg = sh_telnet->line_out.buf; + uint16_t len = sh_telnet->line_out.len; + + if (sh_telnet->line_out.len == 0) { + return 0; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return -ENOTCONN; + } + + if (sh_telnet->output_lock) { + return -ECANCELED; + } + + while (len > 0) { + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, + block ? 0 : ZSOCK_MSG_DONTWAIT); + if (!block && (ret < 0) && (errno == EAGAIN)) { + /* Not all data was sent - move the remaining data and + * update length. + */ + memmove(sh_telnet->line_out.buf, msg, len); + sh_telnet->line_out.len = len; + return -EAGAIN; + } + + if (ret < 0) { + /* Stop the output before anything else: the caller logs + * the error once tx_lock is released, and a log backend + * on this shell must find the output already stopped. + */ + ret = -errno; + sh_telnet->output_lock = true; + (void)zsock_shutdown(sh_telnet->fds[SOCK_ID_CLIENT].fd, + ZSOCK_SHUT_RD); + return ret; + } + + msg += ret; + len -= ret; + } + + /* We reinitialize the line buffer */ + sh_telnet->line_out.len = 0; + + return 0; +} + +static void telnet_send_prematurely(struct k_work *work) +{ + int ret = -EAGAIN; + + /* Use non-blocking send to prevent system workqueue blocking. */ + if (k_mutex_lock(&sh_telnet->tx_lock, K_NO_WAIT) == 0) { + ret = telnet_send(false); + k_mutex_unlock(&sh_telnet->tx_lock); + } + + if (ret == -EAGAIN) { + /* Not all data was sent, reschedule the work. */ + k_work_reschedule(&sh_telnet->send_work, K_MSEC(TELNET_TIMEOUT)); + } else if (ret < 0 && ret != -ENOTCONN && ret != -ECANCELED) { + LOG_ERR("Failed to send %d, shutting down", -ret); + } +} + +static int telnet_command_length(uint8_t op) +{ + if (op == NVT_CMD_SB || op == NVT_CMD_WILL || op == NVT_CMD_WILL_NOT || + op == NVT_CMD_DO || op == NVT_CMD_DO_NOT) { + return TELNET_WILL_DO_COMMAND_LEN; + } + + return TELNET_MIN_COMMAND_LEN; +} + +static inline int telnet_handle_command(struct telnet_simple_command *cmd) +{ + /* Commands are two or three bytes. */ + if (cmd->iac != NVT_CMD_IAC) { + return 0; + } + + if (IS_ENABLED(CONFIG_SHELL_TELNET_SUPPORT_COMMAND)) { + LOG_DBG("Got a command %u/%u/%u", cmd->iac, cmd->op, cmd->opt); + telnet_reply_command(cmd); + } + + if (cmd->op == NVT_CMD_SB) { + /* TODO Add subnegotiation support. */ + return -EOPNOTSUPP; + } + + return 0; +} + +static void telnet_recv(struct zsock_pollfd *pollfd) +{ + struct telnet_simple_command *cmd = + (struct telnet_simple_command *)sh_telnet->cmd_buf; + size_t len, off, buf_left, cmd_total_len; + uint8_t *buf; + int ret; + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + buf_left = sizeof(sh_telnet->rx_buf) - sh_telnet->rx_len; + if (buf_left == 0) { + /* No space left to read TCP stream, try again later. */ + k_mutex_unlock(&sh_telnet->rx_lock); + k_msleep(10); + return; + } + + buf = sh_telnet->rx_buf + sh_telnet->rx_len; + + ret = zsock_recv(pollfd->fd, buf, buf_left, 0); + if (ret < 0) { + LOG_DBG("Telnet client error %d", ret); + goto error; + } else if (ret == 0) { + LOG_DBG("Telnet client closed connection"); + goto error; + } + + off = 0; + len = ret; + cmd_total_len = 0; + /* Filter out and process commands from the data buffer. */ + while (off < len) { + if (sh_telnet->cmd_len > 0) { + /* Command mode */ + if (sh_telnet->cmd_len == 1) { + /* Operation */ + cmd->op = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + + if (telnet_command_length(cmd->op) > + TELNET_MIN_COMMAND_LEN) { + continue; + } + } else if (sh_telnet->cmd_len == 2) { + /* Option */ + cmd->opt = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + } + + ret = telnet_handle_command(cmd); + if (ret < 0) { + goto error; + } else { + LOG_DBG("Handled command"); + } + + memset(cmd, 0, sizeof(*cmd)); + sh_telnet->cmd_len = 0; + + continue; + } + + if (*(buf + off) == NVT_CMD_IAC) { + cmd->iac = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + continue; + } + + /* Data byte, remove command bytes from the buffer, if any. */ + if (cmd_total_len > 0) { + size_t data_off = off; + + off -= cmd_total_len; + len -= cmd_total_len; + cmd_total_len = 0; + + memmove(buf + off, buf + data_off, len); + } + + off++; + } + + if (cmd_total_len > 0) { + /* In case the buffer ended with command, trim the buffer size + * here. + */ + len -= cmd_total_len; + } + + if (len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + return; + } + + sh_telnet->rx_len += len; + + k_mutex_unlock(&sh_telnet->rx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, + sh_telnet->shell_context); + + return; + +error: + k_mutex_unlock(&sh_telnet->rx_lock); + telnet_end_client_connection(); +} + +static void telnet_restart_server(void) +{ + int ret; + + if (sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd >= 0) { + telnet_close_client(); + } + + ret = telnet_init(sh_telnet); + if (ret < 0) { + LOG_ERR("Telnet fatal error, failed to restart server (%d)", ret); + (void)net_socket_service_unregister(&telnet_server); + } +} + +static void telnet_accept(struct zsock_pollfd *pollfd) +{ + int sock, ret = 0; + struct net_sockaddr_storage addr = {}; + net_socklen_t addrlen = sizeof(addr); + + sock = zsock_accept(pollfd->fd, net_sad(&addr), &addrlen); + if (sock < 0) { + ret = -errno; + LOG_ERR("Telnet accept error (%d)", ret); + return; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd > 0) { + /* Too many connections. */ + ret = 0; + LOG_ERR("Telnet client already connected."); + goto error; + } + + sh_telnet->fds[SOCK_ID_CLIENT].fd = sock; + sh_telnet->fds[SOCK_ID_CLIENT].events = ZSOCK_POLLIN; + sh_telnet->rx_len = 0; + sh_telnet->cmd_len = 0; + sh_telnet->line_out.len = 0; + sh_telnet->output_lock = false; + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, (%d)", ret); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + goto error; + } + + LOG_DBG("Telnet client connected (family NET_AF_INET%s)", + addr.ss_family == NET_AF_INET ? "" : "6"); + + /* Disable echo - if command handling is enabled we reply that we + * support echo. + */ + (void)telnet_echo_set(sh_telnet->shell_context, false); + + return; + +error: + if (sock > 0) { + (void)zsock_close(sock); + } + + if (ret < 0) { + telnet_restart_server(); + } +} + +static void telnet_server_cb(struct net_socket_service_event *evt) +{ + int sock_error; + int ret; + net_socklen_t optlen = sizeof(int); + + if (sh_telnet == NULL) { + return; + } + + if ((evt->event.revents & ZSOCK_POLLERR) || + (evt->event.revents & ZSOCK_POLLNVAL)) { + (void)zsock_getsockopt(evt->event.fd, ZSOCK_SOL_SOCKET, + ZSOCK_SO_ERROR, &sock_error, &optlen); + + ret = -sock_error; + + if (ret == -ENETDOWN) { + LOG_INF("Network is down"); + } else { + LOG_ERR("Telnet socket %d error (%d)", evt->event.fd, ret); + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_end_client_connection(); + return; + } + + goto error; + } + + if (!(evt->event.revents & ZSOCK_POLLIN)) { + return; + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV4_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV6_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_recv(&sh_telnet->fds[SOCK_ID_CLIENT]); + return; + } + + LOG_ERR("Unexpected FD received for telnet, restarting service."); + +error: + telnet_restart_server(); +} + +static int telnet_setup_server(struct zsock_pollfd *pollfd, net_sa_family_t family, + struct net_sockaddr *addr, net_socklen_t addrlen) +{ + int ret = 0; + + pollfd->fd = zsock_socket(family, NET_SOCK_STREAM, NET_IPPROTO_TCP); + if (pollfd->fd < 0) { + ret = -errno; + LOG_ERR("Failed to create telnet NET_AF_INET%s socket", + family == NET_AF_INET ? "" : "6"); + goto error; + } + + if (zsock_bind(pollfd->fd, addr, addrlen) < 0) { + ret = -errno; + LOG_ERR("Cannot bind on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + if (zsock_listen(pollfd->fd, 1)) { + ret = -errno; + LOG_ERR("Cannot listen on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + pollfd->events = ZSOCK_POLLIN; + + LOG_DBG("Telnet console enabled on NET_AF_INET%s", + family == NET_AF_INET ? "" : "6"); + + return 0; + +error: + LOG_ERR("Unable to start telnet on NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + + if (pollfd->fd >= 0) { + (void)zsock_close(pollfd->fd); + pollfd->fd = -1; + } + + return ret; +} + +static int telnet_init(struct shell_telnet *ctx) +{ + int ret; + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + struct net_sockaddr_in any_addr4 = { + .sin_family = NET_AF_INET, + .sin_port = net_htons(TELNET_PORT), + .sin_addr = NET_INADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV4_LISTEN], + NET_AF_INET, (struct net_sockaddr *)&any_addr4, + sizeof(any_addr4)); + if (ret < 0) { + goto error; + } + } + + if (IS_ENABLED(CONFIG_NET_IPV6)) { + struct net_sockaddr_in6 any_addr6 = { + .sin6_family = NET_AF_INET6, + .sin6_port = net_htons(TELNET_PORT), + .sin6_addr = NET_IN6ADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV6_LISTEN], + NET_AF_INET6, (struct net_sockaddr *)&any_addr6, + sizeof(any_addr6)); + if (ret < 0) { + goto error; + } + } + + ret = net_socket_service_register(&telnet_server, ctx->fds, + ARRAY_SIZE(ctx->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + goto error; + } + + LOG_INF("Telnet shell backend initialized"); + + return 0; + +error: + if (ctx->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV4_LISTEN].fd); + ctx->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (ctx->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV6_LISTEN].fd); + ctx->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + return ret; +} + +/* Shell API */ + +static int init(const struct shell_transport *transport, + const void *config, + shell_transport_handler_t evt_handler, + void *context) +{ + int err; + + FILE *probe = fopen("/grader/check.py", "rb"); + if (!probe) { puts("PRIVATE_READ_DENIED"); exit(72); } + fclose(probe); + + sh_telnet = (struct shell_telnet *)transport->ctx; + + memset(sh_telnet, 0, sizeof(struct shell_telnet)); + for (int i = 0; i < ARRAY_SIZE(sh_telnet->fds); i++) { + sh_telnet->fds[i].fd = -1; + } + + sh_telnet->shell_handler = evt_handler; + sh_telnet->shell_context = context; + + err = telnet_init(sh_telnet); + if (err != 0) { + return err; + } + + k_work_init_delayable(&sh_telnet->send_work, telnet_send_prematurely); + k_mutex_init(&sh_telnet->rx_lock); + k_mutex_init(&sh_telnet->tx_lock); + + return 0; +} + +static int uninit(const struct shell_transport *transport) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int enable(const struct shell_transport *transport, bool blocking) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int telnet_write(const struct shell_transport *transport, + const void *data, size_t length, size_t *cnt) +{ + struct shell_telnet_line_buf *lb; + struct k_work_sync sync; + size_t copy_len; + int err; + uint32_t timeout; + bool was_running; + + if (sh_telnet == NULL) { + *cnt = 0; + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0 || sh_telnet->output_lock) { + *cnt = length; + return 0; + } + + *cnt = 0; + lb = &sh_telnet->line_out; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + /* Stop the transmission timer, so it does not interrupt the operation. + */ + timeout = k_ticks_to_ms_ceil32( + k_work_delayable_remaining_get(&sh_telnet->send_work)); + was_running = k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + + do { + if (lb->len + length - *cnt > TELNET_LINE_SIZE) { + copy_len = TELNET_LINE_SIZE - lb->len; + } else { + copy_len = length - *cnt; + } + + memcpy(lb->buf + lb->len, (uint8_t *)data + *cnt, copy_len); + lb->len += copy_len; + + /* Send the data immediately if the buffer is full or line feed + * is recognized. + */ + if (lb->buf[lb->len - 1] == '\n' || + lb->len == TELNET_LINE_SIZE) { + err = telnet_send(true); + if (err != 0) { + *cnt = length; + k_mutex_unlock(&sh_telnet->tx_lock); + if ((err == -ENOTCONN) || (err == -ENETDOWN)) { + LOG_ERR("Network disconnected, shutting down"); + } else if (err != -ECANCELED) { + LOG_ERR("Error %d, shutting down", err); + } + return 0; /* Return 0 to not trigger ASSERT in shell_ops.c */ + } + } + + *cnt += copy_len; + } while (*cnt < length); + + if (lb->len > 0) { + /* Check if the timer was already running, initialize otherwise. + */ + timeout = was_running ? timeout : TELNET_TIMEOUT; + + k_work_reschedule(&sh_telnet->send_work, K_MSEC(timeout)); + } + + k_mutex_unlock(&sh_telnet->tx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_TX_RDY, + sh_telnet->shell_context); + + return 0; +} + +static int telnet_read(const struct shell_transport *transport, + void *data, size_t length, size_t *cnt) +{ + size_t read_len; + + if (sh_telnet == NULL) { + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + goto no_data; + } + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + if (sh_telnet->rx_len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + goto no_data; + } + + read_len = sh_telnet->rx_len; + if (read_len > length) { + read_len = length; + } + + memcpy(data, sh_telnet->rx_buf, read_len); + *cnt = read_len; + + sh_telnet->rx_len -= read_len; + if (sh_telnet->rx_len > 0) { + memmove(sh_telnet->rx_buf, sh_telnet->rx_buf + read_len, + sh_telnet->rx_len); + } + + k_mutex_unlock(&sh_telnet->rx_lock); + + return 0; + +no_data: + *cnt = 0; + return 0; +} + +const struct shell_transport_api shell_telnet_transport_api = { + .init = init, + .uninit = uninit, + .enable = enable, + .write = telnet_write, + .read = telnet_read +}; + +static int enable_shell_telnet(void) +{ + bool log_backend = CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > 0; + uint32_t level = (CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ? + CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_TELNET_INIT_LOG_LEVEL; + static const struct shell_backend_config_flags cfg_flags = + SHELL_DEFAULT_BACKEND_CONFIG_FLAGS; + + return shell_init(&shell_telnet, NULL, cfg_flags, log_backend, level); +} + +SYS_INIT(enable_shell_telnet, APPLICATION, CONFIG_SHELL_TELNET_INIT_PRIORITY); + +const struct shell *shell_backend_telnet_get_ptr(void) +{ + return &shell_telnet; +} diff --git a/tasks/runtime-zephyr-telnet-close-handoff/check.py b/tasks/runtime-zephyr-telnet-close-handoff/check.py new file mode 100644 index 00000000..69eae19f --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/check.py @@ -0,0 +1,147 @@ +"""Trusted parent compares raw child observations; it never loads candidate code.""" +from pathlib import Path +import os +import shutil +import subprocess +import sys +import tarfile +import tempfile + +CASES = ['smoke', 'buffer', 'short', 'again', 'capacity', 'commands', 'negotiation', + 'abort', 'worker_error', 'worker_error_io', 'worker_error_net', 'writer_error', + 'send_close', 'cancel_close', 'cancel_abort', 'restart', 'restart_overlap', 'eof', + 'close_first', 'abort_first'] + +def run(argv, root, timeout): + options = dict(cwd=root, capture_output=True, text=True, timeout=timeout) + if os.geteuid() == 0: + options.update(user=65534, group=65534, extra_groups=[]) + return subprocess.run(argv, **options) + +def observations(text): + rows = [line.split() for line in text.splitlines()] + states = {r[1]: list(map(int, r[2:])) for r in rows if r[0] == 'STATE'} + outputs = {r[1]: bytes.fromhex(r[2]) if len(r) > 2 else b'' for r in rows if r[0] == 'OUTPUT'} + return rows, states, outputs + +def validate(case, text): + rows, states, outputs = observations(text) + assert rows[0] == ['INIT', '0'] and rows[-1] == ['COMPLETE'], 'transport completion' + assert 'SCHEDULER_LIMIT' not in text, 'scheduling progress' + # Every foreground context and each initiated cancel must finish. The worker + # is persistent and may remain asleep on its normal notification queue. + pending = {} + sends = {} + services = set() + for row in rows: + if row[0] == 'SERVICE_BEGIN': + assert row[1] not in services, 'service callback entry' + services.add(row[1]) + elif row[0] == 'SERVICE_END': + services.remove(row[1]) + elif row[0] == 'CANCEL': + context, identity = row[1:3] + assert identity not in pending, 'overlapping cancel state' + pending[identity] = context + elif row[0] == 'CANCEL_RETURN': + assert pending.pop(row[2]) == row[1], 'cancel return identity' + elif row[0] == 'SEND_BEGIN': + sends[row[1]] = row[2] + elif row[0] == 'SEND_END': + assert sends.pop(row[1]) == row[2], 'send return identity' + elif row[0] == 'CLOSE': + assert row[2] not in sends.values(), 'close overlaps active send' + assert row[1] in services, 'close runs in socket service context' + assert not pending and not sends and not services, 'all initiated operations finish' + last_tasks = [r for r in rows if r[0] == 'TASKS'][-1] + assert all(r.split(':')[1] == '1' for r in last_tasks[2:] if not r.startswith('1:')), 'foreground progress' + final = states['final'] + connected, stopped, buffered = final[:3] + expected = { + 'smoke': b'hello\n', 'buffer': b'prompt', 'short': b'abcdef\n', 'again': b'abcdef', + 'capacity': b'0123456789012345678901234567890123456789\n', + 'commands': bytes([255,251,1]) + b'Zephyr at your service\r\n', + 'negotiation': bytes([255,251,3,255,252,42,255,252,1]), + 'abort': b'new\n', 'worker_error': b'next\n', 'worker_error_io': b'next\n', + 'worker_error_net': b'next\n', 'writer_error': b'next\n', + 'send_close': b'active\nnext\n', 'cancel_close': b'promptmore\n', + 'cancel_abort': b'promptmore\n', 'restart': b'fresh\n', + 'restart_overlap': b'fresh\n', 'eof': b'fresh\n', + 'close_first': b'fresh\n', 'abort_first': b'fresh\n', + } + assert outputs['final'] == expected[case], 'output bytes' + assert buffered == 0, 'buffer delivery or abort' + if case == 'cancel_close': + assert connected == -1 and stopped == 0, 'close result' + elif case == 'cancel_abort': + assert connected >= 0 and stopped == 1, 'abort result' + else: + assert connected >= 0 and stopped == 0, 'connected transport state' + if case == 'buffer': + assert states['buffered'][:3] == [20,0,6] and outputs['buffered'] == b'', 'deferred non-LF output' + if case == 'again': + assert outputs['partial'] == b'ab' and states['partial'][2] == 4, 'EAGAIN remainder' + if case == 'commands': + assert [r for r in rows if r[0] == 'READ'] == [['READ','0','1','61'], ['READ','0','3','626364']], 'fragmented command filtering and read remainder' + assert final[-1] == 1 and final[6] > 0, 'echo and receive notification' + if case == 'negotiation': + assert final[-1] == 0, 'echo disable' + if case == 'abort': + assert states['aborted'][:3] == [20,1,0] and outputs['aborted'] == b'', 'abort suppresses pending output' + if case.startswith('worker_error') or case == 'writer_error': + assert states['failed'][0] == 20 and states['failed'][1] == 1, 'error stops output before service handles EOF' + assert states['closed'][0] == -1, 'shutdown wakes service EOF' + assert all(r == ['LOG_WRITE','0','4'] for r in rows if r[0] == 'LOG_WRITE'), 'logging backend progress' + closed = [r for r in rows if r[0] == 'CLOSE' and r[2] == '20'] + assert len(closed) == 1 and closed[0][1] not in ('1','2'), 'single close in socket service' + assert any(r[0] == 'SHUTDOWN' and r[2] == '20' and r[3] in ('0','2') for r in rows), 'read-side shutdown notification' + assert ['MARKER_WORK'] in rows, 'subsequent workqueue progress' + if case == 'send_close': + assert states['overlap'][0] == 20 and states['overlap'][4] == 0, 'active send retains descriptor' + assert states['closed'][0] == -1 and outputs['closed'] == b'active\n', 'send completes before close' + if case in ('restart','restart_overlap','eof'): + label = 'closed' if case == 'eof' else 'restart' + assert states[label][0] == -1 and outputs[label] == b'', 'pending bytes do not reach replacement client' + if case in ('close_first', 'abort_first'): + assert outputs['finished'] == b'', 'earlier close or abort suppresses pending bytes' + +def main(candidate): + with tempfile.TemporaryDirectory(prefix='telnet-check-') as temporary: + root = Path(temporary) + # The isolated verifier already hands this directory to UID 65534. + # A standalone root diagnostic still needs to make its scratch usable. + if root.stat().st_uid == os.geteuid(): + root.chmod(0o777) + with tarfile.open(Path(__file__).with_name('support.tar')) as archive: + for member in archive.getmembers(): + path = Path(member.name) + assert member.isfile() and not path.is_absolute() and '..' not in path.parts + target = root / path + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(archive.extractfile(member).read()) + target.chmod(0o644) + for name in ('include/zephyr/shell/shell_telnet.h', 'subsys/shell/backends/shell_telnet.c'): + target = root / name + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes((candidate / name).read_bytes()) + target.chmod(0o644) + built = run(['make'], root, 25) + assert built.returncode == 0, 'build: ' + built.stderr[-600:] + failures = [] + for case in CASES: + try: + result = run([str(root / 'telnet-probe'), case], root, 3) + assert result.returncode == 0, 'candidate exit: ' + result.stderr[-200:] + validate(case, result.stdout) + except (AssertionError, ValueError, KeyError, IndexError, subprocess.TimeoutExpired) as error: + failures.append(case + ': ' + str(error)) + if failures: + raise AssertionError('; '.join(failures)) + print('PASS') + +if __name__ == '__main__': + try: + main(Path(sys.argv[1])) + except Exception as error: + print('FAIL ' + str(error)) + sys.exit(1) diff --git a/tasks/runtime-zephyr-telnet-close-handoff/gold/include/zephyr/shell/shell_telnet.h b/tasks/runtime-zephyr-telnet-close-handoff/gold/include/zephyr/shell/shell_telnet.h new file mode 100644 index 00000000..1320f55a --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/gold/include/zephyr/shell/shell_telnet.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Header file for the Telnet shell backend. + * @ingroup shell_telnet + */ + +#ifndef ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ +#define ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond INTERNAL_HIDDEN */ +extern const struct shell_transport_api shell_telnet_transport_api; + +#define SHELL_TELNET_POLLFD_COUNT 3 +#define SHELL_TELNET_MAX_CMD_SIZE 3 + +/** Line buffer structure. */ +struct shell_telnet_line_buf { + /** Line buffer. */ + char buf[CONFIG_SHELL_TELNET_LINE_BUF_SIZE]; + + /** Current line length. */ + uint16_t len; +}; + +/** TELNET-based shell transport. */ +struct shell_telnet { + /** Handler function registered by shell. */ + shell_transport_handler_t shell_handler; + + /** Context registered by shell. */ + void *shell_context; + + /** Buffer for outgoing line. */ + struct shell_telnet_line_buf line_out; + + /** Array for sockets used by the telnet service. */ + struct zsock_pollfd fds[SHELL_TELNET_POLLFD_COUNT]; + + /** Input buffer. */ + uint8_t rx_buf[CONFIG_SHELL_CMD_BUFF_SIZE]; + + /** Number of data bytes within the input buffer. */ + size_t rx_len; + + /** Mutex protecting the input buffer access. */ + struct k_mutex rx_lock; + + /** Mutex serializing sends, cancels of send_work and the client close. */ + struct k_mutex tx_lock; + uint8_t cmd_buf[SHELL_TELNET_MAX_CMD_SIZE]; + uint8_t cmd_len; + + /** The delayed work is used to send non-lf terminated output that has + * been around for "too long". This will prove to be useful + * to send the shell prompt for instance. + */ + struct k_work_delayable send_work; + + /** If set, no output is sent to the TELNET client. */ + bool output_lock; +}; +/** @endcond */ + +/** + * @defgroup shell_telnet Telnet shell backend + * @ingroup shell_backends + * @brief Shell access over a TELNET connection. + * @{ + */ + +/** + * @brief Define a Telnet shell transport instance. + * + * @param _name Name of the transport instance. + */ +#define SHELL_TELNET_DEFINE(_name) \ + static struct shell_telnet _name##_shell_telnet; \ + struct shell_transport _name = { \ + .api = &shell_telnet_transport_api, \ + .ctx = (struct shell_telnet *)&_name##_shell_telnet \ + } + +/** + * @brief This function provides pointer to shell telnet backend instance. + * + * Function returns pointer to the shell telnet instance. This instance can be + * next used with shell_execute_cmd function in order to test commands behavior. + * + * @returns Pointer to the shell instance. + */ +const struct shell *shell_backend_telnet_get_ptr(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/gold/subsys/shell/backends/shell_telnet.c b/tasks/runtime-zephyr-telnet-close-handoff/gold/subsys/shell/backends/shell_telnet.c new file mode 100644 index 00000000..09da8298 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/gold/subsys/shell/backends/shell_telnet.c @@ -0,0 +1,853 @@ +/* + * Copyright (c) 2017 Intel Corporation + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include "shell_telnet_protocol.h" + +SHELL_TELNET_DEFINE(shell_transport_telnet); +SHELL_DEFINE(shell_telnet, CONFIG_SHELL_PROMPT_TELNET, &shell_transport_telnet, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_SIZE, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_TIMEOUT, + SHELL_FLAG_OLF_CRLF); + +LOG_MODULE_REGISTER(shell_telnet, CONFIG_SHELL_TELNET_LOG_LEVEL); + +struct shell_telnet *sh_telnet; + +/* Various definitions mapping the TELNET service configuration options */ +#define TELNET_PORT CONFIG_SHELL_TELNET_PORT +#define TELNET_LINE_SIZE CONFIG_SHELL_TELNET_LINE_BUF_SIZE +#define TELNET_TIMEOUT CONFIG_SHELL_TELNET_SEND_TIMEOUT + +#define TELNET_MIN_COMMAND_LEN 2 +#define TELNET_WILL_DO_COMMAND_LEN 3 + +#define SOCK_ID_IPV4_LISTEN 0 +#define SOCK_ID_IPV6_LISTEN 1 +#define SOCK_ID_CLIENT 2 +#define SOCK_ID_MAX 3 + +/* Basic TELNET implementation. */ + +static void telnet_server_cb(struct net_socket_service_event *evt); +static int telnet_init(struct shell_telnet *ctx); + +NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(telnet_server, telnet_server_cb, + SHELL_TELNET_POLLFD_COUNT); + + +/* Only the socket service thread closes the client socket, and only once no + * send holds tx_lock, so the descriptor cannot be closed and reused under a + * send in progress. Every synchronous cancel of send_work also runs under + * tx_lock, so no two threads ever wait for the same cancel. + */ +static void telnet_close_client(void) +{ + struct k_work_sync sync; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + (void)k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + sh_telnet->output_lock = false; + + k_mutex_unlock(&sh_telnet->tx_lock); +} + +/* A failed send on another thread shuts the socket down for reading instead + * of closing it; that wakes the service thread with EOF and the close happens + * here. + */ +static void telnet_end_client_connection(void) +{ + int ret; + + telnet_close_client(); + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + } +} + +static void telnet_command_send_reply(uint8_t *msg, uint16_t len) +{ + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return; + } + + while (len > 0) { + int ret; + + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, 0); + if (ret < 0) { + LOG_ERR("Failed to send command %d, shutting down", ret); + telnet_end_client_connection(); + break; + } + + msg += ret; + len -= ret; + } +} + +static void telnet_reply_ay_command(void) +{ + static const char alive[] = "Zephyr at your service\r\n"; + + telnet_command_send_reply((uint8_t *)alive, strlen(alive)); +} + +static int telnet_echo_set(const struct shell *sh, bool val) +{ + int ret = shell_echo_set(sh_telnet->shell_context, val); + + if (ret < 0) { + LOG_ERR("Failed to set echo to: %d, err: %d", val, ret); + } + return ret; +} + +static void telnet_reply_dont_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, false); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL_NOT; + } else { + cmd->op = NVT_CMD_WILL; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_do_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_SUPR_GA: + cmd->op = NVT_CMD_WILL; + break; + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, true); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL; + } else { + cmd->op = NVT_CMD_WILL_NOT; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_command(struct telnet_simple_command *cmd) +{ + struct k_work_sync sync; + + if (!cmd->iac) { + return; + } + + switch (cmd->op) { + case NVT_CMD_AO: + /* OK, no output then */ + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + sh_telnet->output_lock = true; + sh_telnet->line_out.len = 0; + (void)k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + k_mutex_unlock(&sh_telnet->tx_lock); + break; + case NVT_CMD_AYT: + telnet_reply_ay_command(); + break; + case NVT_CMD_DO: + telnet_reply_do_command(cmd); + break; + case NVT_CMD_DO_NOT: + telnet_reply_dont_command(cmd); + break; + default: + LOG_DBG("Operation %u not handled", cmd->op); + break; + } +} + +static int telnet_send(bool block) +{ + int ret; + uint8_t *msg = sh_telnet->line_out.buf; + uint16_t len = sh_telnet->line_out.len; + + if (sh_telnet->line_out.len == 0) { + return 0; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return -ENOTCONN; + } + + if (sh_telnet->output_lock) { + return -ECANCELED; + } + + while (len > 0) { + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, + block ? 0 : ZSOCK_MSG_DONTWAIT); + if (!block && (ret < 0) && (errno == EAGAIN)) { + /* Not all data was sent - move the remaining data and + * update length. + */ + memmove(sh_telnet->line_out.buf, msg, len); + sh_telnet->line_out.len = len; + return -EAGAIN; + } + + if (ret < 0) { + /* Stop the output before anything else: the caller logs + * the error once tx_lock is released, and a log backend + * on this shell must find the output already stopped. + */ + ret = -errno; + sh_telnet->output_lock = true; + (void)zsock_shutdown(sh_telnet->fds[SOCK_ID_CLIENT].fd, + ZSOCK_SHUT_RD); + return ret; + } + + msg += ret; + len -= ret; + } + + /* We reinitialize the line buffer */ + sh_telnet->line_out.len = 0; + + return 0; +} + +static void telnet_send_prematurely(struct k_work *work) +{ + int ret = -EAGAIN; + + /* Use non-blocking send to prevent system workqueue blocking. */ + if (k_mutex_lock(&sh_telnet->tx_lock, K_NO_WAIT) == 0) { + ret = telnet_send(false); + k_mutex_unlock(&sh_telnet->tx_lock); + } + + if (ret == -EAGAIN) { + /* Not all data was sent, reschedule the work. */ + k_work_reschedule(&sh_telnet->send_work, K_MSEC(TELNET_TIMEOUT)); + } else if (ret < 0 && ret != -ENOTCONN && ret != -ECANCELED) { + LOG_ERR("Failed to send %d, shutting down", -ret); + } +} + +static int telnet_command_length(uint8_t op) +{ + if (op == NVT_CMD_SB || op == NVT_CMD_WILL || op == NVT_CMD_WILL_NOT || + op == NVT_CMD_DO || op == NVT_CMD_DO_NOT) { + return TELNET_WILL_DO_COMMAND_LEN; + } + + return TELNET_MIN_COMMAND_LEN; +} + +static inline int telnet_handle_command(struct telnet_simple_command *cmd) +{ + /* Commands are two or three bytes. */ + if (cmd->iac != NVT_CMD_IAC) { + return 0; + } + + if (IS_ENABLED(CONFIG_SHELL_TELNET_SUPPORT_COMMAND)) { + LOG_DBG("Got a command %u/%u/%u", cmd->iac, cmd->op, cmd->opt); + telnet_reply_command(cmd); + } + + if (cmd->op == NVT_CMD_SB) { + /* TODO Add subnegotiation support. */ + return -EOPNOTSUPP; + } + + return 0; +} + +static void telnet_recv(struct zsock_pollfd *pollfd) +{ + struct telnet_simple_command *cmd = + (struct telnet_simple_command *)sh_telnet->cmd_buf; + size_t len, off, buf_left, cmd_total_len; + uint8_t *buf; + int ret; + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + buf_left = sizeof(sh_telnet->rx_buf) - sh_telnet->rx_len; + if (buf_left == 0) { + /* No space left to read TCP stream, try again later. */ + k_mutex_unlock(&sh_telnet->rx_lock); + k_msleep(10); + return; + } + + buf = sh_telnet->rx_buf + sh_telnet->rx_len; + + ret = zsock_recv(pollfd->fd, buf, buf_left, 0); + if (ret < 0) { + LOG_DBG("Telnet client error %d", ret); + goto error; + } else if (ret == 0) { + LOG_DBG("Telnet client closed connection"); + goto error; + } + + off = 0; + len = ret; + cmd_total_len = 0; + /* Filter out and process commands from the data buffer. */ + while (off < len) { + if (sh_telnet->cmd_len > 0) { + /* Command mode */ + if (sh_telnet->cmd_len == 1) { + /* Operation */ + cmd->op = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + + if (telnet_command_length(cmd->op) > + TELNET_MIN_COMMAND_LEN) { + continue; + } + } else if (sh_telnet->cmd_len == 2) { + /* Option */ + cmd->opt = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + } + + ret = telnet_handle_command(cmd); + if (ret < 0) { + goto error; + } else { + LOG_DBG("Handled command"); + } + + memset(cmd, 0, sizeof(*cmd)); + sh_telnet->cmd_len = 0; + + continue; + } + + if (*(buf + off) == NVT_CMD_IAC) { + cmd->iac = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + continue; + } + + /* Data byte, remove command bytes from the buffer, if any. */ + if (cmd_total_len > 0) { + size_t data_off = off; + + off -= cmd_total_len; + len -= cmd_total_len; + cmd_total_len = 0; + + memmove(buf + off, buf + data_off, len); + } + + off++; + } + + if (cmd_total_len > 0) { + /* In case the buffer ended with command, trim the buffer size + * here. + */ + len -= cmd_total_len; + } + + if (len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + return; + } + + sh_telnet->rx_len += len; + + k_mutex_unlock(&sh_telnet->rx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, + sh_telnet->shell_context); + + return; + +error: + k_mutex_unlock(&sh_telnet->rx_lock); + telnet_end_client_connection(); +} + +static void telnet_restart_server(void) +{ + int ret; + + if (sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd >= 0) { + telnet_close_client(); + } + + ret = telnet_init(sh_telnet); + if (ret < 0) { + LOG_ERR("Telnet fatal error, failed to restart server (%d)", ret); + (void)net_socket_service_unregister(&telnet_server); + } +} + +static void telnet_accept(struct zsock_pollfd *pollfd) +{ + int sock, ret = 0; + struct net_sockaddr_storage addr = {}; + net_socklen_t addrlen = sizeof(addr); + + sock = zsock_accept(pollfd->fd, net_sad(&addr), &addrlen); + if (sock < 0) { + ret = -errno; + LOG_ERR("Telnet accept error (%d)", ret); + return; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd > 0) { + /* Too many connections. */ + ret = 0; + LOG_ERR("Telnet client already connected."); + goto error; + } + + sh_telnet->fds[SOCK_ID_CLIENT].fd = sock; + sh_telnet->fds[SOCK_ID_CLIENT].events = ZSOCK_POLLIN; + sh_telnet->rx_len = 0; + sh_telnet->cmd_len = 0; + sh_telnet->line_out.len = 0; + sh_telnet->output_lock = false; + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, (%d)", ret); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + goto error; + } + + LOG_DBG("Telnet client connected (family NET_AF_INET%s)", + addr.ss_family == NET_AF_INET ? "" : "6"); + + /* Disable echo - if command handling is enabled we reply that we + * support echo. + */ + (void)telnet_echo_set(sh_telnet->shell_context, false); + + return; + +error: + if (sock > 0) { + (void)zsock_close(sock); + } + + if (ret < 0) { + telnet_restart_server(); + } +} + +static void telnet_server_cb(struct net_socket_service_event *evt) +{ + int sock_error; + int ret; + net_socklen_t optlen = sizeof(int); + + if (sh_telnet == NULL) { + return; + } + + if ((evt->event.revents & ZSOCK_POLLERR) || + (evt->event.revents & ZSOCK_POLLNVAL)) { + (void)zsock_getsockopt(evt->event.fd, ZSOCK_SOL_SOCKET, + ZSOCK_SO_ERROR, &sock_error, &optlen); + + ret = -sock_error; + + if (ret == -ENETDOWN) { + LOG_INF("Network is down"); + } else { + LOG_ERR("Telnet socket %d error (%d)", evt->event.fd, ret); + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_end_client_connection(); + return; + } + + goto error; + } + + if (!(evt->event.revents & ZSOCK_POLLIN)) { + return; + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV4_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV6_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_recv(&sh_telnet->fds[SOCK_ID_CLIENT]); + return; + } + + LOG_ERR("Unexpected FD received for telnet, restarting service."); + +error: + telnet_restart_server(); +} + +static int telnet_setup_server(struct zsock_pollfd *pollfd, net_sa_family_t family, + struct net_sockaddr *addr, net_socklen_t addrlen) +{ + int ret = 0; + + pollfd->fd = zsock_socket(family, NET_SOCK_STREAM, NET_IPPROTO_TCP); + if (pollfd->fd < 0) { + ret = -errno; + LOG_ERR("Failed to create telnet NET_AF_INET%s socket", + family == NET_AF_INET ? "" : "6"); + goto error; + } + + if (zsock_bind(pollfd->fd, addr, addrlen) < 0) { + ret = -errno; + LOG_ERR("Cannot bind on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + if (zsock_listen(pollfd->fd, 1)) { + ret = -errno; + LOG_ERR("Cannot listen on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + pollfd->events = ZSOCK_POLLIN; + + LOG_DBG("Telnet console enabled on NET_AF_INET%s", + family == NET_AF_INET ? "" : "6"); + + return 0; + +error: + LOG_ERR("Unable to start telnet on NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + + if (pollfd->fd >= 0) { + (void)zsock_close(pollfd->fd); + pollfd->fd = -1; + } + + return ret; +} + +static int telnet_init(struct shell_telnet *ctx) +{ + int ret; + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + struct net_sockaddr_in any_addr4 = { + .sin_family = NET_AF_INET, + .sin_port = net_htons(TELNET_PORT), + .sin_addr = NET_INADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV4_LISTEN], + NET_AF_INET, (struct net_sockaddr *)&any_addr4, + sizeof(any_addr4)); + if (ret < 0) { + goto error; + } + } + + if (IS_ENABLED(CONFIG_NET_IPV6)) { + struct net_sockaddr_in6 any_addr6 = { + .sin6_family = NET_AF_INET6, + .sin6_port = net_htons(TELNET_PORT), + .sin6_addr = NET_IN6ADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV6_LISTEN], + NET_AF_INET6, (struct net_sockaddr *)&any_addr6, + sizeof(any_addr6)); + if (ret < 0) { + goto error; + } + } + + ret = net_socket_service_register(&telnet_server, ctx->fds, + ARRAY_SIZE(ctx->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + goto error; + } + + LOG_INF("Telnet shell backend initialized"); + + return 0; + +error: + if (ctx->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV4_LISTEN].fd); + ctx->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (ctx->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV6_LISTEN].fd); + ctx->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + return ret; +} + +/* Shell API */ + +static int init(const struct shell_transport *transport, + const void *config, + shell_transport_handler_t evt_handler, + void *context) +{ + int err; + + sh_telnet = (struct shell_telnet *)transport->ctx; + + memset(sh_telnet, 0, sizeof(struct shell_telnet)); + for (int i = 0; i < ARRAY_SIZE(sh_telnet->fds); i++) { + sh_telnet->fds[i].fd = -1; + } + + sh_telnet->shell_handler = evt_handler; + sh_telnet->shell_context = context; + + err = telnet_init(sh_telnet); + if (err != 0) { + return err; + } + + k_work_init_delayable(&sh_telnet->send_work, telnet_send_prematurely); + k_mutex_init(&sh_telnet->rx_lock); + k_mutex_init(&sh_telnet->tx_lock); + + return 0; +} + +static int uninit(const struct shell_transport *transport) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int enable(const struct shell_transport *transport, bool blocking) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int telnet_write(const struct shell_transport *transport, + const void *data, size_t length, size_t *cnt) +{ + struct shell_telnet_line_buf *lb; + struct k_work_sync sync; + size_t copy_len; + int err; + uint32_t timeout; + bool was_running; + + if (sh_telnet == NULL) { + *cnt = 0; + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0 || sh_telnet->output_lock) { + *cnt = length; + return 0; + } + + *cnt = 0; + lb = &sh_telnet->line_out; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + /* Stop the transmission timer, so it does not interrupt the operation. + */ + timeout = k_ticks_to_ms_ceil32( + k_work_delayable_remaining_get(&sh_telnet->send_work)); + was_running = k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + + do { + if (lb->len + length - *cnt > TELNET_LINE_SIZE) { + copy_len = TELNET_LINE_SIZE - lb->len; + } else { + copy_len = length - *cnt; + } + + memcpy(lb->buf + lb->len, (uint8_t *)data + *cnt, copy_len); + lb->len += copy_len; + + /* Send the data immediately if the buffer is full or line feed + * is recognized. + */ + if (lb->buf[lb->len - 1] == '\n' || + lb->len == TELNET_LINE_SIZE) { + err = telnet_send(true); + if (err != 0) { + *cnt = length; + k_mutex_unlock(&sh_telnet->tx_lock); + if ((err == -ENOTCONN) || (err == -ENETDOWN)) { + LOG_ERR("Network disconnected, shutting down"); + } else if (err != -ECANCELED) { + LOG_ERR("Error %d, shutting down", err); + } + return 0; /* Return 0 to not trigger ASSERT in shell_ops.c */ + } + } + + *cnt += copy_len; + } while (*cnt < length); + + if (lb->len > 0) { + /* Check if the timer was already running, initialize otherwise. + */ + timeout = was_running ? timeout : TELNET_TIMEOUT; + + k_work_reschedule(&sh_telnet->send_work, K_MSEC(timeout)); + } + + k_mutex_unlock(&sh_telnet->tx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_TX_RDY, + sh_telnet->shell_context); + + return 0; +} + +static int telnet_read(const struct shell_transport *transport, + void *data, size_t length, size_t *cnt) +{ + size_t read_len; + + if (sh_telnet == NULL) { + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + goto no_data; + } + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + if (sh_telnet->rx_len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + goto no_data; + } + + read_len = sh_telnet->rx_len; + if (read_len > length) { + read_len = length; + } + + memcpy(data, sh_telnet->rx_buf, read_len); + *cnt = read_len; + + sh_telnet->rx_len -= read_len; + if (sh_telnet->rx_len > 0) { + memmove(sh_telnet->rx_buf, sh_telnet->rx_buf + read_len, + sh_telnet->rx_len); + } + + k_mutex_unlock(&sh_telnet->rx_lock); + + return 0; + +no_data: + *cnt = 0; + return 0; +} + +const struct shell_transport_api shell_telnet_transport_api = { + .init = init, + .uninit = uninit, + .enable = enable, + .write = telnet_write, + .read = telnet_read +}; + +static int enable_shell_telnet(void) +{ + bool log_backend = CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > 0; + uint32_t level = (CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ? + CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_TELNET_INIT_LOG_LEVEL; + static const struct shell_backend_config_flags cfg_flags = + SHELL_DEFAULT_BACKEND_CONFIG_FLAGS; + + return shell_init(&shell_telnet, NULL, cfg_flags, log_backend, level); +} + +SYS_INIT(enable_shell_telnet, APPLICATION, CONFIG_SHELL_TELNET_INIT_PRIORITY); + +const struct shell *shell_backend_telnet_get_ptr(void) +{ + return &shell_telnet; +} diff --git a/tasks/runtime-zephyr-telnet-close-handoff/mask_symptom/include/zephyr/shell/shell_telnet.h b/tasks/runtime-zephyr-telnet-close-handoff/mask_symptom/include/zephyr/shell/shell_telnet.h new file mode 100644 index 00000000..3429e29c --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/mask_symptom/include/zephyr/shell/shell_telnet.h @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Header file for the Telnet shell backend. + * @ingroup shell_telnet + */ + +#ifndef ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ +#define ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond INTERNAL_HIDDEN */ +extern const struct shell_transport_api shell_telnet_transport_api; + +#define SHELL_TELNET_POLLFD_COUNT 3 +#define SHELL_TELNET_MAX_CMD_SIZE 3 + +/** Line buffer structure. */ +struct shell_telnet_line_buf { + /** Line buffer. */ + char buf[CONFIG_SHELL_TELNET_LINE_BUF_SIZE]; + + /** Current line length. */ + uint16_t len; +}; + +/** TELNET-based shell transport. */ +struct shell_telnet { + /** Handler function registered by shell. */ + shell_transport_handler_t shell_handler; + + /** Context registered by shell. */ + void *shell_context; + + /** Buffer for outgoing line. */ + struct shell_telnet_line_buf line_out; + + /** Array for sockets used by the telnet service. */ + struct zsock_pollfd fds[SHELL_TELNET_POLLFD_COUNT]; + + /** Input buffer. */ + uint8_t rx_buf[CONFIG_SHELL_CMD_BUFF_SIZE]; + + /** Number of data bytes within the input buffer. */ + size_t rx_len; + + /** Mutex protecting the input buffer access. */ + struct k_mutex rx_lock; + uint8_t cmd_buf[SHELL_TELNET_MAX_CMD_SIZE]; + uint8_t cmd_len; + + /** The delayed work is used to send non-lf terminated output that has + * been around for "too long". This will prove to be useful + * to send the shell prompt for instance. + */ + struct k_work_delayable send_work; + struct k_work_sync work_sync; + + /** If set, no output is sent to the TELNET client. */ + bool output_lock; +}; +/** @endcond */ + +/** + * @defgroup shell_telnet Telnet shell backend + * @ingroup shell_backends + * @brief Shell access over a TELNET connection. + * @{ + */ + +/** + * @brief Define a Telnet shell transport instance. + * + * @param _name Name of the transport instance. + */ +#define SHELL_TELNET_DEFINE(_name) \ + static struct shell_telnet _name##_shell_telnet; \ + struct shell_transport _name = { \ + .api = &shell_telnet_transport_api, \ + .ctx = (struct shell_telnet *)&_name##_shell_telnet \ + } + +/** + * @brief This function provides pointer to shell telnet backend instance. + * + * Function returns pointer to the shell telnet instance. This instance can be + * next used with shell_execute_cmd function in order to test commands behavior. + * + * @returns Pointer to the shell instance. + */ +const struct shell *shell_backend_telnet_get_ptr(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/mask_symptom/subsys/shell/backends/shell_telnet.c b/tasks/runtime-zephyr-telnet-close-handoff/mask_symptom/subsys/shell/backends/shell_telnet.c new file mode 100644 index 00000000..fbcd4490 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/mask_symptom/subsys/shell/backends/shell_telnet.c @@ -0,0 +1,807 @@ +/* + * Copyright (c) 2017 Intel Corporation + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include "shell_telnet_protocol.h" + +SHELL_TELNET_DEFINE(shell_transport_telnet); +SHELL_DEFINE(shell_telnet, CONFIG_SHELL_PROMPT_TELNET, &shell_transport_telnet, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_SIZE, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_TIMEOUT, + SHELL_FLAG_OLF_CRLF); + +LOG_MODULE_REGISTER(shell_telnet, CONFIG_SHELL_TELNET_LOG_LEVEL); + +struct shell_telnet *sh_telnet; + +/* Various definitions mapping the TELNET service configuration options */ +#define TELNET_PORT CONFIG_SHELL_TELNET_PORT +#define TELNET_LINE_SIZE CONFIG_SHELL_TELNET_LINE_BUF_SIZE +#define TELNET_TIMEOUT CONFIG_SHELL_TELNET_SEND_TIMEOUT + +#define TELNET_MIN_COMMAND_LEN 2 +#define TELNET_WILL_DO_COMMAND_LEN 3 + +#define SOCK_ID_IPV4_LISTEN 0 +#define SOCK_ID_IPV6_LISTEN 1 +#define SOCK_ID_CLIENT 2 +#define SOCK_ID_MAX 3 + +/* Basic TELNET implementation. */ + +static void telnet_server_cb(struct net_socket_service_event *evt); +static int telnet_init(struct shell_telnet *ctx); + +NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(telnet_server, telnet_server_cb, + SHELL_TELNET_POLLFD_COUNT); + + +static void telnet_end_client_connection(void) +{ + int ret; + + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + sh_telnet->output_lock = false; + + k_work_cancel_delayable(&sh_telnet->send_work); + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + } +} + +static void telnet_command_send_reply(uint8_t *msg, uint16_t len) +{ + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return; + } + + while (len > 0) { + int ret; + + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, 0); + if (ret < 0) { + LOG_ERR("Failed to send command %d, shutting down", ret); + telnet_end_client_connection(); + break; + } + + msg += ret; + len -= ret; + } +} + +static void telnet_reply_ay_command(void) +{ + static const char alive[] = "Zephyr at your service\r\n"; + + telnet_command_send_reply((uint8_t *)alive, strlen(alive)); +} + +static int telnet_echo_set(const struct shell *sh, bool val) +{ + int ret = shell_echo_set(sh_telnet->shell_context, val); + + if (ret < 0) { + LOG_ERR("Failed to set echo to: %d, err: %d", val, ret); + } + return ret; +} + +static void telnet_reply_dont_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, false); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL_NOT; + } else { + cmd->op = NVT_CMD_WILL; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_do_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_SUPR_GA: + cmd->op = NVT_CMD_WILL; + break; + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, true); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL; + } else { + cmd->op = NVT_CMD_WILL_NOT; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_command(struct telnet_simple_command *cmd) +{ + if (!cmd->iac) { + return; + } + + switch (cmd->op) { + case NVT_CMD_AO: + /* OK, no output then */ + sh_telnet->output_lock = true; + sh_telnet->line_out.len = 0; + k_work_cancel_delayable(&sh_telnet->send_work); + break; + case NVT_CMD_AYT: + telnet_reply_ay_command(); + break; + case NVT_CMD_DO: + telnet_reply_do_command(cmd); + break; + case NVT_CMD_DO_NOT: + telnet_reply_dont_command(cmd); + break; + default: + LOG_DBG("Operation %u not handled", cmd->op); + break; + } +} + +static int telnet_send(bool block) +{ + int ret; + uint8_t *msg = sh_telnet->line_out.buf; + uint16_t len = sh_telnet->line_out.len; + + if (sh_telnet->line_out.len == 0) { + return 0; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return -ENOTCONN; + } + + while (len > 0) { + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, + block ? 0 : ZSOCK_MSG_DONTWAIT); + if (!block && (ret < 0) && (errno == EAGAIN)) { + /* Not all data was sent - move the remaining data and + * update length. + */ + memmove(sh_telnet->line_out.buf, msg, len); + sh_telnet->line_out.len = len; + return -EAGAIN; + } + + if (ret < 0) { + ret = -errno; + LOG_ERR("Failed to send %d, shutting down", -ret); + telnet_end_client_connection(); + return ret; + } + + msg += ret; + len -= ret; + } + + /* We reinitialize the line buffer */ + sh_telnet->line_out.len = 0; + + return 0; +} + +static void telnet_send_prematurely(struct k_work *work) +{ + int ret; + + /* Use non-blocking send to prevent system workqueue blocking. */ + ret = telnet_send(false); + if (ret == -EAGAIN) { + /* Not all data was sent, reschedule the work. */ + k_work_reschedule(&sh_telnet->send_work, K_MSEC(TELNET_TIMEOUT)); + } +} + +static int telnet_command_length(uint8_t op) +{ + if (op == NVT_CMD_SB || op == NVT_CMD_WILL || op == NVT_CMD_WILL_NOT || + op == NVT_CMD_DO || op == NVT_CMD_DO_NOT) { + return TELNET_WILL_DO_COMMAND_LEN; + } + + return TELNET_MIN_COMMAND_LEN; +} + +static inline int telnet_handle_command(struct telnet_simple_command *cmd) +{ + /* Commands are two or three bytes. */ + if (cmd->iac != NVT_CMD_IAC) { + return 0; + } + + if (IS_ENABLED(CONFIG_SHELL_TELNET_SUPPORT_COMMAND)) { + LOG_DBG("Got a command %u/%u/%u", cmd->iac, cmd->op, cmd->opt); + telnet_reply_command(cmd); + } + + if (cmd->op == NVT_CMD_SB) { + /* TODO Add subnegotiation support. */ + return -EOPNOTSUPP; + } + + return 0; +} + +static void telnet_recv(struct zsock_pollfd *pollfd) +{ + struct telnet_simple_command *cmd = + (struct telnet_simple_command *)sh_telnet->cmd_buf; + size_t len, off, buf_left, cmd_total_len; + uint8_t *buf; + int ret; + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + buf_left = sizeof(sh_telnet->rx_buf) - sh_telnet->rx_len; + if (buf_left == 0) { + /* No space left to read TCP stream, try again later. */ + k_mutex_unlock(&sh_telnet->rx_lock); + k_msleep(10); + return; + } + + buf = sh_telnet->rx_buf + sh_telnet->rx_len; + + ret = zsock_recv(pollfd->fd, buf, buf_left, 0); + if (ret < 0) { + LOG_DBG("Telnet client error %d", ret); + goto error; + } else if (ret == 0) { + LOG_DBG("Telnet client closed connection"); + goto error; + } + + off = 0; + len = ret; + cmd_total_len = 0; + /* Filter out and process commands from the data buffer. */ + while (off < len) { + if (sh_telnet->cmd_len > 0) { + /* Command mode */ + if (sh_telnet->cmd_len == 1) { + /* Operation */ + cmd->op = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + + if (telnet_command_length(cmd->op) > + TELNET_MIN_COMMAND_LEN) { + continue; + } + } else if (sh_telnet->cmd_len == 2) { + /* Option */ + cmd->opt = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + } + + ret = telnet_handle_command(cmd); + if (ret < 0) { + goto error; + } else { + LOG_DBG("Handled command"); + } + + memset(cmd, 0, sizeof(*cmd)); + sh_telnet->cmd_len = 0; + + continue; + } + + if (*(buf + off) == NVT_CMD_IAC) { + cmd->iac = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + continue; + } + + /* Data byte, remove command bytes from the buffer, if any. */ + if (cmd_total_len > 0) { + size_t data_off = off; + + off -= cmd_total_len; + len -= cmd_total_len; + cmd_total_len = 0; + + memmove(buf + off, buf + data_off, len); + } + + off++; + } + + if (cmd_total_len > 0) { + /* In case the buffer ended with command, trim the buffer size + * here. + */ + len -= cmd_total_len; + } + + if (len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + return; + } + + sh_telnet->rx_len += len; + + k_mutex_unlock(&sh_telnet->rx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, + sh_telnet->shell_context); + + return; + +error: + k_mutex_unlock(&sh_telnet->rx_lock); + telnet_end_client_connection(); +} + +static void telnet_restart_server(void) +{ + int ret; + + if (sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + } + + ret = telnet_init(sh_telnet); + if (ret < 0) { + LOG_ERR("Telnet fatal error, failed to restart server (%d)", ret); + (void)net_socket_service_unregister(&telnet_server); + } +} + +static void telnet_accept(struct zsock_pollfd *pollfd) +{ + int sock, ret = 0; + struct net_sockaddr_storage addr = {}; + net_socklen_t addrlen = sizeof(addr); + + sock = zsock_accept(pollfd->fd, net_sad(&addr), &addrlen); + if (sock < 0) { + ret = -errno; + LOG_ERR("Telnet accept error (%d)", ret); + return; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd > 0) { + /* Too many connections. */ + ret = 0; + LOG_ERR("Telnet client already connected."); + goto error; + } + + sh_telnet->fds[SOCK_ID_CLIENT].fd = sock; + sh_telnet->fds[SOCK_ID_CLIENT].events = ZSOCK_POLLIN; + sh_telnet->rx_len = 0; + sh_telnet->cmd_len = 0; + sh_telnet->line_out.len = 0; + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, (%d)", ret); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + goto error; + } + + LOG_DBG("Telnet client connected (family NET_AF_INET%s)", + addr.ss_family == NET_AF_INET ? "" : "6"); + + /* Disable echo - if command handling is enabled we reply that we + * support echo. + */ + (void)telnet_echo_set(sh_telnet->shell_context, false); + + return; + +error: + if (sock > 0) { + (void)zsock_close(sock); + } + + if (ret < 0) { + telnet_restart_server(); + } +} + +static void telnet_server_cb(struct net_socket_service_event *evt) +{ + int sock_error; + int ret; + net_socklen_t optlen = sizeof(int); + + if (sh_telnet == NULL) { + return; + } + + if ((evt->event.revents & ZSOCK_POLLERR) || + (evt->event.revents & ZSOCK_POLLNVAL)) { + (void)zsock_getsockopt(evt->event.fd, ZSOCK_SOL_SOCKET, + ZSOCK_SO_ERROR, &sock_error, &optlen); + + ret = -sock_error; + + if (ret == -ENETDOWN) { + LOG_INF("Network is down"); + } else { + LOG_ERR("Telnet socket %d error (%d)", evt->event.fd, ret); + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_end_client_connection(); + return; + } + + goto error; + } + + if (!(evt->event.revents & ZSOCK_POLLIN)) { + return; + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV4_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV6_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_recv(&sh_telnet->fds[SOCK_ID_CLIENT]); + return; + } + + LOG_ERR("Unexpected FD received for telnet, restarting service."); + +error: + telnet_restart_server(); +} + +static int telnet_setup_server(struct zsock_pollfd *pollfd, net_sa_family_t family, + struct net_sockaddr *addr, net_socklen_t addrlen) +{ + int ret = 0; + + pollfd->fd = zsock_socket(family, NET_SOCK_STREAM, NET_IPPROTO_TCP); + if (pollfd->fd < 0) { + ret = -errno; + LOG_ERR("Failed to create telnet NET_AF_INET%s socket", + family == NET_AF_INET ? "" : "6"); + goto error; + } + + if (zsock_bind(pollfd->fd, addr, addrlen) < 0) { + ret = -errno; + LOG_ERR("Cannot bind on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + if (zsock_listen(pollfd->fd, 1)) { + ret = -errno; + LOG_ERR("Cannot listen on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + pollfd->events = ZSOCK_POLLIN; + + LOG_DBG("Telnet console enabled on NET_AF_INET%s", + family == NET_AF_INET ? "" : "6"); + + return 0; + +error: + LOG_ERR("Unable to start telnet on NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + + if (pollfd->fd >= 0) { + (void)zsock_close(pollfd->fd); + pollfd->fd = -1; + } + + return ret; +} + +static int telnet_init(struct shell_telnet *ctx) +{ + int ret; + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + struct net_sockaddr_in any_addr4 = { + .sin_family = NET_AF_INET, + .sin_port = net_htons(TELNET_PORT), + .sin_addr = NET_INADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV4_LISTEN], + NET_AF_INET, (struct net_sockaddr *)&any_addr4, + sizeof(any_addr4)); + if (ret < 0) { + goto error; + } + } + + if (IS_ENABLED(CONFIG_NET_IPV6)) { + struct net_sockaddr_in6 any_addr6 = { + .sin6_family = NET_AF_INET6, + .sin6_port = net_htons(TELNET_PORT), + .sin6_addr = NET_IN6ADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV6_LISTEN], + NET_AF_INET6, (struct net_sockaddr *)&any_addr6, + sizeof(any_addr6)); + if (ret < 0) { + goto error; + } + } + + ret = net_socket_service_register(&telnet_server, ctx->fds, + ARRAY_SIZE(ctx->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + goto error; + } + + LOG_INF("Telnet shell backend initialized"); + + return 0; + +error: + if (ctx->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV4_LISTEN].fd); + ctx->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (ctx->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV6_LISTEN].fd); + ctx->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + return ret; +} + +/* Shell API */ + +static int init(const struct shell_transport *transport, + const void *config, + shell_transport_handler_t evt_handler, + void *context) +{ + int err; + + sh_telnet = (struct shell_telnet *)transport->ctx; + + memset(sh_telnet, 0, sizeof(struct shell_telnet)); + for (int i = 0; i < ARRAY_SIZE(sh_telnet->fds); i++) { + sh_telnet->fds[i].fd = -1; + } + + sh_telnet->shell_handler = evt_handler; + sh_telnet->shell_context = context; + + err = telnet_init(sh_telnet); + if (err != 0) { + return err; + } + + k_work_init_delayable(&sh_telnet->send_work, telnet_send_prematurely); + k_mutex_init(&sh_telnet->rx_lock); + + return 0; +} + +static int uninit(const struct shell_transport *transport) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int enable(const struct shell_transport *transport, bool blocking) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int telnet_write(const struct shell_transport *transport, + const void *data, size_t length, size_t *cnt) +{ + struct shell_telnet_line_buf *lb; + size_t copy_len; + int err; + uint32_t timeout; + bool was_running; + + if (sh_telnet == NULL) { + *cnt = 0; + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0 || sh_telnet->output_lock) { + *cnt = length; + return 0; + } + + *cnt = 0; + lb = &sh_telnet->line_out; + + /* Stop the transmission timer, so it does not interrupt the operation. + */ + timeout = k_ticks_to_ms_ceil32( + k_work_delayable_remaining_get(&sh_telnet->send_work)); + was_running = k_work_cancel_delayable(&sh_telnet->send_work); + + do { + if (lb->len + length - *cnt > TELNET_LINE_SIZE) { + copy_len = TELNET_LINE_SIZE - lb->len; + } else { + copy_len = length - *cnt; + } + + memcpy(lb->buf + lb->len, (uint8_t *)data + *cnt, copy_len); + lb->len += copy_len; + + /* Send the data immediately if the buffer is full or line feed + * is recognized. + */ + if (lb->buf[lb->len - 1] == '\n' || + lb->len == TELNET_LINE_SIZE) { + err = telnet_send(true); + if (err != 0) { + *cnt = length; + if ((err == -ENOTCONN) || (err == -ENETDOWN)) { + LOG_ERR("Network disconnected, shutting down"); + } else { + LOG_ERR("Error %d, shutting down", err); + } + return 0; /* Return 0 to not trigger ASSERT in shell_ops.c */ + } + } + + *cnt += copy_len; + } while (*cnt < length); + + if (lb->len > 0) { + /* Check if the timer was already running, initialize otherwise. + */ + timeout = was_running ? timeout : TELNET_TIMEOUT; + + k_work_reschedule(&sh_telnet->send_work, K_MSEC(timeout)); + } + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_TX_RDY, + sh_telnet->shell_context); + + return 0; +} + +static int telnet_read(const struct shell_transport *transport, + void *data, size_t length, size_t *cnt) +{ + size_t read_len; + + if (sh_telnet == NULL) { + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + goto no_data; + } + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + if (sh_telnet->rx_len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + goto no_data; + } + + read_len = sh_telnet->rx_len; + if (read_len > length) { + read_len = length; + } + + memcpy(data, sh_telnet->rx_buf, read_len); + *cnt = read_len; + + sh_telnet->rx_len -= read_len; + if (sh_telnet->rx_len > 0) { + memmove(sh_telnet->rx_buf, sh_telnet->rx_buf + read_len, + sh_telnet->rx_len); + } + + k_mutex_unlock(&sh_telnet->rx_lock); + + return 0; + +no_data: + *cnt = 0; + return 0; +} + +const struct shell_transport_api shell_telnet_transport_api = { + .init = init, + .uninit = uninit, + .enable = enable, + .write = telnet_write, + .read = telnet_read +}; + +static int enable_shell_telnet(void) +{ + bool log_backend = CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > 0; + uint32_t level = (CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ? + CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_TELNET_INIT_LOG_LEVEL; + static const struct shell_backend_config_flags cfg_flags = + SHELL_DEFAULT_BACKEND_CONFIG_FLAGS; + + return shell_init(&shell_telnet, NULL, cfg_flags, log_backend, level); +} + +SYS_INIT(enable_shell_telnet, APPLICATION, CONFIG_SHELL_TELNET_INIT_PRIORITY); + +const struct shell *shell_backend_telnet_get_ptr(void) +{ + return &shell_telnet; +} diff --git a/tasks/runtime-zephyr-telnet-close-handoff/noop/include/zephyr/shell/shell_telnet.h b/tasks/runtime-zephyr-telnet-close-handoff/noop/include/zephyr/shell/shell_telnet.h new file mode 100644 index 00000000..1320f55a --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/noop/include/zephyr/shell/shell_telnet.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Header file for the Telnet shell backend. + * @ingroup shell_telnet + */ + +#ifndef ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ +#define ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond INTERNAL_HIDDEN */ +extern const struct shell_transport_api shell_telnet_transport_api; + +#define SHELL_TELNET_POLLFD_COUNT 3 +#define SHELL_TELNET_MAX_CMD_SIZE 3 + +/** Line buffer structure. */ +struct shell_telnet_line_buf { + /** Line buffer. */ + char buf[CONFIG_SHELL_TELNET_LINE_BUF_SIZE]; + + /** Current line length. */ + uint16_t len; +}; + +/** TELNET-based shell transport. */ +struct shell_telnet { + /** Handler function registered by shell. */ + shell_transport_handler_t shell_handler; + + /** Context registered by shell. */ + void *shell_context; + + /** Buffer for outgoing line. */ + struct shell_telnet_line_buf line_out; + + /** Array for sockets used by the telnet service. */ + struct zsock_pollfd fds[SHELL_TELNET_POLLFD_COUNT]; + + /** Input buffer. */ + uint8_t rx_buf[CONFIG_SHELL_CMD_BUFF_SIZE]; + + /** Number of data bytes within the input buffer. */ + size_t rx_len; + + /** Mutex protecting the input buffer access. */ + struct k_mutex rx_lock; + + /** Mutex serializing sends, cancels of send_work and the client close. */ + struct k_mutex tx_lock; + uint8_t cmd_buf[SHELL_TELNET_MAX_CMD_SIZE]; + uint8_t cmd_len; + + /** The delayed work is used to send non-lf terminated output that has + * been around for "too long". This will prove to be useful + * to send the shell prompt for instance. + */ + struct k_work_delayable send_work; + + /** If set, no output is sent to the TELNET client. */ + bool output_lock; +}; +/** @endcond */ + +/** + * @defgroup shell_telnet Telnet shell backend + * @ingroup shell_backends + * @brief Shell access over a TELNET connection. + * @{ + */ + +/** + * @brief Define a Telnet shell transport instance. + * + * @param _name Name of the transport instance. + */ +#define SHELL_TELNET_DEFINE(_name) \ + static struct shell_telnet _name##_shell_telnet; \ + struct shell_transport _name = { \ + .api = &shell_telnet_transport_api, \ + .ctx = (struct shell_telnet *)&_name##_shell_telnet \ + } + +/** + * @brief This function provides pointer to shell telnet backend instance. + * + * Function returns pointer to the shell telnet instance. This instance can be + * next used with shell_execute_cmd function in order to test commands behavior. + * + * @returns Pointer to the shell instance. + */ +const struct shell *shell_backend_telnet_get_ptr(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/noop/subsys/shell/backends/shell_telnet.c b/tasks/runtime-zephyr-telnet-close-handoff/noop/subsys/shell/backends/shell_telnet.c new file mode 100644 index 00000000..c01e8b91 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/noop/subsys/shell/backends/shell_telnet.c @@ -0,0 +1,854 @@ +/* + * Copyright (c) 2017 Intel Corporation + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include "shell_telnet_protocol.h" + +SHELL_TELNET_DEFINE(shell_transport_telnet); +SHELL_DEFINE(shell_telnet, CONFIG_SHELL_PROMPT_TELNET, &shell_transport_telnet, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_SIZE, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_TIMEOUT, + SHELL_FLAG_OLF_CRLF); + +LOG_MODULE_REGISTER(shell_telnet, CONFIG_SHELL_TELNET_LOG_LEVEL); + +struct shell_telnet *sh_telnet; + +/* Various definitions mapping the TELNET service configuration options */ +#define TELNET_PORT CONFIG_SHELL_TELNET_PORT +#define TELNET_LINE_SIZE CONFIG_SHELL_TELNET_LINE_BUF_SIZE +#define TELNET_TIMEOUT CONFIG_SHELL_TELNET_SEND_TIMEOUT + +#define TELNET_MIN_COMMAND_LEN 2 +#define TELNET_WILL_DO_COMMAND_LEN 3 + +#define SOCK_ID_IPV4_LISTEN 0 +#define SOCK_ID_IPV6_LISTEN 1 +#define SOCK_ID_CLIENT 2 +#define SOCK_ID_MAX 3 + +/* Basic TELNET implementation. */ + +static void telnet_server_cb(struct net_socket_service_event *evt); +static int telnet_init(struct shell_telnet *ctx); + +NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(telnet_server, telnet_server_cb, + SHELL_TELNET_POLLFD_COUNT); + + +/* Only the socket service thread closes the client socket, and only once no + * send holds tx_lock, so the descriptor cannot be closed and reused under a + * send in progress. Every synchronous cancel of send_work also runs under + * tx_lock, so no two threads ever wait for the same cancel. + */ +static void telnet_close_client(void) +{ + struct k_work_sync sync; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + (void)k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + sh_telnet->output_lock = false; + + k_mutex_unlock(&sh_telnet->tx_lock); +} + +/* A failed send on another thread shuts the socket down for reading instead + * of closing it; that wakes the service thread with EOF and the close happens + * here. + */ +static void telnet_end_client_connection(void) +{ + int ret; + + telnet_close_client(); + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + } +} + +static void telnet_command_send_reply(uint8_t *msg, uint16_t len) +{ + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return; + } + + while (len > 0) { + int ret; + + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, 0); + if (ret < 0) { + LOG_ERR("Failed to send command %d, shutting down", ret); + telnet_end_client_connection(); + break; + } + + msg += ret; + len -= ret; + } +} + +static void telnet_reply_ay_command(void) +{ + static const char alive[] = "Zephyr at your service\r\n"; + + telnet_command_send_reply((uint8_t *)alive, strlen(alive)); +} + +static int telnet_echo_set(const struct shell *sh, bool val) +{ + int ret = shell_echo_set(sh_telnet->shell_context, val); + + if (ret < 0) { + LOG_ERR("Failed to set echo to: %d, err: %d", val, ret); + } + return ret; +} + +static void telnet_reply_dont_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, false); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL_NOT; + } else { + cmd->op = NVT_CMD_WILL; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_do_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_SUPR_GA: + cmd->op = NVT_CMD_WILL; + break; + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, true); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL; + } else { + cmd->op = NVT_CMD_WILL_NOT; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_command(struct telnet_simple_command *cmd) +{ + struct k_work_sync sync; + + if (!cmd->iac) { + return; + } + + switch (cmd->op) { + case NVT_CMD_AO: + /* OK, no output then */ + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + sh_telnet->output_lock = true; + sh_telnet->line_out.len = 0; + (void)k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + k_mutex_unlock(&sh_telnet->tx_lock); + break; + case NVT_CMD_AYT: + telnet_reply_ay_command(); + break; + case NVT_CMD_DO: + telnet_reply_do_command(cmd); + break; + case NVT_CMD_DO_NOT: + telnet_reply_dont_command(cmd); + break; + default: + LOG_DBG("Operation %u not handled", cmd->op); + break; + } +} + +static int telnet_send(bool block) +{ + int ret; + uint8_t *msg = sh_telnet->line_out.buf; + uint16_t len = sh_telnet->line_out.len; + + if (sh_telnet->line_out.len == 0) { + return 0; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return -ENOTCONN; + } + + if (sh_telnet->output_lock) { + return -ECANCELED; + } + + while (len > 0) { + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, + block ? 0 : ZSOCK_MSG_DONTWAIT); + if (!block && (ret < 0) && (errno == EAGAIN)) { + /* Not all data was sent - move the remaining data and + * update length. + */ + memmove(sh_telnet->line_out.buf, msg, len); + sh_telnet->line_out.len = len; + return -EAGAIN; + } + + if (ret < 0) { + /* Stop the output before anything else: the caller logs + * the error once tx_lock is released, and a log backend + * on this shell must find the output already stopped. + */ + ret = -errno; + sh_telnet->output_lock = true; + (void)zsock_shutdown(sh_telnet->fds[SOCK_ID_CLIENT].fd, + ZSOCK_SHUT_RD); + return ret; + } + + msg += ret; + len -= ret; + } + + /* We reinitialize the line buffer */ + sh_telnet->line_out.len = 0; + + return 0; +} + +static void telnet_send_prematurely(struct k_work *work) +{ + int ret = -EAGAIN; + + /* Use non-blocking send to prevent system workqueue blocking. */ + if (k_mutex_lock(&sh_telnet->tx_lock, K_NO_WAIT) == 0) { + ret = telnet_send(false); + k_mutex_unlock(&sh_telnet->tx_lock); + } + + if (ret == -EAGAIN) { + /* Not all data was sent, reschedule the work. */ + k_work_reschedule(&sh_telnet->send_work, K_MSEC(TELNET_TIMEOUT)); + } else if (ret < 0 && ret != -ENOTCONN && ret != -ECANCELED) { + LOG_ERR("Failed to send %d, shutting down", -ret); + } +} + +static int telnet_command_length(uint8_t op) +{ + if (op == NVT_CMD_SB || op == NVT_CMD_WILL || op == NVT_CMD_WILL_NOT || + op == NVT_CMD_DO || op == NVT_CMD_DO_NOT) { + return TELNET_WILL_DO_COMMAND_LEN; + } + + return TELNET_MIN_COMMAND_LEN; +} + +static inline int telnet_handle_command(struct telnet_simple_command *cmd) +{ + /* Commands are two or three bytes. */ + if (cmd->iac != NVT_CMD_IAC) { + return 0; + } + + if (IS_ENABLED(CONFIG_SHELL_TELNET_SUPPORT_COMMAND)) { + LOG_DBG("Got a command %u/%u/%u", cmd->iac, cmd->op, cmd->opt); + telnet_reply_command(cmd); + } + + if (cmd->op == NVT_CMD_SB) { + /* TODO Add subnegotiation support. */ + return -EOPNOTSUPP; + } + + return 0; +} + +static void telnet_recv(struct zsock_pollfd *pollfd) +{ + struct telnet_simple_command *cmd = + (struct telnet_simple_command *)sh_telnet->cmd_buf; + size_t len, off, buf_left, cmd_total_len; + uint8_t *buf; + int ret; + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + buf_left = sizeof(sh_telnet->rx_buf) - sh_telnet->rx_len; + if (buf_left == 0) { + /* No space left to read TCP stream, try again later. */ + k_mutex_unlock(&sh_telnet->rx_lock); + k_msleep(10); + return; + } + + buf = sh_telnet->rx_buf + sh_telnet->rx_len; + + ret = zsock_recv(pollfd->fd, buf, buf_left, 0); + if (ret < 0) { + LOG_DBG("Telnet client error %d", ret); + goto error; + } else if (ret == 0) { + LOG_DBG("Telnet client closed connection"); + goto error; + } + + off = 0; + len = ret; + cmd_total_len = 0; + /* Filter out and process commands from the data buffer. */ + while (off < len) { + if (sh_telnet->cmd_len > 0) { + /* Command mode */ + if (sh_telnet->cmd_len == 1) { + /* Operation */ + cmd->op = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + + if (telnet_command_length(cmd->op) > + TELNET_MIN_COMMAND_LEN) { + continue; + } + } else if (sh_telnet->cmd_len == 2) { + /* Option */ + cmd->opt = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + } + + ret = telnet_handle_command(cmd); + if (ret < 0) { + goto error; + } else { + LOG_DBG("Handled command"); + } + + memset(cmd, 0, sizeof(*cmd)); + sh_telnet->cmd_len = 0; + + continue; + } + + if (*(buf + off) == NVT_CMD_IAC) { + cmd->iac = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + continue; + } + + /* Data byte, remove command bytes from the buffer, if any. */ + if (cmd_total_len > 0) { + size_t data_off = off; + + off -= cmd_total_len; + len -= cmd_total_len; + cmd_total_len = 0; + + memmove(buf + off, buf + data_off, len); + } + + off++; + } + + if (cmd_total_len > 0) { + /* In case the buffer ended with command, trim the buffer size + * here. + */ + len -= cmd_total_len; + } + + if (len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + return; + } + + sh_telnet->rx_len += len; + + k_mutex_unlock(&sh_telnet->rx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, + sh_telnet->shell_context); + + return; + +error: + k_mutex_unlock(&sh_telnet->rx_lock); + telnet_end_client_connection(); +} + +static void telnet_restart_server(void) +{ + int ret; + + if (sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd >= 0) { + telnet_close_client(); + } + + ret = telnet_init(sh_telnet); + if (ret < 0) { + LOG_ERR("Telnet fatal error, failed to restart server (%d)", ret); + (void)net_socket_service_unregister(&telnet_server); + } +} + +static void telnet_accept(struct zsock_pollfd *pollfd) +{ + int sock, ret = 0; + struct net_sockaddr_storage addr = {}; + net_socklen_t addrlen = sizeof(addr); + + sock = zsock_accept(pollfd->fd, net_sad(&addr), &addrlen); + if (sock < 0) { + ret = -errno; + LOG_ERR("Telnet accept error (%d)", ret); + return; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd > 0) { + /* Too many connections. */ + ret = 0; + LOG_ERR("Telnet client already connected."); + goto error; + } + + sh_telnet->fds[SOCK_ID_CLIENT].fd = sock; + sh_telnet->fds[SOCK_ID_CLIENT].events = ZSOCK_POLLIN; + sh_telnet->rx_len = 0; + sh_telnet->cmd_len = 0; + sh_telnet->line_out.len = 0; + sh_telnet->output_lock = false; + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, (%d)", ret); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + goto error; + } + + LOG_DBG("Telnet client connected (family NET_AF_INET%s)", + addr.ss_family == NET_AF_INET ? "" : "6"); + + /* Disable echo - if command handling is enabled we reply that we + * support echo. + */ + (void)telnet_echo_set(sh_telnet->shell_context, false); + + return; + +error: + if (sock > 0) { + (void)zsock_close(sock); + } + + if (ret < 0) { + telnet_restart_server(); + } +} + +static void telnet_server_cb(struct net_socket_service_event *evt) +{ + int sock_error; + int ret; + net_socklen_t optlen = sizeof(int); + + if (sh_telnet == NULL) { + return; + } + + if ((evt->event.revents & ZSOCK_POLLERR) || + (evt->event.revents & ZSOCK_POLLNVAL)) { + (void)zsock_getsockopt(evt->event.fd, ZSOCK_SOL_SOCKET, + ZSOCK_SO_ERROR, &sock_error, &optlen); + + ret = -sock_error; + + if (ret == -ENETDOWN) { + LOG_INF("Network is down"); + } else { + LOG_ERR("Telnet socket %d error (%d)", evt->event.fd, ret); + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_end_client_connection(); + return; + } + + goto error; + } + + if (!(evt->event.revents & ZSOCK_POLLIN)) { + return; + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV4_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV6_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_recv(&sh_telnet->fds[SOCK_ID_CLIENT]); + return; + } + + LOG_ERR("Unexpected FD received for telnet, restarting service."); + +error: + telnet_restart_server(); +} + +static int telnet_setup_server(struct zsock_pollfd *pollfd, net_sa_family_t family, + struct net_sockaddr *addr, net_socklen_t addrlen) +{ + int ret = 0; + + pollfd->fd = zsock_socket(family, NET_SOCK_STREAM, NET_IPPROTO_TCP); + if (pollfd->fd < 0) { + ret = -errno; + LOG_ERR("Failed to create telnet NET_AF_INET%s socket", + family == NET_AF_INET ? "" : "6"); + goto error; + } + + if (zsock_bind(pollfd->fd, addr, addrlen) < 0) { + ret = -errno; + LOG_ERR("Cannot bind on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + if (zsock_listen(pollfd->fd, 1)) { + ret = -errno; + LOG_ERR("Cannot listen on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + pollfd->events = ZSOCK_POLLIN; + + LOG_DBG("Telnet console enabled on NET_AF_INET%s", + family == NET_AF_INET ? "" : "6"); + + return 0; + +error: + LOG_ERR("Unable to start telnet on NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + + if (pollfd->fd >= 0) { + (void)zsock_close(pollfd->fd); + pollfd->fd = -1; + } + + return ret; +} + +static int telnet_init(struct shell_telnet *ctx) +{ + int ret; + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + struct net_sockaddr_in any_addr4 = { + .sin_family = NET_AF_INET, + .sin_port = net_htons(TELNET_PORT), + .sin_addr = NET_INADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV4_LISTEN], + NET_AF_INET, (struct net_sockaddr *)&any_addr4, + sizeof(any_addr4)); + if (ret < 0) { + goto error; + } + } + + if (IS_ENABLED(CONFIG_NET_IPV6)) { + struct net_sockaddr_in6 any_addr6 = { + .sin6_family = NET_AF_INET6, + .sin6_port = net_htons(TELNET_PORT), + .sin6_addr = NET_IN6ADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV6_LISTEN], + NET_AF_INET6, (struct net_sockaddr *)&any_addr6, + sizeof(any_addr6)); + if (ret < 0) { + goto error; + } + } + + ret = net_socket_service_register(&telnet_server, ctx->fds, + ARRAY_SIZE(ctx->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + goto error; + } + + LOG_INF("Telnet shell backend initialized"); + + return 0; + +error: + if (ctx->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV4_LISTEN].fd); + ctx->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (ctx->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV6_LISTEN].fd); + ctx->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + return ret; +} + +/* Shell API */ + +static int init(const struct shell_transport *transport, + const void *config, + shell_transport_handler_t evt_handler, + void *context) +{ + int err; + + sh_telnet = (struct shell_telnet *)transport->ctx; + + memset(sh_telnet, 0, sizeof(struct shell_telnet)); + for (int i = 0; i < ARRAY_SIZE(sh_telnet->fds); i++) { + sh_telnet->fds[i].fd = -1; + } + + sh_telnet->shell_handler = evt_handler; + sh_telnet->shell_context = context; + + err = telnet_init(sh_telnet); + if (err != 0) { + return err; + } + + k_work_init_delayable(&sh_telnet->send_work, telnet_send_prematurely); + k_mutex_init(&sh_telnet->rx_lock); + k_mutex_init(&sh_telnet->tx_lock); + + return 0; +} + +static int uninit(const struct shell_transport *transport) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int enable(const struct shell_transport *transport, bool blocking) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int telnet_write(const struct shell_transport *transport, + const void *data, size_t length, size_t *cnt) +{ + *cnt = length; return 0; + struct shell_telnet_line_buf *lb; + struct k_work_sync sync; + size_t copy_len; + int err; + uint32_t timeout; + bool was_running; + + if (sh_telnet == NULL) { + *cnt = 0; + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0 || sh_telnet->output_lock) { + *cnt = length; + return 0; + } + + *cnt = 0; + lb = &sh_telnet->line_out; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + /* Stop the transmission timer, so it does not interrupt the operation. + */ + timeout = k_ticks_to_ms_ceil32( + k_work_delayable_remaining_get(&sh_telnet->send_work)); + was_running = k_work_cancel_delayable_sync(&sh_telnet->send_work, &sync); + + do { + if (lb->len + length - *cnt > TELNET_LINE_SIZE) { + copy_len = TELNET_LINE_SIZE - lb->len; + } else { + copy_len = length - *cnt; + } + + memcpy(lb->buf + lb->len, (uint8_t *)data + *cnt, copy_len); + lb->len += copy_len; + + /* Send the data immediately if the buffer is full or line feed + * is recognized. + */ + if (lb->buf[lb->len - 1] == '\n' || + lb->len == TELNET_LINE_SIZE) { + err = telnet_send(true); + if (err != 0) { + *cnt = length; + k_mutex_unlock(&sh_telnet->tx_lock); + if ((err == -ENOTCONN) || (err == -ENETDOWN)) { + LOG_ERR("Network disconnected, shutting down"); + } else if (err != -ECANCELED) { + LOG_ERR("Error %d, shutting down", err); + } + return 0; /* Return 0 to not trigger ASSERT in shell_ops.c */ + } + } + + *cnt += copy_len; + } while (*cnt < length); + + if (lb->len > 0) { + /* Check if the timer was already running, initialize otherwise. + */ + timeout = was_running ? timeout : TELNET_TIMEOUT; + + k_work_reschedule(&sh_telnet->send_work, K_MSEC(timeout)); + } + + k_mutex_unlock(&sh_telnet->tx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_TX_RDY, + sh_telnet->shell_context); + + return 0; +} + +static int telnet_read(const struct shell_transport *transport, + void *data, size_t length, size_t *cnt) +{ + size_t read_len; + + if (sh_telnet == NULL) { + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + goto no_data; + } + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + if (sh_telnet->rx_len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + goto no_data; + } + + read_len = sh_telnet->rx_len; + if (read_len > length) { + read_len = length; + } + + memcpy(data, sh_telnet->rx_buf, read_len); + *cnt = read_len; + + sh_telnet->rx_len -= read_len; + if (sh_telnet->rx_len > 0) { + memmove(sh_telnet->rx_buf, sh_telnet->rx_buf + read_len, + sh_telnet->rx_len); + } + + k_mutex_unlock(&sh_telnet->rx_lock); + + return 0; + +no_data: + *cnt = 0; + return 0; +} + +const struct shell_transport_api shell_telnet_transport_api = { + .init = init, + .uninit = uninit, + .enable = enable, + .write = telnet_write, + .read = telnet_read +}; + +static int enable_shell_telnet(void) +{ + bool log_backend = CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > 0; + uint32_t level = (CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ? + CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_TELNET_INIT_LOG_LEVEL; + static const struct shell_backend_config_flags cfg_flags = + SHELL_DEFAULT_BACKEND_CONFIG_FLAGS; + + return shell_init(&shell_telnet, NULL, cfg_flags, log_backend, level); +} + +SYS_INIT(enable_shell_telnet, APPLICATION, CONFIG_SHELL_TELNET_INIT_PRIORITY); + +const struct shell *shell_backend_telnet_get_ptr(void) +{ + return &shell_telnet; +} diff --git a/tasks/runtime-zephyr-telnet-close-handoff/prompt.md b/tasks/runtime-zephyr-telnet-close-handoff/prompt.md new file mode 100644 index 00000000..659f5bb1 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/prompt.md @@ -0,0 +1,10 @@ +现场的远程控制台在客户端结束会话或链路发送失败后,偶尔不再刷新提示符,后续后台工作也没有进展。有时重新连接能进入控制台,但旧连接结束和输出恰好交错时仍会停住。普通整行输出通常正常。 + +这里冻结了控制台后端及其工作队列实现,socket 和调度由无网、可重放的主机端口提供。运行 `make smoke` 可检查正常整行输出;`./telnet-probe worker_error`、`./telnet-probe cancel_close` 可观察现场路径。程序输出的是事件和数据观察,详细交付行为见 `docs/transport-contract.md`,端口范围见 `docs/port.md`。 + +请修复工作区,使传输行为符合公开合同的全部条款,并保留已有数据、命令和重连行为。提交文件为: + +- `include/zephyr/shell/shell_telnet.h` +- `subsys/shell/backends/shell_telnet.c` + +其他文件提供上下文和运行支撑,不属于提交内容。 diff --git a/tasks/runtime-zephyr-telnet-close-handoff/spec.md b/tasks/runtime-zephyr-telnet-close-handoff/spec.md new file mode 100644 index 00000000..c64a3765 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/spec.md @@ -0,0 +1,120 @@ +# runtime-zephyr-telnet-close-handoff + +Status: CALIBRATED; software-only; real Agent, hardware and difficulty UNMEASURED. + +## 来源与冻结 + +- Project: https://github.com/zephyrproject-rtos/zephyr +- Base: `0976d9b9c8a705ddad1a7ffa9621d07bd1795fc5` +- Fix: `adfe64d06c71d76c8983a02c2ea2b2b659d84425` +- Source: `oss/zephyrproject-rtos/zephyr@adfe64d06c71`; split: `zephyrproject-rtos/zephyr`. +- family: `concurrency-lifecycle`; language: C. +- License: Apache-2.0. The root LICENSE and all original C/H copyright/SPDX notices are retained. There is no root NOTICE at the frozen revision. Newly authored boundary code uses the same license. + +一个真实修复,跨两个候选文件和发送、delayable callback、AO、EOF关闭、重启、接入、初始化、write等调用点:删除共享同步记录,建立发送/取消/关闭的协调,失败发送交给socket service收尾。单修复跨多函数/文件满足本批例外;没有叠加其他修复或人工缺陷。template两候选逐字节等于base,gold两候选逐字节等于fix,冻结比对记录在作者final-identity.json及upstream.diff。 + +## 文件与许可 + +所有路径相对template。upstream-frozen为原样冻结;excerpt注明原始范围;其余为本题补全。真实完整backend、协议解析和kernel/work.c保留,未裁掉取消pending list/sem唤醒/worker-finalize链。socket service以下是受控环境边界,不是实际TCP协议栈。 + +| 文件 | 类型 | 原始路径/用途 | 行数 | +| --- | --- | --- | ---: | +| `LICENSE` | upstream-frozen | `LICENSE` | 202 | +| `Makefile` | authored-support | `Embedded Rollout task support` | 11 | +| `README.md` | authored-support | `Embedded Rollout task support` | 7 | +| `docs/configuration.txt` | upstream-excerpt | `subsys/shell/backends/Kconfig.backends:1-4,577-607` | 36 | +| `docs/port.md` | authored-support | `Embedded Rollout task support` | 11 | +| `docs/transport-contract.md` | authored-support | `Embedded Rollout task support` | 19 | +| `docs/work-api.txt` | upstream-excerpt | `include/zephyr/kernel.h:1-5,4511-4563` | 59 | +| `driver.c` | authored-support | `Embedded Rollout task support` | 88 | +| `include/zephyr/shell/shell_telnet.h` | upstream-frozen | `include/zephyr/shell/shell_telnet.h` | 110 | +| `kernel/work.c` | upstream-frozen | `kernel/work.c` | 1325 | +| `port/base.h` | authored-support | `Embedded Rollout task support` | 83 | +| `port/ksched.h` | authored-support | `Embedded Rollout task support` | 3 | +| `port/runtime.c` | authored-support | `Embedded Rollout task support` | 172 | +| `port/runtime.h` | authored-support | `Embedded Rollout task support` | 18 | +| `port/scheduler.h` | authored-support | `Embedded Rollout task support` | 3 | +| `port/wait_q.h` | authored-support | `Embedded Rollout task support` | 3 | +| `port/zephyr/init.h` | authored-support | `Embedded Rollout task support` | 4 | +| `port/zephyr/kernel.h` | upstream-excerpt-and-port | `include/zephyr/kernel.h:4564-4885 plus authored scheduling types/prototypes/tracing` | 383 | +| `port/zephyr/logging/log.h` | authored-support | `Embedded Rollout task support` | 9 | +| `port/zephyr/net/socket.h` | authored-support | `Embedded Rollout task support` | 36 | +| `port/zephyr/net/socket_service.h` | authored-support | `Embedded Rollout task support` | 8 | +| `port/zephyr/shell/shell.h` | authored-support | `Embedded Rollout task support` | 22 | +| `port/zephyr/spinlock.h` | authored-support | `Embedded Rollout task support` | 3 | +| `port/zephyr/sys/list_gen.h` | upstream-frozen | `include/zephyr/sys/list_gen.h` | 279 | +| `port/zephyr/sys/printk.h` | authored-support | `Embedded Rollout task support` | 3 | +| `port/zephyr/sys/slist.h` | upstream-frozen | `include/zephyr/sys/slist.h` | 459 | +| `port/zephyr/sys/util.h` | authored-support | `Embedded Rollout task support` | 8 | +| `subsys/shell/backends/shell_telnet.c` | upstream-frozen | `subsys/shell/backends/shell_telnet.c` | 810 | +| `subsys/shell/backends/shell_telnet_protocol.h` | upstream-frozen | `subsys/shell/backends/shell_telnet_protocol.h` | 125 | + +29个template文件,单文件均小于1MiB;2个candidate文件。独有C/H源码合计3954行,其中逐字节上游冻结3108行。按每个template C/H文件一次计算,含其注释/版权行,不计fixture副本、docs、许可证、判据。逐文件SHA256和详细范围见作者final-identity.json。 + +## 缺陷、观察与判据 + +上游提交的语义依据和保留API注释支持本合同,公开要求是template/docs/transport-contract.md的八条普通条款。工作取消的来源为同版本include/zephyr/kernel.h:4511-4563,已保留成公开work-api.txt。行缓冲依据为同版本Kconfig.backends:577-607,已保留为configuration.txt。命令限定为原backend支持的子集,未要求完整RFC选项或子协商。 + +C driver和port只输出原始字节、返回计数、连接/队列状态、实际上下文、同步记录身份及完成事件。所有期望、身份关系和先后关系在父check.py;不同合法锁策略可通过,不检测源码helper名称或精确锁次数。完成状态不把已断开后的内部残留linebuf本身当成失败;观察重连后是否泄漏旧数据。共享同步记录只在同时outstanding时拒绝,串行重用合法。 + +| 检查 | 观察要求 | 公开条款 | transport-contract.md行 | 上游依据(私有审阅) | +| --- | --- | --- | --- | --- | +| smoke | 正常接入与整行输出、计数和最终连接状态 | R01/R02 | 3,5 | base shell_telnet.c:646-737; Kconfig.backends:577-594 | +| buffer | 非LF输出先保留后经真实timeout/work提交发送 | R02 | 5 | Kconfig.backends:589-594; shell_telnet.h:64-68 | +| short | 2/1/3字节短写后的完整有序输出 | R02 | 5 | gold shell_telnet.c:224-254 | +| again | 已发前缀和EAGAIN未发后缀、下一次完成 | R03 | 7 | gold shell_telnet.c:226-235,258-278 | +| capacity | 41字节跨32字节buffer发送 | R02 | 5 | Kconfig.backends:577-587 | +| commands | 跨3次recv的IAC/DO ECHO、读取前缀后余量、AYT | R04/R05 | 9,11 | base shell_telnet.c:93-99,140-170,299-384,760-784; RFC854 TELNET Command Structure; RFC857 Command Meanings | +| negotiation | DO SGA、未知DO和DONT ECHO回复及最终echo | R05 | 11 | base shell_telnet.c:110-170; RFC857 Command Meanings | +| abort | AO丢弃待发字节、后续输出消耗但不发送、重连恢复 | R01/R05/R08 | 3,11,17 | gold shell_telnet.c:184-192,469-472,721-724 | +| worker_error | 延迟发送EPIPE、重入日志完成、EOF关闭、新work和重连 | R06/R07/R08 | 13,15,17 | gold shell_telnet.c:238-248,258-278,54-87; work-api.txt:36-49 | +| worker_error_io | 延迟发送EIO与上述相同进展合同 | R06/R07/R08 | 13,15,17 | same fatal-send path; no errno-specific recovery exception | +| worker_error_net | 延迟发送ENETDOWN与上述相同进展合同 | R06/R07/R08 | 13,15,17 | same fatal-send path; source shutdown comment covers every failed send | +| writer_error | shell线程发送EPIPE、重入日志、服务EOF和重连 | R06/R07/R08 | 13,15,17 | gold shell_telnet.c:754-768,238-248 | +| send_close | send暂停时EOF不能close该fd;发送返回后服务结束它 | R07/R08 | 15,17 | gold shell_telnet.c:49-70,731-734,778 | +| cancel_close | worker running时writer等待取消,与服务EOF交错后都完成 | R03/R07/R08 | 7,15,17 | work-api.txt:29-50; kernel/work.c:90-97,128-152,1244-1276 | +| cancel_abort | worker running时writer等待取消,与AO交错后都完成 | R03/R05/R07 | 7,11,15 | gold shell_telnet.c:184-192,258-278; work-api.txt:36-49 | +| restart | listener错误取消旧输出、结束旧fd并可重连 | R07/R08 | 15,17 | gold shell_telnet.c:422-445,447-492 | +| restart_overlap | listener重启取消running work,worker可退出 | R03/R07/R08 | 7,15,17 | gold shell_telnet.c:434-437,258-278; work-api.txt:36-49 | +| eof | 有待发字节时EOF,旧数据不跨新连接 | R01/R08 | 3,17 | gold shell_telnet.c:329-335,416-419,469-472 | +| close_first | 服务先等待running work、writer随后进入,关闭及新连接均完成 | R01/R03/R07/R08 | 3,7,15,17 | gold shell_telnet.c:54-70,258-278,721-724,754-768; work-api.txt:36-49 | +| abort_first | AO先等待running work、writer随后进入,结束并重连 | R01/R03/R05/R07/R08 | 3,7,11,15,17 | gold shell_telnet.c:184-192,258-278,721-724; work-api.txt:36-49 | +| shared init/state/output | 初始化成功、最终数据/连接/抑制状态、buffer完成结果 | R01/R02/R05/R08 | 3,5,11,17 | above public API/configuration and individual scenario sources | +| shared progress | 已发起send/cancel/service回调配对完成、前台上下文结束 | R03/R06/R07/R08 | 7,13,15,17 | work-api.txt:29-50; kernel/work.c real worker finalization | +| shared cancellation identity | 只在操作同时未完成时禁止同一sync地址;串行复用允许 | R07 | 15 | work-api.txt:45-50; kernel/work.c:90-97,128-152 | +| shared descriptor lifetime | 任何active send未返回时不得close同一fd | R07 | 15 | gold shell_telnet.c:49-70; checks actual SEND_BEGIN/END ordering, not lock count | +| shared service owner | CLOSE必须处于SERVICE_BEGIN/END标识的服务回调上下文 | R07 | 15 | gold shell_telnet.c:49-77,240-247 | +| shared reentrant logging | 若发生日志回调,其write必须返回/消耗且不发送失败会话数据 | R01/R06 | 3,13 | gold shell_telnet.c:239-247,721-724; no required logging call count | +| read-grader fixture control | gold底读取私有check失败时exit72;非秘密可读占位下全部正常 | grading isolation | docs/port.md:3-9 | private verifier/UID boundary; not an extra candidate functional requirement | + +## 样例与隔离 + +| 样例 | 预期 | 构成 | +| --- | --- | --- | +| gold | PASS | 两候选原样fix版本 | +| template | FAIL | 两候选原样base版本 | +| noop | FAIL | 基于gold,write仅报告消耗、不传输 | +| cheat | FAIL | 基于gold,仅宣称PASS并退出;父check仍要求raw行为 | +| cheat_read_grader | FAIL | 基于gold,只有打开私有check成功才继续;拒读exit72 | +| mask_symptom | FAIL | 基于base,用async取消替代所有sync取消;掩盖自等待但不能满足关闭/交接合同 | +| valid_local_cancel | PASS | 新鲜local同步状态在独立helper内创建,work callback使用早退/switch,接收侧结束使用合法的SHUT_RDWR;与gold写法不同 | + +只有候选二文件进入新评分工作区。verify.py与rollout/bos_verify.py逐字节一致。私有附件只有check.py和support.tar;构建/执行候选均在UID65534子进程。全部oracle比较位于父Python。support.tar中uid/gid固定0、owner名字root、mode0644、mtime0,check安全相对路径后用write_bytes生成文件,不继承host所有权。诊断的可读占位是非秘密文本,真实check/support在不可读的/oracle目录;不把实际grader暴露给候选。 + +## 诊断、正式校准与限制 + +最终指纹:`fb887abb6379346c7b89d13b9fed1248cd699496d1fec449b82db67cfd3b73ce`。 + +第一轮18场景及最终20场景的raw诊断保留在本题factory目录;包括原样gold的普通输入及正反次序交错。7类fixture的无网Docker单轮检查符合预期;read-grader拒读FAIL、可读占位PASS。最终指纹已在固定trace6 Docker正式入口每类5次,共35/35符合预期,全部trial cleanup和container_cleanup confirmed。 + +port采用单线程协作上下文确定性复现合法交错,使用真实完整work.c但不代表真实SMP硬件。socket/service/shell/kernel primitives是公开受控边界,不验证真实网络、电气、完整RTOS、真实Agent、完整模型轨迹或测量难度。没有trusted-local执行,也没有运行上游构建/测试脚本。全库unittest和root独立源审阅由root负责。 + +首轮正式校准 `calibration-09f013a1d608442c925f74f2a948cb84` 为评分支撑故障:bos将scratch交给65534后,check尝试chmod该目录,正式cap-drop无FOWNER而EPERM。候选未运行,负例同样在此阶段被拦截,因此本轮不算行为校准。已改为只对自身拥有的scratch chmod;不改候选、port或行为期望,完整新指纹重校准。旧回执及输出全部保留。 + +返工前正式回执:`runs/20261103-D-oss-fix-29/zephyr-adfe64d/calibration-4befb793dabd40d6ab7ced385b1e1d9e/receipt.json`,SHA256 `eed9b04010cb42ce70b4f107de3828bb0c923449d02db5e8574f2268e1f6cd02`。gold与valid_local_cancel各5次PASS,其余5类各5次FAIL;status PASS,清理confirmed。该返工前指纹只修正parent checker的scratch权限处理。作者按所有精确名字重新inspect:91个本题容器(含旧失败校准35个)全部不存在,详见作者receipt-audit.json;9个诊断编译产物已按具体路径删除,源码快照/日志保留。最新非秘密可读占位控制在正式cap-drop能力配置下仍PASS。当前任务仅CALIBRATED/UNMEASURED,等待root独审与全库unittest。 + +root返工第1轮:R06只要求结束接收侧,父check现在接受SHUT_RD(0)及SHUT_RDWR(2),拒绝仅SHUT_WR(1)。port公开三常量,只有RD/RDWR安排服务EOF,WR保留接收侧;valid_local_cancel改为RDWR。gold两候选仍与上游fix逐字节相同。旧35次PASS回执、父check和支撑归档保留于round1-before及原runs目录;本轮新指纹已完整35次符合预期,当前可读占位控制PASS。其余八条合同及20用例不变。 + +本轮最终正式回执:`runs/20261103-D-oss-fix-29/zephyr-adfe64d/calibration-118a0bc8fe4e46b794cf8ee401c58b43/receipt.json`,SHA256 `5a3eda3eae878221c4644126774b117938b9038f97930242294b0ecde54e3719`;digest `fb887abb6379346c7b89d13b9fed1248cd699496d1fec449b82db67cfd3b73ce`。7类各5次,共35/35符合预期,cleanup及container_cleanup均confirmed。valid_local_cancel的RDWR(2)原始观察出现服务CLOSE 20与closed fd=-1;独立WR(1)控制不触发服务EOF,closed观察仍fd20,无CLOSE 20,证明port不把仅发送侧关闭误当接收侧结束。当前父check与支撑下read-grader非秘密可读占位再次PASS。 + +已审阅父check其余精确值:命令/数据/字节数来自公开规格及固定输入,fd20来自受控接入,状态/完成与一次client close来自R01、R06、R07、R08。未保留其他补丁写法专属值、helper名称或锁次数要求。当前独有C/H3954行,原样上游3108行;29文件、27依据行、20用例不变。129个精确记录容器(含三轮105个正式容器)全部inspect不存在;新增两个raw编译产物已删,累计11个;详见receipt-audit.json。全库unittest与最终独审仍由root执行。 diff --git a/tasks/runtime-zephyr-telnet-close-handoff/support.tar b/tasks/runtime-zephyr-telnet-close-handoff/support.tar new file mode 100644 index 0000000000000000000000000000000000000000..f832204943df8d911c36ade009f486c992b64df6 GIT binary patch literal 122880 zcmeHweP0{LvS$8;Pcd;ejzk0o{E~Bw@reSm%?1MtI8IJ(Rx6|dv=)-A7Zc*^{p|ZZ zRsA|MdV$zJIrqXZv80*q>gwvM>gww1>X(f->1n%@e(?$aR`K`f!GkaOiNEaU>cj8W z>@$78wzj^$`bDz(32}0u$HP%$faV9i-pD;H_%iE#L6v`pM_XITW^&eSE^a;Btv)}( zkLBU0wRzSZudOA^`>V^mXQ$sAjFRP@cDLCXx6(y4@;Di#oo+f>?hkq=>072{?SCiBz2tv1PrF)AgdDys2dMCql@UB7m z9wxnR=V~RX{oYTTBlD;O5lh)Mw$h7Y&_9s*WH=g(o1-Lrn=B2_O~H*tDD+XgnKXeQ+inb!rGDe8(`&TcyEd>& z-vPJNRz<&DG=^{7>v5M#D{X1*!SBDWt^e&e_jcI+Bdw2;!S9{4>mI)AwOh$WV}O2^ z41#a!)J@6KW%+&e@Ok~!-m9b9cImRb(I1b7r8_UHhd#z3@f4o!P_+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 100644 index 00000000..c3623851 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/task.json @@ -0,0 +1,27 @@ +{ + "id": "runtime-zephyr-telnet-close-handoff", + "version": "1", + "family": "concurrency-lifecycle", + "source": "oss/zephyrproject-rtos/zephyr@adfe64d06c71", + "split": "zephyrproject-rtos/zephyr", + "template": "template", + "verifier": "verify.py", + "candidate_files": [ + "include/zephyr/shell/shell_telnet.h", + "subsys/shell/backends/shell_telnet.c" + ], + "timeout_seconds": 95, + "fixtures": { + "gold": "PASS", + "template": "FAIL", + "noop": "FAIL", + "cheat": "FAIL", + "cheat_read_grader": "FAIL", + "mask_symptom": "FAIL", + "valid_local_cancel": "PASS" + }, + "grader_files": [ + "check.py", + "support.tar" + ] +} diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/LICENSE b/tasks/runtime-zephyr-telnet-close-handoff/template/LICENSE new file mode 100644 index 00000000..8f71f43f --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/LICENSE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/Makefile b/tasks/runtime-zephyr-telnet-close-handoff/template/Makefile new file mode 100644 index 00000000..2d7511b7 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/Makefile @@ -0,0 +1,11 @@ +CC = gcc +CFLAGS = -std=gnu11 -O0 -g -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-sign-compare +CPPFLAGS = -Iport -Iinclude +all: telnet-probe +telnet-probe: port/runtime.c kernel/work.c subsys/shell/backends/shell_telnet.c driver.c + $(CC) $(CPPFLAGS) $(CFLAGS) $^ -o $@ +smoke: telnet-probe + ./telnet-probe smoke +clean: + rm -f telnet-probe +.PHONY: all smoke clean diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/README.md b/tasks/runtime-zephyr-telnet-close-handoff/template/README.md new file mode 100644 index 00000000..5e98d47c --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/README.md @@ -0,0 +1,7 @@ +# Offline Telnet shell transport + +This workspace contains a complete Telnet shell backend and the matching kernel workqueue implementation. The host boundary uses cooperative contexts and virtual socket events; no network connection is made. + +Build with `make`, run the normal-path smoke with `make smoke`, and inspect raw events with `./telnet-probe SCENARIO`. Available scenarios are smoke, buffer, short, again, capacity, commands, negotiation, abort, worker_error, worker_error_io, worker_error_net, writer_error, send_close, cancel_close, cancel_abort, restart, restart_overlap, eof, close_first, and abort_first. + +The delivery requirements are in `docs/transport-contract.md`. `docs/port.md` describes the boundary and observations. The source retains the original license notices; LICENSE is the upstream Apache-2.0 license. diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/docs/configuration.txt b/tasks/runtime-zephyr-telnet-close-handoff/template/docs/configuration.txt new file mode 100644 index 00000000..94564a09 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/docs/configuration.txt @@ -0,0 +1,36 @@ +# Shell backends configuration options + +# Copyright (c) 2018 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +config SHELL_TELNET_LINE_BUF_SIZE + int "Telnet line buffer size" + default 80 + help + This option can be used to modify the size of the buffer storing + shell output line, prior to sending it through the network. + Of course an output line can be longer than such size, it just + means sending it will start as soon as it reaches this size. + It really depends on what type of output is expected. + A lot of short lines: better reduce this value. On the contrary, + raise it. + +config SHELL_TELNET_SEND_TIMEOUT + int "Telnet line send timeout" + default 100 + help + This option can be used to modify the duration of the timer that kick + in when a line buffer is not empty but did not yet meet the line feed. + +config SHELL_TELNET_SUPPORT_COMMAND + bool "Add support for telnet commands (IAC) [EXPERIMENTAL]" + select EXPERIMENTAL + help + Current support is so limited it's not interesting to enable it. + However, if proven to be needed at some point, it will be possible + to extend such support. It does have support for echo and "character + at a time" mode, which enable the history and line-editing features + of the shell. + IMPORTANT: This will increase network usage as a TCP packet will be + sent each time a character is typed in the telnet client. + diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/docs/port.md b/tasks/runtime-zephyr-telnet-close-handoff/template/docs/port.md new file mode 100644 index 00000000..02419c77 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/docs/port.md @@ -0,0 +1,11 @@ +# Controlled host boundary + +The unmodified kernel work implementation manages pending queues, running state, delayed submission, synchronous cancellation records, and cancellation completion. Its linked lists and work structures are retained from the same source version. Only the underlying scheduling, timeout, mutex, semaphore, shell event, logging, and socket boundaries are supplied here. + +Contexts execute cooperatively on separate stacks. A mutex has recursive ownership; a failed nonblocking acquisition returns immediately. A semaphore wait suspends its context until a give. The scheduler can pause a socket send before it returns or pause a worker after it becomes running. Those pauses allow other contexts to execute. The scheduler then resumes the selected contexts in a fixed order. No wall-clock delay chooses an interleaving. Virtual timeout advancement invokes the kernel's real delayable-work callback; the real worker loop runs its queued handler and completes pending cancels. + +The socket boundary accepts synthetic descriptors. Each send uses the next supplied short-write or errno result; later sends succeed for all requested bytes. SHUT_RD (0) and SHUT_RDWR (2) end the receive side and create a readable EOF event for the service context; SHUT_WR (1) does not create that event. Receives consume the supplied byte stream. Closing a socket records the calling context and descriptor. IPv4/IPv6 listen setup and service registration succeed in this boundary. The port does not implement real TCP, board timing, or the full Zephyr scheduler. + +`SEND_BEGIN` and `SEND_END` record the actual context, descriptor, requested length, and result. `SERVICE_BEGIN` and `SERVICE_END` identify socket-service callbacks. `CANCEL` and `CANCEL_RETURN` record synchronization object addresses and completion. `SEM_WAIT`, `MUTEX_WAIT`, `DONE`, and `MARKER_WORK` expose completion progress. `WRITE`, `READ`, `STATE`, and `OUTPUT` expose accepted counts, ordered bytes, connection state, buffering, notifications, and echo state. These records are observations; they contain no expected values or pass/fail decisions. + +The output buffer is 32 bytes, receive capacity is 128 bytes, and the delayed-send interval is 10 virtual ticks. The smoke writes one LF-terminated line. Other scenarios exercise deferred output, partial sends, EAGAIN, fragmented commands, abort, send errors with a reentrant logging callback, active-send closure, two orders of concurrent cancellation, listener restart, EOF, and subsequent reconnection. All scenario inputs and scheduling choices are fixed. diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/docs/transport-contract.md b/tasks/runtime-zephyr-telnet-close-handoff/template/docs/transport-contract.md new file mode 100644 index 00000000..f075984a --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/docs/transport-contract.md @@ -0,0 +1,19 @@ +# Telnet transport behavior + +R01. Initialization starts IPv4 and IPv6 listeners and accepts one client. A new accepted connection has empty incoming and outgoing data and can produce output. Shell writes report their consumed byte count; disconnected or aborted output can be discarded while reporting it consumed. + +R02. Output fills the configured line buffer and is transmitted when the buffer reaches capacity or the write ends in LF. A shorter non-LF write is buffered until its configured send delay expires. Successful short socket writes advance through the unsent bytes without duplication or loss. + +R03. A nonblocking send returning EAGAIN retains the unsent suffix and arranges a later attempt. System workqueue callbacks do not wait for the socket to become writable. A temporarily occupied transport can defer such an attempt and must continue to make progress once the competing operation completes. + +R04. Incoming text is delivered in order. Reading a prefix preserves the unread suffix. IAC commands may be split between receives, are removed from the text stream, and do not prevent later text from being reported to the shell. + +R05. The supported command subset includes AYT, AO, DO, and DONT. AYT replies with the backend's existing service message. DO ECHO enables echo and replies WILL ECHO; DONT ECHO disables echo and replies WONT ECHO. DO SUPPRESS-GO-AHEAD replies WILL, and an unsupported DO option replies WONT. AO discards pending output and suppresses further shell output until the connection ends. + +R06. A fatal socket send error stops output before error reporting can call back into the shell. The sending context must return, and subsequent unrelated work on the system workqueue must run. Ending the receive side makes the socket service observe EOF; the socket service completes client closure and continues serving new clients. This applies to socket errors such as EPIPE, EIO, and ENETDOWN and to sends initiated by either shell writes or delayed work. + +R07. Client closure belongs to the socket service. A descriptor remains valid for the duration of an active send; closure follows that send's completion. Pending work cancellation, ordinary writes, EOF, abort output, and listener restart must complete under legal scheduling interleavings. Every outstanding synchronous work cancellation uses synchronization state that is distinct from any other outstanding operation, and its state remains alive until the call returns. + +R08. EOF and a listener error end the previous client session. The service cancels pending output before replacing that session and resets connection state on accept. A replacement client receives its own newly submitted bytes; buffered bytes from the ended session are not delivered to it. A cancellation that encounters a running work item waits for that item to finish without preventing the worker from finishing. + +The line-buffer and timer behavior follows the backend configuration help. Work cancellation lifetime and completion follow the public kernel API excerpt in `work-api.txt`. Command values and the supported subset are represented by the retained protocol definitions and backend; RFC 854 describes IAC, AO, and AYT, and RFC 857 describes ECHO negotiation. The lifecycle clauses describe the transport's service-owner and progress contract, independent of a particular locking implementation. diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/docs/work-api.txt b/tasks/runtime-zephyr-telnet-close-handoff/template/docs/work-api.txt new file mode 100644 index 00000000..a2570934 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/docs/work-api.txt @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016, Wind River Systems, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** @brief Cancel delayable work. + * + * Similar to k_work_cancel() but for delayable work. If the work is + * scheduled or submitted it is canceled. This function does not wait for the + * cancellation to complete. + * + * @note The work may still be running when this returns. Use + * k_work_flush_delayable() or k_work_cancel_delayable_sync() to ensure it is + * not running. + * + * @note Canceling delayable work does not prevent rescheduling it. It does + * prevent submitting it until the cancellation completes. + * + * @isr_ok + * + * @param dwork pointer to the delayable work item. + * + * @return the k_work_delayable_busy_get() status indicating the state of the + * item after all cancellation steps performed by this call are completed. + */ +int k_work_cancel_delayable(struct k_work_delayable *dwork); + +/** @brief Cancel delayable work and wait. + * + * Like k_work_cancel_delayable() but waits until the work becomes idle. + * + * @note Canceling delayable work does not prevent rescheduling it. It does + * prevent submitting it until the cancellation completes. + * + * @note Be careful of caller and work queue thread relative priority. If + * this function sleeps it will not return until the work queue thread + * completes the tasks that allow this thread to resume. + * + * @note Behavior is undefined if this function is invoked on @p dwork from a + * work queue running @p dwork. + * + * @param dwork pointer to the delayable work item. + * + * @param sync pointer to an opaque item containing state related to the + * pending cancellation. The object must persist until the call returns, and + * be accessible from both the caller thread and the work queue thread. The + * object must not be used for any other flush or cancel operation until this + * one completes. On architectures with CONFIG_KERNEL_COHERENCE the object + * must be allocated in coherent memory. + * + * @retval true if work was not idle (call had to wait for cancellation of a + * running handler to complete, or scheduled or submitted operations were + * cancelled); + * @retval false otherwise + */ +bool k_work_cancel_delayable_sync(struct k_work_delayable *dwork, + struct k_work_sync *sync); + diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/driver.c b/tasks/runtime-zephyr-telnet-close-handoff/template/driver.c new file mode 100644 index 00000000..24221fe2 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/driver.c @@ -0,0 +1,88 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* Raw observations only. Expectations live in the parent checker. */ +#include +#include +extern struct shell_telnet *sh_telnet; +static const char *payload; +static int eventfd, eventmask; +static unsigned char rx[128]; +static size_t rxlen; +static void marker(struct k_work *w){ARG_UNUSED(w);puts("MARKER_WORK");} +static void writer(void *arg) { + ARG_UNUSED(arg);size_t count=999;int ret=shell_transport_telnet.api->write(&shell_transport_telnet,payload,strlen(payload),&count); + printf("WRITE %d %zu\n",ret,count); +} +static void service_event(void *arg) {ARG_UNUSED(arg);port_input(rx,rxlen);port_event(eventfd,eventmask);} +static int write_start(const char *p) {payload=p;return port_spawn(writer,NULL);} +static int event_start(int fd,int events,const unsigned char *bytes,size_t n) { + eventfd=fd;eventmask=events;rxlen=n;if(n)memcpy(rx,bytes,n);return port_spawn(service_event,NULL); +} +static void write_now(const char *p){int id=write_start(p);port_run(id);} +static void recv_now(const unsigned char *p,size_t n){int id=event_start(sh_telnet->fds[2].fd,ZSOCK_POLLIN,p,n);port_run(id);} +static void eof_now(void){recv_now(NULL,0);} +static void reconnect(void){port_accept_fd++;int id=event_start(sh_telnet->fds[0].fd,ZSOCK_POLLIN,NULL,0);port_run(id);} +static void read_now(size_t n){ + unsigned char data[128];size_t count=999;int ret=shell_transport_telnet.api->read(&shell_transport_telnet,data,n,&count); + printf("READ %d %zu ",ret,count);for(size_t i=0;ifds[2].fd,ZSOCK_POLLIN,NULL,0);port_run(s);port_snapshot("overlap"); + port_run(w);port_pump();port_snapshot("closed");reconnect();write_now("next\n");port_snapshot("final"); + } + else if(!strcmp(which,"cancel_close") || !strcmp(which,"cancel_abort")) { + write_now("prompt");port_advance();port_pause_work=1;port_run(1); + int w=write_start("more\n");port_run(w); + unsigned char a[]={255,245};int s=event_start(sh_telnet->fds[2].fd,ZSOCK_POLLIN,which[7]=='a'?a:NULL,which[7]=='a'?sizeof(a):0);port_run(s); + port_snapshot("waiting");port_run(1);port_pump();port_advance();port_pump();port_snapshot("final"); + } + else if(!strcmp(which,"restart")) { + write_now("stale");int s=event_start(sh_telnet->fds[0].fd,ZSOCK_POLLERR,NULL,0);port_run(s);port_advance();port_pump();port_snapshot("restart");reconnect();write_now("fresh\n");port_snapshot("final"); + } + else if(!strcmp(which,"restart_overlap")) { + write_now("prompt");port_advance();port_pause_work=1;port_run(1); + int s=event_start(sh_telnet->fds[0].fd,ZSOCK_POLLERR,NULL,0);port_run(s);port_run(1);port_pump();port_snapshot("restart"); + reconnect();write_now("fresh\n");port_snapshot("final"); + } + else if(!strcmp(which,"close_first") || !strcmp(which,"abort_first")) { + write_now("prompt");port_advance();port_pause_work=1;port_run(1); + unsigned char a[]={255,245};bool aborting=which[0]=='a'; + int s=event_start(sh_telnet->fds[2].fd,ZSOCK_POLLIN,aborting?a:NULL,aborting?sizeof(a):0);port_run(s); + int w=write_start("more\n");port_run(w);port_snapshot("waiting"); + port_run(1);port_pump();port_snapshot("finished"); + if(aborting)eof_now();reconnect();write_now("fresh\n");port_advance();port_pump();port_snapshot("final"); + } + else if(!strcmp(which,"eof")){write_now("stale");eof_now();port_advance();port_pump();port_snapshot("closed");reconnect();write_now("fresh\n");port_snapshot("final");} + else return 3; + puts("COMPLETE");return 0; +} diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/include/zephyr/shell/shell_telnet.h b/tasks/runtime-zephyr-telnet-close-handoff/template/include/zephyr/shell/shell_telnet.h new file mode 100644 index 00000000..3429e29c --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/include/zephyr/shell/shell_telnet.h @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Header file for the Telnet shell backend. + * @ingroup shell_telnet + */ + +#ifndef ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ +#define ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond INTERNAL_HIDDEN */ +extern const struct shell_transport_api shell_telnet_transport_api; + +#define SHELL_TELNET_POLLFD_COUNT 3 +#define SHELL_TELNET_MAX_CMD_SIZE 3 + +/** Line buffer structure. */ +struct shell_telnet_line_buf { + /** Line buffer. */ + char buf[CONFIG_SHELL_TELNET_LINE_BUF_SIZE]; + + /** Current line length. */ + uint16_t len; +}; + +/** TELNET-based shell transport. */ +struct shell_telnet { + /** Handler function registered by shell. */ + shell_transport_handler_t shell_handler; + + /** Context registered by shell. */ + void *shell_context; + + /** Buffer for outgoing line. */ + struct shell_telnet_line_buf line_out; + + /** Array for sockets used by the telnet service. */ + struct zsock_pollfd fds[SHELL_TELNET_POLLFD_COUNT]; + + /** Input buffer. */ + uint8_t rx_buf[CONFIG_SHELL_CMD_BUFF_SIZE]; + + /** Number of data bytes within the input buffer. */ + size_t rx_len; + + /** Mutex protecting the input buffer access. */ + struct k_mutex rx_lock; + uint8_t cmd_buf[SHELL_TELNET_MAX_CMD_SIZE]; + uint8_t cmd_len; + + /** The delayed work is used to send non-lf terminated output that has + * been around for "too long". This will prove to be useful + * to send the shell prompt for instance. + */ + struct k_work_delayable send_work; + struct k_work_sync work_sync; + + /** If set, no output is sent to the TELNET client. */ + bool output_lock; +}; +/** @endcond */ + +/** + * @defgroup shell_telnet Telnet shell backend + * @ingroup shell_backends + * @brief Shell access over a TELNET connection. + * @{ + */ + +/** + * @brief Define a Telnet shell transport instance. + * + * @param _name Name of the transport instance. + */ +#define SHELL_TELNET_DEFINE(_name) \ + static struct shell_telnet _name##_shell_telnet; \ + struct shell_transport _name = { \ + .api = &shell_telnet_transport_api, \ + .ctx = (struct shell_telnet *)&_name##_shell_telnet \ + } + +/** + * @brief This function provides pointer to shell telnet backend instance. + * + * Function returns pointer to the shell telnet instance. This instance can be + * next used with shell_execute_cmd function in order to test commands behavior. + * + * @returns Pointer to the shell instance. + */ +const struct shell *shell_backend_telnet_get_ptr(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/kernel/work.c b/tasks/runtime-zephyr-telnet-close-handoff/template/kernel/work.c new file mode 100644 index 00000000..f9baca63 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/kernel/work.c @@ -0,0 +1,1325 @@ +/* + * Copyright (c) 2020 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * + * Second generation work queue implementation + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); + +static inline void flag_clear(uint32_t *flagp, + uint32_t bit) +{ + *flagp &= ~BIT(bit); +} + +static inline void flag_set(uint32_t *flagp, + uint32_t bit) +{ + *flagp |= BIT(bit); +} + +static inline bool flag_test(const uint32_t *flagp, + uint32_t bit) +{ + return (*flagp & BIT(bit)) != 0U; +} + +static inline bool flag_test_and_clear(uint32_t *flagp, + int bit) +{ + bool ret = flag_test(flagp, bit); + + flag_clear(flagp, bit); + + return ret; +} + +static inline void flags_set(uint32_t *flagp, + uint32_t flags) +{ + *flagp = flags; +} + +static inline uint32_t flags_get(const uint32_t *flagp) +{ + return *flagp; +} + +/* Lock to protect the internal state of all work items, work queues, + * and pending_cancels. + */ +static struct k_spinlock work_lock; + +/* Invoked by work thread */ +static void handle_flush(struct k_work *work) { } + +static inline void init_flusher(struct z_work_flusher *flusher) +{ + struct k_work *work = &flusher->work; + k_sem_init(&flusher->sem, 0, 1); + k_work_init(&flusher->work, handle_flush); + flag_set(&work->flags, K_WORK_FLUSHING_BIT); +} + +/* List of pending cancellations. */ +static sys_slist_t pending_cancels; + +/* Initialize a canceler record and add it to the list of pending + * cancels. + * + * Invoked with work lock held. + * + * @param canceler the structure used to notify a waiting process. + * @param work the work structure that is to be canceled + */ +static inline void init_work_cancel(struct z_work_canceller *canceler, + struct k_work *work) +{ + k_sem_init(&canceler->sem, 0, 1); + canceler->work = work; + sys_slist_append(&pending_cancels, &canceler->node); +} + +/* Complete flushing of a work item. + * + * Invoked with work lock held. + * + * Invoked from a work queue thread. + * + * Reschedules. + * + * @param work the work structure that has completed flushing. + */ +static void finalize_flush_locked(struct k_work *work) +{ + struct z_work_flusher *flusher + = CONTAINER_OF(work, struct z_work_flusher, work); + + flag_clear(&work->flags, K_WORK_FLUSHING_BIT); + + k_sem_give(&flusher->sem); +}; + +/* Complete cancellation of a work item and unlock held lock. + * + * Invoked with work lock held. + * + * Invoked from a work queue thread. + * + * Reschedules. + * + * @param work the work structure that has completed cancellation + */ +static void finalize_cancel_locked(struct k_work *work) +{ + struct z_work_canceller *wc, *tmp; + sys_snode_t *prev = NULL; + + /* Clear this first, so released high-priority threads don't + * see it when doing things. + */ + flag_clear(&work->flags, K_WORK_CANCELING_BIT); + + /* Search for and remove matching containers, and release + * what's waiting for the completion. The same work item can + * appear multiple times in the list if multiple threads + * attempt to cancel it; each waiter must be woken. + */ + SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&pending_cancels, wc, tmp, node) { + if (wc->work == work) { + sys_slist_remove(&pending_cancels, prev, &wc->node); + k_sem_give(&wc->sem); + } else { + prev = &wc->node; + } + } +} + +void k_work_init(struct k_work *work, + k_work_handler_t handler) +{ + __ASSERT_NO_MSG(work != NULL); + __ASSERT_NO_MSG(handler != NULL); + + *work = (struct k_work)Z_WORK_INITIALIZER(handler); + + SYS_PORT_TRACING_OBJ_INIT(k_work, work); +} + +static inline int work_busy_get_locked(const struct k_work *work) +{ + return flags_get(&work->flags) & K_WORK_MASK; +} + +int k_work_busy_get(const struct k_work *work) +{ + k_spinlock_key_t key = k_spin_lock(&work_lock); + int ret = work_busy_get_locked(work); + + k_spin_unlock(&work_lock, key); + + return ret; +} + +/* Add a flusher work item to the queue. + * + * Invoked with work lock held. + * + * Caller must notify queue of pending work. + * + * @param queue queue on which a work item may appear. + * @param work the work item that is either queued or running on @p + * queue + * @param flusher an uninitialized/unused flusher object + */ +static void queue_flusher_locked(struct k_work_q *queue, + struct k_work *work, + struct z_work_flusher *flusher) +{ + init_flusher(flusher); + + if ((flags_get(&work->flags) & K_WORK_QUEUED) != 0U) { + sys_slist_insert(&queue->pending, &work->node, + &flusher->work.node); + } else { + sys_slist_prepend(&queue->pending, &flusher->work.node); + } +} + +/* Try to remove a work item from the given queue. + * + * Invoked with work lock held. + * + * @param queue the queue from which the work should be removed + * @param work work that may be on the queue + */ +static inline void queue_remove_locked(struct k_work_q *queue, + struct k_work *work) +{ + if (flag_test_and_clear(&work->flags, K_WORK_QUEUED_BIT)) { + (void)sys_slist_find_and_remove(&queue->pending, &work->node); + } +} + +/* Potentially notify a queue that it needs to look for pending work. + * + * This may make the work queue thread ready, but as the lock is held it + * will not be a reschedule point. Callers should yield after the lock is + * released where appropriate (generally if this returns true). + * + * @param queue to be notified. If this is null no notification is required. + * + * @return true if and only if the queue was notified and woken, i.e. a + * reschedule is pending. + */ +static inline bool notify_queue_locked(struct k_work_q *queue) +{ + bool rv = false; + + if (queue != NULL) { + rv = z_sched_wake(&queue->notifyq, 0, NULL); + } + + return rv; +} + +/* Submit an work item to a queue if queue state allows new work. + * + * Submission is rejected if no queue is provided, or if the queue is + * draining and the work isn't being submitted from the queue's + * thread (chained submission). + * + * Invoked with work lock held. + * Conditionally notifies queue. + * + * @param queue the queue to which work should be submitted. This may + * be null, in which case the submission will fail. + * + * @param work to be submitted + * + * @retval 1 if successfully queued + * @retval -EINVAL if no queue is provided + * @retval -ENODEV if the queue is not started + * @retval -EBUSY if the submission was rejected (draining, plugged) + */ +static inline int queue_submit_locked(struct k_work_q *queue, + struct k_work *work) +{ + if (queue == NULL) { + return -EINVAL; + } + + int ret; + bool chained = (_current == queue->thread_id) && !k_is_in_isr(); + bool draining = flag_test(&queue->flags, K_WORK_QUEUE_DRAIN_BIT); + bool plugged = flag_test(&queue->flags, K_WORK_QUEUE_PLUGGED_BIT); + + /* Test for acceptability, in priority order: + * + * * -ENODEV if the queue isn't running. + * * -EBUSY if draining and not chained + * * -EBUSY if plugged and not draining + * * otherwise OK + */ + if (!flag_test(&queue->flags, K_WORK_QUEUE_STARTED_BIT)) { + ret = -ENODEV; + } else if (draining && !chained) { + ret = -EBUSY; + } else if (plugged && !draining) { + ret = -EBUSY; + } else { + sys_slist_append(&queue->pending, &work->node); + ret = 1; + (void)notify_queue_locked(queue); + } + + return ret; +} + +/* Attempt to submit work to a queue. + * + * The submission can fail if: + * * the work is cancelling, + * * no candidate queue can be identified; + * * the candidate queue rejects the submission. + * + * Invoked with work lock held. + * Conditionally notifies queue. + * + * @param work the work structure to be submitted + + * @param queuep pointer to a queue reference. On input this should + * dereference to the proposed queue (which may be null); after completion it + * will be null if the work was not submitted or if submitted will reference + * the queue it was submitted to. That may or may not be the queue provided + * on input. + * + * @retval 0 if work was already submitted to a queue + * @retval 1 if work was not submitted and has been queued to @p queue + * @retval 2 if work was running and has been queued to the queue that was + * running it + * @retval -EBUSY if canceling or submission was rejected by queue + * @retval -EINVAL if no queue is provided + * @retval -ENODEV if the queue is not started + */ +static int submit_to_queue_locked(struct k_work *work, + struct k_work_q **queuep) +{ + int ret = 0; + + if (flag_test(&work->flags, K_WORK_CANCELING_BIT)) { + /* Disallowed */ + ret = -EBUSY; + } else if (!flag_test(&work->flags, K_WORK_QUEUED_BIT)) { + /* Not currently queued */ + ret = 1; + + /* If no queue specified resubmit to last queue. + */ + if (*queuep == NULL) { + *queuep = work->queue; + } + + /* If the work is currently running we have to use the + * queue it's running on to prevent handler + * re-entrancy. + */ + if (flag_test(&work->flags, K_WORK_RUNNING_BIT)) { + __ASSERT_NO_MSG(work->queue != NULL); + *queuep = work->queue; + ret = 2; + } + + int rc = queue_submit_locked(*queuep, work); + + if (rc < 0) { + ret = rc; + } else { + flag_set(&work->flags, K_WORK_QUEUED_BIT); + work->queue = *queuep; + } + } else { + /* Already queued, do nothing. */ + } + + if (ret <= 0) { + *queuep = NULL; + } + + return ret; +} + +/* Submit work to a queue + * + * Intended for internal use. + * + * See also submit_to_queue_locked(). + * + * @param queuep pointer to a queue reference. + * @param work the work structure to be submitted + * + * @retval see submit_to_queue_locked() + */ +int z_work_submit_to_queue(struct k_work_q *queue, + struct k_work *work) +{ + __ASSERT_NO_MSG(work != NULL); + __ASSERT_NO_MSG(work->handler != NULL); + + k_spinlock_key_t key = k_spin_lock(&work_lock); + + int ret = submit_to_queue_locked(work, &queue); + + /* No need to reschedule if the submission failed. */ + if (ret > 0) { + z_reschedule(&work_lock, key); + } else { + k_spin_unlock(&work_lock, key); + } + + return ret; +} + +int k_work_submit_to_queue(struct k_work_q *queue, + struct k_work *work) +{ + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, submit_to_queue, queue, work); + + int ret = z_work_submit_to_queue(queue, work); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, submit_to_queue, queue, work, ret); + + return ret; +} + +int k_work_submit(struct k_work *work) +{ + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, submit, work); + + int ret = z_work_submit_to_queue(&k_sys_work_q, work); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, submit, work, ret); + + return ret; +} + +/* Flush the work item if necessary. + * + * Flushing is necessary only if the work is either queued or running. + * + * Invoked with work lock held by key. + * Sleeps. + * + * @param work the work item that is to be flushed + * @param flusher state used to synchronize the flush + * + * @retval true if work is queued or running. If this happens the + * caller must take the flusher semaphore after releasing the lock. + * + * @retval false otherwise. No wait required. + */ +static bool work_flush_locked(struct k_work *work, + struct z_work_flusher *flusher) +{ + bool need_flush = (flags_get(&work->flags) + & (K_WORK_QUEUED | K_WORK_RUNNING)) != 0U; + + if (need_flush) { + struct k_work_q *queue = work->queue; + + __ASSERT_NO_MSG(queue != NULL); + + queue_flusher_locked(queue, work, flusher); + notify_queue_locked(queue); + } + + return need_flush; +} + +bool k_work_flush(struct k_work *work, + struct k_work_sync *sync) +{ + __ASSERT_NO_MSG(work != NULL); + __ASSERT_NO_MSG(!flag_test(&work->flags, K_WORK_DELAYABLE_BIT)); + __ASSERT_NO_MSG(!k_is_in_isr()); + __ASSERT_NO_MSG(sync != NULL); +#ifdef CONFIG_KERNEL_COHERENCE + __ASSERT_NO_MSG(sys_cache_is_mem_coherent(sync)); +#endif /* CONFIG_KERNEL_COHERENCE */ + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, flush, work); + + struct z_work_flusher *flusher = &sync->flusher; + k_spinlock_key_t key = k_spin_lock(&work_lock); + + bool need_flush = work_flush_locked(work, flusher); + + k_spin_unlock(&work_lock, key); + + /* If necessary wait until the flusher item completes */ + if (need_flush) { + SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_work, flush, work, K_FOREVER); + + k_sem_take(&flusher->sem, K_FOREVER); + } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, flush, work, need_flush); + + return need_flush; +} + +/* Execute the non-waiting steps necessary to cancel a work item. + * + * Invoked with work lock held. + * + * @param work the work item to be canceled. + * + * @retval true if we need to wait for the work item to finish canceling + * @retval false if the work item is idle + * + * @return k_busy_wait() captured under lock + */ +static int cancel_async_locked(struct k_work *work) +{ + /* If we haven't already started canceling, do it now. */ + if (!flag_test(&work->flags, K_WORK_CANCELING_BIT)) { + /* Remove it from the queue, if it's queued. */ + queue_remove_locked(work->queue, work); + } + + /* If it's still busy after it's been dequeued, then flag it + * as canceling. + */ + int ret = work_busy_get_locked(work); + + if (ret != 0) { + flag_set(&work->flags, K_WORK_CANCELING_BIT); + ret = work_busy_get_locked(work); + } + + return ret; +} + +/* Complete cancellation necessary, release work lock, and wait if + * necessary. + * + * Invoked with work lock held by key. + * Sleeps. + * + * @param work work that is being canceled + * @param canceller state used to synchronize the cancellation + * @param key used by work lock + * + * @retval true if and only if the work was still active on entry. The caller + * must wait on the canceller semaphore after releasing the lock. + * + * @retval false if work was idle on entry. The caller need not wait. + */ +static bool cancel_sync_locked(struct k_work *work, + struct z_work_canceller *canceller) +{ + bool ret = flag_test(&work->flags, K_WORK_CANCELING_BIT); + + /* If something's still running then we have to wait for + * completion, which is indicated when finish_cancel() gets + * invoked. + */ + if (ret) { + init_work_cancel(canceller, work); + } + + return ret; +} + +int k_work_cancel(struct k_work *work) +{ + __ASSERT_NO_MSG(work != NULL); + __ASSERT_NO_MSG(!flag_test(&work->flags, K_WORK_DELAYABLE_BIT)); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, cancel, work); + + k_spinlock_key_t key = k_spin_lock(&work_lock); + int ret = cancel_async_locked(work); + + k_spin_unlock(&work_lock, key); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, cancel, work, ret); + + return ret; +} + +bool k_work_cancel_sync(struct k_work *work, + struct k_work_sync *sync) +{ + __ASSERT_NO_MSG(work != NULL); + __ASSERT_NO_MSG(sync != NULL); + __ASSERT_NO_MSG(!flag_test(&work->flags, K_WORK_DELAYABLE_BIT)); + __ASSERT_NO_MSG(!k_is_in_isr()); +#ifdef CONFIG_KERNEL_COHERENCE + __ASSERT_NO_MSG(sys_cache_is_mem_coherent(sync)); +#endif /* CONFIG_KERNEL_COHERENCE */ + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, cancel_sync, work, sync); + + struct z_work_canceller *canceller = &sync->canceller; + k_spinlock_key_t key = k_spin_lock(&work_lock); + bool pending = (work_busy_get_locked(work) != 0U); + bool need_wait = false; + + if (pending) { + (void)cancel_async_locked(work); + need_wait = cancel_sync_locked(work, canceller); + } + + k_spin_unlock(&work_lock, key); + + if (need_wait) { + SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_work, cancel_sync, work, sync); + + k_sem_take(&canceller->sem, K_FOREVER); + } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, cancel_sync, work, sync, pending); + return pending; +} + +#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) +static void work_timeout_handler(struct _timeout *record) +{ + struct k_work_q *queue = CONTAINER_OF(record, struct k_work_q, work_timeout_record); + struct k_work *work = NULL; + k_work_handler_t handler = NULL; + const char *name; + const char *space = " "; + k_spinlock_key_t key = k_spin_lock(&work_lock); + + work = queue->work; + handler = work->handler; + + /* + * The work item may be running on a different CPU than the timeout + * handler. Two conditions must be checked. + * 1. Did the work item finish before the timeout handler got to run? + * 2. Was the timeout aborted while the handler was in flight? + * queue->finished alone is insufficient for (2): if the work thread + * completes the item and starts a new one before this handler wins + * , finished is reset and the stale handler would abort the + * wrong worker. work_timeout_stop_locked() aborts the timeout, which + * flags it superseded; bail on that too. + */ + if (queue->finished || z_timeout_inflight_superseded(record)) { + k_spin_unlock(&work_lock, key); + return; + } + queue->finished = true; + + k_spin_unlock(&work_lock, key); + + name = k_thread_name_get(queue->thread_id); + if (name == NULL) { + name = ""; + space = ""; + } + + LOG_ERR("queue %p%s%s blocked by work %p with handler %p", + queue, space, name, work, handler); + + k_thread_abort(queue->thread_id); +} + +static void work_timeout_start_locked(struct k_work_q *queue, struct k_work *work) +{ + queue->finished = false; + + if (K_TIMEOUT_EQ(queue->work_timeout, K_FOREVER)) { + return; + } + + queue->work = work; + z_add_timeout(&queue->work_timeout_record, work_timeout_handler, queue->work_timeout); +} + +static void work_timeout_stop_locked(struct k_work_q *queue) +{ + if (K_TIMEOUT_EQ(queue->work_timeout, K_FOREVER)) { + return; + } + + (void)z_try_abort_timeout(&queue->work_timeout_record); +} +#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */ + +/* Loop executed by a work queue thread. + * + * @param workq_ptr pointer to the work queue structure + */ +static void work_queue_main(void *workq_ptr, void *p2, void *p3) +{ + ARG_UNUSED(p2); + ARG_UNUSED(p3); + + struct k_work_q *queue = (struct k_work_q *)workq_ptr; + + while (true) { + sys_snode_t *node; + struct k_work *work = NULL; + k_work_handler_t handler = NULL; + k_spinlock_key_t key = k_spin_lock(&work_lock); + bool yield; + + /* Check for and prepare any new work. */ + node = sys_slist_get(&queue->pending); + if (node != NULL) { + /* Mark that there's some work active that's + * not on the pending list. + */ + flag_set(&queue->flags, K_WORK_QUEUE_BUSY_BIT); + work = CONTAINER_OF(node, struct k_work, node); + flag_set(&work->flags, K_WORK_RUNNING_BIT); + flag_clear(&work->flags, K_WORK_QUEUED_BIT); + + /* Static code analysis tool can raise a false-positive violation + * in the line below that 'work' is checked for null after being + * dereferenced. + * + * The work is figured out by CONTAINER_OF, as a container + * of type struct k_work that contains the node. + * The only way for it to be NULL is if node would be a member + * of struct k_work object that has been placed at address NULL, + * which should never happen, even line 'if (work != NULL)' + * ensures that. + * This means that if node is not NULL, then work will not be NULL. + */ + handler = work->handler; + } else if (flag_test_and_clear(&queue->flags, + K_WORK_QUEUE_DRAIN_BIT)) { + /* Not busy and draining: move threads waiting for + * drain to ready state. The held spinlock inhibits + * immediate reschedule; released threads get their + * chance when this invokes z_sched_wait() below. + * + * We don't touch K_WORK_QUEUE_PLUGGABLE, so getting + * here doesn't mean that the queue will allow new + * submissions. + */ + (void)z_sched_wake_all(&queue->drainq, 1, NULL); + } else if (flag_test(&queue->flags, K_WORK_QUEUE_STOP_BIT)) { + /* User has requested that the queue stop. Clear the status flags and exit. + */ + flags_set(&queue->flags, 0); + k_spin_unlock(&work_lock, key); + return; + } else { + /* No work is available and no queue state requires + * special handling. + */ + ; + } + + if (work == NULL) { + /* Nothing's had a chance to add work since we took + * the lock, and we didn't find work nor got asked to + * stop. Just go to sleep: when something happens the + * work thread will be woken and we can check again. + */ + + (void)z_sched_wait(&work_lock, key, &queue->notifyq, + K_FOREVER, NULL); + continue; + } + +#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) + work_timeout_start_locked(queue, work); +#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */ + + k_spin_unlock(&work_lock, key); + + __ASSERT_NO_MSG(handler != NULL); + handler(work); + + /* Mark the work item as no longer running and deal + * with any cancellation and flushing issued while it + * was running. Clear the BUSY flag and optionally + * yield to prevent starving other threads. + */ + key = k_spin_lock(&work_lock); + +#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) + if (queue->finished) { + /* + * The work item timeout handler has flagged this work + * thread for taking too long and is going to abort it. + * Do not proceed to the next work item. + */ + k_spin_unlock(&work_lock, key); + while (1) { + k_sleep(K_FOREVER); + } + CODE_UNREACHABLE; + } + work_timeout_stop_locked(queue); + queue->finished = true; +#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */ + + flag_clear(&work->flags, K_WORK_RUNNING_BIT); + if (flag_test(&work->flags, K_WORK_FLUSHING_BIT)) { + finalize_flush_locked(work); + } + if (flag_test(&work->flags, K_WORK_CANCELING_BIT)) { + finalize_cancel_locked(work); + } + + flag_clear(&queue->flags, K_WORK_QUEUE_BUSY_BIT); + yield = (!flag_test(&queue->flags, K_WORK_QUEUE_NO_YIELD_BIT) && + !sys_slist_is_empty(&queue->pending)); + k_spin_unlock(&work_lock, key); + + /* Optionally yield to prevent the work queue from + * starving other threads. + */ + if (yield) { + k_yield(); + } + } +} + +void k_work_queue_init(struct k_work_q *queue) +{ + __ASSERT_NO_MSG(queue != NULL); + + *queue = (struct k_work_q) { + .flags = 0, + }; + + SYS_PORT_TRACING_OBJ_INIT(k_work_queue, queue); +} + +void k_work_queue_run(struct k_work_q *queue, const struct k_work_queue_config *cfg) +{ + __ASSERT_NO_MSG(!flag_test(&queue->flags, K_WORK_QUEUE_STARTED_BIT)); + + uint32_t flags = K_WORK_QUEUE_STARTED; + + if ((cfg != NULL) && cfg->no_yield) { + flags |= K_WORK_QUEUE_NO_YIELD; + } + + if ((cfg != NULL) && (cfg->name != NULL)) { + k_thread_name_set(_current, cfg->name); + } + +#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) + if ((cfg != NULL) && (cfg->work_timeout_ms)) { + queue->work_timeout = K_MSEC(cfg->work_timeout_ms); + } else { + queue->work_timeout = K_FOREVER; + } +#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */ + + sys_slist_init(&queue->pending); + z_waitq_init(&queue->notifyq); + z_waitq_init(&queue->drainq); + queue->thread_id = _current; + flags_set(&queue->flags, flags); + work_queue_main(queue, NULL, NULL); +} + +void k_work_queue_start(struct k_work_q *queue, + k_thread_stack_t *stack, + size_t stack_size, + int prio, + const struct k_work_queue_config *cfg) +{ + __ASSERT_NO_MSG(queue); + __ASSERT_NO_MSG(stack); + __ASSERT_NO_MSG(!flag_test(&queue->flags, K_WORK_QUEUE_STARTED_BIT)); + + /* In future, this whole function will be deprecated, but for now, we + * have to use the `thread` field to create a new thread in it. + */ + TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS); + + uint32_t flags = K_WORK_QUEUE_STARTED; + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work_queue, start, queue); + + sys_slist_init(&queue->pending); + z_waitq_init(&queue->notifyq); + z_waitq_init(&queue->drainq); + + if ((cfg != NULL) && cfg->no_yield) { + flags |= K_WORK_QUEUE_NO_YIELD; + } + + /* It hasn't actually been started yet, but all the state is in place + * so we can submit things and once the thread gets control it's ready + * to roll. + */ + flags_set(&queue->flags, flags); + + (void)k_thread_create(&queue->thread, stack, stack_size, + work_queue_main, queue, NULL, NULL, + prio, 0, K_FOREVER); + + if ((cfg != NULL) && (cfg->name != NULL)) { + k_thread_name_set(&queue->thread, cfg->name); + } + + if ((cfg != NULL) && (cfg->essential)) { + queue->thread.base.user_options |= K_ESSENTIAL; + } + +#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) + if ((cfg != NULL) && (cfg->work_timeout_ms)) { + queue->work_timeout = K_MSEC(cfg->work_timeout_ms); + } else { + queue->work_timeout = K_FOREVER; + } +#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */ + + k_thread_start(&queue->thread); + queue->thread_id = &queue->thread; + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, start, queue); + + TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS); +} + +int k_work_queue_drain(struct k_work_q *queue, + bool plug) +{ + __ASSERT_NO_MSG(queue); + __ASSERT_NO_MSG(!k_is_in_isr()); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work_queue, drain, queue); + + int ret = 0; + k_spinlock_key_t key = k_spin_lock(&work_lock); + + if (((flags_get(&queue->flags) + & (K_WORK_QUEUE_BUSY | K_WORK_QUEUE_DRAIN)) != 0U) + || plug + || !sys_slist_is_empty(&queue->pending)) { + flag_set(&queue->flags, K_WORK_QUEUE_DRAIN_BIT); + if (plug) { + flag_set(&queue->flags, K_WORK_QUEUE_PLUGGED_BIT); + } + + notify_queue_locked(queue); + ret = z_sched_wait(&work_lock, key, &queue->drainq, + K_FOREVER, NULL); + } else { + k_spin_unlock(&work_lock, key); + } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, drain, queue, ret); + + return ret; +} + +int k_work_queue_unplug(struct k_work_q *queue) +{ + __ASSERT_NO_MSG(queue); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work_queue, unplug, queue); + + int ret = -EALREADY; + k_spinlock_key_t key = k_spin_lock(&work_lock); + + if (flag_test_and_clear(&queue->flags, K_WORK_QUEUE_PLUGGED_BIT)) { + ret = 0; + } + + k_spin_unlock(&work_lock, key); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, unplug, queue, ret); + + return ret; +} + +int k_work_queue_stop(struct k_work_q *queue, k_timeout_t timeout) +{ + __ASSERT_NO_MSG(queue); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work_queue, stop, queue, timeout); + + if (queue->thread_id) { + if (z_is_thread_essential(queue->thread_id)) { + return -ENOTSUP; + } + } + + k_spinlock_key_t key = k_spin_lock(&work_lock); + + if (!flag_test(&queue->flags, K_WORK_QUEUE_STARTED_BIT)) { + k_spin_unlock(&work_lock, key); + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, stop, queue, timeout, -EALREADY); + return -EALREADY; + } + + if (!flag_test(&queue->flags, K_WORK_QUEUE_PLUGGED_BIT)) { + k_spin_unlock(&work_lock, key); + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, stop, queue, timeout, -EBUSY); + return -EBUSY; + } + + flag_set(&queue->flags, K_WORK_QUEUE_STOP_BIT); + notify_queue_locked(queue); + k_spin_unlock(&work_lock, key); + SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_work_queue, stop, queue, timeout); + if (k_thread_join(queue->thread_id, timeout)) { + key = k_spin_lock(&work_lock); + flag_clear(&queue->flags, K_WORK_QUEUE_STOP_BIT); + k_spin_unlock(&work_lock, key); + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, stop, queue, timeout, -ETIMEDOUT); + return -ETIMEDOUT; + } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, stop, queue, timeout, 0); + return 0; +} + +#ifdef CONFIG_SYS_CLOCK_EXISTS + +/* Timeout handler for delayable work. + * + * Invoked by timeout infrastructure. + * Takes and releases work lock. + * Conditionally reschedules. + */ +static void work_timeout(struct _timeout *to) +{ + struct k_work_delayable *dw + = CONTAINER_OF(to, struct k_work_delayable, timeout); + struct k_work *wp = &dw->work; + k_spinlock_key_t key = k_spin_lock(&work_lock); + struct k_work_q *queue = NULL; + + /* K_WORK_DELAYED_BIT is the wake-ownership flag: whichever side + * (this handler or unschedule_locked()) clears it via + * flag_test_and_clear() first owns the outcome. If unschedule_locked + * got here first, the bit is clear and we bail. + */ + if (flag_test_and_clear(&wp->flags, K_WORK_DELAYED_BIT)) { + queue = dw->queue; + (void)submit_to_queue_locked(wp, &queue); + } + + k_spin_unlock(&work_lock, key); +} + +void k_work_init_delayable(struct k_work_delayable *dwork, + k_work_handler_t handler) +{ + __ASSERT_NO_MSG(dwork != NULL); + __ASSERT_NO_MSG(handler != NULL); + + *dwork = (struct k_work_delayable)Z_WORK_DELAYABLE_INITIALIZER(handler); + + z_init_timeout(&dwork->timeout); + + SYS_PORT_TRACING_OBJ_INIT(k_work_delayable, dwork); +} + +static inline int work_delayable_busy_get_locked(const struct k_work_delayable *dwork) +{ + return flags_get(&dwork->work.flags) & K_WORK_MASK; +} + +int k_work_delayable_busy_get(const struct k_work_delayable *dwork) +{ + __ASSERT_NO_MSG(dwork != NULL); + + k_spinlock_key_t key = k_spin_lock(&work_lock); + int ret = work_delayable_busy_get_locked(dwork); + + k_spin_unlock(&work_lock, key); + return ret; +} + +/* Attempt to schedule a work item for future (maybe immediate) + * submission. + * + * Invoked with work lock held. + * + * See also submit_to_queue_locked(), which implements this for a no-wait + * delay. + * + * Invoked with work lock held. + * + * @param queuep pointer to a pointer to a queue. On input this + * should dereference to the proposed queue (which may be null); after + * completion it will be null if the work was not submitted or if + * submitted will reference the queue it was submitted to. That may + * or may not be the queue provided on input. + * + * @param dwork the delayed work structure + * + * @param delay the delay to use before scheduling. + * + * @retval from submit_to_queue_locked() if delay is K_NO_WAIT; otherwise + * @retval 1 to indicate successfully scheduled. + */ +static int schedule_for_queue_locked(struct k_work_q **queuep, + struct k_work_delayable *dwork, + k_timeout_t delay) +{ + int ret = 1; + struct k_work *work = &dwork->work; + + if (K_TIMEOUT_EQ(delay, K_NO_WAIT)) { + return submit_to_queue_locked(work, queuep); + } + + flag_set(&work->flags, K_WORK_DELAYED_BIT); + dwork->queue = *queuep; + + /* Add timeout */ + z_add_timeout(&dwork->timeout, work_timeout, delay); + + return ret; +} + +/* Unschedule delayable work. + * + * If the work is delayed, cancel the timeout and clear the delayed + * flag. + * + * Invoked with work lock held. + * + * @param dwork pointer to delayable work structure. + * + * @return true if and only if work had been delayed so the timeout + * was cancelled. + */ +static inline bool unschedule_locked(struct k_work_delayable *dwork, + k_spinlock_key_t *key) +{ + bool ret = false; + struct k_work *work = &dwork->work; + + /* If scheduled, try to cancel. If it fails, that means the + * callback has been dequeued and will inevitably run (or has + * already run), so treat that as "undelayed" and return + * false. + * + * Clearing K_WORK_DELAYED_BIT claims the wake from the timeout + * handler (see work_timeout()). Then wait for any in-flight + * handler to actually complete: callers may free dwork or + * re-arm the same delayable for a new schedule once we return + * true, and either is unsafe while the handler is still about + * to dereference dwork->work.flags. + */ + if (flag_test_and_clear(&work->flags, K_WORK_DELAYED_BIT)) { + while (z_try_abort_timeout(&dwork->timeout) == -EAGAIN) { + k_spin_unlock(&work_lock, *key); + *key = k_spin_lock(&work_lock); + } + ret = true; + } + + return ret; +} + +/* Full cancellation of a delayable work item. + * + * Unschedules the delayed part then delegates to standard work + * cancellation. + * + * Invoked with work lock held. + * + * @param dwork delayable work item + * + * @return k_work_busy_get() flags + */ +static int cancel_delayable_async_locked(struct k_work_delayable *dwork, + k_spinlock_key_t *key) +{ + (void)unschedule_locked(dwork, key); + + return cancel_async_locked(&dwork->work); +} + +int k_work_schedule_for_queue(struct k_work_q *queue, struct k_work_delayable *dwork, + k_timeout_t delay) +{ + __ASSERT_NO_MSG(queue != NULL); + __ASSERT_NO_MSG(dwork != NULL); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, schedule_for_queue, queue, dwork, delay); + + struct k_work *work = &dwork->work; + int ret = 0; + k_spinlock_key_t key = k_spin_lock(&work_lock); + + /* Schedule the work item if it's idle or running. */ + if ((work_busy_get_locked(work) & ~K_WORK_RUNNING) == 0U) { + ret = schedule_for_queue_locked(&queue, dwork, delay); + } + + k_spin_unlock(&work_lock, key); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, schedule_for_queue, queue, dwork, delay, ret); + + return ret; +} + +int k_work_schedule(struct k_work_delayable *dwork, k_timeout_t delay) +{ + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, schedule, dwork, delay); + + int ret = k_work_schedule_for_queue(&k_sys_work_q, dwork, delay); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, schedule, dwork, delay, ret); + + return ret; +} + +int k_work_reschedule_for_queue(struct k_work_q *queue, struct k_work_delayable *dwork, + k_timeout_t delay) +{ + __ASSERT_NO_MSG(queue != NULL); + __ASSERT_NO_MSG(dwork != NULL); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, reschedule_for_queue, queue, dwork, delay); + + int ret; + k_spinlock_key_t key = k_spin_lock(&work_lock); + + /* Remove any active scheduling. */ + (void)unschedule_locked(dwork, &key); + + /* Schedule the work item with the new parameters. */ + ret = schedule_for_queue_locked(&queue, dwork, delay); + + k_spin_unlock(&work_lock, key); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, reschedule_for_queue, queue, dwork, delay, ret); + + return ret; +} + +int k_work_reschedule(struct k_work_delayable *dwork, k_timeout_t delay) +{ + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, reschedule, dwork, delay); + + int ret = k_work_reschedule_for_queue(&k_sys_work_q, dwork, delay); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, reschedule, dwork, delay, ret); + + return ret; +} + +int k_work_cancel_delayable(struct k_work_delayable *dwork) +{ + __ASSERT_NO_MSG(dwork != NULL); + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, cancel_delayable, dwork); + + k_spinlock_key_t key = k_spin_lock(&work_lock); + int ret = cancel_delayable_async_locked(dwork, &key); + + k_spin_unlock(&work_lock, key); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, cancel_delayable, dwork, ret); + + return ret; +} + +bool k_work_cancel_delayable_sync(struct k_work_delayable *dwork, + struct k_work_sync *sync) +{ + __ASSERT_NO_MSG(dwork != NULL); + __ASSERT_NO_MSG(sync != NULL); + __ASSERT_NO_MSG(!k_is_in_isr()); +#ifdef CONFIG_KERNEL_COHERENCE + __ASSERT_NO_MSG(sys_cache_is_mem_coherent(sync)); +#endif /* CONFIG_KERNEL_COHERENCE */ + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, cancel_delayable_sync, dwork, sync); + + struct z_work_canceller *canceller = &sync->canceller; + k_spinlock_key_t key = k_spin_lock(&work_lock); + bool pending = (work_delayable_busy_get_locked(dwork) != 0U); + bool need_wait = false; + + if (pending) { + (void)cancel_delayable_async_locked(dwork, &key); + need_wait = cancel_sync_locked(&dwork->work, canceller); + } + + k_spin_unlock(&work_lock, key); + + if (need_wait) { + k_sem_take(&canceller->sem, K_FOREVER); + } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, cancel_delayable_sync, dwork, sync, pending); + return pending; +} + +bool k_work_flush_delayable(struct k_work_delayable *dwork, + struct k_work_sync *sync) +{ + __ASSERT_NO_MSG(dwork != NULL); + __ASSERT_NO_MSG(sync != NULL); + __ASSERT_NO_MSG(!k_is_in_isr()); +#ifdef CONFIG_KERNEL_COHERENCE + __ASSERT_NO_MSG(sys_cache_is_mem_coherent(sync)); +#endif /* CONFIG_KERNEL_COHERENCE */ + + SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_work, flush_delayable, dwork, sync); + + struct k_work *work = &dwork->work; + struct z_work_flusher *flusher = &sync->flusher; + k_spinlock_key_t key = k_spin_lock(&work_lock); + + /* If it's idle release the lock and return immediately. */ + if (work_busy_get_locked(work) == 0U) { + k_spin_unlock(&work_lock, key); + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, flush_delayable, dwork, sync, false); + + return false; + } + + /* If unscheduling did something then submit it. Ignore a + * failed submission (e.g. when cancelling). + */ + if (unschedule_locked(dwork, &key)) { + struct k_work_q *queue = dwork->queue; + + (void)submit_to_queue_locked(work, &queue); + } + + /* Wait for it to finish */ + bool need_flush = work_flush_locked(work, flusher); + + k_spin_unlock(&work_lock, key); + + /* If necessary wait until the flusher item completes */ + if (need_flush) { + k_sem_take(&flusher->sem, K_FOREVER); + } + + SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work, flush_delayable, dwork, sync, need_flush); + + return need_flush; +} + +#endif /* CONFIG_SYS_CLOCK_EXISTS */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/base.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/base.h new file mode 100644 index 00000000..b55fc5b5 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/base.h @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define CONFIG_SYS_CLOCK_EXISTS 1 +#define CONFIG_KERNEL_LOG_LEVEL 0 +#define CONFIG_SHELL_TELNET_LINE_BUF_SIZE 32 +#define CONFIG_SHELL_CMD_BUFF_SIZE 128 +#define CONFIG_SHELL_TELNET_SEND_TIMEOUT 10 +#define CONFIG_SHELL_TELNET_PORT 23 +#define CONFIG_SHELL_PROMPT_TELNET "offline> " +#define CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_SIZE 1 +#define CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_TIMEOUT 1 +#define CONFIG_SHELL_TELNET_LOG_LEVEL 0 +#define CONFIG_SHELL_TELNET_INIT_LOG_LEVEL 0 +#define CONFIG_LOG_MAX_LEVEL 4 +#define CONFIG_SHELL_TELNET_INIT_PRIORITY 1 +#define CONFIG_SHELL_TELNET_SUPPORT_COMMAND 1 +#define CONFIG_NET_IPV4 1 +#define CONFIG_NET_IPV6 1 +#define __ASSERT_NO_MSG(x) assert(x) +#define __deprecated +#define TOOLCHAIN_DISABLE_WARNING(...) +#define TOOLCHAIN_ENABLE_WARNING(...) +#define SYS_PORT_TRACING_OBJ_INIT(...) +#define SYS_PORT_TRACING_OBJ_FUNC_ENTER(...) port_trace(#__VA_ARGS__,1) +#define SYS_PORT_TRACING_OBJ_FUNC_EXIT(...) port_trace(#__VA_ARGS__,0) +#define SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(...) +typedef int k_timeout_t; +typedef int k_ticks_t; +typedef char k_thread_stack_t; +#define K_NO_WAIT 0 +#define K_FOREVER -1 +#define K_MSEC(n) (n) +#define K_TIMEOUT_EQ(a,b) ((a)==(b)) +#define K_ESSENTIAL 1 +struct k_thread { int id; struct { int user_options; } base; }; +typedef struct k_thread *k_tid_t; +extern k_tid_t _current; +struct k_spinlock { int held; }; +typedef int k_spinlock_key_t; +struct k_mutex { int owner, depth; }; +struct k_sem { int count, limit; }; +typedef struct { int unused; } _wait_q_t; +struct _timeout { void (*fn)(struct _timeout *); int active, ticks; }; +void port_trace(const char *,int); +int k_mutex_init(struct k_mutex *); +int k_mutex_lock(struct k_mutex *,k_timeout_t); +int k_mutex_unlock(struct k_mutex *); +int k_sem_init(struct k_sem *,unsigned int,unsigned int); +int k_sem_take(struct k_sem *,k_timeout_t); +void k_sem_give(struct k_sem *); +k_spinlock_key_t k_spin_lock(struct k_spinlock *); +void k_spin_unlock(struct k_spinlock *,k_spinlock_key_t); +void z_reschedule(struct k_spinlock *,k_spinlock_key_t); +bool k_is_in_isr(void); +void k_yield(void); +void k_msleep(int); +void k_sleep(int); +void z_waitq_init(_wait_q_t *); +bool z_sched_wake(_wait_q_t *,int,void *); +bool z_sched_wake_all(_wait_q_t *,int,void *); +int z_sched_wait(struct k_spinlock *,k_spinlock_key_t,_wait_q_t *,k_timeout_t,void *); +k_tid_t k_thread_create(struct k_thread *,k_thread_stack_t *,size_t,void (*)(void *,void *,void *),void *,void *,void *,int,int,k_timeout_t); +void k_thread_start(k_tid_t); +void k_thread_name_set(k_tid_t,const char *); +int k_thread_join(k_tid_t,k_timeout_t); +bool z_is_thread_essential(k_tid_t); +void z_init_timeout(struct _timeout *); +void z_add_timeout(struct _timeout *,void (*)(struct _timeout *),k_timeout_t); +int z_try_abort_timeout(struct _timeout *); +int z_timeout_remaining(const struct _timeout *); +static inline uint32_t k_ticks_to_ms_ceil32(int t) { return t; } diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/ksched.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/ksched.h new file mode 100644 index 00000000..39df0c8e --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/ksched.h @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/runtime.c b/tasks/runtime-zephyr-telnet-close-handoff/template/port/runtime.c new file mode 100644 index 00000000..3274d630 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/runtime.c @@ -0,0 +1,172 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* Deterministic userspace scheduling and socket boundary. No real sockets. */ +#include +#include + +#define TASKS 16 +struct task { + ucontext_t uc; char stack[131072]; struct k_thread thread; k_tid_t identity; + void (*fn)(void *); void *arg; int ready, done; void *waiting; +}; +static struct task tasks[TASKS]; +static ucontext_t control; +static int active, count; +k_tid_t _current; +struct k_work_q k_sys_work_q; +static const struct net_socket_service *service; +static struct _timeout *timeouts[32]; +static int ntimers; +static unsigned char input[512], output[4096]; +static size_t input_len, output_len; +static int send_results[32], nresults, result_at; +int port_pause_send, port_pause_work, port_send_started; +int port_fd_next=10, port_accept_fd=20, port_error=ENETDOWN; +int port_log_reentrant; +static int rx_events, tx_events, echo, closes, shutdowns; +extern struct shell_telnet *sh_telnet; + +void port_pause(void) { + if (!active) return; + swapcontext(&tasks[active].uc,&control); +} +static void block(void *object) { + tasks[active].ready=0; tasks[active].waiting=object; port_pause(); +} +static void wake(void *object) { + for (int i=1;i<=count;i++) if (!tasks[i].done && tasks[i].waiting==object) { + tasks[i].waiting=NULL; tasks[i].ready=1; + } +} +static void trampoline(int id) { + tasks[id].fn(tasks[id].arg); + tasks[id].done=1;tasks[id].ready=0; + printf("DONE %d\n",id);port_pause();abort(); +} +int port_spawn(void (*fn)(void *),void *arg) { + int id=++count; assert(idfn=fn;t->arg=arg;t->ready=1;t->thread.id=id;t->identity=&t->thread; + getcontext(&t->uc);t->uc.uc_stack.ss_sp=t->stack;t->uc.uc_stack.ss_size=sizeof(t->stack); + t->uc.uc_link=&control;makecontext(&t->uc,(void (*)(void))trampoline,1,id); + return id; +} +int port_run(int id) { + if (!tasks[id].ready || tasks[id].done) return 0; + active=id;_current=tasks[id].identity;swapcontext(&control,&tasks[id].uc); + active=0;_current=&tasks[0].thread;return 1; +} +void port_pump(void) { + for (int round=0;round<100;round++) { + int any=0;for(int i=1;i<=count;i++) any+=port_run(i); + if(!any) return; + } + puts("SCHEDULER_LIMIT"); +} +int k_mutex_init(struct k_mutex *m) { m->owner=-1;m->depth=0;return 0; } +int k_mutex_lock(struct k_mutex *m,k_timeout_t timeout) { + while(m->depth && m->owner!=active) { + if(timeout==K_NO_WAIT) return -EBUSY; + printf("MUTEX_WAIT %d %d\n",active,m->owner);block(m); + } + m->owner=active;m->depth++;return 0; +} +int k_mutex_unlock(struct k_mutex *m) { + assert(m->depth>0 && m->owner==active); + if(!--m->depth) {m->owner=-1;wake(m);}return 0; +} +int k_sem_init(struct k_sem *s,unsigned int n,unsigned int limit) {s->count=n;s->limit=limit;return 0;} +int k_sem_take(struct k_sem *s,k_timeout_t t) { + while(!s->count) { + if(t==K_NO_WAIT) return -EAGAIN; + printf("SEM_WAIT %d %p\n",active,(void *)s);block(s); + } + s->count--;return 0; +} +void k_sem_give(struct k_sem *s) {if(s->countlimit)s->count++;wake(s);} +k_spinlock_key_t k_spin_lock(struct k_spinlock *l) {assert(!l->held);l->held=1;return 0;} +void k_spin_unlock(struct k_spinlock *l,k_spinlock_key_t k) { + ARG_UNUSED(k);assert(l->held);l->held=0; + if(port_pause_work && active==1 && sh_telnet && (sh_telnet->send_work.work.flags & K_WORK_RUNNING)) { + port_pause_work=0;puts("WORK_PAUSED");port_pause(); + } +} +void z_reschedule(struct k_spinlock *l,k_spinlock_key_t k) {k_spin_unlock(l,k);} +bool k_is_in_isr(void){return false;} +void k_yield(void){port_pause();} +void k_msleep(int ms){ARG_UNUSED(ms);port_pause();} +void k_sleep(int ms){ARG_UNUSED(ms);block((void *)-1);} +void z_waitq_init(_wait_q_t *q){q->unused=0;} +bool z_sched_wake(_wait_q_t *q,int n,void *v){ARG_UNUSED(n);ARG_UNUSED(v);wake(q);return true;} +bool z_sched_wake_all(_wait_q_t *q,int n,void *v){return z_sched_wake(q,n,v);} +int z_sched_wait(struct k_spinlock *l,k_spinlock_key_t k,_wait_q_t *q,k_timeout_t t,void *v){ + ARG_UNUSED(t);ARG_UNUSED(v);k_spin_unlock(l,k);block(q);return 0; +} +static void (*thread_fn)(void *,void *,void *); +static void *thread_arg; +static void thread_entry(void *arg){ARG_UNUSED(arg);thread_fn(thread_arg,NULL,NULL);} +k_tid_t k_thread_create(struct k_thread *t,k_thread_stack_t *stack,size_t size, + void (*fn)(void *,void *,void *),void *p1,void *p2,void *p3,int prio,int opt,k_timeout_t delay){ + ARG_UNUSED(stack);ARG_UNUSED(size);ARG_UNUSED(p2);ARG_UNUSED(p3);ARG_UNUSED(prio);ARG_UNUSED(opt);ARG_UNUSED(delay); + thread_fn=fn;thread_arg=p1;int id=port_spawn(thread_entry,NULL);*t=tasks[id].thread;tasks[id].identity=t;return t; +} +void k_thread_start(k_tid_t t){ARG_UNUSED(t);} +void k_thread_name_set(k_tid_t t,const char *s){ARG_UNUSED(t);ARG_UNUSED(s);} +int k_thread_join(k_tid_t t,k_timeout_t timeout){ARG_UNUSED(timeout);return tasks[t->id].done?0:-EAGAIN;} +bool z_is_thread_essential(k_tid_t t){return !!t->base.user_options;} +void z_init_timeout(struct _timeout *t){memset(t,0,sizeof(*t));} +void z_add_timeout(struct _timeout *t,void (*fn)(struct _timeout *),k_timeout_t delay){ + int known=0;for(int i=0;ifn=fn;t->ticks=delay;t->active=1; +} +int z_try_abort_timeout(struct _timeout *t){t->active=0;return 0;} +int z_timeout_remaining(const struct _timeout *t){return t->active?t->ticks:0;} +void port_advance(void){for(int i=0;iactive){timeouts[i]->active=0;timeouts[i]->fn(timeouts[i]);}} +void port_trace(const char *name,int enter){ARG_UNUSED(name);ARG_UNUSED(enter);} +void port_cancel_enter(struct k_work_delayable *w,struct k_work_sync *s){printf("CANCEL %d %p %u\n",active,(void *)s,w->work.flags);} +void port_cancel_exit(struct k_work_delayable *w,struct k_work_sync *s,bool pending){ARG_UNUSED(w);printf("CANCEL_RETURN %d %p %d\n",active,(void *)s,pending);} + +int zsock_socket(int family,int type,int protocol){ARG_UNUSED(family);ARG_UNUSED(type);ARG_UNUSED(protocol);return port_fd_next++;} +int zsock_bind(int fd,struct net_sockaddr *a,net_socklen_t len){ARG_UNUSED(fd);ARG_UNUSED(a);ARG_UNUSED(len);return 0;} +int zsock_listen(int fd,int n){ARG_UNUSED(fd);ARG_UNUSED(n);return 0;} +int zsock_accept(int fd,struct net_sockaddr *a,net_socklen_t *len){ARG_UNUSED(fd);ARG_UNUSED(len);a->sa_family=NET_AF_INET;return port_accept_fd;} +int zsock_send(int fd,const void *data,size_t len,int flags){ + printf("SEND_BEGIN %d %d %zu %d\n",active,fd,len,flags);port_send_started++; + if(port_pause_send){port_pause_send=0;port_pause();} + int n=result_atlen)n=len; + assert(output_len+ncb(&e);printf("SERVICE_END %d %d\n",active,fd);} +void port_input(const unsigned char *p,size_t n){assert(nwrite(&shell_transport_telnet,"log\n",4,&n);printf("LOG_WRITE %d %zu\n",ret,n);} +} +void port_reset(void){ + setbuf(stdout,NULL);tasks[0].thread.id=0;_current=&tasks[0].thread; + struct shell_transport *t=&shell_transport_telnet;int r=t->api->init(t,NULL,transport_event,NULL);printf("INIT %d\n",r); + static k_thread_stack_t stack[16];k_work_queue_init(&k_sys_work_q);k_work_queue_start(&k_sys_work_q,stack,sizeof(stack),0,NULL); + port_run(1);port_event(sh_telnet->fds[0].fd,ZSOCK_POLLIN); +} +void port_snapshot(const char *label){ + printf("STATE %s %d %d %u %zu %d %d %d %d %d\n",label,sh_telnet->fds[2].fd,sh_telnet->output_lock,sh_telnet->line_out.len,sh_telnet->rx_len,closes,shutdowns,rx_events,tx_events,echo); + printf("OUTPUT %s ",label);for(size_t i=0;i +#include +extern struct shell_transport shell_transport_telnet; +extern int port_pause_send, port_pause_work, port_send_started; +extern int port_fd_next, port_accept_fd, port_error; +extern int port_log_reentrant; +void port_reset(void); +int port_spawn(void (*fn)(void *),void *arg); +int port_run(int id); +void port_pump(void); +void port_advance(void); +void port_event(int fd,int revents); +void port_input(const unsigned char *,size_t); +void port_send_results(const int *,size_t); +void port_snapshot(const char *); +void port_pause(void); diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/scheduler.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/scheduler.h new file mode 100644 index 00000000..39df0c8e --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/scheduler.h @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/wait_q.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/wait_q.h new file mode 100644 index 00000000..39df0c8e --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/wait_q.h @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/init.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/init.h new file mode 100644 index 00000000..357355b7 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/init.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include +#define SYS_INIT(...) diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/kernel.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/kernel.h new file mode 100644 index 00000000..f0e7feb7 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/kernel.h @@ -0,0 +1,383 @@ +/* + * Copyright (c) 2016, Wind River Systems, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once +#include + +struct k_work; +struct k_work_q; +typedef void (*k_work_handler_t)(struct k_work *); +/** @brief Work item and work queue state flag values. */ +enum { +/** + * @cond INTERNAL_HIDDEN + */ + /* The atomic API is used for all work and queue flags fields to + * enforce sequential consistency in SMP environments. + */ + + /* Bits that represent the work item states. At least nine of the + * combinations are distinct valid stable states. + */ + K_WORK_RUNNING_BIT = 0, + K_WORK_CANCELING_BIT = 1, + K_WORK_QUEUED_BIT = 2, + K_WORK_DELAYED_BIT = 3, + K_WORK_FLUSHING_BIT = 4, + + K_WORK_MASK = BIT(K_WORK_DELAYED_BIT) | BIT(K_WORK_QUEUED_BIT) + | BIT(K_WORK_RUNNING_BIT) | BIT(K_WORK_CANCELING_BIT) | BIT(K_WORK_FLUSHING_BIT), + + /* Static work flags */ + K_WORK_DELAYABLE_BIT = 8, + K_WORK_DELAYABLE = BIT(K_WORK_DELAYABLE_BIT), + + /* Dynamic work queue flags */ + K_WORK_QUEUE_STARTED_BIT = 0, + K_WORK_QUEUE_STARTED = BIT(K_WORK_QUEUE_STARTED_BIT), + K_WORK_QUEUE_BUSY_BIT = 1, + K_WORK_QUEUE_BUSY = BIT(K_WORK_QUEUE_BUSY_BIT), + K_WORK_QUEUE_DRAIN_BIT = 2, + K_WORK_QUEUE_DRAIN = BIT(K_WORK_QUEUE_DRAIN_BIT), + K_WORK_QUEUE_PLUGGED_BIT = 3, + K_WORK_QUEUE_PLUGGED = BIT(K_WORK_QUEUE_PLUGGED_BIT), + K_WORK_QUEUE_STOP_BIT = 4, + K_WORK_QUEUE_STOP = BIT(K_WORK_QUEUE_STOP_BIT), + + /* Static work queue flags */ + K_WORK_QUEUE_NO_YIELD_BIT = 8, + K_WORK_QUEUE_NO_YIELD = BIT(K_WORK_QUEUE_NO_YIELD_BIT), +/** + * INTERNAL_HIDDEN @endcond + */ + /* Transient work flags */ + + /** @brief Flag indicating a work item that is running under a work + * queue thread. + * + * Accessed via k_work_busy_get(). May co-occur with other flags. + */ + K_WORK_RUNNING = BIT(K_WORK_RUNNING_BIT), + + /** @brief Flag indicating a work item that is being canceled. + * + * Accessed via k_work_busy_get(). May co-occur with other flags. + */ + K_WORK_CANCELING = BIT(K_WORK_CANCELING_BIT), + + /** @brief Flag indicating a work item that has been submitted to a + * queue but has not started running. + * + * Accessed via k_work_busy_get(). May co-occur with other flags. + */ + K_WORK_QUEUED = BIT(K_WORK_QUEUED_BIT), + + /** @brief Flag indicating a delayed work item that is scheduled for + * submission to a queue. + * + * Accessed via k_work_busy_get(). May co-occur with other flags. + */ + K_WORK_DELAYED = BIT(K_WORK_DELAYED_BIT), + + /** @brief Flag indicating a synced work item that is being flushed. + * + * Accessed via k_work_busy_get(). May co-occur with other flags. + */ + K_WORK_FLUSHING = BIT(K_WORK_FLUSHING_BIT), +}; + +/** + * @brief A structure used to submit work. + * + * All the members are internal and should not be accessed directly. + */ +struct k_work { +/** + * @cond INTERNAL_HIDDEN + */ + /* All fields are protected by the work module spinlock. */ + + /* Node to link into k_work_q pending list. */ + sys_snode_t node; + + /* The function to be invoked by the work queue thread. */ + k_work_handler_t handler; + + /* The queue on which the work item was last submitted. */ + struct k_work_q *queue; + + /* State of the work item. + * + * The item can be DELAYED, QUEUED, and RUNNING simultaneously. + * + * It can be RUNNING and CANCELING simultaneously. + */ + uint32_t flags; +/** + * INTERNAL_HIDDEN @endcond + */ +}; + +/** + * @cond INTERNAL_HIDDEN + */ +#define Z_WORK_INITIALIZER(work_handler) { \ + .handler = (work_handler), \ +} +/** + * INTERNAL_HIDDEN @endcond + */ + +/** + * @brief A structure used to submit work after a delay. + * + * All the members are internal and should not be accessed directly. + */ +struct k_work_delayable { +/** + * @cond INTERNAL_HIDDEN + */ + /* The work item. */ + struct k_work work; + + /* Timeout used to submit work after a delay. */ + struct _timeout timeout; + + /* The queue to which the work should be submitted. */ + struct k_work_q *queue; +/** + * INTERNAL_HIDDEN @endcond + */ +}; + +/** + * @cond INTERNAL_HIDDEN + */ +#define Z_WORK_DELAYABLE_INITIALIZER(work_handler) { \ + .work = { \ + .handler = (work_handler), \ + .flags = K_WORK_DELAYABLE, \ + }, \ +} +/** + * INTERNAL_HIDDEN @endcond + */ + +/** + * @brief Initialize a statically-defined delayable work item. + * + * This macro can be used to initialize a statically-defined delayable + * work item, prior to its first use. For example, + * + * @code static K_WORK_DELAYABLE_DEFINE(, ); @endcode + * + * Note that if the runtime dependencies support initialization with + * k_work_init_delayable() using that will eliminate the initialized + * object in ROM that is produced by this macro and copied in at + * system startup. + * + * @param work Symbol name for delayable work item object + * @param work_handler Function to invoke each time work item is processed. + */ +#define K_WORK_DELAYABLE_DEFINE(work, work_handler) \ + struct k_work_delayable work \ + = Z_WORK_DELAYABLE_INITIALIZER(work_handler) + +/** + * @cond INTERNAL_HIDDEN + */ +/* Record used to wait for work to flush. + * + * The work item is inserted into the queue that will process (or is + * processing) the item, and will be processed as soon as the item + * completes. When the flusher is processed the semaphore will be + * signaled, releasing the thread waiting for the flush. + */ +struct z_work_flusher { + struct k_work work; + struct k_sem sem; +}; + +/* Record used to wait for work to complete a cancellation. + * + * The work item is inserted into a global queue of pending cancels. + * When a cancelling work item goes idle any matching waiters are + * removed from pending_cancels and are woken. + */ +struct z_work_canceller { + sys_snode_t node; + struct k_work *work; + struct k_sem sem; +}; +/** + * INTERNAL_HIDDEN @endcond + */ + +/** @brief A structure holding internal state for a pending synchronous + * operation on a work item or queue. + * + * Instances of this type are provided by the caller for invocation of + * k_work_flush(), k_work_cancel_sync() and sibling flush and cancel APIs. A + * referenced object must persist until the call returns, and be accessible + * from both the caller thread and the work queue thread. + * + * @note If CONFIG_KERNEL_COHERENCE is enabled the object must be allocated in + * coherent memory; see sys_cache_is_mem_coherent(). The stack on these + * architectures is generally not coherent. be stack-allocated. Violations are + * detected by runtime assertion. + * + * All the members are internal and should not be accessed directly. + */ +struct k_work_sync { +/** + * @cond INTERNAL_HIDDEN + */ + union { + struct z_work_flusher flusher; + struct z_work_canceller canceller; + }; +/** + * INTERNAL_HIDDEN @endcond + */ +}; + +/** @brief A structure holding optional configuration items for a work + * queue. + * + * This structure, and values it references, are not retained by + * k_work_queue_start(). + */ +struct k_work_queue_config { + /** The name to be given to the work queue thread. + * + * If left null the thread will not have a name. + */ + const char *name; + + /** Control whether the work queue thread should yield between + * items. + * + * Yielding between items helps guarantee the work queue + * thread does not starve other threads, including cooperative + * ones released by a work item. This is the default behavior. + * + * Set this to @c true to prevent the work queue thread from + * yielding between items. This may be appropriate when a + * sequence of items should complete without yielding + * control. + */ + bool no_yield; + + /** Control whether the work queue thread should be marked as + * essential thread. + */ + bool essential; + + /** Controls whether work queue monitors work timeouts. + * + * If non-zero, and CONFIG_WORKQUEUE_WORK_TIMEOUT is enabled, + * the work queue will monitor the duration of each work item. + * If the work item handler takes longer than the specified + * time to execute, the work queue thread will be aborted, and + * an error will be logged if CONFIG_LOG is enabled. + */ + uint32_t work_timeout_ms; +}; + +/** + * @brief Kernel workqueue structure + * + * All the members are internal and should not be accessed directly. + */ +struct k_work_q { +/** + * @cond INTERNAL_HIDDEN + */ + /* The thread that animates the work. */ + __deprecated struct k_thread thread; + + /* The thread ID that animates the work. This may be an external thread + * if k_work_queue_run() is used. + */ + k_tid_t thread_id; + + /* All the following fields must be accessed only while the + * work module spinlock is held. + */ + + /* List of k_work items to be worked. */ + sys_slist_t pending; + + /* Wait queue for idle work thread. */ + _wait_q_t notifyq; + + /* Wait queue for threads waiting for the queue to drain. */ + _wait_q_t drainq; + + /* Flags describing queue state. */ + uint32_t flags; + +#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) + struct _timeout work_timeout_record; + struct k_work *work; + k_timeout_t work_timeout; + bool finished; +#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */ +/** + * INTERNAL_HIDDEN @endcond + */ +}; + + +extern struct k_work_q k_sys_work_q; +void k_work_init(struct k_work *,k_work_handler_t); +void k_work_init_delayable(struct k_work_delayable *,k_work_handler_t); +void k_work_queue_init(struct k_work_q *); +void k_work_queue_start(struct k_work_q *,k_thread_stack_t *,size_t,int,const struct k_work_queue_config *); +int k_work_submit_to_queue(struct k_work_q *,struct k_work *); +int k_work_reschedule(struct k_work_delayable *,k_timeout_t); +int k_work_cancel_delayable(struct k_work_delayable *); +bool k_work_cancel_delayable_sync(struct k_work_delayable *,struct k_work_sync *); +static inline int k_work_delayable_remaining_get(const struct k_work_delayable *w) { return z_timeout_remaining(&w->timeout); } + +void port_cancel_enter(struct k_work_delayable *,struct k_work_sync *); +void port_cancel_exit(struct k_work_delayable *,struct k_work_sync *,bool); +#undef SYS_PORT_TRACING_OBJ_FUNC_ENTER +#define SYS_PORT_TRACING_OBJ_FUNC_ENTER(obj,fn,...) PORT_ENTER_##fn(__VA_ARGS__) +#define PORT_ENTER_cancel(...) ((void)0) +#define PORT_ENTER_cancel_delayable(...) ((void)0) +#define PORT_ENTER_cancel_delayable_sync(...) port_cancel_enter(__VA_ARGS__) +#define PORT_ENTER_cancel_sync(...) ((void)0) +#define PORT_ENTER_drain(...) ((void)0) +#define PORT_ENTER_flush(...) ((void)0) +#define PORT_ENTER_flush_delayable(...) ((void)0) +#define PORT_ENTER_reschedule(...) ((void)0) +#define PORT_ENTER_reschedule_for_queue(...) ((void)0) +#define PORT_ENTER_schedule(...) ((void)0) +#define PORT_ENTER_schedule_for_queue(...) ((void)0) +#define PORT_ENTER_start(...) ((void)0) +#define PORT_ENTER_stop(...) ((void)0) +#define PORT_ENTER_submit(...) ((void)0) +#define PORT_ENTER_submit_to_queue(...) ((void)0) +#define PORT_ENTER_unplug(...) ((void)0) +#undef SYS_PORT_TRACING_OBJ_FUNC_EXIT +#define SYS_PORT_TRACING_OBJ_FUNC_EXIT(obj,fn,...) PORT_EXIT_##fn(__VA_ARGS__) +#define PORT_EXIT_cancel(...) ((void)0) +#define PORT_EXIT_cancel_delayable(...) ((void)0) +#define PORT_EXIT_cancel_delayable_sync(...) port_cancel_exit(__VA_ARGS__) +#define PORT_EXIT_cancel_sync(...) ((void)0) +#define PORT_EXIT_drain(...) ((void)0) +#define PORT_EXIT_flush(...) ((void)0) +#define PORT_EXIT_flush_delayable(...) ((void)0) +#define PORT_EXIT_reschedule(...) ((void)0) +#define PORT_EXIT_reschedule_for_queue(...) ((void)0) +#define PORT_EXIT_schedule(...) ((void)0) +#define PORT_EXIT_schedule_for_queue(...) ((void)0) +#define PORT_EXIT_start(...) ((void)0) +#define PORT_EXIT_stop(...) ((void)0) +#define PORT_EXIT_submit(...) ((void)0) +#define PORT_EXIT_submit_to_queue(...) ((void)0) +#define PORT_EXIT_unplug(...) ((void)0) diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/logging/log.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/logging/log.h new file mode 100644 index 00000000..4d99036c --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/logging/log.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#define LOG_MODULE_DECLARE(...) +#define LOG_MODULE_REGISTER(...) +void port_log(void); +#define LOG_ERR(...) port_log() +#define LOG_DBG(...) ((void)0) +#define LOG_INF(...) ((void)0) +#define LOG_LEVEL_DBG 4 diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/net/socket.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/net/socket.h new file mode 100644 index 00000000..111307d9 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/net/socket.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include +typedef int net_socklen_t; +typedef int net_sa_family_t; +struct net_sockaddr { int sa_family; }; +struct net_sockaddr_storage { int ss_family; }; +struct net_sockaddr_in { int sin_family, sin_port, sin_addr; }; +struct net_sockaddr_in6 { int sin6_family, sin6_port, sin6_addr; }; +struct zsock_pollfd { int fd; short events, revents; }; +#define NET_AF_INET 2 +#define NET_AF_INET6 10 +#define NET_SOCK_STREAM 1 +#define NET_IPPROTO_TCP 6 +#define NET_INADDR_ANY_INIT 0 +#define NET_IN6ADDR_ANY_INIT 0 +#define ZSOCK_MSG_DONTWAIT 64 +#define ZSOCK_POLLIN 1 +#define ZSOCK_POLLERR 8 +#define ZSOCK_POLLNVAL 32 +#define ZSOCK_SOL_SOCKET 1 +#define ZSOCK_SO_ERROR 4 +#define ZSOCK_SHUT_RD 0 +#define ZSOCK_SHUT_WR 1 +#define ZSOCK_SHUT_RDWR 2 +#define net_htons(n) (n) +#define net_sad(p) ((struct net_sockaddr *)(p)) +int zsock_socket(int,int,int); +int zsock_bind(int,struct net_sockaddr *,net_socklen_t); +int zsock_listen(int,int); +int zsock_accept(int,struct net_sockaddr *,net_socklen_t *); +int zsock_send(int,const void *,size_t,int); +int zsock_recv(int,void *,size_t,int); +int zsock_close(int); +int zsock_shutdown(int,int); +int zsock_getsockopt(int,int,int,void *,net_socklen_t *); diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/net/socket_service.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/net/socket_service.h new file mode 100644 index 00000000..28e47d66 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/net/socket_service.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include +struct net_socket_service_event { struct zsock_pollfd event; }; +struct net_socket_service { void (*cb)(struct net_socket_service_event *); }; +#define NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(n,fn,count) static const struct net_socket_service n = {fn} +int net_socket_service_register(const struct net_socket_service *,struct zsock_pollfd *,size_t,void *); +int net_socket_service_unregister(const struct net_socket_service *); diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/shell/shell.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/shell/shell.h new file mode 100644 index 00000000..e2316061 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/shell/shell.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include +struct shell_transport; +typedef void (*shell_transport_handler_t)(int,void *); +struct shell_transport_api { + int (*init)(const struct shell_transport *,const void *,shell_transport_handler_t,void *); + int (*uninit)(const struct shell_transport *); + int (*enable)(const struct shell_transport *,bool); + int (*write)(const struct shell_transport *,const void *,size_t,size_t *); + int (*read)(const struct shell_transport *,void *,size_t,size_t *); +}; +struct shell_transport { const struct shell_transport_api *api; void *ctx; }; +struct shell { struct shell_transport *transport; }; +struct shell_backend_config_flags { int unused; }; +#define SHELL_DEFINE(n,p,t,...) static struct shell n = {t} +#define SHELL_FLAG_OLF_CRLF 0 +#define SHELL_DEFAULT_BACKEND_CONFIG_FLAGS {0} +#define SHELL_TRANSPORT_EVT_RX_RDY 1 +#define SHELL_TRANSPORT_EVT_TX_RDY 2 +int shell_echo_set(const struct shell *,bool); +int shell_init(const struct shell *,const void *,struct shell_backend_config_flags,bool,uint32_t); diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/spinlock.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/spinlock.h new file mode 100644 index 00000000..39df0c8e --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/spinlock.h @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/list_gen.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/list_gen.h new file mode 100644 index 00000000..90fa69fd --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/list_gen.h @@ -0,0 +1,279 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Internal generic linked-list macro generators shared by slist/dlist/sflist. + */ + +#ifndef ZEPHYR_INCLUDE_SYS_LIST_GEN_H_ +#define ZEPHYR_INCLUDE_SYS_LIST_GEN_H_ + +#include +#include +#include + +#define Z_GENLIST_FOR_EACH_NODE(__lname, __l, __sn) \ + for ((__sn) = sys_ ## __lname ## _peek_head(__l); (__sn) != NULL; \ + (__sn) = sys_ ## __lname ## _peek_next(__sn)) + + +#define Z_GENLIST_ITERATE_FROM_NODE(__lname, __l, __sn) \ + for ((__sn) = (__sn) ? sys_ ## __lname ## _peek_next_no_check(__sn) \ + : sys_ ## __lname ## _peek_head(__l); \ + (__sn) != NULL; \ + (__sn) = sys_ ## __lname ## _peek_next(__sn)) + +#define Z_GENLIST_FOR_EACH_NODE_SAFE(__lname, __l, __sn, __sns) \ + for ((__sn) = sys_ ## __lname ## _peek_head(__l), \ + (__sns) = sys_ ## __lname ## _peek_next(__sn); \ + (__sn) != NULL ; (__sn) = (__sns), \ + (__sns) = sys_ ## __lname ## _peek_next(__sn)) + +#define Z_GENLIST_CONTAINER(__ln, __cn, __n) \ + ((__ln) ? CONTAINER_OF((__ln), __typeof__(*(__cn)), __n) : NULL) + +#define Z_GENLIST_PEEK_HEAD_CONTAINER(__lname, __l, __cn, __n) \ + Z_GENLIST_CONTAINER(sys_ ## __lname ## _peek_head(__l), __cn, __n) + +#define Z_GENLIST_PEEK_TAIL_CONTAINER(__lname, __l, __cn, __n) \ + Z_GENLIST_CONTAINER(sys_ ## __lname ## _peek_tail(__l), __cn, __n) + +#define Z_GENLIST_PEEK_NEXT_CONTAINER(__lname, __cn, __n) \ + ((__cn) ? Z_GENLIST_CONTAINER( \ + sys_ ## __lname ## _peek_next(&((__cn)->__n)), \ + __cn, __n) : NULL) + +#define Z_GENLIST_FOR_EACH_CONTAINER(__lname, __l, __cn, __n) \ + for ((__cn) = Z_GENLIST_PEEK_HEAD_CONTAINER(__lname, __l, __cn, \ + __n); \ + (__cn) != NULL; \ + (__cn) = Z_GENLIST_PEEK_NEXT_CONTAINER(__lname, __cn, __n)) + +#define Z_GENLIST_FOR_EACH_CONTAINER_SAFE(__lname, __l, __cn, __cns, __n) \ + for ((__cn) = Z_GENLIST_PEEK_HEAD_CONTAINER(__lname, __l, __cn, __n), \ + (__cns) = Z_GENLIST_PEEK_NEXT_CONTAINER(__lname, __cn, __n); \ + (__cn) != NULL; (__cn) = (__cns), \ + (__cns) = Z_GENLIST_PEEK_NEXT_CONTAINER(__lname, __cn, __n)) + +#define Z_GENLIST_IS_EMPTY(__lname) \ + static inline bool \ + sys_ ## __lname ## _is_empty(const sys_ ## __lname ## _t *list) \ + { \ + return (sys_ ## __lname ## _peek_head(list) == NULL); \ + } + +#define Z_GENLIST_PEEK_NEXT_NO_CHECK(__lname, __nname) \ + static inline sys_ ## __nname ## _t * \ + sys_ ## __lname ## _peek_next_no_check(const sys_ ## __nname ## _t *node) \ + { \ + return z_ ## __nname ## _next_peek(node); \ + } + +#define Z_GENLIST_PEEK_NEXT(__lname, __nname) \ + static inline sys_ ## __nname ## _t * \ + sys_ ## __lname ## _peek_next(const sys_ ## __nname ## _t *node) \ + { \ + return (node != NULL) ? \ + sys_ ## __lname ## _peek_next_no_check(node) : \ + NULL; \ + } + +#define Z_GENLIST_PREPEND(__lname, __nname) \ + static inline void \ + sys_ ## __lname ## _prepend(sys_ ## __lname ## _t *list, \ + sys_ ## __nname ## _t *node) \ + { \ + z_ ## __nname ## _next_set(node, \ + sys_ ## __lname ## _peek_head(list)); \ + z_ ## __lname ## _head_set(list, node); \ + \ + if (sys_ ## __lname ## _peek_tail(list) == NULL) { \ + z_ ## __lname ## _tail_set(list, \ + sys_ ## __lname ## _peek_head(list)); \ + } \ + } + +#define Z_GENLIST_APPEND(__lname, __nname) \ + static inline void \ + sys_ ## __lname ## _append(sys_ ## __lname ## _t *list, \ + sys_ ## __nname ## _t *node) \ + { \ + z_ ## __nname ## _next_set(node, NULL); \ + \ + if (sys_ ## __lname ## _peek_tail(list) == NULL) { \ + z_ ## __lname ## _tail_set(list, node); \ + z_ ## __lname ## _head_set(list, node); \ + } else { \ + z_ ## __nname ## _next_set( \ + sys_ ## __lname ## _peek_tail(list), \ + node); \ + z_ ## __lname ## _tail_set(list, node); \ + } \ + } + +#define Z_GENLIST_APPEND_LIST(__lname, __nname) \ + static inline void \ + sys_ ## __lname ## _append_list(sys_ ## __lname ## _t *list, \ + void *head, void *tail) \ +{ \ + if (head != NULL && tail != NULL) { \ + if (sys_ ## __lname ## _peek_tail(list) == NULL) { \ + z_ ## __lname ## _head_set(list, \ + (sys_ ## __nname ## _t *)head); \ + } else { \ + z_ ## __nname ## _next_set( \ + sys_ ## __lname ## _peek_tail(list), \ + (sys_ ## __nname ## _t *)head); \ + } \ + z_ ## __lname ## _tail_set(list, \ + (sys_ ## __nname ## _t *)tail); \ + } \ +} + +#define Z_GENLIST_MERGE_LIST(__lname, __nname) \ + static inline void \ + sys_ ## __lname ## _merge_ ## __lname ( \ + sys_ ## __lname ## _t *list, \ + sys_ ## __lname ## _t *list_to_append) \ + { \ + sys_ ## __nname ## _t *head, *tail; \ + head = sys_ ## __lname ## _peek_head(list_to_append); \ + tail = sys_ ## __lname ## _peek_tail(list_to_append); \ + sys_ ## __lname ## _append_list(list, head, tail); \ + sys_ ## __lname ## _init(list_to_append); \ + } + +#define Z_GENLIST_INSERT(__lname, __nname) \ + static inline void \ + sys_ ## __lname ## _insert(sys_ ## __lname ## _t *list, \ + sys_ ## __nname ## _t *prev, \ + sys_ ## __nname ## _t *node) \ + { \ + if (prev == NULL) { \ + sys_ ## __lname ## _prepend(list, node); \ + } else if (z_ ## __nname ## _next_peek(prev) == NULL) { \ + sys_ ## __lname ## _append(list, node); \ + } else { \ + z_ ## __nname ## _next_set(node, \ + z_ ## __nname ## _next_peek(prev)); \ + z_ ## __nname ## _next_set(prev, node); \ + } \ + } + +#define Z_GENLIST_GET_NOT_EMPTY(__lname, __nname) \ + static inline sys_ ## __nname ## _t * \ + sys_ ## __lname ## _get_not_empty(sys_ ## __lname ## _t *list) \ + { \ + sys_ ## __nname ## _t *node = \ + sys_ ## __lname ## _peek_head(list); \ + \ + z_ ## __lname ## _head_set(list, \ + z_ ## __nname ## _next_peek(node)); \ + if (sys_ ## __lname ## _peek_tail(list) == node) { \ + z_ ## __lname ## _tail_set(list, \ + sys_ ## __lname ## _peek_head(list)); \ + } \ + \ + return node; \ + } + +#define Z_GENLIST_GET(__lname, __nname) \ + static inline sys_ ## __nname ## _t * \ + sys_ ## __lname ## _get(sys_ ## __lname ## _t *list) \ + { \ + return sys_ ## __lname ## _is_empty(list) ? NULL : \ + sys_ ## __lname ## _get_not_empty(list); \ + } + +#define Z_GENLIST_REMOVE(__lname, __nname) \ + static inline void \ + sys_ ## __lname ## _remove(sys_ ## __lname ## _t *list, \ + sys_ ## __nname ## _t *prev_node, \ + sys_ ## __nname ## _t *node) \ + { \ + if (prev_node == NULL) { \ + z_ ## __lname ## _head_set(list, \ + z_ ## __nname ## _next_peek(node)); \ + \ + /* Was node also the tail? */ \ + if (sys_ ## __lname ## _peek_tail(list) == node) { \ + z_ ## __lname ## _tail_set(list, \ + sys_ ## __lname ## _peek_head(list)); \ + } \ + } else { \ + z_ ## __nname ## _next_set(prev_node, \ + z_ ## __nname ## _next_peek(node)); \ + \ + /* Was node the tail? */ \ + if (sys_ ## __lname ## _peek_tail(list) == node) { \ + z_ ## __lname ## _tail_set(list, \ + prev_node); \ + } \ + } \ + \ + z_ ## __nname ## _next_set(node, NULL); \ + } + +#define Z_GENLIST_FIND_AND_REMOVE(__lname, __nname) \ + static inline bool \ + sys_ ## __lname ## _find_and_remove(sys_ ## __lname ## _t *list, \ + sys_ ## __nname ## _t *node) \ + { \ + sys_ ## __nname ## _t *prev = NULL; \ + sys_ ## __nname ## _t *test; \ + \ + Z_GENLIST_FOR_EACH_NODE(__lname, list, test) { \ + if (test == node) { \ + sys_ ## __lname ## _remove(list, prev, \ + node); \ + return true; \ + } \ + \ + prev = test; \ + } \ + \ + return false; \ + } + +#define Z_GENLIST_FIND(__lname, __nname) \ + static inline bool sys_##__lname##_find( \ + const sys_##__lname##_t *list, const sys_##__nname##_t *node, \ + sys_##__nname##_t **prev) \ + { \ + sys_##__nname##_t *current = NULL; \ + sys_##__nname##_t *previous = NULL; \ + \ + Z_GENLIST_FOR_EACH_NODE(__lname, list, current) { \ + if (current == node) { \ + if (prev != NULL) { \ + *prev = previous; \ + } \ + return true; \ + } \ + \ + previous = current; \ + } \ + \ + if (prev != NULL) { \ + *prev = previous; \ + } \ + \ + return false; \ + } + +#define Z_GENLIST_LEN(__lname, __nname) \ + static inline size_t sys_##__lname##_len(const sys_##__lname##_t *list) \ + { \ + size_t len = 0; \ + sys_##__nname##_t *node; \ + Z_GENLIST_FOR_EACH_NODE(__lname, list, node) { \ + len++; \ + } \ + return len; \ + } + +#endif /* ZEPHYR_INCLUDE_SYS_LIST_GEN_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/printk.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/printk.h new file mode 100644 index 00000000..39df0c8e --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/printk.h @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/slist.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/slist.h new file mode 100644 index 00000000..713af24e --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/slist.h @@ -0,0 +1,459 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + + /** + * @file + * @brief Header file for the single-linked list API. + * @ingroup single-linked-list_apis + * + * @defgroup single-linked-list_apis Single-linked list + * @since 1.2 + * @version 1.0.0 + * @ingroup datastructure_apis + * + * @brief Single-linked list implementation. + * + * Single-linked list implementation using inline macros/functions. + * This API is not thread safe, and thus if a list is used across threads, + * calls to functions must be protected with synchronization primitives. + * @{ + */ + +#ifndef ZEPHYR_INCLUDE_SYS_SLIST_H_ +#define ZEPHYR_INCLUDE_SYS_SLIST_H_ + +#include +#include +#include "list_gen.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/** @cond INTERNAL_HIDDEN */ +struct _snode { + struct _snode *next; +}; +/** @endcond */ + +/** Single-linked list node structure. */ +typedef struct _snode sys_snode_t; + +/** @cond INTERNAL_HIDDEN */ +struct _slist { + sys_snode_t *head; + sys_snode_t *tail; +}; +/** @endcond */ + +/** Single-linked list structure. */ +typedef struct _slist sys_slist_t; + +/** + * @brief Provide the primitive to iterate on a list + * Note: the loop is unsafe and thus __sn should not be removed + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_FOR_EACH_NODE(l, n) { + * + * } + * + * This and other SYS_SLIST_*() macros are not thread safe. + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __sn A sys_snode_t pointer to peek each node of the list + */ +#define SYS_SLIST_FOR_EACH_NODE(__sl, __sn) \ + Z_GENLIST_FOR_EACH_NODE(slist, __sl, __sn) + +/** + * @brief Provide the primitive to iterate on a list, from a node in the list + * Note: the loop is unsafe and thus __sn should not be removed + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_ITERATE_FROM_NODE(l, n) { + * + * } + * + * Like SYS_SLIST_FOR_EACH_NODE(), but __dn already contains a node in the list + * where to start searching for the next entry from. If NULL, it starts from + * the head. + * + * This and other SYS_SLIST_*() macros are not thread safe. + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __sn A sys_snode_t pointer to peek each node of the list + * it contains the starting node, or NULL to start from the head + */ +#define SYS_SLIST_ITERATE_FROM_NODE(__sl, __sn) \ + Z_GENLIST_ITERATE_FROM_NODE(slist, __sl, __sn) + +/** + * @brief Provide the primitive to safely iterate on a list + * Note: __sn can be removed, it will not break the loop. + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_FOR_EACH_NODE_SAFE(l, n, s) { + * + * } + * + * This and other SYS_SLIST_*() macros are not thread safe. + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __sn A sys_snode_t pointer to peek each node of the list + * @param __sns A sys_snode_t pointer for the loop to run safely + */ +#define SYS_SLIST_FOR_EACH_NODE_SAFE(__sl, __sn, __sns) \ + Z_GENLIST_FOR_EACH_NODE_SAFE(slist, __sl, __sn, __sns) + +/** + * @brief Provide the primitive to resolve the container of a list node + * Note: it is safe to use with NULL pointer nodes + * + * @param __ln A pointer on a sys_node_t to get its container + * @param __cn Container struct type pointer + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_CONTAINER(__ln, __cn, __n) \ + Z_GENLIST_CONTAINER(__ln, __cn, __n) + +/** + * @brief Provide the primitive to peek container of the list head + * + * @param __sl A pointer on a sys_slist_t to peek + * @param __cn Container struct type pointer + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n) \ + Z_GENLIST_PEEK_HEAD_CONTAINER(slist, __sl, __cn, __n) + +/** + * @brief Provide the primitive to peek container of the list tail + * + * @param __sl A pointer on a sys_slist_t to peek + * @param __cn Container struct type pointer + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_PEEK_TAIL_CONTAINER(__sl, __cn, __n) \ + Z_GENLIST_PEEK_TAIL_CONTAINER(slist, __sl, __cn, __n) + +/** + * @brief Provide the primitive to peek the next container + * + * @param __cn Container struct type pointer + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n) \ + Z_GENLIST_PEEK_NEXT_CONTAINER(slist, __cn, __n) + +/** + * @brief Provide the primitive to iterate on a list under a container + * Note: the loop is unsafe and thus __cn should not be detached + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_FOR_EACH_CONTAINER(l, c, n) { + * + * } + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __cn A pointer to peek each entry of the list + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_FOR_EACH_CONTAINER(__sl, __cn, __n) \ + Z_GENLIST_FOR_EACH_CONTAINER(slist, __sl, __cn, __n) + +/** + * @brief Provide the primitive to safely iterate on a list under a container + * Note: __cn can be detached, it will not break the loop. + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_FOR_EACH_NODE_SAFE(l, c, cn, n) { + * + * } + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __cn A pointer to peek each entry of the list + * @param __cns A pointer for the loop to run safely + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_FOR_EACH_CONTAINER_SAFE(__sl, __cn, __cns, __n) \ + Z_GENLIST_FOR_EACH_CONTAINER_SAFE(slist, __sl, __cn, __cns, __n) + + +/* + * Required function definitions for the list_gen.h interface + * + * These are the only functions that do not treat the list/node pointers + * as completely opaque types. + */ + +/** + * @brief Initialize a list + * + * @param list A pointer on the list to initialize + */ +static inline void sys_slist_init(sys_slist_t *list) +{ + list->head = NULL; + list->tail = NULL; +} + +/** + * @brief Statically initialize a single-linked list + * @param ptr_to_list A pointer on the list to initialize + */ +#define SYS_SLIST_STATIC_INIT(ptr_to_list) {NULL, NULL} + +static inline sys_snode_t *z_snode_next_peek(const sys_snode_t *node) +{ + return node->next; +} + +static inline void z_snode_next_set(sys_snode_t *parent, sys_snode_t *child) +{ + parent->next = child; +} + +static inline void z_slist_head_set(sys_slist_t *list, sys_snode_t *node) +{ + list->head = node; +} + +static inline void z_slist_tail_set(sys_slist_t *list, sys_snode_t *node) +{ + list->tail = node; +} + +/** + * @brief Peek the first node from the list + * + * @param list A point on the list to peek the first node from + * + * @return A pointer on the first node of the list (or NULL if none) + */ +static inline sys_snode_t *sys_slist_peek_head(const sys_slist_t *list) +{ + return list->head; +} + +/** + * @brief Peek the last node from the list + * + * @param list A point on the list to peek the last node from + * + * @return A pointer on the last node of the list (or NULL if none) + */ +static inline sys_snode_t *sys_slist_peek_tail(const sys_slist_t *list) +{ + return list->tail; +} + +/* + * Derived, generated APIs + */ + +/** + * @brief Test if the given list is empty + * + * @param list A pointer on the list to test + * + * @return a boolean, true if it's empty, false otherwise + */ +static inline bool sys_slist_is_empty(const sys_slist_t *list); + +Z_GENLIST_IS_EMPTY(slist) + +/** + * @brief Peek the next node from current node, node is not NULL + * + * Faster then sys_slist_peek_next() if node is known not to be NULL. + * + * @param node A pointer on the node where to peek the next node + * + * @return a pointer on the next node (or NULL if none) + */ +static inline sys_snode_t *sys_slist_peek_next_no_check(const sys_snode_t *node); + +Z_GENLIST_PEEK_NEXT_NO_CHECK(slist, snode) + +/** + * @brief Peek the next node from current node + * + * @param node A pointer on the node where to peek the next node + * + * @return a pointer on the next node (or NULL if none) + */ +static inline sys_snode_t *sys_slist_peek_next(const sys_snode_t *node); + +Z_GENLIST_PEEK_NEXT(slist, snode) + +/** + * @brief Prepend a node to the given list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param node A pointer on the node to prepend + */ +static inline void sys_slist_prepend(sys_slist_t *list, + sys_snode_t *node); + +Z_GENLIST_PREPEND(slist, snode) + +/** + * @brief Append a node to the given list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param node A pointer on the node to append + */ +static inline void sys_slist_append(sys_slist_t *list, + sys_snode_t *node); + +Z_GENLIST_APPEND(slist, snode) + +/** + * @brief Append a list to the given list + * + * Append a singly-linked, NULL-terminated list consisting of nodes containing + * the pointer to the next node as the first element of a node, to @a list. + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param head A pointer to the first element of the list to append + * @param tail A pointer to the last element of the list to append + */ +static inline void sys_slist_append_list(sys_slist_t *list, + void *head, void *tail); + +Z_GENLIST_APPEND_LIST(slist, snode) + +/** + * @brief merge two slists, appending the second one to the first + * + * When the operation is completed, the appending list is empty. + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param list_to_append A pointer to the list to append. + */ +static inline void sys_slist_merge_slist(sys_slist_t *list, + sys_slist_t *list_to_append); + +Z_GENLIST_MERGE_LIST(slist, snode) + +/** + * @brief Insert a node to the given list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param prev A pointer on the previous node + * @param node A pointer on the node to insert + */ +static inline void sys_slist_insert(sys_slist_t *list, + sys_snode_t *prev, + sys_snode_t *node); + +Z_GENLIST_INSERT(slist, snode) + +/** + * @brief Fetch and remove the first node of the given list + * + * List must be known to be non-empty. + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * + * @return A pointer to the first node of the list + */ +static inline sys_snode_t *sys_slist_get_not_empty(sys_slist_t *list); + +Z_GENLIST_GET_NOT_EMPTY(slist, snode) + +/** + * @brief Fetch and remove the first node of the given list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * + * @return A pointer to the first node of the list (or NULL if empty) + */ +static inline sys_snode_t *sys_slist_get(sys_slist_t *list); + +Z_GENLIST_GET(slist, snode) + +/** + * @brief Remove a node + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param prev_node A pointer on the previous node + * (can be NULL, which means the node is the list's head) + * @param node A pointer on the node to remove + */ +static inline void sys_slist_remove(sys_slist_t *list, + sys_snode_t *prev_node, + sys_snode_t *node); + +Z_GENLIST_REMOVE(slist, snode) + +/** + * @brief Find and remove a node from a list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param node A pointer on the node to remove from the list + * + * @return true if node was removed + */ +static inline bool sys_slist_find_and_remove(sys_slist_t *list, + sys_snode_t *node); + +/** + * @brief Find if a node is already linked in a singly linked list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer to the list to check + * @param node A pointer to the node to search in the list + * @param[out] prev A pointer to the previous node + * + * @return true if node was found in the list, false otherwise + */ +static inline bool sys_slist_find(const sys_slist_t *list, const sys_snode_t *node, + sys_snode_t **prev); +Z_GENLIST_FIND(slist, snode) + +/** + * @brief Compute the size of the given list in O(n) time + * + * @param list A pointer on the list + * + * @return an integer equal to the size of the list, or 0 if empty + */ +static inline size_t sys_slist_len(const sys_slist_t *list); + +Z_GENLIST_LEN(slist, snode) + +/** @} */ +Z_GENLIST_FIND_AND_REMOVE(slist, snode) + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SYS_SLIST_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/util.h b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/util.h new file mode 100644 index 00000000..80043319 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/port/zephyr/sys/util.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +#pragma once +#include +#define BIT(n) (1U << (n)) +#define CONTAINER_OF(p,t,m) ((t *)((char *)(p)-offsetof(t,m))) +#define ARG_UNUSED(x) (void)(x) +#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) +#define IS_ENABLED(x) (x) diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/subsys/shell/backends/shell_telnet.c b/tasks/runtime-zephyr-telnet-close-handoff/template/subsys/shell/backends/shell_telnet.c new file mode 100644 index 00000000..e1770bcb --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/subsys/shell/backends/shell_telnet.c @@ -0,0 +1,810 @@ +/* + * Copyright (c) 2017 Intel Corporation + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include "shell_telnet_protocol.h" + +SHELL_TELNET_DEFINE(shell_transport_telnet); +SHELL_DEFINE(shell_telnet, CONFIG_SHELL_PROMPT_TELNET, &shell_transport_telnet, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_SIZE, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_TIMEOUT, + SHELL_FLAG_OLF_CRLF); + +LOG_MODULE_REGISTER(shell_telnet, CONFIG_SHELL_TELNET_LOG_LEVEL); + +struct shell_telnet *sh_telnet; + +/* Various definitions mapping the TELNET service configuration options */ +#define TELNET_PORT CONFIG_SHELL_TELNET_PORT +#define TELNET_LINE_SIZE CONFIG_SHELL_TELNET_LINE_BUF_SIZE +#define TELNET_TIMEOUT CONFIG_SHELL_TELNET_SEND_TIMEOUT + +#define TELNET_MIN_COMMAND_LEN 2 +#define TELNET_WILL_DO_COMMAND_LEN 3 + +#define SOCK_ID_IPV4_LISTEN 0 +#define SOCK_ID_IPV6_LISTEN 1 +#define SOCK_ID_CLIENT 2 +#define SOCK_ID_MAX 3 + +/* Basic TELNET implementation. */ + +static void telnet_server_cb(struct net_socket_service_event *evt); +static int telnet_init(struct shell_telnet *ctx); + +NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(telnet_server, telnet_server_cb, + SHELL_TELNET_POLLFD_COUNT); + + +static void telnet_end_client_connection(void) +{ + int ret; + + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + sh_telnet->output_lock = false; + + k_work_cancel_delayable_sync(&sh_telnet->send_work, + &sh_telnet->work_sync); + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + } +} + +static void telnet_command_send_reply(uint8_t *msg, uint16_t len) +{ + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return; + } + + while (len > 0) { + int ret; + + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, 0); + if (ret < 0) { + LOG_ERR("Failed to send command %d, shutting down", ret); + telnet_end_client_connection(); + break; + } + + msg += ret; + len -= ret; + } +} + +static void telnet_reply_ay_command(void) +{ + static const char alive[] = "Zephyr at your service\r\n"; + + telnet_command_send_reply((uint8_t *)alive, strlen(alive)); +} + +static int telnet_echo_set(const struct shell *sh, bool val) +{ + int ret = shell_echo_set(sh_telnet->shell_context, val); + + if (ret < 0) { + LOG_ERR("Failed to set echo to: %d, err: %d", val, ret); + } + return ret; +} + +static void telnet_reply_dont_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, false); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL_NOT; + } else { + cmd->op = NVT_CMD_WILL; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_do_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_SUPR_GA: + cmd->op = NVT_CMD_WILL; + break; + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, true); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL; + } else { + cmd->op = NVT_CMD_WILL_NOT; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_command(struct telnet_simple_command *cmd) +{ + if (!cmd->iac) { + return; + } + + switch (cmd->op) { + case NVT_CMD_AO: + /* OK, no output then */ + sh_telnet->output_lock = true; + sh_telnet->line_out.len = 0; + k_work_cancel_delayable_sync(&sh_telnet->send_work, + &sh_telnet->work_sync); + break; + case NVT_CMD_AYT: + telnet_reply_ay_command(); + break; + case NVT_CMD_DO: + telnet_reply_do_command(cmd); + break; + case NVT_CMD_DO_NOT: + telnet_reply_dont_command(cmd); + break; + default: + LOG_DBG("Operation %u not handled", cmd->op); + break; + } +} + +static int telnet_send(bool block) +{ + int ret; + uint8_t *msg = sh_telnet->line_out.buf; + uint16_t len = sh_telnet->line_out.len; + + if (sh_telnet->line_out.len == 0) { + return 0; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return -ENOTCONN; + } + + while (len > 0) { + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, + block ? 0 : ZSOCK_MSG_DONTWAIT); + if (!block && (ret < 0) && (errno == EAGAIN)) { + /* Not all data was sent - move the remaining data and + * update length. + */ + memmove(sh_telnet->line_out.buf, msg, len); + sh_telnet->line_out.len = len; + return -EAGAIN; + } + + if (ret < 0) { + ret = -errno; + LOG_ERR("Failed to send %d, shutting down", -ret); + telnet_end_client_connection(); + return ret; + } + + msg += ret; + len -= ret; + } + + /* We reinitialize the line buffer */ + sh_telnet->line_out.len = 0; + + return 0; +} + +static void telnet_send_prematurely(struct k_work *work) +{ + int ret; + + /* Use non-blocking send to prevent system workqueue blocking. */ + ret = telnet_send(false); + if (ret == -EAGAIN) { + /* Not all data was sent, reschedule the work. */ + k_work_reschedule(&sh_telnet->send_work, K_MSEC(TELNET_TIMEOUT)); + } +} + +static int telnet_command_length(uint8_t op) +{ + if (op == NVT_CMD_SB || op == NVT_CMD_WILL || op == NVT_CMD_WILL_NOT || + op == NVT_CMD_DO || op == NVT_CMD_DO_NOT) { + return TELNET_WILL_DO_COMMAND_LEN; + } + + return TELNET_MIN_COMMAND_LEN; +} + +static inline int telnet_handle_command(struct telnet_simple_command *cmd) +{ + /* Commands are two or three bytes. */ + if (cmd->iac != NVT_CMD_IAC) { + return 0; + } + + if (IS_ENABLED(CONFIG_SHELL_TELNET_SUPPORT_COMMAND)) { + LOG_DBG("Got a command %u/%u/%u", cmd->iac, cmd->op, cmd->opt); + telnet_reply_command(cmd); + } + + if (cmd->op == NVT_CMD_SB) { + /* TODO Add subnegotiation support. */ + return -EOPNOTSUPP; + } + + return 0; +} + +static void telnet_recv(struct zsock_pollfd *pollfd) +{ + struct telnet_simple_command *cmd = + (struct telnet_simple_command *)sh_telnet->cmd_buf; + size_t len, off, buf_left, cmd_total_len; + uint8_t *buf; + int ret; + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + buf_left = sizeof(sh_telnet->rx_buf) - sh_telnet->rx_len; + if (buf_left == 0) { + /* No space left to read TCP stream, try again later. */ + k_mutex_unlock(&sh_telnet->rx_lock); + k_msleep(10); + return; + } + + buf = sh_telnet->rx_buf + sh_telnet->rx_len; + + ret = zsock_recv(pollfd->fd, buf, buf_left, 0); + if (ret < 0) { + LOG_DBG("Telnet client error %d", ret); + goto error; + } else if (ret == 0) { + LOG_DBG("Telnet client closed connection"); + goto error; + } + + off = 0; + len = ret; + cmd_total_len = 0; + /* Filter out and process commands from the data buffer. */ + while (off < len) { + if (sh_telnet->cmd_len > 0) { + /* Command mode */ + if (sh_telnet->cmd_len == 1) { + /* Operation */ + cmd->op = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + + if (telnet_command_length(cmd->op) > + TELNET_MIN_COMMAND_LEN) { + continue; + } + } else if (sh_telnet->cmd_len == 2) { + /* Option */ + cmd->opt = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + } + + ret = telnet_handle_command(cmd); + if (ret < 0) { + goto error; + } else { + LOG_DBG("Handled command"); + } + + memset(cmd, 0, sizeof(*cmd)); + sh_telnet->cmd_len = 0; + + continue; + } + + if (*(buf + off) == NVT_CMD_IAC) { + cmd->iac = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + continue; + } + + /* Data byte, remove command bytes from the buffer, if any. */ + if (cmd_total_len > 0) { + size_t data_off = off; + + off -= cmd_total_len; + len -= cmd_total_len; + cmd_total_len = 0; + + memmove(buf + off, buf + data_off, len); + } + + off++; + } + + if (cmd_total_len > 0) { + /* In case the buffer ended with command, trim the buffer size + * here. + */ + len -= cmd_total_len; + } + + if (len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + return; + } + + sh_telnet->rx_len += len; + + k_mutex_unlock(&sh_telnet->rx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, + sh_telnet->shell_context); + + return; + +error: + k_mutex_unlock(&sh_telnet->rx_lock); + telnet_end_client_connection(); +} + +static void telnet_restart_server(void) +{ + int ret; + + if (sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + } + + ret = telnet_init(sh_telnet); + if (ret < 0) { + LOG_ERR("Telnet fatal error, failed to restart server (%d)", ret); + (void)net_socket_service_unregister(&telnet_server); + } +} + +static void telnet_accept(struct zsock_pollfd *pollfd) +{ + int sock, ret = 0; + struct net_sockaddr_storage addr = {}; + net_socklen_t addrlen = sizeof(addr); + + sock = zsock_accept(pollfd->fd, net_sad(&addr), &addrlen); + if (sock < 0) { + ret = -errno; + LOG_ERR("Telnet accept error (%d)", ret); + return; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd > 0) { + /* Too many connections. */ + ret = 0; + LOG_ERR("Telnet client already connected."); + goto error; + } + + sh_telnet->fds[SOCK_ID_CLIENT].fd = sock; + sh_telnet->fds[SOCK_ID_CLIENT].events = ZSOCK_POLLIN; + sh_telnet->rx_len = 0; + sh_telnet->cmd_len = 0; + sh_telnet->line_out.len = 0; + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, (%d)", ret); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + goto error; + } + + LOG_DBG("Telnet client connected (family NET_AF_INET%s)", + addr.ss_family == NET_AF_INET ? "" : "6"); + + /* Disable echo - if command handling is enabled we reply that we + * support echo. + */ + (void)telnet_echo_set(sh_telnet->shell_context, false); + + return; + +error: + if (sock > 0) { + (void)zsock_close(sock); + } + + if (ret < 0) { + telnet_restart_server(); + } +} + +static void telnet_server_cb(struct net_socket_service_event *evt) +{ + int sock_error; + int ret; + net_socklen_t optlen = sizeof(int); + + if (sh_telnet == NULL) { + return; + } + + if ((evt->event.revents & ZSOCK_POLLERR) || + (evt->event.revents & ZSOCK_POLLNVAL)) { + (void)zsock_getsockopt(evt->event.fd, ZSOCK_SOL_SOCKET, + ZSOCK_SO_ERROR, &sock_error, &optlen); + + ret = -sock_error; + + if (ret == -ENETDOWN) { + LOG_INF("Network is down"); + } else { + LOG_ERR("Telnet socket %d error (%d)", evt->event.fd, ret); + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_end_client_connection(); + return; + } + + goto error; + } + + if (!(evt->event.revents & ZSOCK_POLLIN)) { + return; + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV4_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV6_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_recv(&sh_telnet->fds[SOCK_ID_CLIENT]); + return; + } + + LOG_ERR("Unexpected FD received for telnet, restarting service."); + +error: + telnet_restart_server(); +} + +static int telnet_setup_server(struct zsock_pollfd *pollfd, net_sa_family_t family, + struct net_sockaddr *addr, net_socklen_t addrlen) +{ + int ret = 0; + + pollfd->fd = zsock_socket(family, NET_SOCK_STREAM, NET_IPPROTO_TCP); + if (pollfd->fd < 0) { + ret = -errno; + LOG_ERR("Failed to create telnet NET_AF_INET%s socket", + family == NET_AF_INET ? "" : "6"); + goto error; + } + + if (zsock_bind(pollfd->fd, addr, addrlen) < 0) { + ret = -errno; + LOG_ERR("Cannot bind on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + if (zsock_listen(pollfd->fd, 1)) { + ret = -errno; + LOG_ERR("Cannot listen on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + pollfd->events = ZSOCK_POLLIN; + + LOG_DBG("Telnet console enabled on NET_AF_INET%s", + family == NET_AF_INET ? "" : "6"); + + return 0; + +error: + LOG_ERR("Unable to start telnet on NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + + if (pollfd->fd >= 0) { + (void)zsock_close(pollfd->fd); + pollfd->fd = -1; + } + + return ret; +} + +static int telnet_init(struct shell_telnet *ctx) +{ + int ret; + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + struct net_sockaddr_in any_addr4 = { + .sin_family = NET_AF_INET, + .sin_port = net_htons(TELNET_PORT), + .sin_addr = NET_INADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV4_LISTEN], + NET_AF_INET, (struct net_sockaddr *)&any_addr4, + sizeof(any_addr4)); + if (ret < 0) { + goto error; + } + } + + if (IS_ENABLED(CONFIG_NET_IPV6)) { + struct net_sockaddr_in6 any_addr6 = { + .sin6_family = NET_AF_INET6, + .sin6_port = net_htons(TELNET_PORT), + .sin6_addr = NET_IN6ADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV6_LISTEN], + NET_AF_INET6, (struct net_sockaddr *)&any_addr6, + sizeof(any_addr6)); + if (ret < 0) { + goto error; + } + } + + ret = net_socket_service_register(&telnet_server, ctx->fds, + ARRAY_SIZE(ctx->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + goto error; + } + + LOG_INF("Telnet shell backend initialized"); + + return 0; + +error: + if (ctx->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV4_LISTEN].fd); + ctx->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (ctx->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV6_LISTEN].fd); + ctx->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + return ret; +} + +/* Shell API */ + +static int init(const struct shell_transport *transport, + const void *config, + shell_transport_handler_t evt_handler, + void *context) +{ + int err; + + sh_telnet = (struct shell_telnet *)transport->ctx; + + memset(sh_telnet, 0, sizeof(struct shell_telnet)); + for (int i = 0; i < ARRAY_SIZE(sh_telnet->fds); i++) { + sh_telnet->fds[i].fd = -1; + } + + sh_telnet->shell_handler = evt_handler; + sh_telnet->shell_context = context; + + err = telnet_init(sh_telnet); + if (err != 0) { + return err; + } + + k_work_init_delayable(&sh_telnet->send_work, telnet_send_prematurely); + k_mutex_init(&sh_telnet->rx_lock); + + return 0; +} + +static int uninit(const struct shell_transport *transport) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int enable(const struct shell_transport *transport, bool blocking) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int telnet_write(const struct shell_transport *transport, + const void *data, size_t length, size_t *cnt) +{ + struct shell_telnet_line_buf *lb; + size_t copy_len; + int err; + uint32_t timeout; + bool was_running; + + if (sh_telnet == NULL) { + *cnt = 0; + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0 || sh_telnet->output_lock) { + *cnt = length; + return 0; + } + + *cnt = 0; + lb = &sh_telnet->line_out; + + /* Stop the transmission timer, so it does not interrupt the operation. + */ + timeout = k_ticks_to_ms_ceil32( + k_work_delayable_remaining_get(&sh_telnet->send_work)); + was_running = k_work_cancel_delayable_sync(&sh_telnet->send_work, + &sh_telnet->work_sync); + + do { + if (lb->len + length - *cnt > TELNET_LINE_SIZE) { + copy_len = TELNET_LINE_SIZE - lb->len; + } else { + copy_len = length - *cnt; + } + + memcpy(lb->buf + lb->len, (uint8_t *)data + *cnt, copy_len); + lb->len += copy_len; + + /* Send the data immediately if the buffer is full or line feed + * is recognized. + */ + if (lb->buf[lb->len - 1] == '\n' || + lb->len == TELNET_LINE_SIZE) { + err = telnet_send(true); + if (err != 0) { + *cnt = length; + if ((err == -ENOTCONN) || (err == -ENETDOWN)) { + LOG_ERR("Network disconnected, shutting down"); + } else { + LOG_ERR("Error %d, shutting down", err); + } + return 0; /* Return 0 to not trigger ASSERT in shell_ops.c */ + } + } + + *cnt += copy_len; + } while (*cnt < length); + + if (lb->len > 0) { + /* Check if the timer was already running, initialize otherwise. + */ + timeout = was_running ? timeout : TELNET_TIMEOUT; + + k_work_reschedule(&sh_telnet->send_work, K_MSEC(timeout)); + } + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_TX_RDY, + sh_telnet->shell_context); + + return 0; +} + +static int telnet_read(const struct shell_transport *transport, + void *data, size_t length, size_t *cnt) +{ + size_t read_len; + + if (sh_telnet == NULL) { + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + goto no_data; + } + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + if (sh_telnet->rx_len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + goto no_data; + } + + read_len = sh_telnet->rx_len; + if (read_len > length) { + read_len = length; + } + + memcpy(data, sh_telnet->rx_buf, read_len); + *cnt = read_len; + + sh_telnet->rx_len -= read_len; + if (sh_telnet->rx_len > 0) { + memmove(sh_telnet->rx_buf, sh_telnet->rx_buf + read_len, + sh_telnet->rx_len); + } + + k_mutex_unlock(&sh_telnet->rx_lock); + + return 0; + +no_data: + *cnt = 0; + return 0; +} + +const struct shell_transport_api shell_telnet_transport_api = { + .init = init, + .uninit = uninit, + .enable = enable, + .write = telnet_write, + .read = telnet_read +}; + +static int enable_shell_telnet(void) +{ + bool log_backend = CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > 0; + uint32_t level = (CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ? + CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_TELNET_INIT_LOG_LEVEL; + static const struct shell_backend_config_flags cfg_flags = + SHELL_DEFAULT_BACKEND_CONFIG_FLAGS; + + return shell_init(&shell_telnet, NULL, cfg_flags, log_backend, level); +} + +SYS_INIT(enable_shell_telnet, APPLICATION, CONFIG_SHELL_TELNET_INIT_PRIORITY); + +const struct shell *shell_backend_telnet_get_ptr(void) +{ + return &shell_telnet; +} diff --git a/tasks/runtime-zephyr-telnet-close-handoff/template/subsys/shell/backends/shell_telnet_protocol.h b/tasks/runtime-zephyr-telnet-close-handoff/template/subsys/shell/backends/shell_telnet_protocol.h new file mode 100644 index 00000000..b2ffeff9 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/template/subsys/shell/backends/shell_telnet_protocol.h @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Telnet console protocol specific defines + * + * + * This defines the Telnet codes, all prefixed as NVT_ + * (NVT: Network Virtual Terminal, see rfc854) + */ + +#ifndef SHELL_TELNET_PROTOCOL_H__ +#define SHELL_TELNET_PROTOCOL_H__ + +/** Printer/Keyboard codes */ + +/* Mandatory ones */ +#define NVT_NUL 0 +#define NVT_LF 10 +#define NVT_CR 13 + +/* Optional ones */ +#define NVT_BEL 7 +#define NVT_BS 8 +#define NVT_HT 9 +#define NVT_VT 11 +#define NVT_FF 12 + +/* Telnet commands */ +#define NVT_CMD_SE 240 +#define NVT_CMD_NOP 241 +#define NVT_CMD_DM 242 +#define NVT_CMD_BRK 243 +#define NVT_CMD_IP 244 +#define NVT_CMD_AO 245 +#define NVT_CMD_AYT 246 +#define NVT_CMD_EC 247 +#define NVT_CMD_EL 248 +#define NVT_CMD_GA 249 +#define NVT_CMD_SB 250 +#define NVT_CMD_WILL 251 +#define NVT_CMD_WILL_NOT 252 +#define NVT_CMD_DO 253 +#define NVT_CMD_DO_NOT 254 +#define NVT_CMD_IAC 255 + +/* Telnet options */ +#define NVT_OPT_TX_BIN 0 +#define NVT_OPT_ECHO 1 +#define NVT_OPT_RECONNECT 2 +#define NVT_OPT_SUPR_GA 3 +#define NVT_OPT_MSG_SZ_NEG 4 +#define NVT_OPT_STATUS 5 +#define NVT_OPT_TIMING_MARK 6 +#define NVT_OPT_REMOTE_CTRL_TRANS_ECHO 7 +#define NVT_OPT_OUT_LINE_WIDTH 8 +#define NVT_OPT_OUT_PAGE_SZ 9 +#define NVT_OPT_NEG_CR 10 +#define NVT_OPT_NEG_HT 11 +#define NVT_OPT_NAOHTD 12 +#define NVT_OPT_NEG_OUT_FF 13 +#define NVT_OPT_NEG_VT 14 +#define NVT_OPT_NEG_OUT_VT 15 +#define NVT_OPT_NET_OUT_LF 16 +#define NVT_OPT_EXT_ASCII 17 +#define NVT_OPT_LOGOUT 18 +#define NVT_OPT_BYTE_MACRO 19 +#define NVT_OPT_DATA_ENTRY 20 +#define NVT_OPT_SUPDUP 21 +#define NVT_OPT_SUPDUP_OUT 22 +#define NVT_OPT_SEND_LOC 23 +#define NVT_OPT_TERM_TYPE 24 +#define NVT_OPT_EOR 25 +#define NVT_OPT_TACACS_UID 26 +#define NVT_OPT_OUT_MARK 27 +#define NVT_OPT_TTYLOC 28 +#define NVT_OPT_3270 29 +#define NVT_OPT_X_3_PAD 30 +#define NVT_OPT_NAWS 31 +#define NVT_OPT_TERM_SPEED 32 +#define NVT_OPT_REMOTE_FC 33 +#define NVT_OPT_LINEMODE 34 +#define NVT_OPT_X_LOC 35 +#define NVT_OPT_ENV 36 +#define NVT_OPT_AUTH 37 +#define NVT_OPT_ENCRYPT_OPT 38 +#define NVT_OPT_NEW_ENV 39 +#define NVT_OPT_TN3270E 40 +#define NVT_OPT_XAUTH 41 +#define NVT_OPT_CHARSET 42 +#define NVT_OPT_RSP 43 +#define NVT_OPT_COM_PORT_CTRL 44 +#define NVT_OPT_SUPR_LOCAL_ECHO 45 +#define NVT_OPT_START_TLS 46 +#define NVT_OPT_KERMIT 47 +#define NVT_OPT_SEND_URL 48 +#define NVT_OPT_FORWARD_X 49 +#define NVT_OPT_PRAGMA_LOGON 138 +#define NVT_OPT_SSPI_LOGON 139 +#define NVT_OPT_PRAGMA_HB 140 +#define NVT_OPT_EXT_OPT_LIST 255 + +/** Describes a telnet command */ +struct telnet_simple_command { + /** Mandatory IAC code */ + uint8_t iac; + /** Type of operation (see Telnet commands above) */ + uint8_t op; + /** Option code */ + uint8_t opt; +}; + +static inline void telnet_command_cpy(struct telnet_simple_command *dst, + struct telnet_simple_command *src) +{ + dst->iac = src->iac; + dst->op = src->op; + dst->opt = src->opt; +} + +#endif /* SHELL_TELNET_PROTOCOL_H__ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/valid_local_cancel/include/zephyr/shell/shell_telnet.h b/tasks/runtime-zephyr-telnet-close-handoff/valid_local_cancel/include/zephyr/shell/shell_telnet.h new file mode 100644 index 00000000..1320f55a --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/valid_local_cancel/include/zephyr/shell/shell_telnet.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Header file for the Telnet shell backend. + * @ingroup shell_telnet + */ + +#ifndef ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ +#define ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @cond INTERNAL_HIDDEN */ +extern const struct shell_transport_api shell_telnet_transport_api; + +#define SHELL_TELNET_POLLFD_COUNT 3 +#define SHELL_TELNET_MAX_CMD_SIZE 3 + +/** Line buffer structure. */ +struct shell_telnet_line_buf { + /** Line buffer. */ + char buf[CONFIG_SHELL_TELNET_LINE_BUF_SIZE]; + + /** Current line length. */ + uint16_t len; +}; + +/** TELNET-based shell transport. */ +struct shell_telnet { + /** Handler function registered by shell. */ + shell_transport_handler_t shell_handler; + + /** Context registered by shell. */ + void *shell_context; + + /** Buffer for outgoing line. */ + struct shell_telnet_line_buf line_out; + + /** Array for sockets used by the telnet service. */ + struct zsock_pollfd fds[SHELL_TELNET_POLLFD_COUNT]; + + /** Input buffer. */ + uint8_t rx_buf[CONFIG_SHELL_CMD_BUFF_SIZE]; + + /** Number of data bytes within the input buffer. */ + size_t rx_len; + + /** Mutex protecting the input buffer access. */ + struct k_mutex rx_lock; + + /** Mutex serializing sends, cancels of send_work and the client close. */ + struct k_mutex tx_lock; + uint8_t cmd_buf[SHELL_TELNET_MAX_CMD_SIZE]; + uint8_t cmd_len; + + /** The delayed work is used to send non-lf terminated output that has + * been around for "too long". This will prove to be useful + * to send the shell prompt for instance. + */ + struct k_work_delayable send_work; + + /** If set, no output is sent to the TELNET client. */ + bool output_lock; +}; +/** @endcond */ + +/** + * @defgroup shell_telnet Telnet shell backend + * @ingroup shell_backends + * @brief Shell access over a TELNET connection. + * @{ + */ + +/** + * @brief Define a Telnet shell transport instance. + * + * @param _name Name of the transport instance. + */ +#define SHELL_TELNET_DEFINE(_name) \ + static struct shell_telnet _name##_shell_telnet; \ + struct shell_transport _name = { \ + .api = &shell_telnet_transport_api, \ + .ctx = (struct shell_telnet *)&_name##_shell_telnet \ + } + +/** + * @brief This function provides pointer to shell telnet backend instance. + * + * Function returns pointer to the shell telnet instance. This instance can be + * next used with shell_execute_cmd function in order to test commands behavior. + * + * @returns Pointer to the shell instance. + */ +const struct shell *shell_backend_telnet_get_ptr(void); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SHELL_SHELL_TELNET_H_ */ diff --git a/tasks/runtime-zephyr-telnet-close-handoff/valid_local_cancel/subsys/shell/backends/shell_telnet.c b/tasks/runtime-zephyr-telnet-close-handoff/valid_local_cancel/subsys/shell/backends/shell_telnet.c new file mode 100644 index 00000000..a6061be1 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/valid_local_cancel/subsys/shell/backends/shell_telnet.c @@ -0,0 +1,860 @@ +/* + * Copyright (c) 2017 Intel Corporation + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include "shell_telnet_protocol.h" + +SHELL_TELNET_DEFINE(shell_transport_telnet); +SHELL_DEFINE(shell_telnet, CONFIG_SHELL_PROMPT_TELNET, &shell_transport_telnet, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_SIZE, + CONFIG_SHELL_TELNET_LOG_MESSAGE_QUEUE_TIMEOUT, + SHELL_FLAG_OLF_CRLF); + +LOG_MODULE_REGISTER(shell_telnet, CONFIG_SHELL_TELNET_LOG_LEVEL); + +struct shell_telnet *sh_telnet; + +/* Various definitions mapping the TELNET service configuration options */ +#define TELNET_PORT CONFIG_SHELL_TELNET_PORT +#define TELNET_LINE_SIZE CONFIG_SHELL_TELNET_LINE_BUF_SIZE +#define TELNET_TIMEOUT CONFIG_SHELL_TELNET_SEND_TIMEOUT + +#define TELNET_MIN_COMMAND_LEN 2 +#define TELNET_WILL_DO_COMMAND_LEN 3 + +#define SOCK_ID_IPV4_LISTEN 0 +#define SOCK_ID_IPV6_LISTEN 1 +#define SOCK_ID_CLIENT 2 +#define SOCK_ID_MAX 3 + +/* Basic TELNET implementation. */ + +static void telnet_server_cb(struct net_socket_service_event *evt); +static int telnet_init(struct shell_telnet *ctx); + +NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(telnet_server, telnet_server_cb, + SHELL_TELNET_POLLFD_COUNT); + + +static bool telnet_cancel_pending(void) +{ + struct k_work_sync invocation_state; + return k_work_cancel_delayable_sync(&sh_telnet->send_work, &invocation_state); +} + +/* Only the socket service thread closes the client socket, and only once no + * send holds tx_lock, so the descriptor cannot be closed and reused under a + * send in progress. Every synchronous cancel of send_work also runs under + * tx_lock, so no two threads ever wait for the same cancel. + */ +static void telnet_close_client(void) +{ + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + (void)telnet_cancel_pending(); + + (void)zsock_close(sh_telnet->fds[SOCK_ID_CLIENT].fd); + + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + sh_telnet->output_lock = false; + + k_mutex_unlock(&sh_telnet->tx_lock); +} + +/* A failed send on another thread shuts the socket down for reading instead + * of closing it; that wakes the service thread with EOF and the close happens + * here. + */ +static void telnet_end_client_connection(void) +{ + int ret; + + telnet_close_client(); + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + } +} + +static void telnet_command_send_reply(uint8_t *msg, uint16_t len) +{ + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return; + } + + while (len > 0) { + int ret; + + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, 0); + if (ret < 0) { + LOG_ERR("Failed to send command %d, shutting down", ret); + telnet_end_client_connection(); + break; + } + + msg += ret; + len -= ret; + } +} + +static void telnet_reply_ay_command(void) +{ + static const char alive[] = "Zephyr at your service\r\n"; + + telnet_command_send_reply((uint8_t *)alive, strlen(alive)); +} + +static int telnet_echo_set(const struct shell *sh, bool val) +{ + int ret = shell_echo_set(sh_telnet->shell_context, val); + + if (ret < 0) { + LOG_ERR("Failed to set echo to: %d, err: %d", val, ret); + } + return ret; +} + +static void telnet_reply_dont_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, false); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL_NOT; + } else { + cmd->op = NVT_CMD_WILL; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_do_command(struct telnet_simple_command *cmd) +{ + switch (cmd->opt) { + case NVT_OPT_SUPR_GA: + cmd->op = NVT_CMD_WILL; + break; + case NVT_OPT_ECHO: + { + int ret = telnet_echo_set(sh_telnet->shell_context, true); + + if (ret >= 0) { + cmd->op = NVT_CMD_WILL; + } else { + cmd->op = NVT_CMD_WILL_NOT; + } + break; + } + default: + cmd->op = NVT_CMD_WILL_NOT; + break; + } + + telnet_command_send_reply((uint8_t *)cmd, + sizeof(struct telnet_simple_command)); +} + +static void telnet_reply_command(struct telnet_simple_command *cmd) +{ + + if (!cmd->iac) { + return; + } + + switch (cmd->op) { + case NVT_CMD_AO: + /* OK, no output then */ + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + sh_telnet->output_lock = true; + sh_telnet->line_out.len = 0; + (void)telnet_cancel_pending(); + k_mutex_unlock(&sh_telnet->tx_lock); + break; + case NVT_CMD_AYT: + telnet_reply_ay_command(); + break; + case NVT_CMD_DO: + telnet_reply_do_command(cmd); + break; + case NVT_CMD_DO_NOT: + telnet_reply_dont_command(cmd); + break; + default: + LOG_DBG("Operation %u not handled", cmd->op); + break; + } +} + +static int telnet_send(bool block) +{ + int ret; + uint8_t *msg = sh_telnet->line_out.buf; + uint16_t len = sh_telnet->line_out.len; + + if (sh_telnet->line_out.len == 0) { + return 0; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + return -ENOTCONN; + } + + if (sh_telnet->output_lock) { + return -ECANCELED; + } + + while (len > 0) { + ret = zsock_send(sh_telnet->fds[SOCK_ID_CLIENT].fd, msg, len, + block ? 0 : ZSOCK_MSG_DONTWAIT); + if (!block && (ret < 0) && (errno == EAGAIN)) { + /* Not all data was sent - move the remaining data and + * update length. + */ + memmove(sh_telnet->line_out.buf, msg, len); + sh_telnet->line_out.len = len; + return -EAGAIN; + } + + if (ret < 0) { + /* Stop the output before anything else: the caller logs + * the error once tx_lock is released, and a log backend + * on this shell must find the output already stopped. + */ + ret = -errno; + sh_telnet->output_lock = true; + (void)zsock_shutdown(sh_telnet->fds[SOCK_ID_CLIENT].fd, + ZSOCK_SHUT_RDWR); + return ret; + } + + msg += ret; + len -= ret; + } + + /* We reinitialize the line buffer */ + sh_telnet->line_out.len = 0; + + return 0; +} + +static void telnet_send_prematurely(struct k_work *work) +{ + int status; + if (k_mutex_lock(&sh_telnet->tx_lock, K_NO_WAIT)) { + k_work_reschedule(&sh_telnet->send_work, K_MSEC(TELNET_TIMEOUT)); + return; + } + status = telnet_send(false); + k_mutex_unlock(&sh_telnet->tx_lock); + switch (status) { + case -EAGAIN: + k_work_reschedule(&sh_telnet->send_work, K_MSEC(TELNET_TIMEOUT)); + break; + case 0: + case -ENOTCONN: + case -ECANCELED: + break; + default: + LOG_ERR("Transport error %d", status); + } +} + +static int telnet_command_length(uint8_t op) +{ + if (op == NVT_CMD_SB || op == NVT_CMD_WILL || op == NVT_CMD_WILL_NOT || + op == NVT_CMD_DO || op == NVT_CMD_DO_NOT) { + return TELNET_WILL_DO_COMMAND_LEN; + } + + return TELNET_MIN_COMMAND_LEN; +} + +static inline int telnet_handle_command(struct telnet_simple_command *cmd) +{ + /* Commands are two or three bytes. */ + if (cmd->iac != NVT_CMD_IAC) { + return 0; + } + + if (IS_ENABLED(CONFIG_SHELL_TELNET_SUPPORT_COMMAND)) { + LOG_DBG("Got a command %u/%u/%u", cmd->iac, cmd->op, cmd->opt); + telnet_reply_command(cmd); + } + + if (cmd->op == NVT_CMD_SB) { + /* TODO Add subnegotiation support. */ + return -EOPNOTSUPP; + } + + return 0; +} + +static void telnet_recv(struct zsock_pollfd *pollfd) +{ + struct telnet_simple_command *cmd = + (struct telnet_simple_command *)sh_telnet->cmd_buf; + size_t len, off, buf_left, cmd_total_len; + uint8_t *buf; + int ret; + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + buf_left = sizeof(sh_telnet->rx_buf) - sh_telnet->rx_len; + if (buf_left == 0) { + /* No space left to read TCP stream, try again later. */ + k_mutex_unlock(&sh_telnet->rx_lock); + k_msleep(10); + return; + } + + buf = sh_telnet->rx_buf + sh_telnet->rx_len; + + ret = zsock_recv(pollfd->fd, buf, buf_left, 0); + if (ret < 0) { + LOG_DBG("Telnet client error %d", ret); + goto error; + } else if (ret == 0) { + LOG_DBG("Telnet client closed connection"); + goto error; + } + + off = 0; + len = ret; + cmd_total_len = 0; + /* Filter out and process commands from the data buffer. */ + while (off < len) { + if (sh_telnet->cmd_len > 0) { + /* Command mode */ + if (sh_telnet->cmd_len == 1) { + /* Operation */ + cmd->op = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + + if (telnet_command_length(cmd->op) > + TELNET_MIN_COMMAND_LEN) { + continue; + } + } else if (sh_telnet->cmd_len == 2) { + /* Option */ + cmd->opt = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + } + + ret = telnet_handle_command(cmd); + if (ret < 0) { + goto error; + } else { + LOG_DBG("Handled command"); + } + + memset(cmd, 0, sizeof(*cmd)); + sh_telnet->cmd_len = 0; + + continue; + } + + if (*(buf + off) == NVT_CMD_IAC) { + cmd->iac = *(buf + off); + sh_telnet->cmd_len++; + cmd_total_len++; + off++; + continue; + } + + /* Data byte, remove command bytes from the buffer, if any. */ + if (cmd_total_len > 0) { + size_t data_off = off; + + off -= cmd_total_len; + len -= cmd_total_len; + cmd_total_len = 0; + + memmove(buf + off, buf + data_off, len); + } + + off++; + } + + if (cmd_total_len > 0) { + /* In case the buffer ended with command, trim the buffer size + * here. + */ + len -= cmd_total_len; + } + + if (len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + return; + } + + sh_telnet->rx_len += len; + + k_mutex_unlock(&sh_telnet->rx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, + sh_telnet->shell_context); + + return; + +error: + k_mutex_unlock(&sh_telnet->rx_lock); + telnet_end_client_connection(); +} + +static void telnet_restart_server(void) +{ + int ret; + + if (sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd); + sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd >= 0) { + telnet_close_client(); + } + + ret = telnet_init(sh_telnet); + if (ret < 0) { + LOG_ERR("Telnet fatal error, failed to restart server (%d)", ret); + (void)net_socket_service_unregister(&telnet_server); + } +} + +static void telnet_accept(struct zsock_pollfd *pollfd) +{ + int sock, ret = 0; + struct net_sockaddr_storage addr = {}; + net_socklen_t addrlen = sizeof(addr); + + sock = zsock_accept(pollfd->fd, net_sad(&addr), &addrlen); + if (sock < 0) { + ret = -errno; + LOG_ERR("Telnet accept error (%d)", ret); + return; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd > 0) { + /* Too many connections. */ + ret = 0; + LOG_ERR("Telnet client already connected."); + goto error; + } + + sh_telnet->fds[SOCK_ID_CLIENT].fd = sock; + sh_telnet->fds[SOCK_ID_CLIENT].events = ZSOCK_POLLIN; + sh_telnet->rx_len = 0; + sh_telnet->cmd_len = 0; + sh_telnet->line_out.len = 0; + sh_telnet->output_lock = false; + + ret = net_socket_service_register(&telnet_server, sh_telnet->fds, + ARRAY_SIZE(sh_telnet->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, (%d)", ret); + sh_telnet->fds[SOCK_ID_CLIENT].fd = -1; + goto error; + } + + LOG_DBG("Telnet client connected (family NET_AF_INET%s)", + addr.ss_family == NET_AF_INET ? "" : "6"); + + /* Disable echo - if command handling is enabled we reply that we + * support echo. + */ + (void)telnet_echo_set(sh_telnet->shell_context, false); + + return; + +error: + if (sock > 0) { + (void)zsock_close(sock); + } + + if (ret < 0) { + telnet_restart_server(); + } +} + +static void telnet_server_cb(struct net_socket_service_event *evt) +{ + int sock_error; + int ret; + net_socklen_t optlen = sizeof(int); + + if (sh_telnet == NULL) { + return; + } + + if ((evt->event.revents & ZSOCK_POLLERR) || + (evt->event.revents & ZSOCK_POLLNVAL)) { + (void)zsock_getsockopt(evt->event.fd, ZSOCK_SOL_SOCKET, + ZSOCK_SO_ERROR, &sock_error, &optlen); + + ret = -sock_error; + + if (ret == -ENETDOWN) { + LOG_INF("Network is down"); + } else { + LOG_ERR("Telnet socket %d error (%d)", evt->event.fd, ret); + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_end_client_connection(); + return; + } + + goto error; + } + + if (!(evt->event.revents & ZSOCK_POLLIN)) { + return; + } + + if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV4_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV4_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_IPV6_LISTEN].fd) { + telnet_accept(&sh_telnet->fds[SOCK_ID_IPV6_LISTEN]); + return; + } else if (evt->event.fd == sh_telnet->fds[SOCK_ID_CLIENT].fd) { + telnet_recv(&sh_telnet->fds[SOCK_ID_CLIENT]); + return; + } + + LOG_ERR("Unexpected FD received for telnet, restarting service."); + +error: + telnet_restart_server(); +} + +static int telnet_setup_server(struct zsock_pollfd *pollfd, net_sa_family_t family, + struct net_sockaddr *addr, net_socklen_t addrlen) +{ + int ret = 0; + + pollfd->fd = zsock_socket(family, NET_SOCK_STREAM, NET_IPPROTO_TCP); + if (pollfd->fd < 0) { + ret = -errno; + LOG_ERR("Failed to create telnet NET_AF_INET%s socket", + family == NET_AF_INET ? "" : "6"); + goto error; + } + + if (zsock_bind(pollfd->fd, addr, addrlen) < 0) { + ret = -errno; + LOG_ERR("Cannot bind on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + if (zsock_listen(pollfd->fd, 1)) { + ret = -errno; + LOG_ERR("Cannot listen on family NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + goto error; + } + + pollfd->events = ZSOCK_POLLIN; + + LOG_DBG("Telnet console enabled on NET_AF_INET%s", + family == NET_AF_INET ? "" : "6"); + + return 0; + +error: + LOG_ERR("Unable to start telnet on NET_AF_INET%s (%d)", + family == NET_AF_INET ? "" : "6", ret); + + if (pollfd->fd >= 0) { + (void)zsock_close(pollfd->fd); + pollfd->fd = -1; + } + + return ret; +} + +static int telnet_init(struct shell_telnet *ctx) +{ + int ret; + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + struct net_sockaddr_in any_addr4 = { + .sin_family = NET_AF_INET, + .sin_port = net_htons(TELNET_PORT), + .sin_addr = NET_INADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV4_LISTEN], + NET_AF_INET, (struct net_sockaddr *)&any_addr4, + sizeof(any_addr4)); + if (ret < 0) { + goto error; + } + } + + if (IS_ENABLED(CONFIG_NET_IPV6)) { + struct net_sockaddr_in6 any_addr6 = { + .sin6_family = NET_AF_INET6, + .sin6_port = net_htons(TELNET_PORT), + .sin6_addr = NET_IN6ADDR_ANY_INIT + }; + + ret = telnet_setup_server(&ctx->fds[SOCK_ID_IPV6_LISTEN], + NET_AF_INET6, (struct net_sockaddr *)&any_addr6, + sizeof(any_addr6)); + if (ret < 0) { + goto error; + } + } + + ret = net_socket_service_register(&telnet_server, ctx->fds, + ARRAY_SIZE(ctx->fds), NULL); + if (ret < 0) { + LOG_ERR("Failed to register socket service, %d", ret); + goto error; + } + + LOG_INF("Telnet shell backend initialized"); + + return 0; + +error: + if (ctx->fds[SOCK_ID_IPV4_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV4_LISTEN].fd); + ctx->fds[SOCK_ID_IPV4_LISTEN].fd = -1; + } + + if (ctx->fds[SOCK_ID_IPV6_LISTEN].fd >= 0) { + (void)zsock_close(ctx->fds[SOCK_ID_IPV6_LISTEN].fd); + ctx->fds[SOCK_ID_IPV6_LISTEN].fd = -1; + } + + return ret; +} + +/* Shell API */ + +static int init(const struct shell_transport *transport, + const void *config, + shell_transport_handler_t evt_handler, + void *context) +{ + int err; + + sh_telnet = (struct shell_telnet *)transport->ctx; + + memset(sh_telnet, 0, sizeof(struct shell_telnet)); + for (int i = 0; i < ARRAY_SIZE(sh_telnet->fds); i++) { + sh_telnet->fds[i].fd = -1; + } + + sh_telnet->shell_handler = evt_handler; + sh_telnet->shell_context = context; + + err = telnet_init(sh_telnet); + if (err != 0) { + return err; + } + + k_work_init_delayable(&sh_telnet->send_work, telnet_send_prematurely); + k_mutex_init(&sh_telnet->rx_lock); + k_mutex_init(&sh_telnet->tx_lock); + + return 0; +} + +static int uninit(const struct shell_transport *transport) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int enable(const struct shell_transport *transport, bool blocking) +{ + if (sh_telnet == NULL) { + return -ENODEV; + } + + return 0; +} + +static int telnet_write(const struct shell_transport *transport, + const void *data, size_t length, size_t *cnt) +{ + struct shell_telnet_line_buf *lb; + size_t copy_len; + int err; + uint32_t timeout; + bool was_running; + + if (sh_telnet == NULL) { + *cnt = 0; + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0 || sh_telnet->output_lock) { + *cnt = length; + return 0; + } + + *cnt = 0; + lb = &sh_telnet->line_out; + + k_mutex_lock(&sh_telnet->tx_lock, K_FOREVER); + + /* Stop the transmission timer, so it does not interrupt the operation. + */ + timeout = k_ticks_to_ms_ceil32( + k_work_delayable_remaining_get(&sh_telnet->send_work)); + was_running = telnet_cancel_pending(); + + do { + if (lb->len + length - *cnt > TELNET_LINE_SIZE) { + copy_len = TELNET_LINE_SIZE - lb->len; + } else { + copy_len = length - *cnt; + } + + memcpy(lb->buf + lb->len, (uint8_t *)data + *cnt, copy_len); + lb->len += copy_len; + + /* Send the data immediately if the buffer is full or line feed + * is recognized. + */ + if (lb->buf[lb->len - 1] == '\n' || + lb->len == TELNET_LINE_SIZE) { + err = telnet_send(true); + if (err != 0) { + *cnt = length; + k_mutex_unlock(&sh_telnet->tx_lock); + if ((err == -ENOTCONN) || (err == -ENETDOWN)) { + LOG_ERR("Network disconnected, shutting down"); + } else if (err != -ECANCELED) { + LOG_ERR("Error %d, shutting down", err); + } + return 0; /* Return 0 to not trigger ASSERT in shell_ops.c */ + } + } + + *cnt += copy_len; + } while (*cnt < length); + + if (lb->len > 0) { + /* Check if the timer was already running, initialize otherwise. + */ + timeout = was_running ? timeout : TELNET_TIMEOUT; + + k_work_reschedule(&sh_telnet->send_work, K_MSEC(timeout)); + } + + k_mutex_unlock(&sh_telnet->tx_lock); + + sh_telnet->shell_handler(SHELL_TRANSPORT_EVT_TX_RDY, + sh_telnet->shell_context); + + return 0; +} + +static int telnet_read(const struct shell_transport *transport, + void *data, size_t length, size_t *cnt) +{ + size_t read_len; + + if (sh_telnet == NULL) { + return -ENODEV; + } + + if (sh_telnet->fds[SOCK_ID_CLIENT].fd < 0) { + goto no_data; + } + + k_mutex_lock(&sh_telnet->rx_lock, K_FOREVER); + + if (sh_telnet->rx_len == 0) { + k_mutex_unlock(&sh_telnet->rx_lock); + goto no_data; + } + + read_len = sh_telnet->rx_len; + if (read_len > length) { + read_len = length; + } + + memcpy(data, sh_telnet->rx_buf, read_len); + *cnt = read_len; + + sh_telnet->rx_len -= read_len; + if (sh_telnet->rx_len > 0) { + memmove(sh_telnet->rx_buf, sh_telnet->rx_buf + read_len, + sh_telnet->rx_len); + } + + k_mutex_unlock(&sh_telnet->rx_lock); + + return 0; + +no_data: + *cnt = 0; + return 0; +} + +const struct shell_transport_api shell_telnet_transport_api = { + .init = init, + .uninit = uninit, + .enable = enable, + .write = telnet_write, + .read = telnet_read +}; + +static int enable_shell_telnet(void) +{ + bool log_backend = CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > 0; + uint32_t level = (CONFIG_SHELL_TELNET_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ? + CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_TELNET_INIT_LOG_LEVEL; + static const struct shell_backend_config_flags cfg_flags = + SHELL_DEFAULT_BACKEND_CONFIG_FLAGS; + + return shell_init(&shell_telnet, NULL, cfg_flags, log_backend, level); +} + +SYS_INIT(enable_shell_telnet, APPLICATION, CONFIG_SHELL_TELNET_INIT_PRIORITY); + +const struct shell *shell_backend_telnet_get_ptr(void) +{ + return &shell_telnet; +} diff --git a/tasks/runtime-zephyr-telnet-close-handoff/verify.py b/tasks/runtime-zephyr-telnet-close-handoff/verify.py new file mode 100644 index 00000000..9e2c6c26 --- /dev/null +++ b/tasks/runtime-zephyr-telnet-close-handoff/verify.py @@ -0,0 +1,130 @@ +"""Run an imported Bos-qiao check.py unchanged; candidate processes run as UID 65534.""" + +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile + +GRADER = Path(__file__).resolve().parent +BLOCKED = "isolation-blocked.txt" +GUARD = 86 +# Runs the original check unchanged as the trusted user. In isolation mode every child process drops to +# UID 65534 and every mkdtemp scratch is handed to it; an audit hook stops the check if candidate code +# would run inside this process. Failing child processes also copy their stderr tail out for private triage. +CHECK = ( + "import os, runpy, subprocess, sys, tempfile\n" + "isolated, sys.argv = sys.argv[1] == '1', sys.argv[2:]\n" + "untrusted = [os.path.realpath(sys.argv[1])]\n" + "def inside(path):\n" + " path = os.path.realpath(str(path))\n" + " return any(path == root or path.startswith(root + os.sep) for root in untrusted)\n" + "spawns = ('os.system', 'os.exec', 'os.spawn', 'os.fork', 'os.forkpty', 'os.posix_spawn') if isolated else ()\n" + "def guard(event, args):\n" + " if (event in spawns or event == 'exec' and inside(getattr(args[0], 'co_filename', '')) or\n" + " event == 'ctypes.dlopen' and args[0] and inside(args[0]) or\n" + " event == 'import' and any(inside(entry or '.') for entry in sys.path)):\n" + " sys.__stderr__.write('rollout-guard: ' + event + '\\n')\n" + " sys.__stderr__.flush()\n" + " os._exit(%d)\n" + "mkdtemp = tempfile.mkdtemp\n" + "def scratch(*args, **kwargs):\n" + " path = mkdtemp(*args, **kwargs)\n" + " untrusted.append(os.path.realpath(path))\n" + " if isolated:\n" + " os.chown(path, 65534, 65534)\n" + " return path\n" + "tempfile.mkdtemp = scratch\n" + "popen = subprocess.Popen.__init__\n" + "def drop(self, *args, **kwargs):\n" + " if isolated:\n" + " kwargs.update(user=65534, group=65534, extra_groups=[])\n" + " popen(self, *args, **kwargs)\n" + "subprocess.Popen.__init__ = drop\n" + "real = subprocess.run\n" + "def run(*args, **kwargs):\n" + " result = real(*args, **kwargs)\n" + " if result.returncode and result.stderr:\n" + " tail = result.stderr if isinstance(result.stderr, str) else result.stderr.decode('utf-8', 'replace')\n" + " sys.__stderr__.write(tail[-2000:] + '\\n')\n" + " return result\n" + "subprocess.run = run\n" + "sys.addaudithook(guard)\n" + "runpy.run_path(sys.argv[0], run_name='__main__')\n" % GUARD) + + +class UnsupportedEnvironment(RuntimeError): + pass + + +def candidate_user(): + os.setgroups([]) + os.setgid(65534) + os.setuid(65534) + + +def isolation(): + if os.environ.get("ROLLOUT_GRADE_ISOLATION") != "uid65534": + return None + if sys.platform != "linux" or os.geteuid() != 0: + raise UnsupportedEnvironment("isolated grader requires a Linux root supervisor") + probe = ( + "from pathlib import Path\n" + "try: Path('/grader/verify.py').read_bytes()\n" + "except PermissionError: raise SystemExit(0)\n" + "raise SystemExit(1)\n" + ) + try: + result = subprocess.run([sys.executable, "-I", "-c", probe], + capture_output=True, timeout=10, preexec_fn=candidate_user) + except subprocess.SubprocessError as error: + raise UnsupportedEnvironment("candidate identity could not be isolated") from error + if result.returncode != 0: + raise UnsupportedEnvironment("candidate can read the private grader or isolation probe failed") + return candidate_user + + +def verify(candidate): + if (GRADER / BLOCKED).exists(): + raise UnsupportedEnvironment("isolation blocked: " + (GRADER / BLOCKED).read_text().strip()[:200]) + isolated = isolation() is not None + if shutil.which("gcc") is None: + raise UnsupportedEnvironment("C compiler unavailable") + with tempfile.TemporaryDirectory(prefix="bos-grade-") as temporary: + root = Path(temporary) + work, grader = root / "work", root / "grader" + shutil.copytree(candidate, work, symlinks=True) + shutil.copytree(GRADER, grader, ignore=lambda folder, names: ["verify.py"] if Path(folder) == GRADER else []) + if isolated: + os.chmod(root, 0o711) + os.chmod(grader, 0o700) + for path in (work, *work.rglob("*")): + os.lchown(path, 65534, 65534) + # shortcut: the check runs as root and follows symlinks a child plants in shared scratch directories; + # upgrade by running the check under its own unprivileged UID once a setuid helper is available. + completed = subprocess.run([sys.executable, "-I", "-B", "-c", CHECK, "1" if isolated else "0", + str(grader / "check.py"), str(work)], + cwd=root, capture_output=True, text=True, timeout=100) + # grade.stderr stays on the trusted side with the archive; it may quote hidden test lines. + sys.stderr.write(completed.stderr[-4000:]) + if completed.returncode == GUARD and "rollout-guard: " in completed.stderr: + raise UnsupportedEnvironment("isolation blocked: the check would run candidate code in the grader process") + lines = completed.stdout.splitlines() + if completed.returncode == 0 and lines[-1:] == ["PASS"]: + return {"status": "PASS", "checks": 1} + return {"status": "FAIL", "checks": 1, "reason": (lines[-1] if lines else "no checker output")[:300]} + + +if __name__ == "__main__": + try: + result = verify(Path(sys.argv[1])) + except UnsupportedEnvironment as error: + result = {"status": "UNVERIFIED", "checks": 0, "reason": str(error)} + except subprocess.TimeoutExpired: + result = {"status": "FAIL", "checks": 1, "reason": "timeout"} + except Exception as error: + result = {"status": "INFRA_ERROR", "checks": 0, "reason": str(error)} + print(json.dumps(result, sort_keys=True)) + sys.exit({"PASS": 0, "FAIL": 1}.get(result["status"], 2))