]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/log
FreeBSD/FreeBSD.git
2 years agonanobsd: default to -j $hw.ncpu
Warner Losh [Thu, 15 Jul 2021 22:07:51 +0000 (16:07 -0600)]
nanobsd: default to -j $hw.ncpu

For paralllel builds, default to using all the CPUs on the system. The
old default of -j 3 was too few.

Sponsored by: Netflix

(cherry picked from commit 7e3a794be3752d21fe760cfb2a38665532c4b6b4)

2 years agonanobsd: Use gpart and create code image before full disk image
Arrigo Marchiori [Thu, 15 Jul 2021 22:06:51 +0000 (16:06 -0600)]
nanobsd: Use gpart and create code image before full disk image

The attached patch brings two main changes to the nanobsd script:
 1- gpart is used instead of fdisk;
 2- the code image is created first, and then used to ``assemble'' the
    full disk image.

The patch was first proposed on the freebsd-embedded list:
http://lists.freebsd.org/pipermail/freebsd-embedded/2012-June/001580.html
and is currently under discussion:
http://lists.freebsd.org/pipermail/freebsd-embedded/2014-January/002216.html

Another effect is that the -f option ("suppress code slice extraction")
now imples the -i option ("suppress disk image build").

imp@ applied Patch by hand to new legacy.sh, plus tweaked for NANO_LOG vs
NANO_OBJ confusion in original.

PR: 186030
Reviewed by: imp@
Differential Revision: https://reviews.freebsd.org/D31102

(cherry picked from commit 587c054bea0f652fdbd86baf5807f21c904ac2ad)

2 years agoloader: make sure CPUTYPE is ignored when building
Warner Losh [Thu, 15 Jul 2021 03:06:08 +0000 (21:06 -0600)]
loader: make sure CPUTYPE is ignored when building

CPUTYPE?=native causes -march=native to be added to the command
line. When the host machine is haswell, this causes some versions of
clang to generate code that can't execute in the efi boot loader
environment. Set _CPUCFLAGS= to undo what's done bsd.cpu.mk. bsd.cpu.mk
is included too early to control with NO_CPU_CFLAGS here. The only other
option is to put that in all the Makefiles, and this is less tedious and
error prone.

PR: 194641
Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D31187
MFC After: 1 week

(cherry picked from commit 7a0c0ff7ee25ff1efa3dbedcb20d2c80178639cc)

2 years agoRemove incorrect __restricted labels from strcspn
Alfonso Gregory [Wed, 14 Jul 2021 21:48:35 +0000 (15:48 -0600)]
Remove incorrect __restricted labels from strcspn

strcspn should never have had the __restrict keywords. While both of
these strings are const, it may have unindended side effects. While this
is the kernel, the POSIX definition also omits restrict.

Reviewed by: imp@
Pull Request: https://github.com/freebsd/freebsd-src/pull/497

(cherry picked from commit 56d33e86b74b197a36f42255824b56715c96a596)

2 years agoFix pmbr issues > 2TB
Emrion [Tue, 13 Jul 2021 20:37:59 +0000 (14:37 -0600)]
Fix pmbr issues > 2TB

These issues have low impact because they require precise circumstances
to trigger one of them. The disk must be > 2 TiB in size and either:
- The primary GPT header is dammaged.
- The freebsd-boot partiton is located farther than the first 2 TiB of
  the disc and one of its sectors takes place at a lba value that makes
  the higher 32 bits of this very value change.
Errors and corrections folow:
- decl and incl don't affect CF, so replace with subl/addl $1
- repe uses %cx, so move size to it with movw
- moving a 64-bit value with %cx of 2 (should be 4) so addresses
  > 2TB will work.

PR: 233180
Reviewed by: imp@ (applied patch using description in bug)
Differential Revision: https://reviews.freebsd.org/D31100

(cherry picked from commit 0ca9f1d4a3b772036309fb1c14262ec77c674c5d)

2 years agocam_iosched: use tunable flag and make a bool really a bool
Warner Losh [Tue, 13 Jul 2021 06:00:33 +0000 (00:00 -0600)]
cam_iosched: use tunable flag and make a bool really a bool

kern.cam.do_dynamic_iosched is really a bool, so change its type to
bool. While I'm here, also use the CTLFLAG_TUN flag instead of a
separate tunable line for it and kern.cam.iosched_alpha_bits.

MFC After: 1 week
Sponsored by: Netflix

(cherry picked from commit a065ccb2804fae834a7c66e29b726ea3dbbceace)

2 years agoFix potential NULL pointer dereference of device physical path
Young Xiao [Tue, 21 May 2019 07:36:29 +0000 (15:36 +0800)]
Fix potential NULL pointer dereference of device physical path

In ata_dev_advinfo() and nvme_dev_advinfo(), if the physical path is
being stored and there is a malloc failure (malloc(9) is called with
M_NOWAIT), we could wind up in a situation where the device's
physpath_len is set to the length the user provided, but the physpath
itself is NULL.

If another context then comes in to fetch the physical path value, we
would wind up trying to memcpy a NULL pointer into the caller's buffer.

So, set the physpath_len to 0 when we free the physpath on entry into
the store case for the physical path.  Reset the length to a non-zero
value only after we've successfully malloced a buffer to hold it.

This code mirrors scsi_xpt.c does already as well.

Signed-off-by: Young Xiao <92siuyang@gmail.com>
Reviewed by: imp
PR: 238014

(cherry picked from commit 431ddd94360a9e86c91294eaa2c7b859911984b7)

2 years agoloader: support.4th resets the read buffer incorrectly
John Hood [Sun, 11 Jul 2021 14:44:12 +0000 (08:44 -0600)]
loader: support.4th resets the read buffer incorrectly

Large nextboot.conf files (over 80 bytes) are not read correctly by the
Forth loader, causing file parsing to abort, and nextboot configuration
fails to apply.

Simple repro:

nextboot -e foo=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
shutdown -r now

That will cause the bug to cause a parse failure but shouldn't otherwise
affect the boot.  Depending on your loader configuration, you may also
have to set beastie_disable and/or reduce the number of modules loaded
to see the error on a small console screen.  12.0 or CURRENT users will
also have to explicitly use the Forth loader instead of the Lua loader.
The error will look something like:

Warning: syntax error on file /boot/loader.conf.local
foo="xxxxxxxxxxxxxxnextboot_enable="YES"
                                    ^
/boot/support.4th has crude file I/O buffering, which uses a buffer
'read_buffer', defined to be 80 bytes by the 'read_buffer_size'
constant.  The loader first tastes nextboot.conf, reading and parsing
the first line in it for nextboot_enable="YES".  If this is true, then
it reopens the file and parses it like other loader .conf files.

Unfortunately, the file I/O buffering code does not fully reset the
buffer state in the reset_line_reading word.  If the last file was read
to the end, that doesn't matter; the file buffer is treated as empty
anyway.  But in the nextboot.conf case, the loader will not read to the
end of file if it is over 80 bytes, and the file buffer may be reused
when reading the next file.  When the file is reread, the corrupt text
may cause file parsing to abort on bad syntax (if the corrupt line has
<>2 quotes in it), the wrong variable to be set, no variable to be set
at all, or (if the splice happens to land at a line ending) something
approximating normal operation.

The bug is very old, dating back to at least 2000 if not before, and is
still present in 12.0 and CURRENT r345863 (though it is now hidden by
the Lua loader by default).

Suggested one-line attached.  This does change the behavior of the
reset_line_reading word, which is exported in the line-reading
dictionary (though the export is not documented in loader man pages).
But repo history shows it was probably exported for the PNP support
code, which was never included in the loader build, and was removed 5
months ago.

One thing that puzzles me: how has this bug gone unnoticed/unfixed for
nearly 2 decades?  I find it hard to believe that nobody's tried to do
something interesting with nextboot, like load a kernel and filesystem,
which is what I'm doing.

PR: 239315
Reviewed by: imp

(cherry picked from commit 9c1c02093b90ae49745a174eb26ea85dd1990eec)

