Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/gossip_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ bool gossip_store_readhdr(int gossip_store_fd, size_t off,
r = pread(gossip_store_fd, &buf, HDR_AND_TYPE_SIZE, off);
if (r != HDR_AND_TYPE_SIZE)
return false;
if (!(buf.hdr.flags & CPU_TO_BE16(GOSSIP_STORE_COMPLETED_BIT)))
return false;
*len = be16_to_cpu(buf.hdr.len);
if (flags)
*flags = be16_to_cpu(buf.hdr.flags);
Expand Down
6 changes: 3 additions & 3 deletions common/gossip_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct gossip_rcvd_filter;
/* First byte of file is the version.
*
* Top three bits mean incompatible change.
* As of this writing, major == 0, minor == 13.
* As of this writing, major == 0, minor == 15.
*/
#define GOSSIP_STORE_MAJOR_VERSION_MASK 0xE0
#define GOSSIP_STORE_MINOR_VERSION_MASK 0x1F
Expand All @@ -30,9 +30,9 @@ struct gossip_rcvd_filter;
#define GOSSIP_STORE_DELETED_BIT 0x8000U

/**
* Bit of flags we use to mark an important record.
* Bit of flags indicating record has been written.
*/
#define GOSSIP_STORE_PUSH_BIT 0x4000U
#define GOSSIP_STORE_COMPLETED_BIT 0x2000U

/**
* Bit of flags used to mark a channel announcement closed (not deleted for 12 blocks)
Expand Down
63 changes: 33 additions & 30 deletions common/gossmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,15 @@ static bool map_catchup(struct gossmap *map, bool must_be_clean, bool *changed)
reclen = msglen + sizeof(ghdr);

flags = be16_to_cpu(ghdr.flags);

/* Not finished, this can happen. */
if (!(flags & GOSSIP_STORE_COMPLETED_BIT))
break;

if (flags & GOSSIP_STORE_DELETED_BIT)
continue;

/* Partial write, this can happen. */
/* Partial write, should not happen with completed records. */
if (map->map_end + reclen > map->map_size)
break;

Expand All @@ -706,8 +711,10 @@ static bool map_catchup(struct gossmap *map, bool must_be_clean, bool *changed)
map->logcb(map->cbarg,
LOG_BROKEN,
"Truncated gossmap record @%"PRIu64
"/%"PRIu64" (len %zu): waiting",
map->map_end, map->map_size, msglen);
"/%"PRIu64" (len %zu): waiting%s",
map->map_end, map->map_size, msglen,
gossmap_has_mmap(map) ? " and disabling mmap" : "");
gossmap_disable_mmap(map);
if (must_be_clean)
return false;
break;
Expand All @@ -725,10 +732,12 @@ static bool map_catchup(struct gossmap *map, bool must_be_clean, bool *changed)
map->logcb(map->cbarg,
LOG_BROKEN,
"Bad checksum on gossmap record @%"PRIu64
"/%"PRIu64" should be %u (%s): waiting",
"/%"PRIu64" should be %u (%s): waiting%s",
map->map_end, map->map_size,
be32_to_cpu(ghdr.crc),
tal_hexstr(tmpctx, msgbuf, msglen));
tal_hexstr(tmpctx, msgbuf, msglen),
gossmap_has_mmap(map) ? " and disabling mmap" : "");
gossmap_disable_mmap(map);
if (must_be_clean)
return false;
break;
Expand Down Expand Up @@ -779,12 +788,9 @@ static bool load_gossip_store(struct gossmap *map, bool must_be_clean)
map->local_announces = NULL;
map->local_updates = NULL;

/* gossipd uses pwritev(), which is not consistent with mmap on OpenBSD! */
#ifndef __OpenBSD__
/* If this fails, we fall back to read */
map->mmap = mmap(NULL, map->map_size, PROT_READ, MAP_SHARED, map->fd, 0);
if (map->mmap == MAP_FAILED)
#endif /* __OpenBSD__ */
map->mmap = NULL;

/* We only support major version 0 */
Expand Down Expand Up @@ -1211,20 +1217,19 @@ bool gossmap_refresh(struct gossmap *map)
/* You must remove local modifications before this. */
assert(!map->local_announces);

/* If file has gotten larger, try rereading */
/* If file has gotten larger, remap */
len = lseek(map->fd, 0, SEEK_END);
if (len == map->map_size)
return false;
if (len != map->map_size) {
if (map->mmap)
munmap(map->mmap, map->map_size);
map->map_size = len;

if (map->mmap)
munmap(map->mmap, map->map_size);
map->map_size = len;
/* gossipd uses pwritev(), which is not consistent with mmap on OpenBSD! */
#ifndef __OpenBSD__
map->mmap = mmap(NULL, map->map_size, PROT_READ, MAP_SHARED, map->fd, 0);
if (map->mmap == MAP_FAILED)
#endif /* __OpenBSD__ */
map->mmap = NULL;
if (map->mmap) {
map->mmap = mmap(NULL, map->map_size, PROT_READ, MAP_SHARED, map->fd, 0);
if (map->mmap == MAP_FAILED)
map->mmap = NULL;
}
}

map_catchup(map, false, &changed);
return changed;
Expand Down Expand Up @@ -1857,16 +1862,14 @@ int gossmap_fd(const struct gossmap *map)
return map->fd;
}

const u8 *gossmap_fetch_tail(const tal_t *ctx, const struct gossmap *map)
bool gossmap_has_mmap(const struct gossmap *map)
{
size_t len;
u8 *p;
return map->mmap != NULL;
}

/* Shouldn't happen... */
if (map->map_end > map->map_size)
return NULL;
len = map->map_size - map->map_end;
p = tal_arr(ctx, u8, len);
map_copy(map, map->map_size, p, len);
return p;
void gossmap_disable_mmap(struct gossmap *map)
{
if (map->mmap)
munmap(map->mmap, map->map_size);
map->mmap = NULL;
}
7 changes: 4 additions & 3 deletions common/gossmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ struct gossmap *gossmap_load_(const tal_t *ctx,
...),
void *cb_arg);

/* Disable mmap. Noop if already disabled. */
void gossmap_disable_mmap(struct gossmap *map);
bool gossmap_has_mmap(const struct gossmap *map);

/* Call this before using to ensure it's up-to-date. Returns true if something
* was updated. Note: this can scramble node and chan indexes! */
bool gossmap_refresh(struct gossmap *map);
Expand Down Expand Up @@ -304,7 +308,4 @@ u64 gossmap_lengths(const struct gossmap *map, u64 *total);

/* Debugging: connectd wants to enumerate fds */
int gossmap_fd(const struct gossmap *map);

/* Fetch unprocessed part of gossmap */
const u8 *gossmap_fetch_tail(const tal_t *ctx, const struct gossmap *map);
#endif /* LIGHTNING_COMMON_GOSSMAP_H */
Loading
Loading