2 years agomips: Implement basic pmap_kenter_device, pmap_kremove_device
Conrad Meyer [Thu, 16 May 2019 19:10:48 +0000 (19:10 +0000)]
mips: Implement basic pmap_kenter_device, pmap_kremove_device

Unbreak mips.BERI_DE4_SDROOT build, which uses device xdma. Device xdma
depends on the pmap_kenter_device APIs.

Reported by: tinderbox (local)
Sponsored by: Dell EMC Isilon

(cherry picked from commit fa3ac573a2f712393cebd195bf7331cff3d1b8fe)

2 years agoAdd 'gpio' since mmc now requires gpio_if.h.
John Baldwin [Thu, 16 Apr 2020 20:45:54 +0000 (20:45 +0000)]
Add 'gpio' since mmc now requires gpio_if.h.

(cherry picked from commit fdbb2410cb08214939b0b1570cc375f231abf63e)

2 years agoarm: add device syscon to TEGRA124
Mateusz Guzik [Wed, 21 Jul 2021 12:08:32 +0000 (12:08 +0000)]
arm: add device syscon to TEGRA124

Unbreaks the build.

This is a direct commit.

Sponsored by: Rubicon Communications, LLC ("Netgate")

2 years agoarm: hide pc_mpidr behind #if __ARM_ARCH >= 6
Mateusz Guzik [Wed, 21 Jul 2021 12:06:34 +0000 (12:06 +0000)]
arm: hide pc_mpidr behind #if __ARM_ARCH >= 6

This is a direct commit.

Unbreaks the build of:
DB-78XXX
DB-88F5XXX
DB-88F6XXX
RT1310
LINT-V5
TS7800

Sponsored by: Rubicon Communications, LLC ("Netgate")

2 years agoarm: add atomic_store_rel_64
Mateusz Guzik [Wed, 21 Jul 2021 12:05:38 +0000 (12:05 +0000)]
arm: add atomic_store_rel_64

This unbreaks building linux compat in tinderbox.

This is a direct commit.

Sponsored by: Rubicon Communications, LLC ("Netgate")

2 years agoPartially revert previous commit.
Alexander Motin [Wed, 14 Jul 2021 19:46:22 +0000 (15:46 -0400)]
Partially revert previous commit.

ns identify does not allow nsid of 0xffffffff.  Only general identify
allows it.

MFC after: 1 week

(cherry picked from commit d1a0eab9fbc426ed8cbbddcd90720180c44ce36b)

2 years agoFor ns identify/delete allow nsid of 0xffffffff.
Alexander Motin [Wed, 14 Jul 2021 18:56:34 +0000 (14:56 -0400)]
For ns identify/delete allow nsid of 0xffffffff.

The first should report common parameters, the second should delete
all namespaces.

MFC after: 1 week

(cherry picked from commit dbf1dce8d6d2d0eedac5402c1f137a80805179ca)

2 years agoFilter out LBA formats with LBA Data Size of 0.
Alexander Motin [Wed, 14 Jul 2021 18:01:22 +0000 (14:01 -0400)]
Filter out LBA formats with LBA Data Size of 0.

According to the spec: "If the value reported is 0h, then LBA format
is not supported / used or is not currently available".

MFC after: 1 week

(cherry picked from commit d379886a4170fa3d95a5354612a5e2c15b104841)

2 years agoFix local-unbound setup for some IPv6 deployments.
Dag-Erling Smørgrav [Wed, 10 Mar 2021 14:01:38 +0000 (14:01 +0000)]
Fix local-unbound setup for some IPv6 deployments.

PR: 250984
MFC after: 1 week

2 years agotcpdump: decode packets on pfsync interfaces
Luiz Otavio O Souza [Mon, 12 Jul 2021 17:15:15 +0000 (19:15 +0200)]
tcpdump: decode packets on pfsync interfaces

Reviewed by: kp, scottl
Obtained from: pfsense
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D31153

(cherry picked from commit 3f240bdf3652ea823af34b0f399e2c8be5a6320a)

2 years agoarm: replace byte-by-byte memcmp with an already existing asm variant
Mateusz Guzik [Mon, 19 Jul 2021 09:58:15 +0000 (09:58 +0000)]
arm: replace byte-by-byte memcmp with an already existing asm variant

So happens bcmp as found in support.S is in fact renamed memcmp, add
memcmp back as a symbol.

This is a direct commit to keep the old symbol around, see
9ef5b65085ed2acd ("arm: bcmp -> memcmp") in main for reference.

Sponsored by: Rubicon Communications, LLC ("Netgate")

2 years agortwn: make sure to not write in upper bits of txdseq
Andriy Gapon [Mon, 21 Jun 2021 08:05:04 +0000 (11:05 +0300)]
rtwn: make sure to not write in upper bits of txdseq

ni_txseqs is kept as 16-bit counter, but we need to trim the upper four
bits as they may have special meanings for the firmware / hardware.
For instance, bit 15 enables hardware / firmware generation of sequence
numbers that overrides sequence numbers programmed by the driver.

Reviewed by: adrian

(cherry picked from commit 7544c1d20d8b7127ed3511edeb94e01d328daebb)

2 years agopmc(3): mandoc clean ups
Gordon Bergling [Mon, 12 Jul 2021 04:28:03 +0000 (06:28 +0200)]
pmc(3): mandoc clean ups

- new sentence, new line
- tab in filled text
- unusual Xr order
- skipping paragraph macro: Pp before Ss

Reviewed by: bcr
Differential Revision: https://reviews.freebsd.org/D31143

(cherry picked from commit 0b1293252543802b809b5f13f554e5d6391d3445)

2 years agoprocctl(PROC_ASLR_STATUS): fix vmspace leak
Konstantin Belousov [Wed, 14 Jul 2021 23:40:04 +0000 (02:40 +0300)]
procctl(PROC_ASLR_STATUS): fix vmspace leak

(cherry picked from commit 0bdb2cbf9d7c4366a0668b4563c8630538a50086)

2 years agodevmatch: don't announce autoloading so much
Warner Losh [Thu, 8 Jul 2021 19:53:18 +0000 (13:53 -0600)]
devmatch: don't announce autoloading so much

devmatch rc script would announce it was loading a module multiple
times. It used kldload -n so it really wasn't loading it that many
times, but the message is confusing. Use kldstat to see if we need to
load the module before saying we do. This fixes the vast majority of the
problems. It may be possible to race devmatch with a user invocation and
devd, though quite hard. In that case we'll announce things twice, but
still only load it once. No attempt is made to fix this.

PR: 232782
MFC After: 2 weeks
Sponsored by: Netflix

2 years agodevmatch: Be tolerant of .ko being present.
Warner Losh [Thu, 8 Jul 2021 19:44:21 +0000 (13:44 -0600)]
devmatch: Be tolerant of .ko being present.

We document that we did not need .ko on the module names in
devmatch_blocklist, but we really needed them. Keep the documentation
the same, but strip the .ko when we need to use the names so you can
specify either.

PR: 256240
MFC After: 2 weeks
Sponsored by: Netflix

2 years agodevmatch: defer until after kld
Helge Oldach [Wed, 7 Jul 2021 20:43:35 +0000 (14:43 -0600)]
devmatch: defer until after kld

devmatch loads a number of things automatically. Allow the list of
things to load to happen first in case those drivers affect what would
be loaded. Normally, this will produce the same results, but there's
some special cases that may not when drivers are loaded that report
other drivers missing, like virtio_pci.

PR: 253287
Reviewed by: imp
MFC After: 2 weeks

2 years agodevmatch: improve naming of devmatch config variable
Ceri Davies [Fri, 18 Jun 2021 12:17:30 +0000 (13:17 +0100)]
devmatch: improve naming of devmatch config variable

Accept the old rc.conf variable if the new one is not present for
compatability.

Approved by: imp
Differential Revision: https://reviews.freebsd.org/D30806

2 years agoconfig_intrhook: provide config_intrhook_drain
Warner Losh [Thu, 11 Mar 2021 15:42:09 +0000 (08:42 -0700)]
config_intrhook: provide config_intrhook_drain

config_intrhook_drain will remove the hook from the list as
config_intrhook_disestablish does if the hook hasn't been called.  If it has,
config_intrhook_drain will wait for the hook to be disestablished in the normal
course (or expedited, it's up to the driver to decide how and when
to call config_intrhook_disestablish).

This is intended for removable devices that use config_intrhook and might be
attached early in boot, but that may be removed before the kernel can call the
config_intrhook or before it ends. To prevent all races, the detach routine will
need to call config_intrhook_train.

Sponsored by: Netflix, Inc
Reviewed by: jhb, mav, gde (in D29006 for man page)
Differential Revision: https://reviews.freebsd.org/D29005

(cherry picked from commit e52368365db3c0a696b37bfc09d08b7093b41b57)

2 years agosbuf(9): Add NOWAIT dynamic buffer extension mode
Conrad Meyer [Wed, 7 Aug 2019 19:23:07 +0000 (19:23 +0000)]
sbuf(9): Add NOWAIT dynamic buffer extension mode

The goal is to avoid some kinds of low-memory deadlock when formatting
heap-allocated buffers.

Reviewed by: vangyzen
Sponsored by: Dell EMC Isilon
Differential Revision: https://reviews.freebsd.org/D21015

(cherry picked from commit 71db411eb627601a5f9a3464f88d75741927ce60)

2 years agopf: bound DIOCGETSTATESV2 memory use
Kristof Provost [Thu, 8 Jul 2021 13:01:19 +0000 (15:01 +0200)]
pf: bound DIOCGETSTATESV2 memory use

Rather than allocating however much memory userspace asks for we only
allocate enough for a handful of states, and copy to userspace for each
completed row.
We start out with enough space for 16 states (per row), but grow that as
required. In most configurations we expect at most a handful of states
per row (more than that would have other negative effects on packet
processing performance).

Reviewed by: mjg
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D31111

(cherry picked from commit 3fc12ae042040192aa43984106a75663aaa9e0f5)

2 years agolibpfctl: migrate to DIOCGETSTATESV2
Kristof Provost [Tue, 6 Jul 2021 11:05:19 +0000 (13:05 +0200)]
libpfctl: migrate to DIOCGETSTATESV2

Stop using the *NV version to retrieve states, as its performance is
unacceptably bad.

For 1,000,000 states the nvlist version needed ~100 seconds to retrieve
the states, the new version needs ~3 seconds.

Reviewed by: mjg
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D31098

(cherry picked from commit be70c7a50d324dd56f3a0d37d8372e7855dd580b)

2 years agopf: add DIOCGETSTATESV2
Kristof Provost [Tue, 6 Jul 2021 10:12:12 +0000 (12:12 +0200)]
pf: add DIOCGETSTATESV2

Add a new version of the DIOCGETSTATES call, which extends the struct to
include the original interface information.

MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D31097

(cherry picked from commit c6bf20a2a46dc36bf881ac594454f71379828a9a)

2 years agodummynet: reduce console spam
Luiz Otavio O Souza [Thu, 11 Feb 2016 13:35:01 +0000 (07:35 -0600)]
dummynet: reduce console spam

Only print this warning when boot verbose is enabled.
This can get pretty annoying (and useless) in some systems.

Reviewed by: kp
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit c5dd8bac0b96e11da02181bd1dbee677e270842d)

2 years agopf: pf_killstates() never fails, so remove the return value
Kristof Provost [Thu, 8 Jul 2021 08:54:16 +0000 (10:54 +0200)]
pf: pf_killstates() never fails, so remove the return value

Suggested by: mjg
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 34641052826c718566b994b75cd2bddb53a21583)

2 years agopf: Handle errors returned by pf_killstates()
Kristof Provost [Mon, 5 Jul 2021 12:21:03 +0000 (14:21 +0200)]
pf: Handle errors returned by pf_killstates()

Happily this wasn't a real bug, because pf_killstates() never fails, but
we should check the return value anyway, in case it does ever start
returning errors.

Reported by: clang --analyze
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit fa96701c8abbc29aad7f8f8d6b823bd7f89c6c15)

2 years agopf: Remove unneeded NULL check
Kristof Provost [Mon, 5 Jul 2021 12:02:06 +0000 (14:02 +0200)]
pf: Remove unneeded NULL check

pidx is never NULL, and is used unconditionally later on in the
function.
Add an assertion, as documentation for the requirement to provide an idx
pointer.

Reported by: clang --analyze
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 8cceacc0f1ee6a77c5f0566b8e6b0f054160fb20)

2 years agorc.d: connect sysctl_lastload
Eugene Grosbein [Thu, 20 May 2021 04:51:31 +0000 (11:51 +0700)]
rc.d: connect sysctl_lastload

Add recently added sysctl_lastload.

(cherry picked from commit 20eb969793921dce9e524d19fc02b84cabd98f74)

2 years agoipfw: reload sysctl.conf variables if needed
Eugene Grosbein [Mon, 17 May 2021 21:03:15 +0000 (04:03 +0700)]
ipfw: reload sysctl.conf variables if needed

Currently ipfw has multiple components that are not parts
of GENERIC kernel like dummynet etc. They can bring in important
sysctls if enabled with rc.conf(5) and loaded with ipfw startup script
by means of "required_modules" after initial consult
with /etc/sysctl.conf at boot time. Here is an example of one
increasing limit for dummynet hold queues that defaults to 100:

net.inet.ip.dummynet.pipe_slot_limit=1000

This makes it possible to use ipfw/dummynet rules such as:

ipfw pipe 1 config bw 50Mbit/s queue 1000

Such rule is rejected unless above sysctl is applied.
Another example is a group of net.inet.ip.alias.* sysctls
created after libalias.ko loaded as dependency of ipfw_nat.

This is not a problem if corresponding code compiled in custom kernel
so sysctls exist when sysctl.conf is read early or kernel modules
loaded with a loader. This change makes it work also for GENERIC
and modules loaded by means of rc.conf(5) settings.

(cherry picked from commit f5b5de1a3210234f3a6864c88a2d3e11ac2dbf04)

2 years agorc.d: unbreak sysctl lastload
Eugene Grosbein [Wed, 19 May 2021 13:02:31 +0000 (20:02 +0700)]
rc.d: unbreak sysctl lastload

/etc/rc.d/securelevel is supposed to run /etc/rc.d/sysctl lastload
late at boot time to apply /etc/sysctl.conf settings that fail
to apply early. However, this does not work in default configuration
because of kern_securelevel_enable="NO" by default.

Add new script /etc/rc.d/sysctl_lastload that starts unconditionally.

Reported by: Marek Zarychta
MFC after: 1 month

(cherry picked from commit f4b38c360e63a6e66245efedbd6c070f9c0aee55)

2 years agoUPDATING: Not unusual side effect of the awk bug fixed in 40a925385fa6b
Warner Losh [Fri, 16 Jul 2021 04:05:31 +0000 (22:05 -0600)]
UPDATING: Not unusual side effect of the awk bug fixed in 40a925385fa6b

You might not be able to build the kernel if you have an awk between Jul
10th and today. In stable/12, more (all) platforms than -current or
stable/13 have the first offset of the thread structure in the 0xfxx
range, which triggers this problem. If you've updated awk, you'll likely
need to follow the instructions here to install a fixed version before
you'll be able to build a kernel.

Sponsored by: Netflix

2 years agoSkip netgraph tests when WITHOUT_NETGRAPH is set
Ed Maste [Tue, 6 Jul 2021 13:44:15 +0000 (09:44 -0400)]
Skip netgraph tests when WITHOUT_NETGRAPH is set

PR: 256986
Reported by: John Marshall
MFC after: 1 week
Sponsored by: The FreeBSD Foundation

(cherry picked from commit c9144ec14d2a5a53cfe91ada1b3b9c06b78dc999)

2 years agoawk: revert upstream's attempt to disallow hex strings
Warner Losh [Thu, 15 Jul 2021 22:46:06 +0000 (16:46 -0600)]
awk: revert upstream's attempt to disallow hex strings

Upstream one-true-awk decided to disallow hex strings as numbers. This
is in line with awk's behavior prior to C99, and allowed by the POSIX
standard. The standard, however, allows them to be treated as numbers
because that's what the standard said in the 2001 through 2004 editions.
Since 2001, the nawk in FreeBSD has treated them as numbers, so restore
that behavior, allowed by the standard.

A number of scripts in the FreeBSD tree depend on this interpretation,
including scripts to build the kernel which had mysteriously started
failing for some people and not others. By re-allowing 0x hex numbers,
this fixes those scripts and restores POLA.

Upstream issue: https://github.com/onetrueawk/awk/issues/126
Sponsored by: Netflix
Reviewed by: kevans
MFC After: asap due to regression alrady merged to stable
Differential Revision: https://reviews.freebsd.org/D31199

(cherry picked from commit d4d252c49976de33d0a2926df733744d0b8d95fa)

2 years agopf: add pf_find_state_all_exists
Mateusz Guzik [Thu, 8 Jul 2021 13:11:57 +0000 (15:11 +0200)]
pf: add pf_find_state_all_exists

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 19d6e29b872232c47190344f3dfded2f73edd8ae)

2 years agopf: padalign global locks found in pf.c
Mateusz Guzik [Mon, 5 Jul 2021 09:26:29 +0000 (11:26 +0200)]
pf: padalign global locks found in pf.c

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit f649cff58721f493f218a4d1fb88a12255945472)

2 years agopf: allow table stats clearing and reading with ruleset rlock
Mateusz Guzik [Fri, 2 Jul 2021 12:55:57 +0000 (14:55 +0200)]
pf: allow table stats clearing and reading with ruleset rlock

Instead serialize against these operations with a dedicated lock.

Prior to the change, When pushing 17 mln pps of traffic, calling
DIOCRGETTSTATS in a loop would restrict throughput to about 7 mln.  With
the change there is no slowdown.

Reviewed by: kp (previous version)
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit dc1ab04e4c9ede3606985e0cce1200e3060ac166)

2 years agopf: depessimize table handling
Mateusz Guzik [Fri, 2 Jul 2021 11:19:56 +0000 (13:19 +0200)]
pf: depessimize table handling

Creating tables and zeroing their counters induces excessive IPIs (14
per table), which in turns kills single- and multi-threaded performance.

Work around the problem by extending per-CPU counters with a general
counter populated on "zeroing" requests -- it stores the currently found
sum. Then requests to report the current value are the sum of per-CPU
counters subtracted by the saved value.

Sample timings when loading a config with 100k tables on a 104-way box:

stock:

pfctl -f tables100000.conf  0.39s user 69.37s system 99% cpu 1:09.76 total
pfctl -f tables100000.conf  0.40s user 68.14s system 99% cpu 1:08.54 total

patched:

pfctl -f tables100000.conf  0.35s user 6.41s system 99% cpu 6.771 total
pfctl -f tables100000.conf  0.48s user 6.47s system 99% cpu 6.949 total

Reviewed by: kp (previous version)
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit f92c21a28cd856834249a008771b2f002e477a39)

2 years agolibalias: fix divide by zero causing panic
Stefan Eßer [Sat, 10 Jul 2021 11:00:56 +0000 (13:00 +0200)]
libalias: fix divide by zero causing panic

The packet_limit can fall to 0, leading to a divide by zero abort in
the "packets % packet_limit".

An possible solution would be to apply a lower limit of 1 after the
calculation of packet_limit, but since any number modulo 1 gives 0,
the more efficient solution is to skip the modulo operation for
packet_limit <= 1.

Reported by: Karl Denninger <karl@denninger.net>

(cherry picked from commit 58080fbca09fda6d5f011d37059edbca8ceb4c58)

2 years agopf: rename pf_state to pf_kstate
Kristof Provost [Tue, 6 Jul 2021 09:42:20 +0000 (11:42 +0200)]
pf: rename pf_state to pf_kstate

Indicate that this is a kernel-only structure, and make it easier to
distinguish from others used to communicate with userspace.

Reviewed by: mjg
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D31096

(cherry picked from commit 211cddf9e3a1bc0d4b1b94bea7d16a47b5a17f49)

2 years agotcp: tolerate missing timestamps
Michael Tuexen [Fri, 11 Jun 2021 18:14:34 +0000 (20:14 +0200)]
tcp: tolerate missing timestamps

Some TCP stacks negotiate TS support, but do not send TS at all
or not for keep-alive segments. Since this includes modern widely
deployed stacks, tolerate the violation of RFC 7323 per default.

Reviewed by: rgrimes, rrs, rscheff
Differential Revision: https://reviews.freebsd.org/D30740
Sponsored by: Netflix, Inc.

(cherry picked from commit 870af3f4dc57a6bbfc03f6a49ca0d5b7ff1b975a)

2 years agozfsd: Check for error from zpool_vdev_online
Ryan Moeller [Tue, 6 Jul 2021 16:00:18 +0000 (16:00 +0000)]
zfsd: Check for error from zpool_vdev_online

Onlining a vdev can fail. Log the error if it does.

Reviewed by: mav, asomers
MFC after: 1 week
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D30882

(cherry picked from commit 53b438b2425c374f6147ac80b3330a9ec08432bb)

2 years agonvme(4): Report NPWA before NPWG as stripesize.
Alexander Motin [Tue, 6 Jul 2021 02:19:48 +0000 (22:19 -0400)]
nvme(4): Report NPWA before NPWG as stripesize.

New Samsung 980 SSDs report Namespace Preferred Write Alignment of
8 (4KB) and Namespace Preferred Write Granularity of 32 (16KB).
My quick tests show that 16KB is a minimal sequential write size
when the SSD reaches peak IOPS, so writing much less is very slow.
But writing slightly less or slightly more does not change much,
so it seems not so much a size granularity as minimum I/O size.

Thinking about different stripesize consumers:
 - Partition alignment should be based on NPWA by definition.
 - ZFS ashift in part of forcing alignment of all I/Os should also
be based on NPWA.  In part of forcing size granularity, if really
needed, it may be set to NPWG, but too big value can make ZFS too
space-inefficient, and the 16KB is actually the biggest supported
value there now.
 - ZFS recordsize/volblocksize could potentially be tuned up toward
NPWG to work as I/O size granularity, but enabled compression makes
it too fuzzy.  And those are normally user-configurable things.
 - ZFS I/O aggregation code could definitely use Optimal Write Size
value and may be NPWG, but we don't have fields in GEOM now to report
the minimal and optimal I/O sizes, and even maximal is not reported
outside GEOM DISK to be used by ZFS.

MFC after: 1 week

(cherry picked from commit e3bcd07d834def94dcf570ac7350ca2c454ebf10)

2 years agoclang: stop linking _p libs for -pg as of FreeBSD 14
Ed Maste [Sat, 26 Jun 2021 23:58:16 +0000 (19:58 -0400)]
clang: stop linking _p libs for -pg as of FreeBSD 14

In FreeBSD 14 we will stop providing _p libraries (compiled with -pg).

[Note this is controlled by the target version.  There is no change for
FreeBSD <= 13.]

Reviewed by: dim (upstream)
Obtained from: LLVM 699d47472c3f
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D30861

(cherry picked from commit b762974cf4b9ea77f1decf4a6d829372f0a97f75)

2 years agoibstat: Include prototype for sysctlbyname().
Hans Petter Selasky [Thu, 27 May 2021 07:39:49 +0000 (09:39 +0200)]
ibstat: Include prototype for sysctlbyname().

Fixes the following compile warning:
      implicit declaration of function 'sysctlbyname' is invalid in C99
      [-Wimplicit-function-declaration]

Found by: J87
Differential Revision: https://reviews.freebsd.org/D30484
Sponsored by: Mellanox Technologies // NVIDIA Networking

(cherry picked from commit 16fa3dcba027d13dcda9ee78e6057e3e5a79f80c)

2 years agoImprove handling of USB device re-open in the LibUSB v1.x API.
Hans Petter Selasky [Fri, 11 Jun 2021 15:06:10 +0000 (17:06 +0200)]
Improve handling of USB device re-open in the LibUSB v1.x API.

Make sure the "device_is_gone" flag is cleared after every successful open,
so that the "device_is_gone" flag doesn't persist forever.

Found by: sergii.dmytruk@3mdeb.com
PR: 256296
Sponsored by: Mellanox Technologies // NVIDIA Networking

(cherry picked from commit 6847ea50196f1a685be408a24f01cb8d407da19c)

2 years agoThis should have been committed in r348511 with the awk update.
Warner Losh [Sun, 2 Jun 2019 16:44:50 +0000 (16:44 +0000)]
This should have been committed in r348511 with the awk update.

It was in my tree, the build worked, but I committed from contrib/one-true-awk
rather than the top level, so was omitted.

(cherry picked from commit e9b659753c07b539df17441012b0bfa5ebdbe0ea)

2 years agoone-true-awk: import 20210221 (1e4bc42c53a1) which fixes a number of bugs
Warner Losh [Wed, 7 Jul 2021 23:30:35 +0000 (17:30 -0600)]
one-true-awk: import 20210221 (1e4bc42c53a1) which fixes a number of bugs

Import the latest bsd-features branch of the one-true-awk upstream:

o Move to bison for $YACC
o Set close-on-exec flag for file and pipe redirects that aren't std*
o lots of little fixes to modernize ocde base
o free sval member before setting it
o fix a bug where a{0,3} could match aaaa
o pull in systime and strftime from NetBSD awk
o pull in fixes from {Net,Free,Open}BSD (normalized our code with them)
o add BSD extensions and, or, xor, compl, lsheift, rshift (mostly a nop)

Also revert a few of the trivial FreeBSD changes that were done slightly
differently in the upstreaming process. Also, our PR database may have
been mined by upstream for these fixes, and Mikolaj Golub may deserve
credit for some of the fixes in this update.

Suggested by: Mikolaj Golub <to.my.trociny@gmail.com>
PR: 143363,143365,143368,143369,143373,143375,214782
Sponsored by: Netflix

(cherry picked from commit f39dd6a9784467f0db5886012b3f4b13899be6b8)

2 years agoFix another UBSan error in awk
Alex Richardson [Mon, 21 Sep 2020 19:03:12 +0000 (19:03 +0000)]
Fix another UBSan error in awk

This applies my upstreamed fix: https://github.com/onetrueawk/awk/commit/ad9bd2f40a89ec9533b92254b86a756cf4f40fd4
Found By: UBSan

(cherry picked from commit 1116946093d537569a29692a6a86de59cd49384e)

2 years agoawk: Fix subobject out-of-bounds access
Alex Richardson [Mon, 21 Sep 2020 19:03:07 +0000 (19:03 +0000)]
awk: Fix subobject out-of-bounds access

When matching a regex with ^, it would attempt to access
gototab[NSTATES][NCHARS+2], and therefore access the state for the \002
character instead. This change is required to run awk under CHERI (with
sub-object bounds) and when running with UBSan instrumentation.

This was committed upstream as https://github.com/onetrueawk/awk/commit/cbf924342b63a095a4c6842280c3085b1b63ae45

Found by: CHERI (with subobject bounds enabled)
Obtained from: CheriBSD
Reviewed By: imp
Differential Revision: https://reviews.freebsd.org/D26509

(cherry picked from commit ae692c42cb46a5e72772070070840b15dd5d6bd8)

2 years agoAnother partial revert of r301289.
Warner Losh [Mon, 3 Jun 2019 05:25:22 +0000 (05:25 +0000)]
Another partial revert of r301289.

In this case, a change was made in one-true-awk from *FS to
getsval(fsloc) in a line just after one of the lines that had the 0 ->
NULL change. It works both ways as far as I can tell.  It looks like a
bug fix, but I've not tried to track down which ancient version of
one-true-awk it was in (github starts too late for tracking this
down). Before and after the changes the regression suite is passes
100% relative to the un-modified one-true-awk.

(cherry picked from commit d9e8cf281b12ca943d150b29ae5a847b0df888f3)

2 years agoFix mismerge that crept into r301289.
Warner Losh [Mon, 3 Jun 2019 05:25:16 +0000 (05:25 +0000)]
Fix mismerge that crept into r301289.

The conversion of 0 -> NULL required a rebase at some point, as noted
in r301289 when pfg commited it. In that rebase, three lines remained
that had been removed in a prior version of awk, and one of them had a
0 -> NULL change causing a conflict. The conflict should have been
resolved by removing the three lines, but wasn't. This introduces a
regression into f.split3 test which prior to this commit we were
failing, but a pure onetrueawk wasn't. Remove the offending 3 lines.

(cherry picked from commit 31d232c2a3c18fb05e5b2f1ec42872708cbfd27e)

2 years agoRevert r348518
Warner Losh [Sun, 2 Jun 2019 20:52:21 +0000 (20:52 +0000)]
Revert r348518

It should not have happened. The change is actually in upstream and I misread the diffs.

(cherry picked from commit adb46ac4c0a64c2cd1fbf46022f4aafb4e605cd3)

2 years agoReapply r301691:
Warner Losh [Sun, 2 Jun 2019 20:47:15 +0000 (20:47 +0000)]
Reapply r301691:

Revert r301689 - one-true-awk: Avoid a NULL dereference.

I got this wrong and the coverity report doesn't match the NetBSD change,
which was thought for a different version.

The change wouldn't hurt but let's wait until upstream figures this out.

(cherry picked from commit 2675e1b91dcfa744a414a42f5522f4fed958476c)

2 years agoReapply r315426 by pfg:
Warner Losh [Sun, 2 Jun 2019 16:30:53 +0000 (16:30 +0000)]
Reapply r315426 by pfg:

|    MFV r315425: one-true-awk: have calloc(3) do the multiplication.

(cherry picked from commit 06d1e65393f95e0fd2d068fe118487f754e4248b)

2 years agoReapply r301289 by pfg:
Warner Losh [Sun, 2 Jun 2019 16:28:20 +0000 (16:28 +0000)]
Reapply r301289 by pfg:

|    MFV r300961: one-true-awk: replace 0 with NULL for pointers
|    Also remove a redundant semicolon.
|    Also had to rebase on upstream pull.

(cherry picked from commit 10ce5b990fc0c874071c21e47d02194337732f27)

2 years agoMerge from upstream at 4189ef5d from https://github.com/onetrueawk/awk.git
Warner Losh [Sun, 2 Jun 2019 16:25:07 +0000 (16:25 +0000)]
Merge from upstream at 4189ef5d from https://github.com/onetrueawk/awk.git

Note: this backs out a number of changes we've made to awk because
they aren't upstream, but are on the vendor branch. Those will be
reapplied. svn makes it needlessly difficult to know which ones, but
at least r315426, r301289, and maybe r301691, though there may be
others too. None of these are critical, so bisecting through this
point is safe for all but awk regression tests :).

(cherry picked from commit b5253557294400621041b8ce1dfbf11e124c1575)

2 years agoUpdate awk(1) manual to state an exception to egrep(1)-like RE syntax
Devin Teske [Fri, 2 Nov 2018 23:03:40 +0000 (23:03 +0000)]
Update awk(1) manual to state an exception to egrep(1)-like RE syntax

Reviewed by: imp, jmg
MFC after: 3 days
Sponsored by: Smule, Inc.
Differential Revision: https://reviews.freebsd.org/D17739

(cherry picked from commit e0ff4751f0a9757f0bfc776b5da5421f6294daf8)

2 years agobhyve: Fix NVMe iovec construction for large IOs
Chuck Tuffli [Sun, 27 Jun 2021 22:14:52 +0000 (15:14 -0700)]
bhyve: Fix NVMe iovec construction for large IOs

The UEFI driver included with Rocky Linux 8.4 uncovered an existing bug
in the NVMe emulation's construction of iovec's.

By default, NVMe data transfer operations use a scatter-gather list in
which all entries point to a fixed size memory region. For example, if
the Memory Page Size is 4KiB, a 2MiB IO requires 512 entries. Lists
themselves are also fixed size (default is 512 entries).

Because the list size is fixed, the last entry is special. If the IO
requires more than 512 entries, the last entry in the list contains the
address of the next list of entries. But if the IO requires exactly 512
entries, the last entry points to data.

The NVMe emulation missed this logic and unconditionally treated the
last entry as a pointer to the next list. Fix is to check if the
remaining data is greater than the page size before using the last entry
as a pointer to the next list.

PR: 256422
Reported by: dave@syix.com
Tested by: jason@tubnor.net
Relnotes: yes

(cherry picked from commit 91064841d72b285a146a3f1c32cb447251e062ea)

2 years agolibpfctl: memory leak fix
Kristof Provost [Wed, 30 Jun 2021 11:02:35 +0000 (13:02 +0200)]
libpfctl: memory leak fix

We must remember to free the nvlist we create from the kernel's response
to DIOCGETSTATESNV, on every iteration.

Reviewed by: donner
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D30957

(cherry picked from commit 0e9f1892ec739d7fbd854af699507167a0a5dde2)

2 years agopf: getstates: avoid taking the hashrow lock if the row is empty
Kristof Provost [Tue, 29 Jun 2021 09:34:49 +0000 (11:34 +0200)]
pf: getstates: avoid taking the hashrow lock if the row is empty

Reviewed by: mjg
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D30946

(cherry picked from commit a19ff8ce9b58548a5f965db2c46eb03c38b15edb)

2 years agopf: Reduce the data returned in DIOCGETSTATESNV
Kristof Provost [Tue, 29 Jun 2021 08:26:40 +0000 (10:26 +0200)]
pf: Reduce the data returned in DIOCGETSTATESNV

This call is particularly slow due to the large amount of data it
returns. Remove all fields pfctl does not use. There is no functional
impact to pfctl, but it somewhat speeds up the call.

It might affect other (i.e. non-FreeBSD) code that uses the new
interface, but this call is very new, so there's unlikely to be any. No
releases contained the previous version, so we choose to live with the
ABI modification.

Reviewed by: donner
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D30944

(cherry picked from commit 34285eefddc99c994c3e5374ba7836cc7cfc8e2e)

2 years agopf tests: Stress state retrieval
Kristof Provost [Mon, 28 Jun 2021 10:48:20 +0000 (12:48 +0200)]
pf tests: Stress state retrieval

Create and retrieve 20.000 states. There have been issues with nvlists
causing very slow state retrieval. We don't impose a specific limit on
the time required to retrieve the states, but do log it. In excessive
cases the Kyua timeout will fail this test.

Reviewed by: donner
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D30943

(cherry picked from commit d8d43b2de1fa679179f7089cb3c31e6780ec82af)

2 years agopf tests: ftp-proxy test
Kristof Provost [Thu, 1 Jul 2021 15:15:36 +0000 (17:15 +0200)]
pf tests: ftp-proxy test

Basic test case for ftp-proxy

PR: 256917
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit dd82fd3543022017b84007ac1a0d45fc683f9850)

2 years agoftp-proxy: Revert incorrect migration to libpfctl
Kristof Provost [Thu, 1 Jul 2021 15:16:10 +0000 (17:16 +0200)]
ftp-proxy: Revert incorrect migration to libpfctl

libpfctl supports creating rules, but not (yet) adding addresses to a
pool. Adding addresses certainly does not work through adding a rule.

PR: 256917
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 8923ea6c867fd75b08b76883ec122c429a4018f9)

2 years agodummynet: fix sysctls
Kristof Provost [Thu, 1 Jul 2021 07:42:34 +0000 (09:42 +0200)]
dummynet: fix sysctls

The sysctl nodes which use V_dn_cfg must be marked as CTLFLAG_VNET so
that we use the correct per-vnet offset

PR: 256819
Reviewed by: donner
MFC after: 1 week
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D30974

(cherry picked from commit 8f76eebce424de064f65fec5cdd105446a2de3bd)

2 years agoMount and unmount devfs around calls to add packages.
Warner Losh [Fri, 21 Jun 2019 03:49:36 +0000 (03:49 +0000)]
Mount and unmount devfs around calls to add packages.

pkg now uses /dev/null for some of its operations. NanoBSD's packaging
stuff didn't mount that for the chroot it ran in, so any config that
added packages would see the error:
pkg: Cannot open /dev/null:No such file or directory
when trying to actually add those packages. It's easy enough for
nanobsd to mount /dev and it won't hurt anything that was already
working and may help things that weren't (like this). I moved the
mount/unmount pair to be in the right push/pop order from the
submitted patch.

PR: 238727
Submitted by: mike tancsa
Tested by: Karl Denninger

(cherry picked from commit 804b78634d543667a37ac459e692fe42c9dd30e8)

2 years agomergemaster: handle symbolic links during update.
Warner Losh [Wed, 18 Nov 2020 19:22:24 +0000 (19:22 +0000)]
mergemaster: handle symbolic links during update.

/etc/os-release is now a symbolic link to a generated file. Make
mergemaster cope with symbolic links generically. I'm no longer
a big mergemaster user, so this has only been lightly tested
by me, though Kimura-san has ran it through its paces.

Submitted by: Yasushiro KIMURA-san
PR: 242212
MFC After: 2 weeks

(cherry picked from commit 30a56f9ef72705f2f3646ce4330cbc20675a465e)

2 years agoarm: Make sure we can handle a thumb entry point.
Olivier Houchard [Wed, 30 Jun 2021 20:56:50 +0000 (22:56 +0200)]
arm: Make sure we can handle a thumb entry point.

Similarly to what's been done on arm64 with commit
712c060c94fd447c91b0e6218c12a431206b487a, when executing a binary, if the
entry point is a thumb symbol, then make sure we set the PSL_T flag, otherwise
the CPU will interpret it in ARM mode, and that will likely leads to an
undefined instruction.

PR: 256899
MFC after:  1 week

(cherry picked from commit 8c3bd133dd52824e427e350c65eae1fd9eb5a3cd)
Signed-off-by: Olivier Houchard <cognet@FreeBSD.org>
2 years agoAdd deprecation notice for WITH_PROFILE option
Ed Maste [Sun, 27 Jun 2021 17:21:26 +0000 (13:21 -0400)]
Add deprecation notice for WITH_PROFILE option

As discussed on freebsd-current [1] and freebsd-arch [2] and review
D30833, FreeBSD 14 will ship without the _p.a libraries built with -pg.
Both upstream and base system (in commit b762974cf4b9) Clang have been
modified to remove the special case for linking against these libraries.

Clang's -pg support and mcount() remain, so building with -pg can still
be used on code that the user builds; we just do not provide prebuilt
libraries compiled with -pg.  A similar change is still needed for GCC.

[1]  https://lists.freebsd.org/pipermail/freebsd-current/2020-January/075105.html
[2] https://lists.freebsd.org/archives/freebsd-arch/2021-June/000016.html

MFC after: 1 week
Sponsored by: The FreeBSD Foundation

(cherry picked from commit 175841285e289edebb6603da39f02549521ce950)

Clarify notice for profiled libraries in FreeBSD 14

Reported by: kevans
Fixes: 175841285e28 ("Add deprecation notice for...")
Sponsored by: The FreeBSD Foundation

(cherry picked from commit f94360971e649fa684ef3b7e72839b59c7242bdb)

2 years agolibnv: optimize nvlist size calculation
Mariusz Zaborski [Fri, 11 Jun 2021 15:35:36 +0000 (17:35 +0200)]
libnv: optimize nvlist size calculation

If we had a multiple nvlist, during nvlist_pack, we calculated the size
of every nvlist separately. For example, if we had a nvlist with three
nodes each containing another (A contains B, and B contains C), we first
calculated the size of nvlist A (which contains B, C), then we calculate
the size of B (which contains C, notice that we already did the
calculation of B, when we calculate A), and finally C. This means that
this calculation was O(N!). This was done because each time we pack
nvlist, we have to put its size in the header
(the separate header for A, B, and C).

To not break the ABI and to reduce the complexity of nvlist_size,
instead of calculating the nvlist size when requested,
we track the size of each nvlist.

Reported by: pjd, kp
Tested by: kp

(cherry picked from commit 89d5cbb82294c8624e66f920d50353057ccab14b)

2 years agolibnv: fix revert
Mariusz Zaborski [Sun, 17 Feb 2019 18:32:19 +0000 (18:32 +0000)]
libnv: fix revert

Reported by: jenkins

(cherry picked from commit 3bea7b5b05f200df4cabee12e405b8feade16f0e)

2 years agolibnv: fix double free
Mariusz Zaborski [Sun, 17 Feb 2019 18:26:27 +0000 (18:26 +0000)]
libnv: fix double free

In r343986 we introduced a double free. The structure was already
freed fixed in the r302966. This problem was introduced
because the GitHub version was out of sync with the FreeBSD one.

Submitted by: Mindaugas Rasiukevicius <rmind@netbsd.org>
MFC with: r343986

(cherry picked from commit d97753b5c8f1d32fbcdcbb0d129b49f808245865)

2 years agolibnv: fix memory leaks
Mariusz Zaborski [Sun, 10 Feb 2019 23:30:54 +0000 (23:30 +0000)]
libnv: fix memory leaks

Free the data array for NV_TYPE_DESCRIPTOR_ARRAY case.

MFC after: 2 weeks

(cherry picked from commit 0020c845a086766b3315372f006363f8ad76ac54)

2 years agolibnv: fix memory leaks
Mariusz Zaborski [Sun, 10 Feb 2019 23:28:55 +0000 (23:28 +0000)]
libnv: fix memory leaks

nvpair_create_stringv: free the temporary string; this fix affects
nvlist_add_stringf() and nvlist_add_stringv().

nvpair_remove_nvlist_array (NV_TYPE_NVLIST_ARRAY case): free the chain
of nvpairs (as resetting it prevents nvlist_destroy() from freeing it).
Note: freeing the chain in nvlist_destroy() is not sufficient, because
it would still leak through nvlist_take_nvlist_array().  This affects
all nvlist_*_nvlist_array() use

Submitted by: Mindaugas Rasiukevicius <rmind@netbsd.org>
Reported by: clang/gcc ASAN
MFC after: 2 weeks

(cherry picked from commit b5d787d93b3d83f28e87e1f8cc740cb160f8f0ac)

2 years agosbin/md5: add md5sum to synopsis in the man-page
Stefan Eßer [Sat, 19 Jun 2021 11:27:16 +0000 (13:27 +0200)]
sbin/md5: add md5sum to synopsis in the man-page

When invoked with "sum" following the hash name, the -c option takes a
file argument, not a string argument.

(cherry picked from commit 84f35b6f86efd004a8f25ef6eecea0f5d02733c4)

sbin/md5: improve compatibility with coreutils -c option

The -c option expects a digest file in either BSD or coreutils format.

The output for matched and mismatched files is identical to that
of the coreutils version.

The review of these changes included test cases that have already
been committed for the functionality that existed before.
Another test script is added to cover the coreutils compatible
extension implemented by this patch.

This commit contains a tests/Makefile that has been cleaned up
compared to the review version, using an implicit rule to apply the
TESTBASE path at build time (and the scripts have been renamed to
have an extension of .SH instead of .sh to trigger this rule).

Reviewed by:    imp
Differential Revision:  https://reviews.freebsd.org/D30812

(cherry picked from commit b33d1898c1b0e6d76b52eb48078260588802dc95)

md5: Improve compatibility with coreutils and format fix

The previous changes that added support for the coreutils -c option
modified the output generated by passing -r to match that of the
coreutils versions. The difference is that coreutils separates the
hash from the file name by two blanks "  " (or a blank followed by
an asterisk " *" with the -b option denoting).

While most scripts or users will not notice the difference, it might
be considered a violation of POLA and this commit reverts the change
for the non-sum programs. These will print a single blank " " as the
separator, as they die before the previous commit.

In order to still generate output that is identical to that of the
coreutils programs, this commit generates the "  " or " *" separator
used by them for the -sum versions, depending on the presence of the
-b option.

(cherry picked from commit b33d1898c1b0e6d76b52eb48078260588802dc95)

md5: Fix cross-build after c2870e576bd2

On macOS and Linux the current set of headers do not end up pulling in
sys/param.h, causing MAXPATHLEN to not be defined and the build to fail.

(cherry picked from commit cab31e0e216c7defefd4aba14693ba2252ea7308)

2 years agomd5: portability fix -- include stdbool.h explicitly
Warner Losh [Thu, 20 May 2021 17:26:46 +0000 (11:26 -0600)]
md5: portability fix -- include stdbool.h explicitly

stdbool.h needs to be included to use type bool variables. Due to
namespace pollution, this gets brought in on FreeBSD, but not on
other systems. Include it explicilty.

Noticed by: arichards@
Sponsored by: Netflix

2 years agomd5: Create md5sum, etc compatible programs
Warner Losh [Wed, 19 May 2021 17:26:20 +0000 (11:26 -0600)]
md5: Create md5sum, etc compatible programs

On Linux, there's a similar set of programs to ours, but that end in the
letters 'sum'. These act basically like FreeBSD versions run with the -r
option. Add code so that when the program ends in 'sum' you get the
linux -r behavior. This is enough to make most things that use sha*sum
work correctly (the -c / --check options, as well as the long args are
not implemented). When running with the -sum programs, ignore -t instead
of running internal speed tests and make -c an error.

Reviewed by: sef, and kp and allanjude (earlier version)
Relnotes: yes
Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D30309

2 years agosbin/md5: Create /usr/tests/sbin/md5 directory for placing tests
Li-Wen Hsu [Fri, 25 Jun 2021 10:29:46 +0000 (18:29 +0800)]
sbin/md5: Create /usr/tests/sbin/md5 directory for placing tests

Author: Li-Wen Hsu <lwhsu@FreeBSD.org>
Sponsored by: The FreeBSD Foundation

(cherry picked from commit cb194afef50dc4e1473413a69462aaf03cfe0ce4)

sbin/md5: add tests

While the correctness of the supported hash algorithms can be tested
with the built-in self-test feature, these test cases are meant to
detect changes in the output format.

A follow-up commit will improve the compatibility with the coreutils
versions of the hash programs, and these tests should detect any
unintended side-effects of such a change on existing functionality.

(cherry picked from commit 884fc5527a3ffd9830b941cbec328de1623044cc)

sbin/md5: fix test case to not write outside temp directory

The bsd-p-test did create files 1.out .. 8.out in the test directory.
This has been fixed and the temporary output files are now written
to the temporary directory.

(cherry picked from commit 95d3044bbdadfca6120a3e7c61c9ebb9a476f369)

2 years agolibalias: Switch to SPLAY trees
Lutz Donnerhacke [Thu, 27 May 2021 21:42:54 +0000 (23:42 +0200)]
libalias: Switch to SPLAY trees

Current data structure is using a hash of unordered lists.  Those
unordered lists are quite efficient, because the least recently
inserted entries are most likely to be used again.  In order to avoid
long search times in other cases, the lists are hashed into many
buckets.  Unfortunatly a search for a miss needs an exhaustive
inspection and a careful definition of the hash.

Splay trees offer a similar feature - almost O(1) for access of the
least recently used entries), and amortized O(ln(n) - for almost all
other cases.  Get rid of the hash.

Now the data structure should able to quickly react to external
packets without eating CPU cycles for breakfast, preventing a DoS.

PR: 192888
Discussed with: Dimitry Luhtionov
Differential Revision: https://reviews.freebsd.org/D30516
Differential Revision: https://reviews.freebsd.org/D30536
Differential Revision: https://reviews.freebsd.org/D30844

(cherry picked from commit 935fc93af157dee352eb4b6c83f8a2a9e7fd9a4e)
(cherry picked from commit d261e57deacb0d00d9e827447f235df83dda3e3a)
(cherry picked from commit f70c98a2f5d993dc518efd606aa341eda99400ef)
(cherry picked from commit 25392fac9488bcae5c451500df2e2945430484a6)
(cherry picked from commit 2f4d91f9cb22fc65eb65407e8118b433a5d71976)
(cherry picked from commit 4060e77f49d1b9fd2254f3f4da94fd64fce83f72)

2 years agolibalias: Restructure
Lutz Donnerhacke [Tue, 25 May 2021 13:03:34 +0000 (15:03 +0200)]
libalias: Restructure

Clean up the database handling in order to switch to more efficient
data structures.  The development of this patch was artificially split
in to many small steps to ease reviewing.

- Common search terms
- Separate fully qualified search
- Separate table for partial links
- Cleanup _FindLinkIn
- Factor out the outgoing search function
- Factor out a common idiom to return found links
- Reorder incoming links by grouping of common search terms
- Remove LSNAT from outgoing search
- Group internal structure semantically
- Separate table for PPTP
- Use AliasRange instead of PORT_BASE
- Remove temporary state deleteAllLinks from global struct
- Avoid uninitialized expiration

Discussed with: Dimitry Luhtionov
Differential Revision: https://reviews.freebsd.org/D30568
Differential Revision: https://reviews.freebsd.org/D30569
Differential Revision: https://reviews.freebsd.org/D30570
Differential Revision: https://reviews.freebsd.org/D30571
Differential Revision: https://reviews.freebsd.org/D30572
Differential Revision: https://reviews.freebsd.org/D30573
Differential Revision: https://reviews.freebsd.org/D30574
Differential Revision: https://reviews.freebsd.org/D30575
Differential Revision: https://reviews.freebsd.org/D30580
Differential Revision: https://reviews.freebsd.org/D30581
Differential Revision: https://reviews.freebsd.org/D30604
Differential Revision: https://reviews.freebsd.org/D30582

(cherry picked from commit d41044ddfdbc2a026570cae11d5fc2e18f5e4b92)
(cherry picked from commit 32f9c2ceb3ec8266e48ec0f6d1556fd98ef12db6)
(cherry picked from commit cac129e6030095c33e95c5ce1cdcb9c5c21efce9)
(cherry picked from commit 19dcc4f2250b3850991366e3058ca8a64b2f135d)
(cherry picked from commit d5419034381d0e8e8c99f0f678118b197144bfff)
(cherry picked from commit d4ab07d2aeb13b1aae9ad5d73e5fe77131f2350c)
(cherry picked from commit 492d3b7109b45ff30fd199b7eeed797447746e42)
(cherry picked from commit 7b44ff4c52ce9e7a89e772566a249e5481fe0ac4)
(cherry picked from commit 1178dda53d10b096e0b99e356d15b7c24390e099)
(cherry picked from commit 9efcad61d8309ecad3c15392b277fd329a1e45e4)
(cherry picked from commit fe83900f9fa931e3d0942ec9c0709896b887c3d7)
(cherry picked from commit d989935b5bcd880353f0de89eda958c45e7e3342)
(cherry picked from commit b50a4dce185481bebf8096c27588ae04a4a2fd7c)
(cherry picked from commit f28455344483310cfd1aa5c0bdd4d014810c0e32)

2 years agolibalias: Promote per instance global variable timeStamp
Lutz Donnerhacke [Tue, 25 May 2021 07:07:21 +0000 (09:07 +0200)]
libalias: Promote per instance global variable timeStamp

Summary:
- Use LibAliasTime as a real global variable for central timekeeping.
- Reduce number of syscalls in user space considerably.
- Dynamically adjust the packet counters to match the second resolution.
- Only check the first few packets after a time increase for expiry.

Discussed with: hselasky
Differential Revision: https://reviews.freebsd.org/D30566

(cherry picked from commit ef828d39be8e4b6d922db080ed41611702dec56d)

2 years agolibalias: Stats are unsigned
Lutz Donnerhacke [Mon, 31 May 2021 22:08:24 +0000 (00:08 +0200)]
libalias: Stats are unsigned

Stats counters are used as unsigned valued (i.e. printf("%u")) but are
defined as signed int.  This causes trouble later, so fix it early.

Differential Revision: https://reviews.freebsd.org/D30587

(cherry picked from commit 3fd20a79e7b55d135bbc5fa197980fe0e9ee0f14)

2 years agolibalias: tidy up housekeeping
Lutz Donnerhacke [Sat, 15 May 2021 15:35:36 +0000 (17:35 +0200)]
libalias: tidy up housekeeping

Replace current expensive, but sparsly called housekeeping
by a single, repetive action.

This is part of a larger restructure of libalias in order to switch to
more efficient data structures.  The whole restructure process is
split into 15 reviews to ease reviewing.  All those steps will be
squashed into a single commit for MFC in order to hide the
intermediate states from production systems.

Reviewed by: hselasky
Differential Revision: https://reviews.freebsd.org/D30277

(cherry picked from commit 294799c6b081faece556a5010a4f51552e131c2f)

2 years agorefcount: add refcount_releasen
Mateusz Guzik [Mon, 5 Jul 2021 12:45:32 +0000 (12:45 +0000)]
refcount: add refcount_releasen

This is a direct commit as the routine in main was added as a side
effect of functionality which later got reverted.

2 years agopf: make DIOCGETSTATESNV iterations killable
Mateusz Guzik [Fri, 2 Jul 2021 07:28:48 +0000 (09:28 +0200)]
pf: make DIOCGETSTATESNV iterations killable

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 48d5b86364fa8e86318805d0078ccf9066e94ae8)

2 years agopfctl: cache getprotobynumber results
Mateusz Guzik [Thu, 1 Jul 2021 19:25:43 +0000 (21:25 +0200)]
pfctl: cache getprotobynumber results

As for example pfctl -ss keeps calling it, it saves a lot of overhead
from elided parsing of /etc/nsswitch.conf and /etc/protocols.

Sample result when running a pre-nvlist binary with nfs root and dumping
7 mln states:
before: 24.817u 62.993s 1:28.52 99.1%
after: 8.064u 1.117s 0:18.87 48.5%

Idea by Jim Thompson

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 858937bea4599d254a97ee6321683f8629604e15)

2 years agopf: revert: Use counter(9) for pf_state byte/packet tracking
Mateusz Guzik [Mon, 28 Jun 2021 18:50:56 +0000 (20:50 +0200)]
pf: revert: Use counter(9) for pf_state byte/packet tracking

stats are not shared and consequently per-CPU counters only waste
memory.

No slowdown was measured when passing over 20M pps.

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 55cc305dfcad0ad7c4f528fa47f7473927e8223a)

2 years agopf: make sure the dtrace probe has safe access to state
Mateusz Guzik [Mon, 28 Jun 2021 19:32:20 +0000 (21:32 +0200)]
pf: make sure the dtrace probe has safe access to state

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit d26ef5c7ac830812f07a02787f25fed5d6f8609e)

2 years agopf: deduplicate V_pf_state_z handling with pfsync
Mateusz Guzik [Mon, 28 Jun 2021 18:18:26 +0000 (20:18 +0200)]
pf: deduplicate V_pf_state_z handling with pfsync

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 803dfe3da0e30520227a791ba5c2b93da16e1cf5)

2 years agopf: fix error-case leaks in pf_create_state
Mateusz Guzik [Mon, 28 Jun 2021 18:29:58 +0000 (20:29 +0200)]
pf: fix error-case leaks in pf_create_state

The hand-rolled clean up failed to free counters.

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit 7f025db57c7879bfa43ac74ecd95903439fdd1d0)

2 years agopf: assert that sizeof(struct pf_state) <= 312
Mateusz Guzik [Mon, 28 Jun 2021 12:49:40 +0000 (14:49 +0200)]
pf: assert that sizeof(struct pf_state) <= 312

To prevent accidentally going over a threshold which makes UMA fit only
12 objects per page instead of 13.

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit e6dd0e2e8d4f59275fb576a9ccb9d356d36457bb)

2 years agopf: factor out state allocation into pf_alloc_state
Mateusz Guzik [Mon, 28 Jun 2021 12:22:31 +0000 (14:22 +0200)]
pf: factor out state allocation into pf_alloc_state

Reviewed by: kp
Sponsored by: Rubicon Communications, LLC ("Netgate")

(cherry picked from commit ccb17a21041e9206b80fa9f64b6ec20233df6403)